Commit 2e0a74ba990e291ef4eace047d50af05ca81daef

Martin Mitas 2017-08-16T16:01:45

Fix problematic link destinations with angle brackets. Fixes #24.

diff --git a/md4c/md4c.c b/md4c/md4c.c
index 0b10c72..7b2c186 100644
--- a/md4c/md4c.c
+++ b/md4c/md4c.c
@@ -1997,14 +1997,6 @@ md_is_link_destination_B(MD_CTX* ctx, OFF beg, OFF max_end, OFF* p_end,
     return TRUE;
 }
 
-static inline int
-md_is_link_destination(MD_CTX* ctx, OFF beg, OFF max_end, OFF* p_end,
-                       OFF* p_contents_beg, OFF* p_contents_end)
-{
-    return (md_is_link_destination_A(ctx, beg, max_end, p_end, p_contents_beg, p_contents_end)  ||
-            md_is_link_destination_B(ctx, beg, max_end, p_end, p_contents_beg, p_contents_end));
-}
-
 static int
 md_is_link_title(MD_CTX* ctx, const MD_LINE* lines, int n_lines, OFF beg,
                  OFF* p_end, int* p_beg_line_index, int* p_end_line_index,
@@ -2069,7 +2061,9 @@ md_is_link_title(MD_CTX* ctx, const MD_LINE* lines, int n_lines, OFF beg,
  * If there is an error (cannot alloc memory for storing it), we return -1.
  */
 static int
-md_is_link_reference_definition(MD_CTX* ctx, const MD_LINE* lines, int n_lines)
+md_is_link_reference_definition_helper(
+            MD_CTX* ctx, const MD_LINE* lines, int n_lines,
+            int (*is_link_dest_fn)(MD_CTX*, OFF, OFF, OFF*, OFF*, OFF*))
 {
     OFF label_contents_beg;
     OFF label_contents_end;
@@ -2113,8 +2107,8 @@ md_is_link_reference_definition(MD_CTX* ctx, const MD_LINE* lines, int n_lines)
     }
 
     /* Link destination. */
-    if(!md_is_link_destination(ctx, off, lines[line_index].end,
-                &off, &dest_contents_beg, &dest_contents_end))
+    if(!is_link_dest_fn(ctx, off, lines[line_index].end,
+                        &off, &dest_contents_beg, &dest_contents_end))
         return FALSE;
 
     /* (Optional) title. Note we interpret it as an title only if nothing
@@ -2200,6 +2194,16 @@ abort:
     return -1;
 }
 
+static inline int
+md_is_link_reference_definition(MD_CTX* ctx, const MD_LINE* lines, int n_lines)
+{
+    int ret;
+    ret = md_is_link_reference_definition_helper(ctx, lines, n_lines, md_is_link_destination_A);
+    if(ret == 0)
+        ret = md_is_link_reference_definition_helper(ctx, lines, n_lines, md_is_link_destination_B);
+    return ret;
+}
+
 static int
 md_is_link_reference(MD_CTX* ctx, const MD_LINE* lines, int n_lines,
                      OFF beg, OFF end, MD_LINK_ATTR* attr)
@@ -2255,8 +2259,9 @@ abort:
 }
 
 static int
-md_is_inline_link_spec(MD_CTX* ctx, const MD_LINE* lines, int n_lines,
-                       OFF beg, OFF* p_end, MD_LINK_ATTR* attr)
+md_is_inline_link_spec_helper(MD_CTX* ctx, const MD_LINE* lines, int n_lines,
+                              OFF beg, OFF* p_end, MD_LINK_ATTR* attr,
+                              int (*is_link_dest_fn)(MD_CTX*, OFF, OFF, OFF*, OFF*, OFF*))
 {
     int line_index = 0;
     int tmp_line_index;
@@ -2284,10 +2289,14 @@ md_is_inline_link_spec(MD_CTX* ctx, const MD_LINE* lines, int n_lines,
     }
 
     /* (Optional) link destination. */
-    if(!md_is_link_destination(ctx, off, lines[line_index].end,
-            &off, &attr->dest_beg, &attr->dest_end)) {
-        attr->dest_beg = off;
-        attr->dest_end = off;
+    if(!is_link_dest_fn(ctx, off, lines[line_index].end,
+                        &off, &attr->dest_beg, &attr->dest_end)) {
+        if(is_link_dest_fn == md_is_link_destination_B) {
+            attr->dest_beg = off;
+            attr->dest_end = off;
+        } else {
+            return FALSE;
+        }
     }
 
     /* (Optional) title. */
@@ -2341,6 +2350,14 @@ abort:
     return ret;
 }
 
+static inline int
+md_is_inline_link_spec(MD_CTX* ctx, const MD_LINE* lines, int n_lines,
+                       OFF beg, OFF* p_end, MD_LINK_ATTR* attr)
+{
+    return md_is_inline_link_spec_helper(ctx, lines, n_lines, beg, p_end, attr, md_is_link_destination_A) ||
+           md_is_inline_link_spec_helper(ctx, lines, n_lines, beg, p_end, attr, md_is_link_destination_B);
+}
+
 static void
 md_free_ref_defs(MD_CTX* ctx)
 {