module main import os import regex { regex_opt } import time { now } const ( query = r"(?Phttps?)|(?Pftps?)://(?P[\w_]+.)+" header = "::TTL::" footer = '
::FTR::
' ) struct Cfg { base_directory string = '${os.getenv('HOME')}/.gtc_gen' 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 = genv('GTC_GEN_LICENSE', 'License: CC-BY-SA-4.0') } fn main() { 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(genv('EDITOR', 'vi'), [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) } else {return d} } fn parse_links(input string) string { mut re := regex.regex_opt(query) or { panic(err) } mut output := input 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 { println("$p has no lines") 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) } } 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(c.about_file) { content := os.read_file(c.about_file) or { panic(err) } about = '

    About

    ${parse_links(content)}


    ' } 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 - 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\n\tGTC_GEN_LICENSE\tlicense of the content \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. posts are stored in \$HOME/.gtc_gen\n~/.gtc_gen.about is uses as an about -section on the index.") exit(1) }