first commit
This commit is contained in:
27
awk/collect_dir_info.awk
Normal file
27
awk/collect_dir_info.awk
Normal file
@@ -0,0 +1,27 @@
|
||||
BEGIN {
|
||||
slen = length(src)
|
||||
}
|
||||
|
||||
{
|
||||
if (length($0) <= slen) {
|
||||
next
|
||||
}
|
||||
|
||||
rel = substr($0, slen + 2)
|
||||
parent = rel
|
||||
if (sub(/\/[^\/]+$/, "", parent) == 0) {
|
||||
parent = "."
|
||||
}
|
||||
|
||||
all[parent]++
|
||||
if (is_dir[rel]) {
|
||||
dirs[parent]++
|
||||
}
|
||||
is_dir[parent] = 1
|
||||
}
|
||||
|
||||
END {
|
||||
for (parent in all) {
|
||||
printf "%s|%d|%d\n", parent, all[parent], dirs[parent]
|
||||
}
|
||||
}
|
||||
104
awk/generate_sidebar.awk
Normal file
104
awk/generate_sidebar.awk
Normal file
@@ -0,0 +1,104 @@
|
||||
function title_from_name(name) {
|
||||
gsub(/\.md$/, "", name)
|
||||
gsub(/-/, " ", name)
|
||||
return name
|
||||
}
|
||||
|
||||
BEGIN {
|
||||
n_dlines = split(dinfo, dlines, "\n")
|
||||
for (i = 1; i <= n_dlines; i++) {
|
||||
if (split(dlines[i], dparts, "|") == 3) {
|
||||
d_all[dparts[1]] = dparts[2]
|
||||
d_dirs[dparts[1]] = dparts[3]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
rel = substr($0, length(src) + 2)
|
||||
all_paths[rel] = 1
|
||||
ordered_paths[count++] = rel
|
||||
|
||||
dir = rel
|
||||
if (sub(/\/[^\/]+$/, "", dir) == 0) {
|
||||
dir = "."
|
||||
}
|
||||
|
||||
md_count[dir]++
|
||||
if (rel ~ /index\.md$/) {
|
||||
has_index[dir] = 1
|
||||
}
|
||||
}
|
||||
|
||||
END {
|
||||
print "<ul>"
|
||||
if ("index.md" in all_paths) {
|
||||
print "<li><a href=\"/index.html\">Home</a></li>"
|
||||
}
|
||||
|
||||
depth = 0
|
||||
prev_n = 0
|
||||
|
||||
for (idx = 0; idx < count; idx++) {
|
||||
rel = ordered_paths[idx]
|
||||
if (rel == "index.md") {
|
||||
continue
|
||||
}
|
||||
|
||||
n = split(rel, parts, "/")
|
||||
common = 0
|
||||
for (i = 1; i < n && i < prev_n; i++) {
|
||||
if (parts[i] == prev_parts[i]) {
|
||||
common = i
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
while (depth > 0 && opened_levels[depth] > common) {
|
||||
print "</ul></li>"
|
||||
delete opened_levels[depth]
|
||||
depth--
|
||||
}
|
||||
|
||||
for (i = common + 1; i < n; i++) {
|
||||
dir_path = ""
|
||||
for (j = 1; j <= i; j++) {
|
||||
dir_path = dir_path parts[j] "/"
|
||||
}
|
||||
|
||||
this_d = ""
|
||||
for (j = 1; j <= i; j++) {
|
||||
this_d = (this_d == "" ? parts[j] : this_d "/" parts[j])
|
||||
}
|
||||
|
||||
if (flatten == "true" && d_all[this_d] == 1 && d_dirs[this_d] == 1) {
|
||||
continue
|
||||
}
|
||||
|
||||
printf "<li><a href=\"/%sindex.html\">%s</a><ul>\n", dir_path, title_from_name(parts[i])
|
||||
opened_levels[++depth] = i
|
||||
}
|
||||
|
||||
curr_dir = rel
|
||||
if (sub(/\/[^\/]+$/, "", curr_dir) == 0) {
|
||||
curr_dir = "."
|
||||
}
|
||||
is_single = (single_file_index == "true" && md_count[curr_dir] == 1 && !has_index[curr_dir])
|
||||
|
||||
if (parts[n] != "index.md" && !is_single) {
|
||||
path = "/" rel
|
||||
gsub(/\.md$/, ".html", path)
|
||||
printf "<li><a href=\"%s\">%s</a></li>\n", path, title_from_name(parts[n])
|
||||
}
|
||||
|
||||
prev_n = n
|
||||
split(rel, prev_parts, "/")
|
||||
}
|
||||
|
||||
while (depth > 0) {
|
||||
print "</ul></li>"
|
||||
depth--
|
||||
}
|
||||
print "</ul>"
|
||||
}
|
||||
538
awk/markdown_embed.awk
Normal file
538
awk/markdown_embed.awk
Normal file
@@ -0,0 +1,538 @@
|
||||
function is_global_url(src) {
|
||||
return (src ~ /^https?:\/\//)
|
||||
}
|
||||
|
||||
function split_src(src, base, qpos, hpos, cutpos) {
|
||||
base = src
|
||||
qpos = index(base, "?")
|
||||
hpos = index(base, "#")
|
||||
cutpos = 0
|
||||
if (qpos > 0) cutpos = qpos
|
||||
if (hpos > 0 && (cutpos == 0 || hpos < cutpos)) cutpos = hpos
|
||||
if (cutpos > 0) base = substr(base, 1, cutpos - 1)
|
||||
return base
|
||||
}
|
||||
|
||||
function ext_of(src, base, n, parts) {
|
||||
base = split_src(src)
|
||||
n = split(base, parts, ".")
|
||||
if (n < 2) return ""
|
||||
return tolower(parts[n])
|
||||
}
|
||||
|
||||
function is_image_ext(ext) {
|
||||
return (ext ~ /^(png|jpe?g|gif|bmp|webp|svg|ico)$/)
|
||||
}
|
||||
|
||||
function is_audio_ext(ext) {
|
||||
return (ext ~ /^(mp3|wav|ogg|m4a|aac|flac)$/)
|
||||
}
|
||||
|
||||
function is_video_ext(ext) {
|
||||
return (ext ~ /^(mp4|webm|ogv|mov|m4v)$/)
|
||||
}
|
||||
|
||||
function is_inline_text_ext(ext) {
|
||||
return (ext ~ /^(html|txt|md|css|js|mjs|cjs|json|xml|yml|yaml|toml|ini|conf|c|h|cpp|hpp|rs|go|py|sh|lua|php|java|kt|swift|sql|csv|tsv|log)$/)
|
||||
}
|
||||
|
||||
function dirname_of(path, p) {
|
||||
p = path
|
||||
if (sub(/\/[^\/]*$/, "", p)) return p
|
||||
return "."
|
||||
}
|
||||
|
||||
function resolve_local_path(src, rel, candidate) {
|
||||
rel = split_src(src)
|
||||
if (substr(rel, 1, 1) == "/") {
|
||||
rel = substr(rel, 2)
|
||||
if (site_root != "") {
|
||||
candidate = site_root "/" rel
|
||||
if ((getline _tmp < candidate) >= 0) {
|
||||
close(candidate)
|
||||
return candidate
|
||||
}
|
||||
close(candidate)
|
||||
}
|
||||
candidate = rel
|
||||
if ((getline _tmp < candidate) >= 0) {
|
||||
close(candidate)
|
||||
return candidate
|
||||
}
|
||||
close(candidate)
|
||||
|
||||
if (rel == "styles.css" && fallback_file != "") {
|
||||
candidate = fallback_file
|
||||
if ((getline _tmp < candidate) >= 0) {
|
||||
close(candidate)
|
||||
return candidate
|
||||
}
|
||||
close(candidate)
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
candidate = input_dir "/" rel
|
||||
if ((getline _tmp < candidate) >= 0) {
|
||||
close(candidate)
|
||||
return candidate
|
||||
}
|
||||
close(candidate)
|
||||
|
||||
candidate = rel
|
||||
if ((getline _tmp < candidate) >= 0) {
|
||||
close(candidate)
|
||||
return candidate
|
||||
}
|
||||
close(candidate)
|
||||
return ""
|
||||
}
|
||||
|
||||
function read_file(path, out, line, rc) {
|
||||
out = ""
|
||||
while ((rc = getline line < path) > 0) {
|
||||
out = out line "\n"
|
||||
}
|
||||
close(path)
|
||||
return out
|
||||
}
|
||||
|
||||
function escape_html(s, t) {
|
||||
t = s
|
||||
gsub(/&/, "\\&", t)
|
||||
gsub(/</, "\\<", t)
|
||||
gsub(/>/, "\\>", t)
|
||||
return t
|
||||
}
|
||||
|
||||
function css_highlight_line(line, m, prop, val) {
|
||||
if (line ~ /^[[:space:]]*\/\*.*\*\/[[:space:]]*$/) {
|
||||
return "<span class=\"tok-com\">" line "</span>"
|
||||
}
|
||||
|
||||
if (line ~ /^[[:space:]]*[^{}][^{}]*\{[[:space:]]*$/) {
|
||||
sub(/\{[[:space:]]*$/, "", line)
|
||||
return "<span class=\"tok-sel\">" line "</span><span class=\"tok-punc\">{</span>"
|
||||
}
|
||||
|
||||
if (line ~ /^[[:space:]]*\}[[:space:]]*$/) {
|
||||
return "<span class=\"tok-punc\">}</span>"
|
||||
}
|
||||
|
||||
if (match(line, /^([[:space:]]*)(--?[A-Za-z0-9_-]+)([[:space:]]*:[[:space:]]*)([^;]*)(;?[[:space:]]*)$/, m)) {
|
||||
prop = "<span class=\"tok-prop\">" m[2] "</span>"
|
||||
gsub(/var\(--[A-Za-z0-9_-]+\)/, "<span class=\"tok-var\">&</span>", m[4])
|
||||
val = "<span class=\"tok-val\">" m[4] "</span>"
|
||||
return m[1] prop m[3] val m[5]
|
||||
}
|
||||
|
||||
return line
|
||||
}
|
||||
|
||||
function highlight_code_block_line(line) {
|
||||
return css_highlight_line(line)
|
||||
}
|
||||
|
||||
function highlight_css_block(text, n, i, lines, out) {
|
||||
n = split(text, lines, "\n")
|
||||
out = ""
|
||||
for (i = 1; i <= n; i++) {
|
||||
out = out css_highlight_line(lines[i])
|
||||
if (i < n) out = out "\n"
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
function render_code_include(src, force_inline, ext, local_path, content) {
|
||||
if (is_global_url(src)) return ""
|
||||
|
||||
ext = ext_of(src)
|
||||
if (!force_inline && !is_inline_text_ext(ext)) return ""
|
||||
|
||||
local_path = resolve_local_path(src)
|
||||
if (local_path == "") return ""
|
||||
|
||||
content = read_file(local_path)
|
||||
if (content ~ /\n$/) sub(/\n$/, "", content)
|
||||
content = escape_html(content)
|
||||
if (ext == "css") {
|
||||
content = highlight_css_block(content)
|
||||
}
|
||||
return content
|
||||
}
|
||||
|
||||
function render_embed(src, alt, has_alt, force_inline, ext, local_path, content) {
|
||||
if (force_inline && !is_global_url(src)) {
|
||||
local_path = resolve_local_path(src)
|
||||
if (local_path != "") {
|
||||
content = read_file(local_path)
|
||||
if (content ~ /\n$/) sub(/\n$/, "", content)
|
||||
return content
|
||||
}
|
||||
}
|
||||
|
||||
ext = ext_of(src)
|
||||
|
||||
if (is_global_url(src)) {
|
||||
if (is_image_ext(ext)) {
|
||||
if (has_alt) return "<img alt=\"" alt "\" src=\"" src "\" />"
|
||||
return "<img src=\"" src "\" />"
|
||||
}
|
||||
if (is_audio_ext(ext)) return "<audio controls src=\"" src "\"></audio>"
|
||||
if (is_video_ext(ext)) return "<video controls src=\"" src "\"></video>"
|
||||
return "<iframe src=\"" src "\"></iframe>"
|
||||
}
|
||||
|
||||
if (is_image_ext(ext)) {
|
||||
if (has_alt) return "<img alt=\"" alt "\" src=\"" src "\" />"
|
||||
return "<img src=\"" src "\" />"
|
||||
}
|
||||
if (is_audio_ext(ext)) return "<audio controls src=\"" src "\"></audio>"
|
||||
if (is_video_ext(ext)) return "<video controls src=\"" src "\"></video>"
|
||||
|
||||
if (is_inline_text_ext(ext)) {
|
||||
local_path = resolve_local_path(src)
|
||||
if (local_path != "") {
|
||||
content = read_file(local_path)
|
||||
if (content ~ /\n$/) sub(/\n$/, "", content)
|
||||
return content
|
||||
}
|
||||
}
|
||||
|
||||
return "<iframe src=\"" src "\"></iframe>"
|
||||
}
|
||||
|
||||
function extract_attr(tag, attr, pat, m, token) {
|
||||
pat = attr "=\"[^\"]*\""
|
||||
if (match(tag, pat)) {
|
||||
token = substr(tag, RSTART, RLENGTH)
|
||||
sub(/^[^=]*="/, "", token)
|
||||
sub(/"$/, "", token)
|
||||
return token
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
function trim_ws(s) {
|
||||
sub(/^[[:space:]]+/, "", s)
|
||||
sub(/[[:space:]]+$/, "", s)
|
||||
return s
|
||||
}
|
||||
|
||||
function extract_vertical_align(style, rest, part, pos, key, val) {
|
||||
rest = style
|
||||
while (rest != "") {
|
||||
pos = index(rest, ";")
|
||||
if (pos > 0) {
|
||||
part = substr(rest, 1, pos - 1)
|
||||
rest = substr(rest, pos + 1)
|
||||
} else {
|
||||
part = rest
|
||||
rest = ""
|
||||
}
|
||||
part = trim_ws(part)
|
||||
if (part == "") continue
|
||||
pos = index(part, ":")
|
||||
if (pos == 0) continue
|
||||
key = tolower(trim_ws(substr(part, 1, pos - 1)))
|
||||
val = trim_ws(substr(part, pos + 1))
|
||||
if (key == "vertical-align" && val != "") return val
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
function td_has_vertical_align(td_tag, style_attr) {
|
||||
style_attr = extract_attr(td_tag, "style")
|
||||
if (style_attr == "") return 0
|
||||
return (extract_vertical_align(style_attr) != "")
|
||||
}
|
||||
|
||||
function add_td_vertical_align(td_tag, align, style_attr, repl) {
|
||||
style_attr = extract_attr(td_tag, "style")
|
||||
if (style_attr == "") {
|
||||
sub(/>$/, " style=\"vertical-align: " align ";\">", td_tag)
|
||||
return td_tag
|
||||
}
|
||||
repl = style_attr
|
||||
if (repl !~ /;[[:space:]]*$/) repl = repl ";"
|
||||
repl = repl " vertical-align: " align ";"
|
||||
gsub(/&/, "\\&", repl)
|
||||
gsub(/</, "\\<", repl)
|
||||
gsub(/>/, "\\>", repl)
|
||||
sub("style=\"" style_attr "\"", "style=\"" repl "\"", td_tag)
|
||||
return td_tag
|
||||
}
|
||||
|
||||
function apply_td_vertical_align(line, out, rest, seg, td_tag, img_tag, after_td, after_img, style_attr, align, new_td) {
|
||||
out = ""
|
||||
rest = line
|
||||
while (match(rest, /<td[^>]*>[[:space:]]*<img[^>]*>/)) {
|
||||
out = out substr(rest, 1, RSTART - 1)
|
||||
seg = substr(rest, RSTART, RLENGTH)
|
||||
rest = substr(rest, RSTART + RLENGTH)
|
||||
|
||||
after_td = index(seg, ">")
|
||||
if (after_td == 0) {
|
||||
out = out seg
|
||||
continue
|
||||
}
|
||||
td_tag = substr(seg, 1, after_td)
|
||||
after_img = index(seg, "<img")
|
||||
if (after_img == 0) {
|
||||
out = out seg
|
||||
continue
|
||||
}
|
||||
img_tag = substr(seg, after_img)
|
||||
|
||||
style_attr = extract_attr(img_tag, "style")
|
||||
align = extract_vertical_align(style_attr)
|
||||
if (align != "" && !td_has_vertical_align(td_tag)) {
|
||||
new_td = add_td_vertical_align(td_tag, align)
|
||||
seg = new_td substr(seg, after_td + 1)
|
||||
}
|
||||
out = out seg
|
||||
}
|
||||
return out rest
|
||||
}
|
||||
|
||||
function rewrite_img_tags(line, out, rest, tag, src, alt, force_inline_tag, pre, post, repl) {
|
||||
out = ""
|
||||
rest = line
|
||||
while (match(rest, /<img[^>]*\/?>/)) {
|
||||
pre = substr(rest, 1, RSTART - 1)
|
||||
tag = substr(rest, RSTART, RLENGTH)
|
||||
post = substr(rest, RSTART + RLENGTH)
|
||||
src = extract_attr(tag, "src")
|
||||
alt = extract_attr(tag, "alt")
|
||||
force_inline_tag = extract_attr(tag, "data-force-inline")
|
||||
if (is_image_ext(ext_of(src)) && force_inline_tag == "") {
|
||||
# Preserve hand-written <img> attributes (style/class/etc) for normal images.
|
||||
repl = tag
|
||||
} else {
|
||||
repl = render_embed(src, alt, (alt != ""), (force_inline_tag != ""))
|
||||
}
|
||||
out = out pre repl
|
||||
rest = post
|
||||
}
|
||||
return out rest
|
||||
}
|
||||
|
||||
function rewrite_double_bang_with_parens(line, out, rest, token, inside, src, alt, sep, pre, post, repl) {
|
||||
out = ""
|
||||
rest = line
|
||||
while (match(rest, /!!\[[^]]*\]\([^)]*\)/)) {
|
||||
pre = substr(rest, 1, RSTART - 1)
|
||||
token = substr(rest, RSTART, RLENGTH)
|
||||
post = substr(rest, RSTART + RLENGTH)
|
||||
|
||||
inside = token
|
||||
sub(/^!!\[/, "", inside)
|
||||
sep = index(inside, "](")
|
||||
alt = substr(inside, 1, sep - 1)
|
||||
src = substr(inside, sep + 2)
|
||||
sub(/\)$/, "", src)
|
||||
|
||||
repl = render_embed(src, alt, (alt != ""), 1)
|
||||
out = out pre repl
|
||||
rest = post
|
||||
}
|
||||
return out rest
|
||||
}
|
||||
|
||||
function rewrite_double_bang_bare(line, out, rest, token, src, pre, post, repl) {
|
||||
out = ""
|
||||
rest = line
|
||||
while (match(rest, /!!\[[^]]+\]/)) {
|
||||
pre = substr(rest, 1, RSTART - 1)
|
||||
token = substr(rest, RSTART, RLENGTH)
|
||||
post = substr(rest, RSTART + RLENGTH)
|
||||
src = token
|
||||
sub(/^!!\[/, "", src)
|
||||
sub(/\]$/, "", src)
|
||||
repl = render_embed(src, "", 0, 1)
|
||||
out = out pre repl
|
||||
rest = post
|
||||
}
|
||||
return out rest
|
||||
}
|
||||
|
||||
function rewrite_bare_bang(line, out, rest, token, src, pre, post, repl) {
|
||||
out = ""
|
||||
rest = line
|
||||
while (match(rest, /!\[[^]]+\]/)) {
|
||||
pre = substr(rest, 1, RSTART - 1)
|
||||
token = substr(rest, RSTART, RLENGTH)
|
||||
post = substr(rest, RSTART + RLENGTH)
|
||||
src = token
|
||||
sub(/^!\[/, "", src)
|
||||
sub(/\]$/, "", src)
|
||||
repl = render_embed(src, "", 0, 0)
|
||||
out = out pre repl
|
||||
rest = post
|
||||
}
|
||||
return out rest
|
||||
}
|
||||
|
||||
function rewrite_noncode_line(line, out, rest, pstart, pend, code_seg, noncode) {
|
||||
out = ""
|
||||
rest = line
|
||||
while (1) {
|
||||
pstart = index(rest, "<code>")
|
||||
if (pstart == 0) {
|
||||
noncode = rest
|
||||
noncode = rewrite_img_tags(noncode)
|
||||
noncode = rewrite_double_bang_with_parens(noncode)
|
||||
noncode = rewrite_double_bang_bare(noncode)
|
||||
noncode = rewrite_bare_bang(noncode)
|
||||
out = out noncode
|
||||
break
|
||||
}
|
||||
|
||||
noncode = substr(rest, 1, pstart - 1)
|
||||
noncode = rewrite_img_tags(noncode)
|
||||
noncode = rewrite_double_bang_with_parens(noncode)
|
||||
noncode = rewrite_double_bang_bare(noncode)
|
||||
noncode = rewrite_bare_bang(noncode)
|
||||
out = out noncode
|
||||
|
||||
rest = substr(rest, pstart)
|
||||
pend = index(rest, "</code>")
|
||||
if (pend == 0) {
|
||||
out = out rest
|
||||
break
|
||||
}
|
||||
code_seg = substr(rest, 1, pend + length("</code>") - 1)
|
||||
out = out code_seg
|
||||
rest = substr(rest, pend + length("</code>"))
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
function rewrite_code_double_bang_with_parens(line, out, rest, token, inside, src, sep, pre, post, repl) {
|
||||
out = ""
|
||||
rest = line
|
||||
while (match(rest, /!!\[[^]]*\]\([^)]*\)/)) {
|
||||
pre = substr(rest, 1, RSTART - 1)
|
||||
token = substr(rest, RSTART, RLENGTH)
|
||||
post = substr(rest, RSTART + RLENGTH)
|
||||
|
||||
inside = token
|
||||
sub(/^!!\[/, "", inside)
|
||||
sep = index(inside, "](")
|
||||
src = substr(inside, sep + 2)
|
||||
sub(/\)$/, "", src)
|
||||
|
||||
repl = render_code_include(src, 1)
|
||||
if (repl == "") repl = token
|
||||
out = out pre repl
|
||||
rest = post
|
||||
}
|
||||
return out rest
|
||||
}
|
||||
|
||||
function rewrite_code_double_bang_bare(line, out, rest, token, src, pre, post, repl) {
|
||||
out = ""
|
||||
rest = line
|
||||
while (match(rest, /!!\[[^]]+\]/)) {
|
||||
pre = substr(rest, 1, RSTART - 1)
|
||||
token = substr(rest, RSTART, RLENGTH)
|
||||
post = substr(rest, RSTART + RLENGTH)
|
||||
src = token
|
||||
sub(/^!!\[/, "", src)
|
||||
sub(/\]$/, "", src)
|
||||
repl = render_code_include(src, 1)
|
||||
if (repl == "") repl = token
|
||||
out = out pre repl
|
||||
rest = post
|
||||
}
|
||||
return out rest
|
||||
}
|
||||
|
||||
function rewrite_code_bang_with_parens(line, out, rest, token, inside, src, sep, pre, post, repl) {
|
||||
out = ""
|
||||
rest = line
|
||||
while (match(rest, /!\[[^]]*\]\([^)]*\)/)) {
|
||||
pre = substr(rest, 1, RSTART - 1)
|
||||
token = substr(rest, RSTART, RLENGTH)
|
||||
post = substr(rest, RSTART + RLENGTH)
|
||||
|
||||
inside = token
|
||||
sub(/^!\[/, "", inside)
|
||||
sep = index(inside, "](")
|
||||
src = substr(inside, sep + 2)
|
||||
sub(/\)$/, "", src)
|
||||
|
||||
repl = render_code_include(src, 0)
|
||||
if (repl == "") repl = token
|
||||
out = out pre repl
|
||||
rest = post
|
||||
}
|
||||
return out rest
|
||||
}
|
||||
|
||||
function rewrite_code_bare_bang(line, out, rest, token, src, pre, post, repl) {
|
||||
out = ""
|
||||
rest = line
|
||||
while (match(rest, /!\[[^]]+\]/)) {
|
||||
pre = substr(rest, 1, RSTART - 1)
|
||||
token = substr(rest, RSTART, RLENGTH)
|
||||
post = substr(rest, RSTART + RLENGTH)
|
||||
src = token
|
||||
sub(/^!\[/, "", src)
|
||||
sub(/\]$/, "", src)
|
||||
repl = render_code_include(src, 0)
|
||||
if (repl == "") repl = token
|
||||
out = out pre repl
|
||||
rest = post
|
||||
}
|
||||
return out rest
|
||||
}
|
||||
|
||||
function restore_plain_markers(line) {
|
||||
gsub(/\034P0\034/, "*", line)
|
||||
gsub(/\034P1\034/, "_", line)
|
||||
gsub(/\034P2\034/, "`", line)
|
||||
gsub(/\034P3\034/, "[", line)
|
||||
gsub(/\034P4\034/, "]", line)
|
||||
gsub(/\034P5\034/, "(", line)
|
||||
gsub(/\034P6\034/, ")", line)
|
||||
gsub(/\034P7\034/, "!", line)
|
||||
gsub(/\034P8\034/, "$", line)
|
||||
gsub(/<mfmplain>/, "<span class=\"mfm-plain\">", line)
|
||||
gsub(/<\/mfmplain>/, "</span>", line)
|
||||
return line
|
||||
}
|
||||
|
||||
BEGIN {
|
||||
input_dir = dirname_of(input_file)
|
||||
in_pre_code = 0
|
||||
}
|
||||
|
||||
{
|
||||
line = $0
|
||||
|
||||
start_pre = (line ~ /<pre><code>/)
|
||||
end_pre = (line ~ /<\/code><\/pre>/)
|
||||
|
||||
if (in_pre_code || start_pre) {
|
||||
gsub(/\\!\[/, "\034ESC_BANG_OPEN\034", line)
|
||||
line = rewrite_code_double_bang_with_parens(line)
|
||||
line = rewrite_code_double_bang_bare(line)
|
||||
line = rewrite_code_bang_with_parens(line)
|
||||
line = rewrite_code_bare_bang(line)
|
||||
gsub(/\034ESC_BANG_OPEN\034/, "![", line)
|
||||
line = highlight_code_block_line(line)
|
||||
} else {
|
||||
line = rewrite_noncode_line(line)
|
||||
}
|
||||
|
||||
line = apply_td_vertical_align(line)
|
||||
line = restore_plain_markers(line)
|
||||
print line
|
||||
|
||||
if (start_pre && !end_pre) {
|
||||
in_pre_code = 1
|
||||
} else if (in_pre_code && end_pre) {
|
||||
in_pre_code = 0
|
||||
}
|
||||
}
|
||||
32
awk/render_template.awk
Normal file
32
awk/render_template.awk
Normal file
@@ -0,0 +1,32 @@
|
||||
function replace_all(text, token, value, pos, token_len) {
|
||||
token_len = length(token)
|
||||
while ((pos = index(text, token)) > 0) {
|
||||
text = substr(text, 1, pos - 1) value substr(text, pos + token_len)
|
||||
}
|
||||
return text
|
||||
}
|
||||
|
||||
{
|
||||
line = $0
|
||||
line = replace_all(line, "{{TITLE}}", title)
|
||||
line = replace_all(line, "{{NAV}}", nav)
|
||||
line = replace_all(line, "{{FOOTER}}", footer)
|
||||
line = replace_all(line, "{{CSS}}", style_path)
|
||||
line = replace_all(line, "{{HEAD_EXTRA}}", head_extra)
|
||||
line = replace_all(line, "{{HEADER_BRAND}}", header_brand)
|
||||
|
||||
pos = index(line, "{{CONTENT}}")
|
||||
if (pos > 0) {
|
||||
printf "%s", substr(line, 1, pos - 1)
|
||||
while ((getline content_line < "-") > 0) {
|
||||
gsub(/\.md\)/, ".html)", content_line)
|
||||
gsub(/\.md"/, ".html\"", content_line)
|
||||
gsub(/\.md\?/, ".html?", content_line)
|
||||
gsub(/\.md#/, ".html#", content_line)
|
||||
print content_line
|
||||
}
|
||||
printf "%s\n", substr(line, pos + 11)
|
||||
} else {
|
||||
print line
|
||||
}
|
||||
}
|
||||
64
awk/replace_variables.awk
Normal file
64
awk/replace_variables.awk
Normal file
@@ -0,0 +1,64 @@
|
||||
{
|
||||
raw[++n] = $0
|
||||
lines[n] = $0
|
||||
rest = $0
|
||||
|
||||
while (match(rest, /--[A-Za-z0-9_-]+[[:space:]]*:[[:space:]]*[^;]+;/)) {
|
||||
decl = substr(rest, RSTART, RLENGTH)
|
||||
|
||||
name = decl
|
||||
sub(/:.*/, "", name)
|
||||
gsub(/[[:space:]]/, "", name)
|
||||
|
||||
value = decl
|
||||
sub(/^[^:]*:[[:space:]]*/, "", value)
|
||||
sub(/;[[:space:]]*$/, "", value)
|
||||
|
||||
vars[name] = value
|
||||
rest = substr(rest, RSTART + RLENGTH)
|
||||
}
|
||||
}
|
||||
|
||||
END {
|
||||
in_root = 0
|
||||
for (i = 1; i <= n; i++) {
|
||||
line = raw[i]
|
||||
if (!in_root && line ~ /:root[[:space:]]*\{/) {
|
||||
in_root = 1
|
||||
lines[i] = ""
|
||||
if (line ~ /\}/) {
|
||||
in_root = 0
|
||||
}
|
||||
continue
|
||||
}
|
||||
if (in_root) {
|
||||
lines[i] = ""
|
||||
if (line ~ /\}/) {
|
||||
in_root = 0
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (i = 1; i <= n; i++) {
|
||||
line = lines[i]
|
||||
if (line == "") {
|
||||
continue
|
||||
}
|
||||
changed = 1
|
||||
iter = 0
|
||||
|
||||
while (changed && iter < 10) {
|
||||
changed = 0
|
||||
iter++
|
||||
for (name in vars) {
|
||||
token = "var(" name ")"
|
||||
while ((pos = index(line, token)) > 0) {
|
||||
line = substr(line, 1, pos - 1) vars[name] substr(line, pos + length(token))
|
||||
changed = 1
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
print line
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user