did another refactor

This commit is contained in:
Abdullah Islam 2023-05-10 16:19:06 +06:00
parent f35ce48f7d
commit 2d83bf4275
7 changed files with 44 additions and 77 deletions

View File

@ -6,8 +6,12 @@ all: subdirs \
$(SOURCES:src/%.gmi=html/%.html) \
$(SOURCES:src/%.gmi=gmi/%.gmi) \
gmi/sitemap.gmi \
html/sitemap.html \
gmi/feed.atom \
html/sitemap.html
html/feed.atom \
test:
something
clean:
rm -rf html/* gmi/*
@ -16,16 +20,19 @@ subdirs:
find src -mindepth 1 -type d -printf '%P\0' | xargs -0 -I{} mkdir -p gmi/{} html/{}
html/%.html: src/%.gmi scripts/html.awk
mawk -f scripts/common_functions.awk -f scripts/html.awk -- $< > $@
mawk -f scripts/common.awk -f ~/projects/awk_functions/lib.awk -f scripts/html.awk -- $< > $@
gmi/%.gmi: src/%.gmi scripts/gemini.awk
mawk -f scripts/common_functions.awk -f scripts/gemini.awk -- $< > $@
mawk -f scripts/common.awk -f ~/projects/awk_functions/lib.awk -f scripts/gemini.awk -- $< > $@
gmi/sitemap.gmi: $(SITEMAP_INDEX_FILES) scripts/sitemap_gmi.awk
mawk -f scripts/common_functions.awk -f scripts/sitemap_gmi.awk -- $(SITEMAP_INDEX_FILES) > $@
mawk -f scripts/common.awk -f ~/projects/awk_functions/lib.awk -f scripts/sitemap_gmi.awk -- $(SITEMAP_INDEX_FILES) > $@
html/sitemap.html: $(SITEMAP_INDEX_FILES) scripts/sitemap_html.awk
mawk -f scripts/common_functions.awk -f scripts/sitemap_html.awk -- $(SITEMAP_INDEX_FILES) > $@
mawk -f scripts/common.awk -f ~/projects/awk_functions/lib.awk -f scripts/sitemap_html.awk -- $(SITEMAP_INDEX_FILES) > $@
gmi/feed.atom: src/feed.ass scripts/feed.awk
mawk -f scripts/common_functions.awk -f scripts/feed.awk -- $< > $@
mawk -f scripts/common.awk -f ~/projects/awk_functions/lib.awk -f scripts/feed.awk -- $< > $@
html/feed.atom: src/feed.ass scripts/feed.awk
mawk -f scripts/common.awk -f ~/projects/awk_functions/lib.awk -f scripts/feed.awk -- $< > $@

11
scripts/common.awk Normal file
View File

@ -0,0 +1,11 @@
function scanLink(text, link, description) {
match(text, "^=>([ ]*)([^ ]*)")
link = substr(text, 3, RSTART + RLENGTH - 3)
description = substr(text, RSTART + RLENGTH)
sub("^([ ]*)", "", link)
sub("^([ ]*)", "", description)
return (RSTART == 0)
}

View File

@ -1,52 +0,0 @@
function getDirname(path, pathComponents, dirname) {
sub("/$", "", path)
pathComponents[1] = ""
split(path, pathComponents, "/")
dirname = ""
if (length(pathComponents) < 2) return "."
for (i = 1; i <= length(pathComponents) - 1; i++)
dirname = dirname pathComponents[i] "/"
return dirname
}
function resolvePath(path, resolvedPath, pathComponents) {
sub("/$", "", path)
pathComponents[1] = ""
split(path, pathComponents, "/")
resolvedPath = ""
for (i = 1; i <= length(pathComponents); i++)
if (pathComponents[i] == "." || (i > 1 && pathComponents[i] == "")) continue
else if (i+1 <= length(pathComponents) && pathComponents[i+1] == "..") i++
else if (i == length(pathComponents)) resolvedPath = resolvedPath pathComponents[i]
else resolvedPath = resolvedPath pathComponents[i] "/"
return resolvedPath
}
function relpath(base, path, resolvedPath, pathComponents) {
sprintf("realpath --relative-to=\"%s\" \"%s\"", base, path) | getline resolvedPath
return resolvedPath
}
function toFilename(string, result) {
result = tolower(string)
sub("^([ ]*)", "", result)
sub("([ ]*)$", "", result)
gsub(" ", "_", result)
return result
}
function abspath(base, path, resolvedPath, pathComponents) {
sprintf("realpath \"%s\"", path) | getline resolvedPath
return resolvedPath
}
function hashbased_uuidgen(string, namespace, uuid) {
sprintf("uuidgen -s -N \"%s\" -n \"%s\"", string, namespace) | getline uuid
return uuid
}

View File

@ -1,16 +1,15 @@
BEGIN {
getline
dirname = getDirname(relpath(ENVIRON["PWD"], FILENAME))
printf "Navigation:\n\
=> %s home\n\
=> %s notes\n\
=> %s useful\n\
=> %s sitemap\n", \
relpath(dirname, ENVIRON["PWD"] "/src/index.gmi"), \
relpath(dirname, ENVIRON["PWD"] "/src/notes/index.gmi"), \
relpath(dirname, ENVIRON["PWD"] "/src/useful/index.gmi"), \
relpath(dirname, ENVIRON["PWD"] "/src/sitemap.gmi")
relpath(getDirname(FILENAME), ENVIRON["PWD"] "/src/index.gmi"), \
relpath(getDirname(FILENAME), ENVIRON["PWD"] "/src/notes/index.gmi"), \
relpath(getDirname(FILENAME), ENVIRON["PWD"] "/src/useful/index.gmi"), \
relpath(getDirname(FILENAME), ENVIRON["PWD"] "/src/sitemap.gmi")
}
{ print }

View File

@ -1,7 +1,7 @@
BEGIN {
title = "techn0path - no title"
articleBody = ""
tocText = ""
subHeadings[0] = ""
FS = " "
}
@ -20,14 +20,11 @@ BEGIN {
articleBody = articleBody sprintf("\
<h2 id='%s'>%s</h2>\n\
<a href='#%s'>Permalink</a><br><br>\n", \
convertToFilename(headingText), \
toFilename(headingText), \
headingText, \
convertToFilename(headingText))
toFilename(headingText))
if (tocText == "")
tocText = "<details id='toc'> <summary>Table of content</summary> <nav> <ol>\n"
tocText = tocText sprintf("<li><a href='#%s'>%s</a></li>\n", convertToFilename(headingText), headingText)
subHeadings[length(subHeadings)] = headingText
}
else articleBody = articleBody sprintf("<h%i>%s</h%i>\n", headingLevel, headingText, headingLevel)
next
@ -103,8 +100,15 @@ BEGIN {
}
END {
if (tocText != "")
tocText = tocText "</ol> </nav> </details>"
if (length(subHeadings) > 1) {
tocText = "<details id='toc'> <summary>Table of content</summary> <nav> <ol>\n"
for (i = 1; i < length(subHeadings); i++) {
tocText = tocText sprintf("<li><a href='#%s'>%s</a></li>\n", toFilename(subHeadings[i]), subHeadings[i])
}
tocText = tocText "</ol> </nav> </details>\n"
}
navbarText = sprintf("<nav id='navbar'>\n\
<a href='%s'>home</a> | \

View File

@ -22,9 +22,7 @@ FILENAME != filename {
sub("^([ ]*)", "", link)
sub("^([ ]*)", "", description)
printf "%s\n", resolvePath(ENVIRON["PWD"] "/" getDirname(FILENAME) "/" link)
link = relpath(ENVIRON["PWD"] "/src", abspath(ENVIRON["PWD"], getDirname(FILENAME) "/" link))
link = relpath(ENVIRON["PWD"] "/src", resolvePath(ENVIRON["PWD"] "/" getDirname(FILENAME) "/" link))
printf "=> %s %s\n", link, description
}

View File

@ -25,9 +25,9 @@ BEGIN {
gsub("'", "&apos;", description)
gsub("\"", "&quot;", description)
link = relpath(ENVIRON["PWD"] "/src", abspath(ENVIRON["PWD"], getDirname(FILENAME) "/" link))
link = relpath(ENVIRON["PWD"] "/src", resolvePath(ENVIRON["PWD"] "/" getDirname(FILENAME) "/" link))
if (link !~ "^(\w*):") sub("\.gmi$", ".html", link)
if (link !~ "^([a-zA-Z0-9_]):") sub("\.gmi$", ".html", link)
sitemapBody = sitemapBody sprintf("<a href='%s'>%s</a><br>\n", link, description)
}