From 937a39c9c0cd7ad9cd8dcc0c74ace00e38fc2838 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Sun, 21 Jul 2024 13:58:08 +0300 Subject: (single) Implemented inline links and fixed some minor issues --- single.awk | 62 +++++++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 41 insertions(+), 21 deletions(-) (limited to 'single.awk') 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) "&" substr($0, ampstart + 1) + } +} +# Auto-escape < +/[[:blank:]]*$/ { next } /^> / { @@ -61,12 +82,6 @@ BEGIN { } } -# Inline HTML -/^[[:blank:]]*<\/?[[:alnum:]]+/ { - print - next -} - # Horizontal rules /^(-|\*|_) ?(-|\*|_) ?(-|\*|_|[[:blank:]])+$/ { print "
" @@ -76,7 +91,7 @@ BEGIN { # Line break / $/ { - sub(/ $/, "
") + sub(/[[:blank:]]* $/, "
") } # 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 "

" } -!prev_blankline && blankline && lengthStack("bq-level") == 0 { print "

" } +prev_blankline && !blankline && lengthStack("bq-level") == 0 && lengthStack("list-level") == 0 { print "

" } +!prev_blankline && blankline && lengthStack("bq-level") == 0 && lengthStack("list-level") == 0 { print "

" } # 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) "&" 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) "" inner_anchor "" substr($0, aend + 1) } } -# Auto-escape < -/