]> git.plutz.net Git - cgilite/blobdiff - markdown.awk
tidy up variable declarations, remove additional line break after headline
[cgilite] / markdown.awk
index 506d22d08474b5f4a14dc5953c1dda121f4b9678..ae1cb934132d9d54c66755a850707d77db9a5217 100755 (executable)
@@ -54,7 +54,7 @@
 #   - [x] Pipe table (php md, pandoc)
 # - [x] Line blocks (pandoc)
 # - [x] Task lists (pandoc, custom)
-# - [ ] Definition lists (php md, pandoc)
+# - [x] Definition lists (php md, pandoc)
 # - [-] Numbered example lists (pandoc)
 # - [-] Metadata blocks (pandoc)
 # - [x] Metadata blocks (custom)
@@ -323,7 +323,7 @@ function headline( hlvl, htxt, attrib, LOCAL, sec, n, HL) {
   return sec "<section class=\"" (attrib ? "h" hlvl " " attrib : "h" hlvl)  "\" id=\"" hid "\">" \
          "<h" hlvl (attrib ? " class=\"" attrib "\"" : "") ">" inline( htxt ) \
          "<a class=\"anchor\" href=\"#" hid "\"></a>" \
-         "</h" hlvl ">\n\n";
+         "</h" hlvl ">\n";
 }
 
 # Nested Block, resets heading counters
@@ -342,7 +342,7 @@ function _nblock( block, LOCAL, hlsav, sec, n ) {
   return block sec;
 }
 
-function _block( block, LOCAL, st, len, hlvl, htxt, sec, guard, code, indent, attrib ) {
+function _block( block, LOCAL, st, len, text, title, attrib, href, guard, code, indent ) {
   gsub( /^\n+|\n+$/, "", block );
 
   if ( block == "" ) {
@@ -380,7 +380,7 @@ function _block( block, LOCAL, st, len, hlvl, htxt, sec, guard, code, indent, at
   } else if ( match( block, /^> /) ) {
     match( block, /(^|\n)[[:space:]]*(\n|$)/ ) || match(block, /$/);
     len = RLENGTH; st = RSTART;
-    return "<blockquote>\n" _nblock( gensub( /(^|\n)> /, "\n", "g", substr(block, 1, st - 1) ) ) "</blockquote>\n\n" \
+    return "<blockquote>" gensub( /^\n|\n$/, "", "g",  _nblock( gensub( /(^|\n)> /, "\n", "g", substr(block, 1, st - 1) ) ) ) "</blockquote>\n\n" \
            _block( substr(block, st + len) );
 
   # Pipe Tables (pandoc / php md / gfm )
@@ -514,11 +514,11 @@ function _block( block, LOCAL, st, len, hlvl, htxt, sec, guard, code, indent, at
   # Line Blocks (pandoc)
   } else if ( match(block, /^\| [^\n]*(\n|$)(\| [^\n]*(\n|$)|[ \t]+[^\n[:space:]][^\n]*(\n|$))*/) ) {
     len = RLENGTH; st = RSTART;
-    code = substr(block, 1, len);
-    gsub(/\n[[:space:]]+/, " ", code);
-    gsub(/\n\| /, "\n", code);
-    gsub(/^\| |\n$/, "", code);
-    return "<div class=\"line-block\">" gensub(/\n/, "<br>\n", "g", inline( code )) "</div>\n" \
+    text = substr(block, 1, len);
+    gsub(/\n[[:space:]]+/, " ", text);
+    gsub(/\n\| /, "\n", text);
+    gsub(/^\| |\n$/, "", text);
+    return "<div class=\"line-block\">" gensub(/\n/, "<br>\n", "g", inline( text )) "</div>\n" \
            _block( substr( block, len + 1) );
 
   # Indented Code Block
@@ -590,6 +590,17 @@ function _block( block, LOCAL, st, len, hlvl, htxt, sec, guard, code, indent, at
   gsub("(^|\n) {0," indent "}", "\n", list);
   return "\n<ol>\n" _list( substr(list, 2) ) "</ol>\n" _block( block );
 
+  # Definition list
+  } else if (match( block, "^(([ \t]*\n)*[^:\n \t][^\n]+\n" \
+                           "([ \t]*\n)* ? ? ?:[ \t][^\n]+(\n|$)" \
+                           "(([ \t]*\n)* ? ? ?:[ \t][^\n]+(\n|$)" \
+                           "|[^:\n \t][^\n]+(\n|$)" \
+                           "|( ? ? ?\t|  +)[^\n]+(\n|$)" \
+                           "|([ \t]*\n)+( ? ? ?\t|  +)[^\n]+(\n|$))*)+" \
+  )) {
+  list = substr( block, 1, RLENGTH); block = substr( block, RLENGTH + 1);
+  return "\n<dl>\n" _dlist( list ) "</dl>\n" _block( block );
+
   # First Order Heading H1 + Attrib
   } else if ( match( block, /^([^\n]+)([ \t]*\{([^\}\n]+)\})\n===+(\n|$)/ ) ) {
     len = RLENGTH;
@@ -721,6 +732,31 @@ function _block( block, LOCAL, st, len, hlvl, htxt, sec, guard, code, indent, at
   }
 }
 
+function _dlist (block, LOCAL, len, st, text, indent, p) {
+  if (match( block, "^([ \t]*\n)*[^:\n \t][^\n]+\n" )) {
+    len = RLENGTH; text = substr(block, 1, len);
+    gsub( "(^\n*|\n*$)", "", text );
+    return "<dt>" inline( text ) "</dt>\n" _dlist( substr(block, len + 1) );
+  } else if (match( block, "^([ \t]*\n)* ? ? ?:[ \t][^\n]+(\n|$)" \
+                         "([^:\n \t][^\n]+(\n|$)" \
+                         "|( ? ? ?\t|  +)[^\n]+(\n|$)" \
+                         "|([ \t]*\n)+( ? ? ?\t|  +)[^\n]+(\n|$))*" \
+  )) {
+    len = RLENGTH; text = substr(block, 1, len);
+    if (match( text, "^ ? ? ?:[ \t][^\n]+(\n|$)" \
+                    "([^:\n \t][^\n]+(\n|$)" \
+                    "|( ? ? ?\t|  +)[^\t\n ][^\n]*(\n|$))*$" \
+    )) p = 1; else p = 0;
+    sub( "^([ \t]*\n)*", "", text);
+    match(text, "^ ? ? ?:(\t| +)"); indent = RLENGTH;
+    sub( "^ ? ? ?:(\t| +)", "", text);
+    gsub( "(^|\n) {0," indent "}", "\n", text );
+    text = _block(text);
+    if (p) gsub("(^\n*<p>|</p>\n*$)", "", text);
+    return "<dd>" text "</dd>\n" _dlist( substr(block, len + 1) );
+  }
+}
+
 function _list( block, last, LOCAL, p) {
   if ( ! length(block) ) return "";
   gsub(/^([-+*]|[0-9]+\.|#\.)(  ? ? ?|\t)/, "", block)