From 24eaa0c22391fe290db3c5052f9abbacc80d48ff Mon Sep 17 00:00:00 2001 From: Sarmonsiill Date: Wed, 27 Oct 2021 11:50:33 +0000 Subject: [PATCH] Clean up code --- gtc_gen.v | 144 ++++++++++++++++++++++++++---------------------------- 1 file changed, 69 insertions(+), 75 deletions(-) diff --git a/gtc_gen.v b/gtc_gen.v index cdc2528..883c0f1 100644 --- a/gtc_gen.v +++ b/gtc_gen.v @@ -1,104 +1,98 @@ module main import os -import regex +import regex { regex_opt } import time { now } const ( - bdir = '${os.getenv('HOME')}/.gtc_gen' - ed = os.getenv('EDITOR') - name = 'gtc_gen' - afile = '${os.getenv('HOME')}/.gtc_gen.about' - - /* regexp */ query = r"(?Phttps?)|(?Pftps?)://(?P[\w_]+.)+" - - /* html templates */ - hdr = "::TTL::" - ftr = '
::FTR::
' + header = "::TTL::" + footer = '
::FTR::
' ) +struct Cfg { + base_directory string = '${os.getenv('HOME')}/.gtc_gen' + editor string = os.getenv('EDITOR') + application_name string = 'gtc_gen' + about_file string = '${os.getenv('HOME')}/.gtc_gen.about' + auth string = genv('GTC_GEN_AUTH', 'unknown') + mail string = genv('GTC_GEN_MAIL', 'unknown@example.org') + site_name string = genv('GTC_GEN_SITE', 'gtc_gen -site') + target string = genv('GTC_GEN_TARGET', './site') + url string = genv('GTC_GEN_URL', 'https://example.org') + entries_dir string = os.join_path(genv('GTC_GEN_TARGET', './site'),'entries') + copyleft string = 'License: CC-BY-SA-4.0' +} fn main() { - if !os.is_dir(bdir) { - os.mkdir(bdir) or { panic(err) } - } - if os.args.len != 2 { - help() - } + c := Cfg{} + if !os.is_dir(c.base_directory) {os.mkdir(c.base_directory) or {panic(err)}} + if os.args.len != 2 { help() } match os.args[1] { - 'a' { os.execvp(ed, [os.join_path(bdir, '${now().unix}.post')]) or { panic(err) } } - 'm' { make() } + 'a' { os.execvp(c.editor, + [os.join_path(c.base_directory, '${now().unix}.post')]) + or { panic(err) } } + 'm' { c.make() } else { help() } } } fn genv(k string, d string) string { - if os.getenv(k) != '' { return os.getenv(k) } - return d + if os.getenv(k) != '' { return os.getenv(k) } else {return d} } -fn make() { +fn parse_links(input string) string { mut re := regex.regex_opt(query) or { panic(err) } - auth := genv('GTC_GEN_AUTH', 'unknown') - mail := genv('GTC_GEN_MAIL', 'no@example.org') - site := genv('GTC_GEN_SITE', 'gtc_gen -site') - trgt := genv('GTC_GEN_TARGET', './site') - url := genv('GTC_GEN_URL', 'https://example.org') - ptrgt := os.join_path(trgt, 'entries') - cp_rgt := 'License: CC-BY-SA-4.0' - if !os.is_dir(ptrgt) {os.mkdir_all(ptrgt) or { panic(err) }} - fls := os.glob('$bdir/*.post') or { panic(err) } - mut indx_arr := []string{} - for f in fls { - cnt := os.read_file(f) or { panic(err) } - cnt_arr := cnt.split_into_lines() - if cnt_arr.len < 1 { continue } - n := f.split('/').last().trim('.post') - lt := time.unix(n.int()) - ttl := cnt_arr[0].capitalize() - mut p_cnt := cnt_arr[1..].join('
') - lnks := re.find_all_str(p_cnt) - for lnk in lnks { - p_cnt = p_cnt.replace(lnk, "${lnk}") - } - indx_arr << "
  • $ttl
  • " - os.write_file(os.join_path(ptrgt, '${n}.html'), hdr.replace('::TTL::', '$ttl | $site') + - "

    $site

    $ttl

    written: -$lt.ymmdd() $lt.hhmm() by $auth ($mail)

    -

    ${p_cnt}

    " + - ftr.replace('::FTR::', cp_rgt)) or { panic(err) } + mut output := '' + for l in re.find_all_str(input) {output=input.replace(l, "${l}")} + return output +} + +fn (c Cfg) make_entries_list() string { + posts := os.glob('${c.base_directory}/*.post') or {panic(err)} + mut arr := '' + for p in posts { + content := os.read_file(p) or { panic(err) } + lines := content.split_into_lines() + if lines.len < 1 { continue } + name := p.split('/').last().trim('.post') + ts := time.unix(name.int()) + title := lines[0].capitalize() + parsed_content := parse_links(lines[1..].join('
    ')) + arr = "
  • $title
  • "+arr + os.write_file( + os.join_path(c.entries_dir, '${name}.html'), + header.replace('::TTL::', '$title | $c.site_name') + + "

    $c.site_name

    $title

    written: +$ts.ymmdd() $ts.hhmm() by $c.auth ($c.mail)

    +

    ${parsed_content}

    " + + footer.replace('::FTR::', c.copyleft)) or { panic(err) } } - indx_arr = indx_arr.reverse() - /* if we find ~/.gtc_gen.about we use that text content as an about - * -section on the index page. */ + return arr +} + +fn (c Cfg) make() { + if !os.is_dir(c.entries_dir){os.mkdir_all(c.entries_dir) or {panic(err)}} mut about := '' - if os.is_file(afile) { - acnt := os.read_file(afile) or { panic(err) } - about = '

    About

    ${acnt}


    ' - lnks := re.find_all_str(about) - for lnk in lnks { - about = about.replace(lnk, "${lnk}") - } + if os.is_file(c.about_file) { + content := os.read_file(c.about_file) or { panic(err) } + about = '

    About

    ${parse_links(content)}


    ' } - os.write_file(os.join_path(trgt, 'index.html'), hdr.replace('::TTL::', site) + - "

    $site

    ${about}
      ${indx_arr.join('')}
    " + - ftr.replace('::FTR::', cp_rgt)) or { panic(err) } + os.write_file( + os.join_path(c.target, 'index.html'), + header.replace('::TTL::', c.site_name)+ + "

    $c.site_name

    ${about}"+ + "
      ${c.make_entries_list()}
    "+ + footer.replace('::FTR::', c.copyleft)) or { panic(err) } } fn help() { - println("gtc_gen help - made by sarmonsiill \n -\t$name a\tadd a new post -\t$name m\ttranspile posts into site -\n--\nEnvironment variables\n -\tGTC_GEN_AUTH\tPrinted as author in site if defined -\tGTC_GEN_MAIL\tPrinted as email in site if defined -\tGTC_GEN_SITE\tUsed as site title if defined -\tGTC_GEN_TARGET\tLocation of transpiled website if defined -\tGTC_GEN_URL\tThe domain of the site. Don't forget to include https:// or http:// + println("gtc_gen - made by sarmonsiill \n +\tgtc_gen a\tadd a new post\n\tgtc_gen m\ttranspile posts +\n--\nEnvironment variables\n\n\tGTC_GEN_AUTH\tname of author +\tGTC_GEN_MAIL\temail of author\n\tGTC_GEN_SITE\tsite title +\tGTC_GEN_TARGET\tsite output\n\tGTC_GEN_URL\tdomain of the site. incl https:// \nFirst line of the post will become the header. Filename will become timestamp. -Raw posts are stored in \$HOME/.gtc_gen -If ~/.gtc_gen.about exists it uses that text content as an about -section on the index. -That is it.") +posts are stored in \$HOME/.gtc_gen\n~/.gtc_gen.about is uses as an about -section on the index.") exit(1) }