]> git.plutz.net Git - cgilite/commitdiff
unified attibute parsing master
authorPaul Hänsch <paul@plutz.net>
Thu, 18 Dec 2025 06:31:25 +0000 (07:31 +0100)
committerPaul Hänsch <paul@plutz.net>
Thu, 18 Dec 2025 06:31:25 +0000 (07:31 +0100)
markdown.awk
tests-markdown.sh

index a9a6e2b0f8f06b4afc29518a641ee11f869ad161..d1f6616dd7aa7e93131230a25edc950900ecab5c 100755 (executable)
@@ -211,21 +211,20 @@ function inline( line, LOCAL, len, text, code, href, guard, ret ) {
       }
 
     # inline images
-    } else if ( match(line, "^!" lix "\\([\n\t ]*" lid "([\n\t ]+" lit ")?[\n\t ]*\\)(\\{[a-zA-Z \t-]*\\})?") ) {
+    } else if ( match(line, "^!" lix "\\([\n\t ]*" lid "([\n\t ]+" lit ")?[\n\t ]*\\)("rattr")?") ) {
       len = RLENGTH; text = href = title = attrib = substr( line, 1, len);
 
       sub("^!\\[", "", text);
-      sub("\\]\\([\n\t ]*" lid "([\n\t ]+" lit ")?[\n\t ]*\\)(\\{[a-zA-Z \t-]*\\})?$", "", text);
+      sub("\\]\\([\n\t ]*" lid "([\n\t ]+" lit ")?[\n\t ]*\\)("rattr")?$", "", text);
 
       sub("^!" lix "\\([\n\t ]*", "", href);
-      sub("([\n\t ]+" lit ")?[\n\t ]*\\)(\\{[a-zA-Z \t-]*\\})?$", "", href);
+      sub("([\n\t ]+" lit ")?[\n\t ]*\\)("rattr")?$", "", href);
 
       sub("^!" lix "\\([\n\t ]*" lid, "", title);
-      sub("[\n\t ]*\\)(\\{[a-zA-Z \t-]*\\})?$", "", title);
+      sub("[\n\t ]*\\)("rattr")?$", "", title);
       sub("^[\n\t ]+", "", title);
 
-      sub("^!" lix "\\([\n\t ]*" lid "([\n\t ]+" lit ")?[\n\t ]*\\)", "", attrib);
-      sub(/^\{[ \t]*/, "", attrib); sub(/[ \t]*\}$/, "", attrib); gsub(/[ \t]+/, " ", attrib);
+      attrib = _attr(attrib);
 
       if ( match(href, /^<.*>$/) ) { sub(/^</, "", href); sub(/>$/, "", href); }
            if ( match(title, /^".*"$/) ) { sub(/^"/, "", title); sub(/"$/, "", title); }
@@ -649,11 +648,10 @@ function _block( block, LOCAL, st, len, text, title, attrib, href, guard, code,
       }
 
     # First Order Heading H1 + Attrib
-    } else if ( match( block, /^([^\n]+)([ \t]*\{([^\}\n]+)\})\n===+(\n|$)/ ) ) {
+    } else if ( match( block, "^([^\n]+)([ \t]*"rattr")\n===+(\n|$)" ) ) {
       len = RLENGTH; text = attrib = block;
       sub(/([ \t]*\{([^\}\n]+)\})\n===+(\n.*)?$/, "", text);
-      sub(/\}\n===+(\n.*)?$/, "", attrib); sub(/^([^\n]+)[ \t]*\{/, "", attrib);
-      gsub(/[^a-zA-Z0-9_-]+/, " ", attrib); gsub(/(^ | $)/, "", attrib);
+      attrib = _attr(attrib);
 
       ret = ret headline(1, text, attrib) ; block = substr( block, len + 1 );
       continue;
@@ -667,11 +665,10 @@ function _block( block, LOCAL, st, len, text, title, attrib, href, guard, code,
       continue;
 
     # Second Order Heading H2 + Attrib
-    } else if ( match( block, /^([^\n]+)([ \t]*\{([^\}\n]+)\})\n---+(\n|$)/ ) ) {
+    } else if ( match( block, "^([^\n]+)([ \t]*"rattr")\n---+(\n|$)" ) ) {
       len = RLENGTH; text = attrib = block;
       sub(/([ \t]*\{([^\}\n]+)\})\n---+(\n.*)?$/, "", text);
-      sub(/\}\n---+(\n.*)?$/, "", attrib); sub(/^([^\n]+)[ \t]*\{/, "", attrib);
-      gsub(/[^a-zA-Z0-9_-]+/, " ", attrib); gsub(/(^ | $)/, "", attrib);
+      attrib = _attr(attrib);
 
       ret = ret headline(2, text, attrib) ; block = substr( block, len + 1);
       continue;
@@ -685,15 +682,13 @@ function _block( block, LOCAL, st, len, text, title, attrib, href, guard, code,
       continue;
 
     # # Nth Order Heading H1 H2 H3 H4 H5 H6 + Attrib
-    } else if ( match( block, /^##?#?#?#?#?[^#\n]([^\n#]|#[^\t\n# ]|#[\t ]+[^\t\n ])+#*[\t ]*\{[\ta-zA-Z -]*\}(\n|$)/ ) ) {
+    } else if ( match( block, "^##?#?#?#?#?[^#\n]([^\n#]|#[^\t\n# ]|#[\t ]+[^\t\n ])+#*[\t ]*"rattr"(\n|$)" ) ) {
       len = RLENGTH; text = attrib = substr(block, 1, len);
       match(block, /^##?#?#?#?#?[^#]/); n = RLENGTH - 1;
       text = substr(text, n + 1); sub(/^[ \t]*/, "", text);
       sub(/[ \t]*#*([ \t]*\{([a-zA-Z \t-]*)\})(\n.*)?$/, "", text);
 
-      sub(/^##?#?#?#?#?[^#\n]([^\n#]|#[^\t\n# ]|#[\t ]+[^\t\n ])+#*[\t ]*\{/, "", attrib);
-      sub(/\}(\n.*)?$/, "", attrib);
-      gsub(/[^a-zA-Z0-9_-]+/, " ", attrib); gsub(/(^ | $)/, "", attrib);
+      attrib = _attr(attrib);
 
       ret = ret headline( n, text, attrib ); block = substr( block, len + 1);
       continue;
@@ -709,7 +704,7 @@ function _block( block, LOCAL, st, len, text, title, attrib, href, guard, code,
       continue;
 
     # block images (wrapped in <figure>)
-    } else if ( match(block, "^!" lix "\\([\n\t ]*" lid "([\n\t ]+" lit ")?[\n\t ]*\\)(\\{[a-zA-Z \t-]*\\})?(\n|$)") ) {
+    } else if ( match(block, "^!" lix "\\([\n\t ]*" lid "([\n\t ]+" lit ")?[\n\t ]*\\)("rattr")?(\n|$)") ) {
       len = RLENGTH; text = href = title = attrib = substr( block, 1, len);
 
       sub("^!\\[", "", text);
@@ -722,9 +717,7 @@ function _block( block, LOCAL, st, len, text, title, attrib, href, guard, code,
       sub("[\n\t ]*\\)(\\{[a-zA-Z \t-]*\\})?(\n.*)?$", "", title);
       sub("^[\n\t ]+", "", title);
 
-      sub("^!" lix "\\([\n\t ]*" lid "([\n\t ]+" lit ")?[\n\t ]*\\)", "", attrib);
-      sub("(\n.*)?$", "", attrib);
-      sub(/^\{[ \t]*/, "", attrib); sub(/[ \t]*\}$/, "", attrib); gsub(/[ \t]+/, " ", attrib);
+      attrib = _attr(attrib);
 
       if ( match(href, /^<.*>$/) ) { sub(/^</, "", href); sub(/>$/, "", href); }
            if ( match(title, /^".*"$/) ) { sub(/^"/, "", title); sub(/"$/, "", title); }
@@ -908,6 +901,13 @@ function _dlist (block, LOCAL, len, st, text, indent, it, p) {
   }
 }
 
+function _attr (attrib) {
+  if ( sub(/.*\{[ \t.#]*/, "", attrib) ) if ( sub(/[ \t]*\}(\n.*)?$/, "", attrib) ) {
+    gsub(/[ \t.#]+/, " ", attrib);
+    return attrib;
+  } else return "";
+}
+
 BEGIN {
   # Global Vars
   file = ""; rl_href[""] = ""; rl_title[""] = "";
@@ -922,6 +922,8 @@ BEGIN {
   iea =    "\\*([^*[:space:]]|[^*[:space:]]" na "*[^*[:space:]])\\*"     # inner <em> (asterisk)
   isa = "\\*\\*([^*[:space:]]|[^*[:space:]]" na "*[^*[:space:]])\\*\\*"  # inner <strong> (asterisk)
 
+  rattr = "\\{[ \t]*([.#]?([a-zA-Z0-9]+-)*[a-zA-Z0-9]+[ \t]+)*([.#]?([a-zA-Z0-9]+-)*[a-zA-Z0-9]+)[ \t]*\\}"
+
   lix="\\[(\\\\[^\n]|[^]\n\\\\[])*\\]"  # link text
   lid="(<(\\\\[^\n]|[^\n<>\\\\])*>|(\\\\.|[^()\"'\\\\])+|([^<\n\t ()\\\\]|\\\\[^\n])(\\\\[\n]|[^\n\t \\(\\)\\\\])*)"  # link dest
   lit="(\"(\\\\.|[^\"\\\\])*\"|'(\\\\.|[^'\\\\])*'|\\((\\\\.|[^\\(\\)\\\\])*\\))"  # link text
index bee2e37c8de53ec0b4a5e3c7ee398d4f2f20fe92..37943215ad13785906d6a310f742423d01d26920 100755 (executable)
@@ -93,7 +93,7 @@ assert '[Wikipedia](<http://de.wikipedia.org> "Online Encyclopedia")' "<p><a hre
 assert ' ![Testbild](Test Bild.jpg)' '<p> <img src="Test Bild.jpg" alt="Testbild"></p>' "inline image"
 assert ' ![Testbild](Test Bild.jpg "German Television *test* image ca. 1994")' '<p> <img src="Test Bild.jpg" alt="Testbild" title="German Television *test* image ca. 1994"></p>' "inline image"
 assert ' ![Testbild *ARD*](Test Bild.jpg){tv ard function-check}' '<p> <img src="Test Bild.jpg" alt="Testbild *ARD*" class="tv ard function-check"></p>' "inline image"
-# assert ' ![Testbild *ARD*](Test Bild.jpg){#tv .ard .function-check}' '<p> <img src="Test Bild.jpg" alt="Testbild *ARD*" class="tv ard check"></p>' "inline image id/classes"
+assert ' ![Testbild *ARD*](Test Bild.jpg){#tv .ard .function-check}' '<p> <img src="Test Bild.jpg" alt="Testbild *ARD*" class="tv ard function-check"></p>' "inline image id/classes"
 
 assert '[![Wikipedia](wikilogo.png)](<http://de.wikipedia.org>)'\
        '<p><a href="http://de.wikipedia.org"><img src="wikilogo.png" alt="Wikipedia"></a></p>'\