Add support for underlined headers

This commit is contained in:
Konstantin Nazarov 2021-07-10 22:45:07 +00:00
parent e15d8a56ef
commit eacefbbbe8
Signed by: knazarov
GPG key ID: 4CFE0A42FA409C22
2 changed files with 31 additions and 6 deletions

View file

@ -3,12 +3,23 @@ BEGIN {
in_code = 0
}
function parse_header(str) {
match(str, /#+/);
hnum = RLENGTH;
function parse_header(str, hnum, content) {
if (substr(str, 1, 1) == "#") {
match(str, /#+/);
hnum = RLENGTH;
content = parse_line(substr(str, hnum + 1, length(str) - hnum ));
return "<h" hnum ">" content "</h" hnum ">";
content = parse_line(substr(str, hnum + 1, length(str) - hnum ));
return "<h" hnum ">" content "</h" hnum ">";
}
if (match(body, /^[^\n]+\n=+$/)) {
gsub(/\n=+$/, "", str);
return "<h1>" parse_line(str) "</h1>"
}
if (match(body, /^[^\n]+\n-+$/)) {
gsub(/\n-+$/, "", str);
return "<h2>" parse_line(str) "</h2>"
}
return "";
}
function read_line(str, pos, res, i) {
@ -254,7 +265,7 @@ function parse_block(str) {
if (match(str, /^```\n.*```$/) || match(str, /^ /)) {
return parse_code(str);
}
if (substr(str, 1, 1) == "#") {
if (substr(str, 1, 1) == "#" || match(body, /^[^\n]+\n[-=]+$/)) {
return parse_header(str);
}
else if (substr(str, 1, 1) == ">") {
@ -278,6 +289,9 @@ function line_continues(body, line) {
if (match(body, /^#* /))
return 0;
if (match(body, /^[^\n]+\n[-=]+$/))
return 0;
if (line != "")
return 1;

11
test.sh
View file

@ -86,6 +86,17 @@ check <<-EOF
<h3> Header3</h3>
EOF
check <<-EOF
underlined header 1
===================
underlined header 2
-------------------
---
<h1>underlined header 1</h1>
<h2>underlined header 2</h2>
EOF
check <<-EOF
**bold**
---