changed a bunch of code, added resolvePath functions in scripts/common_functions.awk, and renamed tmpl to scripts

This commit is contained in:
Abdullah Islam 2023-05-08 18:07:48 +06:00
parent b5ed80d2da
commit f35ce48f7d
8 changed files with 60 additions and 61 deletions

View File

@ -15,17 +15,17 @@ clean:
subdirs:
find src -mindepth 1 -type d -printf '%P\0' | xargs -0 -I{} mkdir -p gmi/{} html/{}
html/%.html: src/%.gmi tmpl/html.awk
mawk -f tmpl/common_functions.awk -f tmpl/html.awk -- $< > $@
html/%.html: src/%.gmi scripts/html.awk
mawk -f scripts/common_functions.awk -f scripts/html.awk -- $< > $@
gmi/%.gmi: src/%.gmi tmpl/gemini.awk
mawk -f tmpl/common_functions.awk -f tmpl/gemini.awk -- $< > $@
gmi/%.gmi: src/%.gmi scripts/gemini.awk
mawk -f scripts/common_functions.awk -f scripts/gemini.awk -- $< > $@
gmi/sitemap.gmi: $(SITEMAP_INDEX_FILES) tmpl/sitemap_gmi.awk
mawk -f tmpl/common_functions.awk -f tmpl/sitemap_gmi.awk -- $(SITEMAP_INDEX_FILES) > $@
gmi/sitemap.gmi: $(SITEMAP_INDEX_FILES) scripts/sitemap_gmi.awk
mawk -f scripts/common_functions.awk -f scripts/sitemap_gmi.awk -- $(SITEMAP_INDEX_FILES) > $@
html/sitemap.html: $(SITEMAP_INDEX_FILES) tmpl/sitemap_html.awk
mawk -f tmpl/common_functions.awk -f tmpl/sitemap_html.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) > $@
gmi/feed.atom: src/feed.ass tmpl/feed.awk
mawk -f tmpl/common_functions.awk -f tmpl/feed.awk -- $< > $@
gmi/feed.atom: src/feed.ass scripts/feed.awk
mawk -f scripts/common_functions.awk -f scripts/feed.awk -- $< > $@

View File

@ -1,41 +1,39 @@
function getDirname(path, pathComponents, dirname) {
pathComponents[1] = ""
sub("/$", "", path)
pathComponents[1] = ""
split(path, pathComponents, "/")
dirname = ""
if (length(pathComponents) < 2) return "."
else for (i = 1; i < length(pathComponents); i++)
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 resolvePath(base, path, resolvedPath, pathComponents) {
# pathComponents[1] = ""
# sub("/$", "", path)
# split(path, pathComponents, "/")
# if (pathComponents[1] == "")
# return path
# else if (pathComponents[1] == ".") {
# resolvedPath = base
# for (i = 1; i < length(pathComponents) + 1; i++)
resolvedPath = resolvedPath "/" pathComponents[i]
# }
# else if (pathComponents[1] == "..")
# resolvedPath = getDirname(base) path
# return resolvedPath
sprintf("realpath \"%s\"", path) | getline resolvedPath
return resolvedPath
}
function convertToFilename(string, result) {
function toFilename(string, result) {
result = tolower(string)
sub("^([ ]*)", "", result)
sub("([ ]*)$", "", result)
@ -43,6 +41,11 @@ function convertToFilename(string, 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

@ -4,9 +4,7 @@ BEGIN {
nfeedItems = 0
}
/^#/ { next } # skip comments
NF >= 2 {
NF >= 2 && $0 !~ "^#" {
date = $1
link = $2
@ -42,6 +40,7 @@ END {
uuid
for (i = 0; i < nfeedItems; i++) {
sprintf("date -d \"%s\" '+%%Y-%%m-%%dT%%TZ'", feedItems[i, "date"]) | getline date
printf "<entry>\n\
<title>%s</title>\n\
<link href='%s' />\n\
@ -52,7 +51,7 @@ END {
feedItems[i, "title"], \
feedItems[i, "link"], \
hashbased_uuidgen(feedItems[i, "title"] " - " feedItems[i, "date"], "@url"), \
feedItems[i, "date"], \
date, \
feedItems[i, "title"]
}
}

View File

@ -8,44 +8,42 @@ BEGIN {
/^#/ {
match($0, "^#([#]*)")
headingLevel = RLENGTH
heading = substr($0, RSTART + RLENGTH, length($0))
headingText = substr($0, RSTART + RLENGTH)
sub("^([ ]*)", "", heading)
sub("^([ ]*)", "", headingText)
if (headingLevel == 1) {
articleBody = articleBody sprintf("<h1>%s</h1>\n", heading)
title = heading
articleBody = articleBody sprintf("<h1>%s</h1>\n", headingText)
title = headingText
}
else if (headingLevel == 2) {
articleBody = articleBody sprintf("\
<h2 id='%s'>%s</h2>\n\
<a href='#%s'>Permalink</a><br><br>\n", \
convertToFilename(heading), \
heading, \
convertToFilename(heading))
convertToFilename(headingText), \
headingText, \
convertToFilename(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(heading), heading)
tocText = tocText sprintf("<li><a href='#%s'>%s</a></li>\n", convertToFilename(headingText), headingText)
}
else
articleBody = articleBody sprintf("<h%i>%s</h%i>\n", headingLevel, heading, headingLevel)
else articleBody = articleBody sprintf("<h%i>%s</h%i>\n", headingLevel, headingText, headingLevel)
next
}
/^=>/ {
match($0, "^=>([ ]*)([^ ]*)")
link = substr($0, 3, RSTART + RLENGTH - 3)
description = substr($0, RSTART + RLENGTH, length($0))
url = substr($0, 3, RSTART + RLENGTH - 3)
description = substr($0, RSTART + RLENGTH)
sub("^([ ]*)", "", link)
sub("^([ ]*)", "", url)
sub("^([ ]*)", "", description)
if (link !~ "^([a-zA-Z0-9_]*):")
sub("\.gmi$", ".html", link)
if (url !~ "^([a-zA-Z0-9_]*):")
sub("\.gmi$", ".html", url)
gsub("&", "&amp;", description)
gsub("<", "&lt;", description)
@ -53,8 +51,7 @@ BEGIN {
gsub("'", "&apos;", description)
gsub("\"", "&quot;", description)
articleBody = articleBody sprintf("<a href='%s'>%s</a><br>\n", link, description)
articleBody = articleBody sprintf("<a href='%s'>%s</a><br>\n", url, description)
next
}
@ -70,12 +67,10 @@ BEGIN {
gsub("\"", "\&quot;")
articleBody = articleBody $0 "\n"
getline
}
articleBody = articleBody "</pre>\n"
next
}
@ -89,12 +84,11 @@ BEGIN {
gsub("'", "\&apos;")
gsub("\"", "\&quot;")
articleBody = articleBody substr($0, RSTART + RLENGTH, length($0)) "\n"
articleBody = articleBody substr($0, RSTART + RLENGTH) "\n"
getline
}
articleBody = articleBody "</blockquote>\n"
next
}
@ -152,4 +146,5 @@ body { margin: 15px; }\n\
navbarText, \
tocText, \
articleBody
}

View File

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

View File

@ -25,7 +25,7 @@ BEGIN {
gsub("'", "&apos;", description)
gsub("\"", "&quot;", description)
link = relpath(ENVIRON["PWD"] "/src", resolvePath(ENVIRON["PWD"], getDirname(FILENAME) "/" link))
link = relpath(ENVIRON["PWD"] "/src", abspath(ENVIRON["PWD"], getDirname(FILENAME) "/" link))
if (link !~ "^(\w*):") sub("\.gmi$", ".html", link)