From: Paul Hänsch Date: Sun, 2 Jul 2023 22:43:01 +0000 (+0200) Subject: Squashed 'cgilite/' changes from 9b2f5908..854547d4 X-Git-Url: https://git.plutz.net/?a=commitdiff_plain;h=f5a7a98f217d9a047cba2c9c92c21baa46d9e0db;p=shellwiki Squashed 'cgilite/' changes from 9b2f5908..854547d4 854547d4 headline atttributes b4a3f6a4 heading text in id 33570b0d block level prefix to avoid repeating headline IDs 516bc32d nesting of
in block elements 925f0423 unified headline function git-subtree-dir: cgilite git-subtree-split: 854547d423e0a1bc2d4437cb15e7c51d8be4904a --- diff --git a/markdown.awk b/markdown.awk index 0e6d949..506d22d 100755 --- a/markdown.awk +++ b/markdown.awk @@ -40,6 +40,7 @@ # ---------------------------- # - [x] Automatic
-wrapping (custom) # - ? Heading identifiers (php md, pandoc) +# - [x] Heading attributes (custom) # - [x] Automatic heading identifiers (custom) # - [x] Fenced code blocks (php md, pandoc) # - [x] Fenced code attributes @@ -307,19 +308,37 @@ function inline( line, LOCAL, len, code, href, guard ) { } } +function headline( hlvl, htxt, attrib, LOCAL, sec, n, HL) { + split( gensub( /^(.* )?([0-9]+( [0-9]+){5})$/, "\\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 = ""; for ( n = 2; n <= blvl; n++) { hid = hid BL[n] "/"; } + hid = hid HL[1]; for ( n = 2; n <= hlvl; n++) { hid = hid "." HL[n] ; } + hid = hid ":" URL(htxt, 1); + + hstack = gensub( /^(.* )?([0-9]+( [0-9]+){5})$/, "\\1" ,"1", hstack) \ + HL[1] " " HL[2] " " HL[3] " " HL[4] " " HL[5] " " HL[6]; + + return sec "
" \ + "" inline( htxt ) \ + "" \ + "\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; +function _nblock( block, LOCAL, hlsav, sec, n ) { + hlsav = hstack; + hstack = hstack " 0 0 0 0 0 0"; # Block Level - blvl++; + blvl++; BL[blvl]++; + for ( n = blvl + 1; n in BL; n++) { delete BL[n]; } block = _block( block ); + split( gensub( /^(.* )?([0-9]+( [0-9]+){5})$/, "\\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; } @@ -571,50 +590,60 @@ function _block( block, LOCAL, st, len, hlvl, htxt, sec, guard, code, indent, at gsub("(^|\n) {0," indent "}", "\n", list); return "\n
    \n" _list( substr(list, 2) ) "
\n" _block( block ); - # First Order Heading H1 - } else if ( match( block, /^[^\n]+\n===+(\n|$)/ ) ) { - len = RLENGTH; sec = ""; + # First Order Heading H1 + Attrib + } else if ( match( block, /^([^\n]+)([ \t]*\{([^\}\n]+)\})\n===+(\n|$)/ ) ) { + len = RLENGTH; + text = gensub(/^([^\n]+)([ \t]*\{([^\}\n]+)\})\n===+(\n.*)?$/, "\\1", 1, block) + attrib = gensub(/^([^\n]+)([ \t]*\{([^\}\n]+)\})\n===+(\n.*)?$/, "\\3", 1, block) + gsub(/[^a-zA-Z0-9_-]+/, " ", attrib); gsub(/(^ | $)/, "", attrib); + + return headline(1, text, attrib) \ + _block( substr( block, len + 1 ) ); - 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); + # First Order Heading H1 + } else if ( match( block, /^([^\n]+)\n===+(\n|$)/ ) ) { + len = RLENGTH; + text = gensub(/^([^\n]+)\n===+(\n.*)?$/, "\\1", 1, block) - return sec "
1)?"":" id=\"" hid "\"") ">" \ - "

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

\n\n" \ + return headline(1, text, 0) \ _block( substr( block, len + 1 ) ); - # Second Order Heading H2 - } else if ( match( block, /^[^\n]+\n---+(\n|$)/ ) ) { - len = RLENGTH; sec = ""; + # Second Order Heading H2 + Attrib + } else if ( match( block, /^([^\n]+)([ \t]*\{([^\}\n]+)\})\n---+(\n|$)/ ) ) { + len = RLENGTH; + text = gensub(/^([^\n]+)([ \t]*\{([^\}\n]+)\})\n---+(\n.*)?$/, "\\1", 1, block) + attrib = gensub(/^([^\n]+)([ \t]*\{([^\}\n]+)\})\n---+(\n.*)?$/, "\\3", 1, block) + gsub(/[^a-zA-Z0-9_-]+/, " ", attrib); gsub(/(^ | $)/, "", attrib); + + return headline(2, text, attrib) \ + _block( substr( block, len + 1) ); - 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); + # Second Order Heading H2 + } else if ( match( block, /^([^\n]+)\n---+(\n|$)/ ) ) { + len = RLENGTH; + text = gensub(/^([^\n]+)\n---+(\n.*)?$/, "\\1", 1, block) - return sec "
1)?"":" id=\"" hid "\"") ">" \ - "

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

\n\n" \ + return headline(2, text, 0) \ _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 = ""; + # Nth Order Heading H1 H2 H3 H4 H5 H6 + Attrib + } else if ( match( block, /^(#{1,6})[ \t]*(([^ \t\n]+|[ \t]+[^ \t\n#]|[ \t]+#+[ \t]*[^ \t\n#])+)[ \t]*#*([ \t]*\{([a-zA-Z \t-]*)\})(\n|$)/ ) ) { + len = RLENGTH; + n = gensub(/^(#{1,6})[ \t]*(([^ \t\n]+|[ \t]+[^ \t\n#]|[ \t]+#+[ \t]*[^ \t\n#])+)[ \t]*#*([ \t]*\{([a-zA-Z \t-]*)\})(\n.*)?$/, "\\1", 1, block); + text = gensub(/^(#{1,6})[ \t]*(([^ \t\n]+|[ \t]+[^ \t\n#]|[ \t]+#+[ \t]*[^ \t\n#])+)[ \t]*#*([ \t]*\{([a-zA-Z \t-]*)\})(\n.*)?$/, "\\2", 1, block); + attrib = gensub(/^(#{1,6})[ \t]*(([^ \t\n]+|[ \t]+[^ \t\n#]|[ \t]+#+[ \t]*[^ \t\n#])+)[ \t]*#*([ \t]*\{([a-zA-Z \t-]*)\})(\n.*)?$/, "\\5", 1, block); + gsub(/[^a-zA-Z0-9_-]+/, " ", attrib); gsub(/(^ | $)/, "", attrib); - 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); + return headline( length(n), text, attrib ) \ + _block( substr( block, len + 1) ); - 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); + # Nth Order Heading H1 H2 H3 H4 H5 H6 + } else if ( match( block, /^(#{1,6})[ \t]*(([^ \t\n]+|[ \t]+[^ \t\n#]|[ \t]+#+[ \t]*[^ \t\n#])+)[ \t]*#*(\n|$)/ ) ) { + len = RLENGTH; + n = gensub(/^(#{1,6})[ \t]*(([^ \t\n]+|[ \t]+[^ \t\n#]|[ \t]+#+[ \t]*[^ \t\n#])+)[ \t]*#*(\n.*)?$/, "\\1", 1, block); + text = gensub(/^(#{1,6})[ \t]*(([^ \t\n]+|[ \t]+[^ \t\n#]|[ \t]+#+[ \t]*[^ \t\n#])+)[ \t]*#*(\n.*)?$/, "\\2", 1, block); - return sec "
1)?"":" id=\"" hid "\"") ">" \ - "" inline( htxt ) \ - ((blvl > 1)?"":"") \ - "\n\n" \ + return headline( length(n), text, 0 ) \ _block( substr( block, len + 1) ); # block images (wrapped in
)