]> git.plutz.net Git - cgilite/blobdiff - markdown.awk
allow bullet symbol as list marker (for copy/paste from office documents)
[cgilite] / markdown.awk
index eb40210c9bc78c8cacadda40389c73df2e8d6d6b..6d22e3912e6f5e3580bffb61babf36b077117b39 100755 (executable)
@@ -5,6 +5,20 @@
 # Meant to support all features of John Grubers basic Markdown
 # + a number of common extensions, mostly inspired by Pandoc Markdown
 
+# Copyright 2021 - 2023 Paul Hänsch
+# 
+# Permission to use, copy, modify, and/or distribute this software for any
+# purpose with or without fee is hereby granted, provided that the above
+# copyright notice and this permission notice appear in all copies.
+# 
+# THE SOFTWARE IS PROVIDED “AS IS” AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
+# SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
+# IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
 # Supported Features / TODO:
 # ==========================
 # [x] done    [ ] todo    [-] not planned    ? unsure
@@ -41,7 +55,7 @@
 # - [x] Automatic <section>-wrapping (custom)
 # -  ?  Heading identifiers (php md, pandoc)
 #   - [x] Heading attributes (custom)
-#   - [ ] <hr> ends section
+#   - [ ] <hr> terminates section
 # - [x] Automatic heading identifiers (custom)
 # - [x] Fenced code blocks (php md, pandoc)
 #   - [x] Fenced code attributes
@@ -690,39 +704,25 @@ function _block( block, LOCAL, st, len, text, title, attrib, href, guard, code,
     list = substr( block, 1, RLENGTH); block = substr( block, RLENGTH + 1);
     return "\n<dl>\n" _dlist( list ) "</dl>\n" _block( block );
 
-  # Unordered list
-  } else if ( match( block, "(^|\n) ? ? ?[-+*][ \t][^\n]+(\n|$)" \
-                 "(([ \t]*\n)* ? ? ?[-+*][ \t][^\n]+(\n|$)" \
-                 "|([ \t]*\n)*( ? ? ?\t|  +)[^\n]+(\n|$)" \
-                 "|[^\n \t][^\n]+(\n|$))*" ) ) {
-    st = RSTART; len = RLENGTH; list = substr( block, RSTART, RLENGTH);
-    sub("^\n", "", list); match(list, "^ ? ? ?[-+*]"); indent = RLENGTH;
-    gsub( "(^|\n) {0," indent - 1 "}", "\n", list); sub("^\n", "", list);
-
-    text = substr(block, 1, st - 1); block = substr(block, st + len);
-    if (match( list, "\n([0-9]+\\.|#\\.)[ \t]" )) {
-      block = substr(list, RSTART + 1) block;
-      list = substr(list, 1, RSTART);
-    }
-
-    return _block( text ) "<ul>\n" _list( list, "[-+*]" ) "</ul>\n" _block( block );
-
-  # Ordered list
-  } else if ( match( block, "(^|\n) ? ? ?([0-9]+\\.|#\\.)[ \t][^\n]+(\n|$)" \
-                 "(([ \t]*\n)* ? ? ?([0-9]+\\.|#\\.)[ \t][^\n]+(\n|$)" \
-                 "|([ \t]*\n)*( ? ? ?\t|  +)[^\n]+(\n|$)" \
-                 "|[^\n \t][^\n]+(\n|$))*" ) ) {
-    st = RSTART; len = RLENGTH; list = substr( block, RSTART, RLENGTH);
-    sub("^\n", "", list); match(list, "^ ? ? ?[0-9#]"); indent = RLENGTH;
-    gsub( "(^|\n) {0," indent - 1 "}", "\n", list); sub("^\n", "", list);
-
-    text = substr(block, 1, st - 1); block = substr(block, st + len);
-    if (match( list, "\n[-+*][ \t]" )) {
-      block = substr(list, RSTART + 1) block;
-      list = substr(list, 1, RSTART);
-    }
-
-    return _block( text ) "<ol>\n" _list( list, "([0-9]+\\.|#\\.)" ) "</ol>\n" _block( block );
+  # Unordered list types
+  } else if ( text = _startlist( block, "ul", "-",   "([+*•]|[0-9]+\\.|#\\.|[0-9]+\\)|#\\))") ) {
+    return text;
+  } else if ( text = _startlist( block, "ul", "\\+", "([-*•]|[0-9]+\\.|#\\.|[0-9]+\\)|#\\))") ) {
+    return text;
+  } else if ( text = _startlist( block, "ul", "\\*", "([-+•]|[0-9]+\\.|#\\.|[0-9]+\\)|#\\))") ) {
+    return text;
+  } else if ( text = _startlist( block, "ul", "•", "([-+*]|[0-9]+\\.|#\\.|[0-9]+\\)|#\\))") ) {
+    return text;
+
+  # Ordered list types
+  } else if ( text = _startlist( block, "ol", "[0-9]+\\.", "([-+*•]|#\\.|[0-9]+\\)|#\\))") ) {
+    return text;
+  } else if ( text = _startlist( block, "ol", "[0-9]+\\)", "([-+*•]|[0-9]+\\.|#\\.|#\\))") ) {
+    return text;
+  } else if ( text = _startlist( block, "ol", "#\\.", "([-+*•]|[0-9]+\\.|[0-9]+\\)|#\\))") ) {
+    return text;
+  } else if ( text = _startlist( block, "ol", "#\\)", "([-+*•]|[0-9]+\\.|#\\.|[0-9]+\\))") ) {
+    return text;
 
   # Split paragraphs
   } else if ( match( block, /(^|\n)[[:space:]]*(\n|$)/) ) {
@@ -741,16 +741,38 @@ function _block( block, LOCAL, st, len, text, title, attrib, href, guard, code,
   }
 }
 
-function _list (block, mark, LOCAL, len, st, text, indent, task) {
+function _startlist(block, type, mark, exclude, LOCAL, st, len, list, indent, text) {
+  if (match( block, "(^|\n) ? ? ?" mark "[ \t][^\n]+(\n|$)" \
+                                   "(([ \t]*\n)* ? ? ?" mark "[ \t][^\n]+(\n|$)" \
+                                   "|([ \t]*\n)*( ? ? ?\t|  +)[^\n]+(\n|$)" \
+                                   "|[^\n \t][^\n]+(\n|$))*" ) ) {
+    st = RSTART; len = RLENGTH; list = substr( block, st, len);
+
+    sub("^\n", "", list); match(list, "^ ? ? ?"); indent = RLENGTH;
+    gsub( "(^|\n) {0," indent "}", "\n", list); sub("^\n", "", list);
+
+    text = substr(block, 1, st - 1); block = substr(block, st + len);
+    if (match(text, /\n[[:space:]]*\n/)) return 0;
+    if (match(text, "(^|\n) ? ? ?" exclude "[ \t][^\n]+")) return 0;
+    if (match( list, "\n" exclude "[ \t]" )) {
+      block = substr(list, RSTART + 1) block;
+      list = substr(list, 1, RSTART);
+    }
+
+    return _block( text ) "<" type ">\n" _list( list, mark ) "</" type ">\n" _block( block );
+  } else return 0;
+}
+
+function _list (block, mark, p, LOCAL, len, st, text, indent, task) {
   if ( match(block, "^([ \t]*\n)*$")) return;
+
   match(block, "^" mark "[ \t]"); indent = RLENGTH;
   sub("^" mark "[ \t]", "", block);
 
-  match( block, "\n" mark "[ \t][^\n]+(\n|$)" \
-      "(([ \t]*\n)* ? ? ?" mark "[ \t][^\n]+(\n|$)" \
-      "|([ \t]*\n)*( ? ? ?\t|  +)[^\n]+(\n|$)" \
-      "|[^\n \t][^\n]+(\n|$))*");
-  (RLENGTH == -1) ? st = length(block) + 1 : st = RSTART;
+  if (match(block, /\n[ \t]*\n/)) p = 1;
+
+  match( block, "\n" mark "[ \t][^\n]+(\n|$)" );
+  st = (RLENGTH == -1) ? length(block) + 1 : RSTART;
   text = substr(block, 1, st); block = substr(block, st + 1);
 
   gsub("\n {0," indent "}", "\n", text);
@@ -760,13 +782,13 @@ function _list (block, mark, LOCAL, len, st, text, indent, task) {
          match( text, /^\[\/\]/  ) ? "<li class=\"task partial\"><input type=checkbox disabled>"      : \
          match( text, /^\[\?\]/  ) ? "<li class=\"task unsure\"><input type=checkbox disabled>"       : \
          match( text, /^\[[xX]\]/) ? "<li class=\"task done\"><input type=checkbox disabled checked>" : "<li>";
-  sub(/^\[[-? /xX]\]/, "", text);
+  sub(/^\[[-? \/xX]\]/, "", text);
 
   text = _nblock( text );
-  if (match( text, "^<p>(</p[^>]|</[^p]|<[^/]|[^<])*</p>\n$" ))
+  if ( ! p && match( text, "^<p>(</p[^>]|</[^p]|<[^/]|[^<])*</p>\n$" ))
      gsub( "(^<p>|</p>\n$)", "", text);
 
-  return task text "</li>\n" _list(block, mark);
+  return task text "</li>\n" _list(block, mark, p);
 }
 
 function _dlist (block, LOCAL, len, st, text, indent, p) {