Summary Files Commits Branches @commit @diff @stats
path: root / single.awk
diff options
context:
space:
mode:
Commit937a39c9c0cd7ad9cd8dcc0c74ace00e38fc2838 (patch)
Author
 Syndamia <kamen@syndamia.com>
2024-07-21 13:58:08 +0300
Committer
 Syndamia <kamen@syndamia.com>
2024-07-21 13:58:08 +0300
Files45dd9d74d755d28386a7cded3249ab63ad4ede35 /single.awk
csg-937a39c9c0cd7ad9cd8dcc0c74ace00e38fc2838.tar
csg-937a39c9c0cd7ad9cd8dcc0c74ace00e38fc2838.tar.gz
csg-937a39c9c0cd7ad9cd8dcc0c74ace00e38fc2838.zip
Parentab0ff28184b83587297ac1c9f11e110d6b76a80f (diff)
(single) Implemented inline links and fixed some minor issues
1 files changed, 41 insertions, 21 deletions
-rw-r--r--single.awk62
diff --git a/single.awk b/single.awk
index eadb7d1..4e2e19a 100644
--- a/single.awk
+++ b/single.awk
@@ -43,6 +43,27 @@ BEGIN {
blankline = 0
}
+# Inline HTML
+/^[[:blank:]]*<\/?[[:alnum:]]+/ {
+ print
+ next
+}
+
+# Auto-escape &
+/&/ {
+ ampstart = 0
+ while (match(substr($0, ampstart + 1), /&/)) {
+ ampstart += RSTART
+ if (match(substr($0, ampstart), /&[[:alnum:]#]+;/)) { continue }
+
+ $0 = substr($0, 1, ampstart - 1) "&amp;" substr($0, ampstart + 1)
+ }
+}
+# Auto-escape <
+/</ {
+ gsub(/</, "\\&lt;")
+}
+
# Blockquote Part 1/2
/^>[[:blank:]]*$/ { next }
/^> / {
@@ -61,12 +82,6 @@ BEGIN {
}
}
-# Inline HTML
-/^[[:blank:]]*<\/?[[:alnum:]]+/ {
- print
- next
-}
-
# Horizontal rules
/^(-|\*|_) ?(-|\*|_) ?(-|\*|_|[[:blank:]])+$/ {
print "<hr>"
@@ -76,7 +91,7 @@ BEGIN {
# Line break
/ $/ {
- sub(/ $/, "</br>")
+ sub(/[[:blank:]]* $/, "</br>")
}
# atx-style headers
@@ -124,11 +139,10 @@ function abs(x) { return ((x < 0) ? -x : x) }
}
$0 = substr($0, RSTART + RLENGTH)
}
-lengthStack("list-level") > 0 { blankline = 1 }
# Paragraphs
-prev_blankline && !blankline && lengthStack("bq-level") == 0 { print "<p>" }
-!prev_blankline && blankline && lengthStack("bq-level") == 0 { print "</p>" }
+prev_blankline && !blankline && lengthStack("bq-level") == 0 && lengthStack("list-level") == 0 { print "<p>" }
+!prev_blankline && blankline && lengthStack("bq-level") == 0 && lengthStack("list-level") == 0 { print "</p>" }
# Blockquote Part 2/2
blankline {
@@ -138,20 +152,26 @@ blankline {
}
}
-# Auto-escape &
-/&/ {
- ampstart = 0
- while (match(substr($0, ampstart + 1), /&/)) {
- ampstart += RSTART
- if (match(substr($0, ampstart), /&[[:alnum:]#]+;/)) { continue }
+# Links
+/\[[^\]]*\]\([^)]*\)/ {
+ while (match($0, /\[[^\]]*\]\(/)) {
+ astart = RSTART
+ inner_anchor = substr($0, RSTART + 1, RLENGTH - 3)
- $0 = substr($0, 1, ampstart - 1) "&amp;" substr($0, ampstart + 1)
+ aend = astart + RLENGTH - 1
+ match(substr($0, astart), /\]\([^)]*)/)
+ aend += RLENGTH - 2
+ href = substr($0, astart + RSTART + 1, RLENGTH - 3)
+
+ title = ""
+ if (match(href, /"[^"]*"[[:blank:]]*$/)) {
+ title = " title=" substr(href, RSTART, RLENGTH)
+ href = substr(href, 1, RSTART - 1)
+ }
+
+ $0 = substr($0, 1, astart - 1) "<a href=\"" href "\"" title ">" inner_anchor "</a>" substr($0, aend + 1)
}
}
-# Auto-escape <
-/</ {
- gsub(/</, "\\&lt;")
-}
# Emphasis
/__.+__/ {