gtc_gen/gtc_gen.v

81 lines
3.1 KiB
V

module main
import os
import time { now }
const (
bdir = '${os.getenv('HOME')}/.gtc_gen'
ed = os.getenv('EDITOR')
name = 'gtc_gen'
// html templates
hdr = "<!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>"
ftr = '</body><footer>::FTR::</footer></html>'
)
fn main() {
if !os.is_dir(bdir) {
os.mkdir(bdir) 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() }
else { help() }
}
}
fn genv(k string, d string) string {
if os.getenv(k) != '' { return os.getenv(k) }
return d
}
fn make() {
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 := 'copyleft $auth ($mail)'
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].title()
indx_arr << "<li><a href='${url}/entries/${n}.html'>$ttl</a></li>"
os.write_file(os.join_path(ptrgt, '${n}.html'), hdr.replace('::TTL::', '$ttl | $site') +
"<h1><a href='$url'>$site</a></h1><h2>$ttl</h2><p>written:
$lt.ymmdd() $lt.hhmm() by $auth ($mail)</p>
<p>${cnt_arr[1..].join('<br>')}</p>" +
ftr.replace('::FTR::', cp_rgt)) or { panic(err) }
}
indx_arr = indx_arr.reverse()
os.write_file(os.join_path(trgt, 'index.html'), hdr.replace('::TTL::', site) +
"<h1><a href='$url'>$site</a></h1><ul>${indx_arr.join('')}</ul>" +
ftr.replace('::FTR::', cp_rgt)) or { panic(err) }
}
fn help() {
println("gtc_gen help - made by sarmonsiill <sarmonsiill@tilde.guru>\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://
\nFirst line of the post will become the header. Filename will become timestamp.
Raw posts are stored in \$HOME/.gtc_gen
That is it.")
exit(1)
}