Clean up code

This commit is contained in:
Sarmonsiill 2021-10-27 11:50:33 +00:00
parent 82b8200632
commit 24eaa0c223
1 changed files with 69 additions and 75 deletions

144
gtc_gen.v
View File

@ -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"(?P<format>https?)|(?P<format>ftps?)://(?P<token>[\w_]+.)+"
/* 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>'
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'
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: <a href="https://creativecommons.org/licenses/by-sa/4.0/">CC-BY-SA-4.0</a>'
}
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: <a href="https://creativecommons.org/licenses/by-sa/4.0/">CC-BY-SA-4.0</a>'
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('<br>')
lnks := re.find_all_str(p_cnt)
for lnk in lnks {
p_cnt = p_cnt.replace(lnk, "<a href='${lnk}' target='_blank'>${lnk}</a>")
}
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>${p_cnt}</p>" +
ftr.replace('::FTR::', cp_rgt)) or { panic(err) }
mut output := ''
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 { 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) }
}
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 = '<h2>About</h2><p>${acnt}</p><hr />'
lnks := re.find_all_str(about)
for lnk in lnks {
about = about.replace(lnk, "<a href='${lnk}' target='_blank'>${lnk}</a>")
}
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(trgt, 'index.html'), hdr.replace('::TTL::', site) +
"<h1><a href='$url'>$site</a></h1>${about}<ul>${indx_arr.join('')}</ul>" +
ftr.replace('::FTR::', cp_rgt)) or { panic(err) }
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 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://
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
\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)
}