X-Git-Url: https://git.plutz.net/?p=cgilite;a=blobdiff_plain;f=markdown.awk;h=6271996440dab3b37132f8eccdecbf6094a4db99;hp=e63541bebfa78ffc70053ca23a1d94ff6b586c66;hb=9bb22561275a9ce0d5403a12fe7e8aeeeb2792e9;hpb=b3075fd95ed1f474fbe0a4eff68228e72a7ec653 diff --git a/markdown.awk b/markdown.awk index e63541b..6271996 100755 --- a/markdown.awk +++ b/markdown.awk @@ -46,9 +46,9 @@ # - ? Simple table (pandoc) # - ? Multiline table (pandoc) # - ? Grid table (pandoc) -# - ? Pipe table (php md pandoc) +# - [x] Pipe table (php md pandoc) # - [x] Line blocks (pandoc) -# - [x] Task lists (pandoc) +# - [x] Task lists (pandoc, custom) # - [ ] Definition lists (php md, pandoc) # - [-] Numbered example lists (pandoc) # - [-] Metadata blocks (pandoc) @@ -296,6 +296,53 @@ function _block( block, LOCAL, st, len, hlvl, htxt, guard, code, indent, attrib return "
\n" _block( gensub( /(^|\n)> /, "\n", "g", substr(block, 1, st - 1) ) ) "
\n\n" \ _block( substr(block, st + len) ); + # Pipe Tables (pandoc / php md / gfm ) + } else if ( match(block, "^((\\|)?([^\n]+\\|)+[^\n]+(\\|)?)\n" \ + "((\\|)?:?(-+:?[\\|+])+:?-+:?(\\|)?)\n" \ + "((\\|)?([^\n]+\\|)+[^\n]+(\\|)?(\n|$))+" ) ) { + len = RLENGTH; st = RSTART; + #initialize empty arrays + split("", talign); split("", tarray); + cols = 0; ttext = ""; cnt=0; + + # table header and alignment + split( gensub( /(^\||\|$)/, "", "g", \ + gensub( /(^|[^\\])\\\|/, "\\1\\|", "g", \ + substr(block, 1, match(block, /(\n|$)/)) \ + )), tarray, /\|/); + block = substr(block, match(block, /(\n|$)/) + 1 ); + cols = split( \ + gensub( /(^\||\|$)/, "", "g", \ + substr(block, 1, match(block, /(\n|$)/)) \ + ), talign, /[+\|]/); + block = substr(block, match(block, /(\n|$)/) + 1 ); + + for( cnt = 1; cnt < cols; cnt++ ) { + if (match(talign[cnt], /:-+:/)) talign[cnt]="center"; + else if (match(talign[cnt], /-+:/)) talign[cnt]="right"; + else if (match(talign[cnt], /:-+/)) talign[cnt]="left"; + else talign[cnt]=""; + } + + ttext = "\n" + for (cnt = 1; cnt < cols; cnt++) + ttext = ttext "" inline(tarray[cnt]) "" + ttext = ttext "\n\n" + + while ( match(block, "^((\\|)?([^\n]+\\|)+[^\n]+(\\|)?(\n|$))+" ) ){ + split( gensub( /(^\||\|$)/, "", "g", \ + gensub( /(^|[^\\])\\\|/, "\\1\\|", "g", \ + substr(block, 1, match(block, /(\n|$)/)) \ + )), tarray, /\|/); + block = substr(block, match(block, /(\n|$)/) + 1 ); + + ttext = ttext "" + for (cnt = 1; cnt < cols; cnt++) + ttext = ttext "" inline(tarray[cnt]) "" + ttext = ttext "\n" + } + return "" ttext "
\n" _block( substr(block, len + 1) ); + # Line Blocks (pandoc) } else if ( match(block, /^\| [^\n]*(\n|$)(\| [^\n]*(\n|$)|[ \t]+[^\n[:space:]][^\n]*(\n|$))*/) ) { len = RLENGTH; st = RSTART; @@ -337,7 +384,7 @@ function _block( block, LOCAL, st, len, hlvl, htxt, guard, code, indent, attrib } else if ( match( block, /^(~~~+|```+)/ ) ) { guard = substr( block, 1, RLENGTH ); code = gensub(/^[^\n]+\n/, "", 1, block); - attrib = gensub(/^:::+[ \t]*\{?[ \t]*([^\}\n]*)\}?[ \t]*\n.*$/, "\\1", 1, block); + attrib = gensub(/^(~~~+|```+)[ \t]*\{?[ \t]*([^\}\n]*)\}?[ \t]*\n.*$/, "\\2", 1, block); gsub(/[^a-zA-Z0-9_-]+/, " ", attrib); gsub(/(^ | $)/, "", attrib); if ( match(code, "(^|\n)" guard "+(\n|$)" ) ) { @@ -440,12 +487,28 @@ function _list( block, last, LOCAL, p) { } sub( /\n$/, "", p ); - # Task List (pandoc) - if ( p ~ /^\[ \].*/ ) { p = "" substr(p, 4); } - else if ( p ~ /^\[[xX]\].*/ ) { p = "" substr(p, 4); } - else if ( p ~ /^

\[ \].*/ ) { p = "

" substr(p, 7); } - else if ( p ~ /^

\[[xX]\].*/ ) { p = "

" substr(p, 7); } - return "

  • " p "
  • \n" _list( block, last ); + # Task List (pandoc, custom) + if ( p ~ /^\[ \].*/ ) { return "
  • " \ + substr(p, 4) "
  • \n" _list( block, last ); + } else if ( p ~ /^\[-\].*/ ) { return "
  • " \ + substr(p, 4) "
  • \n" _list( block, last ); + } else if ( p ~ /^\[\?\].*/ ) { return "
  • " \ + substr(p, 4) "
  • \n" _list( block, last ); + } else if ( p ~ /^\[\/\].*/ ) { return "
  • " \ + substr(p, 4) "
  • \n" _list( block, last ); + } else if ( p ~ /^\[[xX]\].*/ ) { return "
  • " \ + substr(p, 4) "
  • \n" _list( block, last ); + } else if ( p ~ /^

    \[ \].*/ ) { return "

  • " \ + substr(p, 7) "

  • \n" _list( block, last ); + } else if ( p ~ /^

    \[-\].*/ ) { return "

  • " \ + substr(p, 7) "

  • \n" _list( block, last ); + } else if ( p ~ /^

    \[\?\].*/ ) { return "

  • " \ + substr(p, 7) "

  • \n" _list( block, last ); + } else if ( p ~ /^

    \[\/\].*/ ) { return "

  • " \ + substr(p, 7) "

  • \n" _list( block, last ); + } else if ( p ~ /^

    \[[xX]\].*/ ) { return "

  • " \ + substr(p, 7) "

  • \n" _list( block, last ); + } else { return "
  • " p "
  • \n" _list( block, last ); } } BEGIN {