Summary Files Commits Branches @commit @diff @stats
path: root / single.awk
diff options
context:
space:
mode:
Commit8e46a6d6e38cce30fd2a8212d058ae64b8e7fabb (patch)
Author
 Syndamia <kamen@syndamia.com>
2024-07-21 15:30:12 +0300
Committer
 Syndamia <kamen@syndamia.com>
2024-07-21 15:30:12 +0300
Filesdcc3439efd0ee324f425137df8d6088a0b2ea9fc /single.awk
csg-8e46a6d6e38cce30fd2a8212d058ae64b8e7fabb.tar
csg-8e46a6d6e38cce30fd2a8212d058ae64b8e7fabb.tar.gz
csg-8e46a6d6e38cce30fd2a8212d058ae64b8e7fabb.zip
Parent90b77f288c5f62b5119861976517910e2230ec00 (diff)
(single) Implemented setex-style headings
1 files changed, 47 insertions, 26 deletions
-rw-r--r--single.awk73
diff --git a/single.awk b/single.awk
index 1ceb577..46ff93d 100644
--- a/single.awk
+++ b/single.awk
@@ -4,7 +4,7 @@
# [ ] Block elements
# [x] Paragraph
# [x] Line break
-# [.] Headers (atx and setex)
+# [x] Headers (atx and setex)
# [x] Blockquotes
# [x] Lists
# [ ] Code blocks
@@ -32,10 +32,18 @@
BEGIN {
prev_blankline = blankline = 1
+ skipprint = 1
newStack("bq-level")
newStack("list-level")
}
+function println(line) {
+ if (0 == line) line = $0
+ if (!skipprint) print prev
+ prev = line
+ skipprint = 0
+}
+
{
prev_blankline = blankline
blankline = 1
@@ -47,7 +55,7 @@ BEGIN {
# Inline HTML
/^[[:blank:]]*<\/?[[:alnum:]]+/ {
- print
+ println()
next
}
@@ -74,41 +82,50 @@ BEGIN {
$0 = substr($0, RLENGTH + 1)
level += RSTART
while (topStack("bq-level") > level) {
- print "</blockquote>"
+ println("</blockquote>")
popStack("bq-level")
}
if (topStack("bq-level") < level) {
- print "<blockquote>"
+ println("<blockquote>")
pushStack("bq-level", level)
}
}
}
-# Horizontal rules
-/^(-|\*|_) ?(-|\*|_) ?(-|\*|_|[[:blank:]])+$/ {
- print "<hr>"
- blankline = 1
- next
-}
-
-# Line break
-/ $/ {
- sub(/[[:blank:]]* $/, "</br>")
-}
-
# atx-style headers
/^[[:blank:]]*#+/ {
match($0, /#+/)
if (RLENGTH <= 6) {
gsub(/^[[:blank:]]*#+[[:blank:]]*/, "")
gsub(/[[:blank:]]*#+[[:blank:]]*$/, "")
- print "<h" RLENGTH ">" $0 "</h" RLENGTH ">"
+ println("<h" RLENGTH ">" $0 "</h" RLENGTH ">")
blankline = 1
next
}
}
+# setex-style headers
+!prev_blankline && /^[[:blank:]]*(=|-)+[[:blank:]]*$/ {
+ sub(/^<p>/, "", prev)
+ hlevel = 1 + ($0 ~ /-/)
+ print "<h" hlevel ">" prev "</h" hlevel ">"
+ prev = ""
+ blankline = 1
+ skipprint = 1
+ next
+}
+
+# Horizontal rules
+/^(-|\*|_) ?(-|\*|_) ?(-|\*|_|[[:blank:]])+$/ {
+ println("<hr>")
+ blankline = 1
+ next
+}
+
+# Line break
+/ $/ {
+ sub(/[[:blank:]]* $/, "</br>")
+}
-# setex-style headers TODO
# Code blocks
/^( |\t)[[:blank:]]*/ {
@@ -123,8 +140,8 @@ function abs(x) { return ((x < 0) ? -x : x) }
match($0, /(\*|\+|-|[[:digit:]]+\.) /)
if (RLENGTH == -1) RLENGTH = 0
while (indent + RLENGTH < abs(topStack("list-level"))) {
- if (topStack("list-level") < 0) print "</ol>"
- else print "</ul>"
+ if (topStack("list-level") < 0) println("</ol>")
+ else println("</ul>")
popStack("list-level")
}
}
@@ -133,23 +150,23 @@ function abs(x) { return ((x < 0) ? -x : x) }
if (abs(topStack("list-level")) < RSTART) {
if (RLENGTH > 2) { xl = "<ol>"; mult = -1 }
else { xl = "<ul>"; mult = 1 }
- print xl "<li>"
+ println(xl "<li>")
pushStack("list-level", mult * (RSTART + RLENGTH - 1))
}
else {
- print "</li><li>"
+ println("</li><li>")
}
$0 = substr($0, RSTART + RLENGTH)
}
# Paragraphs
-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>" }
+prev_blankline && !blankline && lengthStack("bq-level") == 0 && lengthStack("list-level") == 0 { $0 = "<p>" $0 }
+!prev_blankline && blankline && lengthStack("bq-level") == 0 && lengthStack("list-level") == 0 { prev = prev "</p>" }
# Blockquote Part 2/2
blankline {
while (lengthStack("bq-level") > 0) {
- print "</blockquote>"
+ println("</blockquote>")
popStack("bq-level")
}
}
@@ -194,9 +211,13 @@ function surround(brace, opening, closing) {
surround("`", "<code>", "</code>")
}
-{ print }
+
+{
+ println()
+}
END {
+ print prev
if (!blankline && lengthStack("bq-level") == 0) { print "</p>" }
while (lengthStack("bq-level") > 0) {
print "</blockquote>"