From e4e71fc25db2c57ac72ba2ae103f868ffbfbd6ea Mon Sep 17 00:00:00 2001 From: Syndamia Date: Wed, 7 Aug 2024 18:09:37 +0300 Subject: feat(awk): Implemented reference links --- demo/src/index.md | 10 ++++++++++ meta.awk | 23 +++++++++++++++++++++++ single.awk | 32 +++++++++++++++++++++----------- 3 files changed, 54 insertions(+), 11 deletions(-) diff --git a/demo/src/index.md b/demo/src/index.md index 2032b72..b435f85 100644 --- a/demo/src/index.md +++ b/demo/src/index.md @@ -103,3 +103,13 @@ Lorem `ipsum` dolor tiada ad;l ``kfj `skigj`` jjsa - [ ] *A* - [x] **B** + +[me]: https://syndamia.com + +Some links: [Google][], [this is a page about me][me] + +[Google]: "Google's website" + +![Picture of me][mepic] + +[mepic]: https://syndamia.com/img/face.jpeg (Photo of myself) diff --git a/meta.awk b/meta.awk index 98651fd..9171250 100644 --- a/meta.awk +++ b/meta.awk @@ -1,6 +1,10 @@ function emit(ind, val) { print "meta[\"" FILENAME "\", \"" ind "\"]=\"" val "\"" } +function erit(id, href, title) { + print "reference[\"" id "\", \"href\"]=\"" href "\"" + print "reference[\"" id "\", \"title\"]=\"\\\"" title "\\\"\"" +} title != "" && stopdesc == 0 && /^[[:alpha:]]/ { startedGathering = 1 @@ -29,6 +33,25 @@ title == "" && /^# / { title = $0 } +/^\[[^^].*\]:/ { + match($0, /^\[.+\]:/) + id = substr($0, 2, RLENGTH - 3) + + href = substr($0, RLENGTH + 1) + reftitle = "" + if (match(href, /[ \t]+["'(].*[)'"]$/)) { + reftitle = substr(href, RSTART) + href = substr(href, 1, RSTART - 1) + + sub(/^[ \t]["'(]*/, "", reftitle) + sub(/[)'"][ \t]*$/, "", reftitle) + } + sub(/^[ \t]*?[ \t]*$/, "", href) + + erit(id, href, reftitle) +} + END { emit("title", title) emit("description", desc) diff --git a/single.awk b/single.awk index 415e042..f366ead 100644 --- a/single.awk +++ b/single.awk @@ -81,6 +81,12 @@ function autoinsert(arrname, isbefore) { next } +# Link definition +/^\[.+\]:/ { + blankline = 1 + next +} + # Auto-escape & /&/ { ampstart = 0 @@ -252,29 +258,33 @@ blankline { } # Links -/!?\[[^\]]*\]\([^)]*\)/ { +/!?\[[^\]]*\] ?[(\[][^)\]]*[\])]/ { isimg = 0 - while (match($0, /!?\[[^\]]*\]\(/)) { + while (match($0, /!?\[[^\]]*\] ?[(\[]/)) { if (substr($0, RSTART, 1) == "!") { isimg = 1 RSTART++ RLENGTH-- } astart = RSTART ## astart = indexof("[") - inner_anchor = substr($0, RSTART + 1, RLENGTH - 3) ## ...[xxxxx](...)... + has_space = substr($0, RSTART + RLENGTH - 2, 1) == " " + inner_anchor = substr($0, RSTART + 1, RLENGTH - 3 - has_space) ## ...[xxxxx](...)... aend = RSTART + RLENGTH - 1 - match(substr($0, astart), /\]\([^)]*\)/) - aend += RLENGTH - 2 ## aend = indexof(")") - href = substr($0, astart + RSTART + 1, RLENGTH - 3) ## ...[...](xxxxx)... - - if (match(href, /[ \t]"[^"]*"[ \t]*$/)) { + match(substr($0, astart), /\] ?[(\[][^)\]]*[\])]/) + aend += RLENGTH - 2 - has_space ## aend = indexof(")") + href = substr($0, astart + RSTART + 1 + has_space, RLENGTH - 3 - has_space) ## ...[...](xxxxx)... + + title = "\"\"" + if (substr($0, astart + RSTART + has_space, 1) == "[") { ## is reference link + if (href == "") href = inner_anchor + title = reference[href, "title"] + href = reference[href, "href"] + } + else if (match(href, /[ \t]"[^"]*"[ \t]*$/)) { ## has title title = substr(href, RSTART + 1, RLENGTH - 1) href = substr(href, 1, RSTART - 1) } - else { - title = "\"\"" - } sub(/[ \t]+/, "", href) if (isimg) elem = "\""" -- cgit v1.3.1-23-gd82d