Summary Files Commits Branches @commit @diff @stats
path: root / single.awk
diff options
context:
space:
mode:
Commite4e71fc25db2c57ac72ba2ae103f868ffbfbd6ea (patch)
Author
 Syndamia <kamen@syndamia.com>
2024-08-07 18:09:37 +0300
Committer
 Syndamia <kamen@syndamia.com>
2024-08-07 18:09:37 +0300
Files21dcdcf32f4fee388027568c81209b513ca71a5e /single.awk
csg-e4e71fc25db2c57ac72ba2ae103f868ffbfbd6ea.tar
csg-e4e71fc25db2c57ac72ba2ae103f868ffbfbd6ea.tar.gz
csg-e4e71fc25db2c57ac72ba2ae103f868ffbfbd6ea.zip
Parent707916075efadc55ce432095c333b3025cd01321 (diff)
feat(awk): Implemented reference links
1 files changed, 20 insertions, 10 deletions
-rw-r--r--single.awk30
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)...
+ match(substr($0, astart), /\] ?[(\[][^)\]]*[\])]/)
+ aend += RLENGTH - 2 - has_space ## aend = indexof(")")
+ href = substr($0, astart + RSTART + 1 + has_space, RLENGTH - 3 - has_space) ## ...[...](xxxxx)...
- if (match(href, /[ \t]"[^"]*"[ \t]*$/)) {
+ 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 = "<img src=\"" href "\" alt=\"" inner_anchor "\" title=" title ">"