gtc_gen/gtc_gen.v

97 lines
4.2 KiB
V

module main
import os
import regex { regex_opt }
import time { now }
const (
query = r"(?P<format>https?)|(?P<format>ftps?)://(?P<token>[\w_]+.)+"
header = "<!DOCTYPE html><html><head><title>::TTL::</title><style>*{box-sizing:border-box;border-spacing:0}html{font:12pt/1.6 Helvetica, sans-serif}body{position:relative;margin:auto;max-width:50rem;color:#433;padding:3.1rem 0.6rem 0;overflow-x:hidden}footer{margin:10rem 0 0}a{color:#07c;cursor:pointer}a{text-decoration:underline solid #d1d1d1;text-underline-position:under}a:hover{color:#088;border-color:#088}ul{padding-top:0.5rem}li{margin-bottom:0.5rem}h1,h2{margin:1.5em 0 0;line-height:1.2em;margin-top:0.5em}h1{font-size:2.2em;font-weight:300}h2{font-size:2.0em;font-weight:300;font-variant-caps:small-caps}hr{border:0;border-top:0.1rem solid #d1d1d1}p > cite:before{content:' ('}p > cite:after{content:') '}</style></head><body>"
footer = '</body><footer>::FTR::</footer></html>'
)
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: <a href="https://creativecommons.org/licenses/by-sa/4.0/">CC-BY-SA-4.0</a>')
}
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, "<a href='${l}' target='_blank'>${l}</a>")}
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('<br>'))
arr = "<li><a href='${c.url}/entries/${name}.html'>$title</a></li>"+arr
os.write_file(
os.join_path(c.entries_dir, '${name}.html'),
header.replace('::TTL::', '$title | $c.site_name') +
"<h1><a href='$c.url'>$c.site_name</a></h1><h2>$title</h2><p>written:
$ts.ymmdd() $ts.hhmm() by $c.auth ($c.mail)</p>
<p>${parsed_content}</p>" +
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 = '<h2>About</h2><p>${parse_links(content)}</p><hr />'
}
os.write_file(
os.join_path(c.target, 'index.html'),
header.replace('::TTL::', c.site_name)+
"<h1><a href='$c.url'>$c.site_name</a></h1>${about}"+
"<ul>${c.make_entries_list()}</ul>"+
footer.replace('::FTR::', c.copyleft)) or { panic(err) }
}
fn help() {
println("gtc_gen - made by sarmonsiill <sarmonsiill@tilde.guru>\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)
}