Skip to content

Commit

Permalink
Create site with documentation #164 : fix #links
Browse files Browse the repository at this point in the history
  • Loading branch information
xonixx committed Mar 6, 2024
1 parent 5d879fc commit 8380ddc
Showing 1 changed file with 23 additions and 9 deletions.
32 changes: 23 additions & 9 deletions mdbooker.awk
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,39 @@ BEGIN {
H = 0
Title = Content = ""
delete PathElements
delete Link2Path
}

match($0, /^#+/) { handleTitle(RLENGTH); next }
{ Content = Content "\n" $0; next }
match($0, /^#+/) { handleTitle(RLENGTH, 1) }

function handleTitle(h, md,indent,dir,i,path) {
function handleTitle(h,pass, md,indent,dir,i,path) {
if (Title) {
for (i = 2; i < H; i++)
dir = dir (dir ? "-" : "") fname(PathElements[i])
print "generating: " (path = dir (dir ? "-" : "") (md = fname(Title) ".md")) "..."
print Content > BOOK path
if ((indent = H - 2) < 0)
indent = 0
printf "%" (indent * 4) "s%s[%s](%s)\n", "", 1 == H ? "" : "- ", Title, path >> SUMMARY
path = dir (dir ? "-" : "") (md = fname(Title) ".md")
if (pass == 1) {
Link2Path[linkify(Title)] = path
} else {
print "generating: " path "..."
print Content > BOOK path
if ((indent = H - 2) < 0)
indent = 0
printf "%" (indent * 4) "s%s[%s](%s)\n", "", 1 == H ? "" : "- ", Title, path >> SUMMARY
}
}
Content = "# " (Title = PathElements[H = h] = trim(substr($0, h + 1)))
}

END { handleTitle(-1) }
END { handleTitle(-1, 1); pass2() }

function pass2() {
while (getline < FILENAME > 0) {
if (match($0, /^#+/)) { handleTitle(RLENGTH, 2) }
else { Content = Content "\n" $0 }
}
handleTitle(-1, 2)
}

function linkify(t) { t = tolower(t); gsub(/ /, "-", t); gsub(/[^-a-z0-9_]/, "", t); return t }
function fname(s) { gsub(/ /, "_", s); return s }
function trim(s) { sub(/^[ \t\r\n]+/, "", s); sub(/[ \t\r\n]+$/, "", s); return s }

0 comments on commit 8380ddc

Please sign in to comment.