]> git.plutz.net Git - cgilite/blobdiff - markdown.awk
introducing definition lists!
[cgilite] / markdown.awk
index 8ac8b6323dc20277efc35767f8d140a2afc17957..be7d723b6b96d0efa7ccdc9ecc5be38c881f1026 100755 (executable)
@@ -40,6 +40,7 @@
 # ----------------------------
 # - [x] Automatic <section>-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
@@ -53,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)
@@ -307,7 +308,7 @@ function inline( line, LOCAL, len, code, href, guard ) {
   }
 }
 
-function headline( hlvl, htxt, LOCAL, sec, n, HL) {
+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]?"</section>":""); }
@@ -319,8 +320,8 @@ function headline( hlvl, htxt, LOCAL, sec, n, HL) {
   hstack = gensub( /^(.* )?([0-9]+( [0-9]+){5})$/, "\\1" ,"1", hstack) \
            HL[1] " " HL[2] " " HL[3] " " HL[4] " " HL[5] " " HL[6];
 
-  return sec "<section class=\"h" hlvl "\" id=\"" hid "\">" \
-         "<h" hlvl ">" inline( htxt ) \
+  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";
 }
@@ -379,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 )
@@ -589,28 +590,71 @@ 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;
+    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 ) );
+
   # First Order Heading H1
-  } else if ( match( block, /^[^\n]+\n===+(\n|$)/ ) ) {
+  } else if ( match( block, /^([^\n]+)\n===+(\n|$)/ ) ) {
     len = RLENGTH;
+    text   = gensub(/^([^\n]+)\n===+(\n.*)?$/, "\\1", 1, block)
 
-    return headline(1, gensub( /\n.*$/, "", "g", block )) \
+    return headline(1, text, 0) \
            _block( substr( block, len + 1 ) );
 
+  # 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) );
+
   # Second Order Heading H2
-  } else if ( match( block, /^[^\n]+\n---+(\n|$)/ ) ) {
+  } else if ( match( block, /^([^\n]+)\n---+(\n|$)/ ) ) {
     len = RLENGTH;
+    text   = gensub(/^([^\n]+)\n---+(\n.*)?$/, "\\1", 1, block)
 
-    return headline(2, gensub( /\n.*$/, "", "g", block )) \
+    return headline(2, text, 0) \
+           _block( substr( block, len + 1) );
+
+  # 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);
+
+    return headline( length(n), text, attrib ) \
            _block( substr( block, len + 1) );
 
   # Nth Order Heading H1 H2 H3 H4 H5 H6
-  } else if ( match( block, /^#{1,6}[ \t]*[^\n]+([ \t]*#*)(\n|$)/ ) ) {
+  } 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 headline( \
-             length( gensub( /^(#{1,6}).*$/, "\\1", "g", block ) ), \
-             gensub(/^#{1,6}[ \t]*(([^ \t\n]+|[ \t]+[^ \t\n#]|[ \t]+#+[^\n#])+)([ \t]*#*)(\n.*)?$/, "\\1", 1, block) \
-           ) \
+    return headline( length(n), text, 0 ) \
            _block( substr( block, len + 1) );
 
   # block images (wrapped in <figure>)
@@ -688,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)