]> git.plutz.net Git - cgilite/blobdiff - markdown.awk
Extension: Arrows
[cgilite] / markdown.awk
index 63bb044e8a69c13a87c58c71194ecceaf9b381b8..4e1dfa63eeae56ad0c16a46caf3a8b58dd782c3c 100755 (executable)
@@ -42,6 +42,8 @@
 # - [x] Automatic heading identifiers (custom)
 # - [x] Fenced code blocks (php md, pandoc)
 #   - [x] Fenced code attributes
+# - [x] Images (as block elements, <figure>-wrapped) (custom)
+#   - [x] reference style block images
 # - [/] Tables
 #   -  ?  Simple table (pandoc)
 #   -  ?  Multiline table (pandoc)
@@ -72,7 +74,7 @@
 # -  ?  ... three-dot ellipsis (smartypants)
 # - [-] en-dash (smartypants)
 # - [ ] Automatic em-dash / en-dash
-# - [ ] Automatic -> Arrows <-
+# - [x] Automatic -> Arrows <- (custom)
 
 function debug(text) { printf "\n---\n%s\n---\n", text > "/dev/stderr"; }
 
@@ -111,7 +113,7 @@ function inline( line, LOCAL, len, code, href, guard ) {
   if ( line ~ /^$/ ) {  # Recursion End
     return "";
 
-  #  omit processing of escaped characters
+  # omit processing of escaped characters
   } else if ( line ~ /^\\[]\\`\*_\{\}\(\)#\+-\.![]/) {
     return substr(line, 2, 1) inline( substr(line, 3) );
 
@@ -154,11 +156,15 @@ function inline( line, LOCAL, len, code, href, guard ) {
     return "<a href=\"mailto:" href "\">" href "</a>" inline( substr( line, len + 1) );
 
   # inline links
-  } else if ( match(line, /^\[([^]]+)\]\(([^"\)]+)([[:space:]]+"([^"]+)")?\)/) ) {
+  #                                 ,_______________________Image____________________________,
+  } else if ( match(line, /^\[([^]]+|!\[[^]]+\]\([^"\)]+([ \t]+"[^"]+")?\)(\{[a-zA-Z \t-]*\})?)\]\(([^"\)]+)([[:space:]]+"([^"]+)")?\)/) ) {
     len = RLENGTH;
-    text  = gensub(/^\[([^]]+)\]\(([^"\)]+)([[:space:]]+"([^"]+)")?\)/, "\\1", 1, substr(line, 1, len) );
-    href  = gensub(/^\[([^]]+)\]\(([^"\)]+)([[:space:]]+"([^"]+)")?\)/, "\\2", 1, substr(line, 1, len) );
-    title = gensub(/^\[([^]]+)\]\(([^"\)]+)([[:space:]]+"([^"]+)")?\)/, "\\4", 1, substr(line, 1, len) );
+    text  = gensub(/^\[([^]]+|!\[[^]]+\]\([^"\)]+([ \t]+"[^"]+")?\)(\{[a-zA-Z \t-]*\})?)\]\(([^"\)]+)([[:space:]]+"([^"]+)")?\)/, \
+                   "\\1", 1, substr(line, 1, len) );
+    href  = gensub(/^\[([^]]+|!\[[^]]+\]\([^"\)]+([ \t]+"[^"]+")?\)(\{[a-zA-Z \t-]*\})?)\]\(([^"\)]+)([[:space:]]+"([^"]+)")?\)/, \
+                   "\\4", 1, substr(line, 1, len) );
+    title = gensub(/^\[([^]]+|!\[[^]]+\]\([^"\)]+([ \t]+"[^"]+")?\)(\{[a-zA-Z \t-]*\})?)\]\(([^"\)]+)([[:space:]]+"([^"]+)")?\)/, \
+                   "\\6", 1, substr(line, 1, len) );
     if ( title ) {
       return "<a href=\"" URL(href) "\" title=\"" HTML(title) "\">" inline( text ) "</a>" inline( substr( line, len + 1) );
     } else {
@@ -280,6 +286,14 @@ function inline( line, LOCAL, len, code, href, guard ) {
     len = RLENGTH;
     return substr( line, 1, len ) inline(substr(line, len + 1));
 
+  # Arrows
+  } else if ( line ~ /^-->( |$)/) {  # ignore multidash-arrow
+    return "--&gt;" inline( substr(line, 4) );
+  } else if ( line ~ /^<-( |$)/) {
+    return "&larr;" inline( substr(line, 3) );
+  } else if ( line ~ /^->( |$)/) {
+    return "&rarr;" inline( substr(line, 3) );
+
   # Escape lone HTML character
   } else if ( match( line, /^[&<>"']/) ) {
     return HTML(substr(line, 1, 1)) inline(substr(line, 2));
@@ -550,6 +564,64 @@ function _block( block, LOCAL, st, len, hlvl, htxt, guard, code, indent, attrib
            "<a class=\"anchor\" href=\"#" hid "\"></a></h" hlvl ">\n\n" \
            _block( substr( block, len + 1) );
 
+  # block images (wrapped in <figure>)
+  } else if ( match(block, /^!\[([^]]+)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)(\{([a-zA-Z \t-]*)\})?(\n|$)/) ) {
+    len = RLENGTH;
+    text   = gensub(/^!\[([^]]+)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)(\{([a-zA-Z \t-]*)\})?(\n.*)?$/, "\\1", "g", block);
+    href   = gensub(/^!\[([^]]+)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)(\{([a-zA-Z \t-]*)\})?(\n.*)?$/, "\\2", "g", block);
+    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) "\">" \
+               "<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) "\">" \
+               "<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) "\">" \
+             "</figure>\n\n" \
+             _block( substr( block, len + 1) );
+    } else {
+      return "<figure data-src=\"" URL(href) "\">" \
+               "<img src=\"" URL(href) "\" alt=\"" HTML(text) "\">" \
+             "</figure>\n\n" \
+             _block( substr( block, len + 1) );
+    }
+
+  # reference style images (block)
+  } else if ( match(line, /^!\[([^]]+)\] ?\[([^]]*)\](\n|$)/ ) ) {
+    len = RLENGTH;
+    text = gensub(/^!\[([^\n]+)\] ?\[([^\n]*)\](\n.*)?$/, "\\1", 1, block);
+      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) "\">" \
+               "<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) "\">" \
+             "</figure>\n\n" \
+             _block( substr( block, len + 1) );
+    } else {
+      return "<p>" HTML(substr(block, 1, len)) "</p>\n" _block( substr(block, len + 1) );
+    }
+
+  # Macros (standalone <<macro>> calls handled as block, so they are not wrapped in paragraph)
+  } else if ( AllowMacros && match( block, /^<<(([^>]|>[^>])+)>>(\n|$)/) ) {
+    len = RLENGTH;
+    text = gensub(/^<<(([^>]|>[^>])+)>>(\n.*)?$/, "\\1", 1, block);
+    return macro(text) _block(substr(block, len + 1));
+
   # Split paragraphs
   } else if ( match( block, /(^|\n)[[:space:]]*(\n|$)/) ) {
     len = RLENGTH; st = RSTART;