]> git.plutz.net Git - cgilite/commitdiff
bugfix do not escape # character in link references
authorPaul Hänsch <paul@plutz.net>
Wed, 21 Jun 2023 18:42:01 +0000 (20:42 +0200)
committerPaul Hänsch <paul@plutz.net>
Wed, 21 Jun 2023 18:42:01 +0000 (20:42 +0200)
markdown.awk

index 29dc64d48e6432100228c90a858463f88f645f8d..0e6d949c4abb66633b68ba5ac770a5605c22d72f 100755 (executable)
@@ -90,13 +90,13 @@ function HTML ( text ) {
   return text;
 }
 
-function URL ( text ) {
+function URL ( text, sharp ) {
   gsub( /&/,  "%26",  text );
   gsub( /"/,  "%22", text );
   gsub( /'/,  "%27", text );
   gsub( /`/,  "%60", text );
   gsub( /\?/,  "%3F", text );
-  gsub( /#/,  "%23", text );
+  if (sharp) gsub( /#/,  "%23", text );
   gsub( /\[/,  "%5B", text );
   gsub( /\]/,  "%5D", text );
   gsub( / /,  "%20", text );
@@ -196,16 +196,16 @@ function inline( line, LOCAL, len, code, href, guard ) {
     title  = gensub(/^!\[([^]]*)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)(\{([a-zA-Z \t-]*)\})?/, "\\4", "g", substr(line, 1, len) );
     attrib = gensub(/^!\[([^]]*)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)(\{([a-zA-Z \t-]*)\})?/, "\\6", "g", substr(line, 1, len) );
     if ( title && attrib ) {
-      return "<img src=\"" URL(href) "\" alt=\"" HTML(text) "\" title=\"" HTML(title) "\" class=\"" HTML(attrib) "\">" \
+      return "<img src=\"" URL(href, 1) "\" alt=\"" HTML(text) "\" title=\"" HTML(title) "\" class=\"" HTML(attrib) "\">" \
              inline( substr( line, len + 1) );
     } else if ( title ) {
-      return "<img src=\"" URL(href) "\" alt=\"" HTML(text) "\" title=\"" HTML(title) "\">" \
+      return "<img src=\"" URL(href, 1) "\" alt=\"" HTML(text) "\" title=\"" HTML(title) "\">" \
              inline( substr( line, len + 1) );
     } else if ( attrib ) {
-      return "<img src=\"" URL(href) "\" alt=\"" HTML(text) "\" class=\"" HTML(attrib) "\">" \
+      return "<img src=\"" URL(href, 1) "\" alt=\"" HTML(text) "\" class=\"" HTML(attrib) "\">" \
              inline( substr( line, len + 1) );
     } else {
-      return "<img src=\"" URL(href) "\" alt=\"" HTML(text) "\">" \
+      return "<img src=\"" URL(href, 1) "\" alt=\"" HTML(text) "\">" \
              inline( substr( line, len + 1) );
     }
 
@@ -216,10 +216,10 @@ function inline( line, LOCAL, len, code, href, guard ) {
       id = gensub(/^!\[([^\n]*)\] ?\[([^\n]*)\].*/, "\\2", 1, substr(line, 1, len) );
     if ( ! id ) id = text;
     if ( rl_href[id] && rl_title[id] ) {
-      return "<img src=\"" URL(rl_href[id]) "\" alt=\"" HTML(text) "\" title=\"" HTML(rl_title[id]) "\">" \
+      return "<img src=\"" URL(rl_href[id], 1) "\" alt=\"" HTML(text) "\" title=\"" HTML(rl_title[id]) "\">" \
              inline( substr( line, len + 1) );
     } else if ( rl_href[id] ) {
-      return "<img src=\"" URL(rl_href[id]) "\" alt=\"" HTML(text) "\">" \
+      return "<img src=\"" URL(rl_href[id], 1) "\" alt=\"" HTML(text) "\">" \
              inline( substr( line, len + 1) );
     } else {
       return "" HTML(substr(line, 1, len)) inline( substr(line, len + 1) );
@@ -577,7 +577,7 @@ function _block( block, LOCAL, st, len, hlvl, htxt, sec, guard, code, indent, at
 
     for ( n = 1; n <= 6; n++ ) { sec = sec (HL[n]?"</section>":""); }
     HL[1]++; HL[2] = 0; HL[3] = 0; HL[4] = 0; HL[5] = 0; HL[6] = 0;
-    hid = HL[1] ":" URL(gensub( /\n.*$/, "", "g", block ));
+    hid = HL[1] ":" URL(gensub( /\n.*$/, "", "g", block ), 1);
 
     return sec "<section class=\"h1\"" ((blvl > 1)?"":" id=\"" hid "\"") ">" \
            "<h1>" inline( gensub( /\n.*$/, "", "g", block ) ) \
@@ -591,7 +591,7 @@ function _block( block, LOCAL, st, len, hlvl, htxt, sec, guard, code, indent, at
 
     for ( n = 2; n <= 6; n++ ) { sec = sec (HL[n]?"</section>":""); }
     HL[2]++; HL[3] = 0; HL[4] = 0; HL[5] = 0; HL[6] = 0;
-    hid= HL[1] "." HL[2] ":" URL(gensub( /\n.*$/, "", "g", block ));
+    hid= HL[1] "." HL[2] ":" URL(gensub( /\n.*$/, "", "g", block ), 1);
 
     return sec "<section class=\"h2\"" ((blvl > 1)?"":" id=\"" hid "\"") ">" \
            "<h2>" inline( gensub( /\n.*$/, "", "g", block ) ) \
@@ -609,7 +609,7 @@ function _block( block, LOCAL, st, len, hlvl, htxt, sec, guard, code, indent, at
     for ( n = hlvl; n <= 6; n++ ) { sec = sec (HL[n]?"</section>":""); }
     HL[hlvl]++; for ( n = hlvl + 1; n <= 6; n++) { HL[n] = 0;}
     hid = HL[1]; for ( n = 2; n <= hlvl; n++) { hid = hid "." HL[n] ; }
-    hid = hid ":" URL(htxt);
+    hid = hid ":" URL(htxt, 1);
 
     return sec "<section class=\"h" hlvl "\"" ((blvl > 1)?"":" id=\"" hid "\"") ">" \
            "<h" hlvl ">" inline( htxt ) \
@@ -625,25 +625,25 @@ function _block( block, LOCAL, st, len, hlvl, htxt, sec, guard, code, indent, at
     title  = gensub(/^!\[([^]]*)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)(\{([a-zA-Z \t-]*)\})?(\n.*)?$/, "\\4", "g", block);
     attrib = gensub(/^!\[([^]]*)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)(\{([a-zA-Z \t-]*)\})?(\n.*)?$/, "\\6", "g", block);
     if ( title && attrib ) {
-      return "<figure data-src=\"" URL(href) "\" class=\"" HTML(attrib) "\">" \
-               "<img src=\"" URL(href) "\" alt=\"" HTML(text) "\" class=\"" HTML(attrib) "\">" \
+      return "<figure data-src=\"" URL(href, 1) "\" class=\"" HTML(attrib) "\">" \
+               "<img src=\"" URL(href, 1) "\" alt=\"" HTML(text) "\" class=\"" HTML(attrib) "\">" \
                "<figcaption>" inline(title) "</figcaption>" \
              "</figure>\n\n" \
              _block( substr( block, len + 1) );
     } else if ( title ) {
-      return "<figure data-src=\"" URL(href) "\">" \
-               "<img src=\"" URL(href) "\" alt=\"" HTML(text) "\">" \
+      return "<figure data-src=\"" URL(href, 1) "\">" \
+               "<img src=\"" URL(href, 1) "\" alt=\"" HTML(text) "\">" \
                "<figcaption>" inline(title) "</figcaption>" \
              "</figure>\n\n" \
              _block( substr( block, len + 1) );
     } else if ( attrib ) {
-      return "<figure data-src=\"" URL(href) "\" class=\"" HTML(attrib) "\">" \
-               "<img src=\"" URL(href) "\" alt=\"" HTML(text) "\" class=\"" HTML(attrib) "\">" \
+      return "<figure data-src=\"" URL(href, 1) "\" class=\"" HTML(attrib) "\">" \
+               "<img src=\"" URL(href, 1) "\" alt=\"" HTML(text) "\" class=\"" HTML(attrib) "\">" \
              "</figure>\n\n" \
              _block( substr( block, len + 1) );
     } else {
-      return "<figure data-src=\"" URL(href) "\">" \
-               "<img src=\"" URL(href) "\" alt=\"" HTML(text) "\">" \
+      return "<figure data-src=\"" URL(href, 1) "\">" \
+               "<img src=\"" URL(href, 1) "\" alt=\"" HTML(text) "\">" \
              "</figure>\n\n" \
              _block( substr( block, len + 1) );
     }
@@ -655,14 +655,14 @@ function _block( block, LOCAL, st, len, hlvl, htxt, sec, guard, code, indent, at
       id = gensub(/^!\[([^\n]*)\] ?\[([^\n]*)\](\n.*)?$/, "\\2", 1, block);
     if ( ! id ) id = text;
     if ( rl_href[id] && rl_title[id] ) {
-      return "<figure data-src=\"" URL(rl_href[id]) "\">" \
-               "<img src=\"" URL(rl_href[id]) "\" alt=\"" HTML(text) "\">" \
+      return "<figure data-src=\"" URL(rl_href[id], 1) "\">" \
+               "<img src=\"" URL(rl_href[id], 1) "\" alt=\"" HTML(text) "\">" \
                "<figcaption>" inline(rl_title[id]) "</figcaption>" \
              "</figure>\n\n" \
              _block( substr( block, len + 1) );
     } else if ( rl_href[id] ) {
-      return "<figure data-src=\"" URL(rl_href[id]) "\">" \
-               "<img src=\"" URL(rl_href[id]) "\" alt=\"" HTML(text) "\">" \
+      return "<figure data-src=\"" URL(rl_href[id], 1) "\">" \
+               "<img src=\"" URL(rl_href[id], 1) "\" alt=\"" HTML(text) "\">" \
              "</figure>\n\n" \
              _block( substr( block, len + 1) );
     } else {