From: Paul Hänsch Date: Thu, 22 Jun 2023 14:37:18 +0000 (+0200) Subject: unified headline function X-Git-Url: https://git.plutz.net/?a=commitdiff_plain;h=925f0423dcc9040fa530e9090db2b7c914243451;p=cgilite unified headline function --- diff --git a/markdown.awk b/markdown.awk index 0e6d949..a2bb989 100755 --- a/markdown.awk +++ b/markdown.awk @@ -307,19 +307,34 @@ function inline( line, LOCAL, len, code, href, guard ) { } } +function headline( hlvl, htxt, LOCAL, sec, n, HL) { + split( gensub( /^(.* )?(([0-9]+ ){5}[0-9]+)$/, "\\2" ,"1", hstack), HL); + + for ( n = hlvl; n <= 6; n++ ) { sec = sec (HL[n]?"":""); } + HL[hlvl]++; for ( n = hlvl + 1; n <= 6; n++) { HL[n] = 0;} + hid = HL[1]; for ( n = 2; n <= hlvl; n++) { hid = hid "." HL[n] ; } + # hid = hid ":" URL(htxt, 1); + + hstack = HL[1] " " HL[2] " " HL[3] " " HL[4] " " HL[5] " " HL[6]; + + return sec "
10)?"":" id=\"" hid "\"") ">" \ + "

" inline( htxt ) \ + ((blvl > 0)?"":"") \ + "

\n\n"; +} + # Nested Block, resets heading counters function _nblock( block, LOCAL, hlsav, sec ) { - # Keeping arrays in a local scope is awkward, so we serialize the HL array - # into the scalar hlsav - hlsav = HL[1] " " HL[2] " " HL[3] " " HL[4] " " HL[5] " " HL[6]; - HL[1] = 0; HL[2] = 0; HL[3] = 0; HL[4] = 0; HL[5] = 0; HL[6] = 0; + hlsav = hstack; + hstack = "0 0 0 0 0 0"; # Block Level blvl++; block = _block( block ); + split( gensub( /^(.* )?(([0-9]+ ){5}[0-9]+)$/, "\2" ,"1", hstack), HL); sec = ""; for ( n = 1; n <= 6; n++ ) { sec = sec (HL[n]?"
":""); } - split(hlsav, HL); blvl--; + hstack = hlsav; blvl--; return block sec; } @@ -573,48 +588,26 @@ function _block( block, LOCAL, st, len, hlvl, htxt, sec, guard, code, indent, at # First Order Heading H1 } else if ( match( block, /^[^\n]+\n===+(\n|$)/ ) ) { - len = RLENGTH; sec = ""; - - for ( n = 1; n <= 6; n++ ) { sec = sec (HL[n]?"":""); } - HL[1]++; HL[2] = 0; HL[3] = 0; HL[4] = 0; HL[5] = 0; HL[6] = 0; - hid = HL[1] ":" URL(gensub( /\n.*$/, "", "g", block ), 1); + len = RLENGTH; - return sec "
1)?"":" id=\"" hid "\"") ">" \ - "

" inline( gensub( /\n.*$/, "", "g", block ) ) \ - ((blvl > 1)?"":"") \ - "

\n\n" \ + return headline(1, gensub( /\n.*$/, "", "g", block )) \ _block( substr( block, len + 1 ) ); # Second Order Heading H2 } else if ( match( block, /^[^\n]+\n---+(\n|$)/ ) ) { - len = RLENGTH; sec = ""; - - for ( n = 2; n <= 6; n++ ) { sec = sec (HL[n]?"
":""); } - HL[2]++; HL[3] = 0; HL[4] = 0; HL[5] = 0; HL[6] = 0; - hid= HL[1] "." HL[2] ":" URL(gensub( /\n.*$/, "", "g", block ), 1); + len = RLENGTH; - return sec "
1)?"":" id=\"" hid "\"") ">" \ - "

" inline( gensub( /\n.*$/, "", "g", block ) ) \ - ((blvl > 1)?"":"") \ - "

\n\n" \ + return headline(2, gensub( /\n.*$/, "", "g", block )) \ _block( substr( block, len + 1) ); # Nth Order Heading H1 H2 H3 H4 H5 H6 } else if ( match( block, /^#{1,6}[ \t]*[^\n]+([ \t]*#*)(\n|$)/ ) ) { - len = RLENGTH; sec = ""; - - hlvl = length( gensub( /^(#{1,6}).*$/, "\\1", "g", block ) ); - htxt = gensub(/^#{1,6}[ \t]*(([^ \t\n]+|[ \t]+[^ \t\n#]|[ \t]+#+[^\n#])+)([ \t]*#*)(\n.*)?$/, "\\1", 1, block); - - for ( n = hlvl; n <= 6; n++ ) { sec = sec (HL[n]?"
":""); } - HL[hlvl]++; for ( n = hlvl + 1; n <= 6; n++) { HL[n] = 0;} - hid = HL[1]; for ( n = 2; n <= hlvl; n++) { hid = hid "." HL[n] ; } - hid = hid ":" URL(htxt, 1); + len = RLENGTH; - return sec "
1)?"":" id=\"" hid "\"") ">" \ - "" inline( htxt ) \ - ((blvl > 1)?"":"") \ - "\n\n" \ + return headline( \ + length( gensub( /^(#{1,6}).*$/, "\\1", "g", block ) ), \ + gensub(/^#{1,6}[ \t]*(([^ \t\n]+|[ \t]+[^ \t\n#]|[ \t]+#+[^\n#])+)([ \t]*#*)(\n.*)?$/, "\\1", 1, block) \ + ) \ _block( substr( block, len + 1) ); # block images (wrapped in
)