Fix problematic link destinations with angle brackets. Fixes #24.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
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)
{