Move autolink detection into md_collect_marks(). It has to be done at the same time as raw inline HTML and code spans.
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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
diff --git a/md4c/md4c.c b/md4c/md4c.c
index e08869a..188cd80 100644
--- a/md4c/md4c.c
+++ b/md4c/md4c.c
@@ -123,16 +123,15 @@ struct MD_CTX_tag {
#endif
/* For resolving of inline spans. */
- MD_MARKCHAIN mark_chains[7];
+ MD_MARKCHAIN mark_chains[6];
#define PTR_CHAIN ctx->mark_chains[0]
#define TABLECELLBOUNDARIES ctx->mark_chains[1]
-#define LOWERTHEN_OPENERS ctx->mark_chains[2]
-#define ASTERISK_OPENERS ctx->mark_chains[3]
-#define UNDERSCORE_OPENERS ctx->mark_chains[4]
-#define TILDE_OPENERS ctx->mark_chains[5]
-#define BRACKET_OPENERS ctx->mark_chains[6]
+#define ASTERISK_OPENERS ctx->mark_chains[2]
+#define UNDERSCORE_OPENERS ctx->mark_chains[3]
+#define TILDE_OPENERS ctx->mark_chains[4]
+#define BRACKET_OPENERS ctx->mark_chains[5]
#define OPENERS_CHAIN_FIRST 2
-#define OPENERS_CHAIN_LAST 6
+#define OPENERS_CHAIN_LAST 5
int n_table_cell_boundaries;
@@ -2660,7 +2659,6 @@ md_rollback(MD_CTX* ctx, int opener_index, int closer_index, int how)
switch(mark_opener->ch) {
case '*': chain = &ASTERISK_OPENERS; break;
case '_': chain = &UNDERSCORE_OPENERS; break;
- case '<': chain = &LOWERTHEN_OPENERS; break;
case '~': chain = &TILDE_OPENERS; break;
default: MD_UNREACHABLE(); break;
}
@@ -2811,6 +2809,111 @@ md_is_code_span(MD_CTX* ctx, OFF beg, OFF max_end,
}
static int
+md_is_autolink_uri(MD_CTX* ctx, OFF beg, OFF max_end, OFF* p_end)
+{
+ OFF off = beg+1;
+
+ MD_ASSERT(CH(beg) == _T('<'));
+
+ /* Check for scheme. */
+ if(off >= max_end || !ISASCII(off))
+ return FALSE;
+ off++;
+ while(1) {
+ if(off >= max_end)
+ return FALSE;
+ if(off - beg > 32)
+ return FALSE;
+ if(CH(off) == _T(':') && off - beg >= 3)
+ break;
+ if(!ISALNUM(off) && CH(off) != _T('+') && CH(off) != _T('-') && CH(off) != _T('.'))
+ return FALSE;
+ off++;
+ }
+
+ /* Check the path after the scheme. */
+ while(off < max_end && CH(off) != _T('>')) {
+ if(ISWHITESPACE(off) || ISCNTRL(off) || CH(off) == _T('<'))
+ return FALSE;
+ off++;
+ }
+
+ if(off >= max_end)
+ return FALSE;
+
+ MD_ASSERT(CH(off) == _T('>'));
+ *p_end = off+1;
+ return TRUE;
+}
+
+static int
+md_is_autolink_email(MD_CTX* ctx, OFF beg, OFF max_end, OFF* p_end)
+{
+ OFF off = beg + 1;
+ int label_len;
+
+ MD_ASSERT(CH(beg) == _T('<'));
+
+ /* The code should correspond to this regexp:
+ /^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+
+ @[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?
+ (?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/
+ */
+
+ /* Username (before '@'). */
+ while(off < max_end && (ISALNUM(off) || ISANYOF(off, _T(".!#$%&'*+/=?^_`{|}~-"))))
+ off++;
+ if(off <= beg+1)
+ return FALSE;
+
+ /* '@' */
+ if(off >= max_end || CH(off) != _T('@'))
+ return FALSE;
+ off++;
+
+ /* Labels delimited with '.'; each label is sequence of 1 - 62 alnum
+ * characters or '-', but '-' is not allowed as first or last char. */
+ label_len = 0;
+ while(off < max_end) {
+ if(ISALNUM(off))
+ label_len++;
+ else if(CH(off) == _T('-') && label_len > 0)
+ label_len++;
+ else if(CH(off) == _T('.') && label_len > 0 && CH(off-1) != _T('-'))
+ label_len = 0;
+ else
+ break;
+
+ if(label_len > 62)
+ return FALSE;
+
+ off++;
+ }
+
+ if(label_len <= 0 || off >= max_end || CH(off) != _T('>') || CH(off-1) == _T('-'))
+ return FALSE;
+
+ *p_end = off+1;
+ return TRUE;
+}
+
+static int
+md_is_autolink(MD_CTX* ctx, OFF beg, OFF max_end, OFF* p_end, int* p_missing_mailto)
+{
+ if(md_is_autolink_uri(ctx, beg, max_end, p_end)) {
+ *p_missing_mailto = FALSE;
+ return TRUE;
+ }
+
+ if(md_is_autolink_email(ctx, beg, max_end, p_end)) {
+ *p_missing_mailto = TRUE;
+ return TRUE;
+ }
+
+ return FALSE;
+}
+
+static int
md_collect_marks(MD_CTX* ctx, const MD_LINE* lines, int n_lines, int table_mode)
{
int i;
@@ -2975,8 +3078,12 @@ md_collect_marks(MD_CTX* ctx, const MD_LINE* lines, int n_lines, int table_mode)
}
/* A potential autolink or raw HTML start/end. */
- if(ch == _T('<') || ch == _T('>')) {
- if(!(ctx->parser.flags & MD_FLAG_NOHTMLSPANS) && ch == _T('<')) {
+ if(ch == _T('<')) {
+ int is_autolink;
+ OFF autolink_end;
+ int missing_mailto;
+
+ if(!(ctx->parser.flags & MD_FLAG_NOHTMLSPANS)) {
int is_html;
OFF html_end;
@@ -3002,7 +3109,19 @@ md_collect_marks(MD_CTX* ctx, const MD_LINE* lines, int n_lines, int table_mode)
}
}
- PUSH_MARK(ch, off, off+1, (ch == _T('<') ? MD_MARK_POTENTIAL_OPENER : MD_MARK_POTENTIAL_CLOSER));
+ is_autolink = md_is_autolink(ctx, off, lines[n_lines-1].end,
+ &autolink_end, &missing_mailto);
+ if(is_autolink) {
+ PUSH_MARK((missing_mailto ? _T('@') : _T('<')), off, off+1,
+ MD_MARK_OPENER | MD_MARK_RESOLVED | MD_MARK_AUTOLINK);
+ PUSH_MARK(_T('>'), autolink_end-1, autolink_end,
+ MD_MARK_CLOSER | MD_MARK_RESOLVED | MD_MARK_AUTOLINK);
+ ctx->marks[ctx->n_marks-2].next = ctx->n_marks-1;
+ ctx->marks[ctx->n_marks-1].prev = ctx->n_marks-2;
+ off = autolink_end;
+ continue;
+ }
+
off++;
continue;
}
@@ -3143,138 +3262,6 @@ abort:
return ret;
}
-static int
-md_is_autolink_uri(MD_CTX* ctx, OFF beg, OFF end)
-{
- OFF off = beg;
-
- /* Check for scheme. */
- if(off >= end || !ISASCII(off))
- return FALSE;
- off++;
- while(1) {
- if(off >= end)
- return FALSE;
- if(off - beg > 32)
- return FALSE;
- if(CH(off) == _T(':') && off - beg >= 2)
- break;
- if(!ISALNUM(off) && CH(off) != _T('+') && CH(off) != _T('-') && CH(off) != _T('.'))
- return FALSE;
- off++;
- }
-
- /* Check the path after the scheme. */
- while(off < end) {
- if(ISWHITESPACE(off) || ISCNTRL(off) || CH(off) == _T('<') || CH(off) == _T('>'))
- return FALSE;
- off++;
- }
-
- return TRUE;
-}
-
-static int
-md_is_autolink_email(MD_CTX* ctx, OFF beg, OFF end)
-{
- OFF off = beg;
- int label_len;
-
- /* The code should correspond to this regexp:
- /^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+
- @[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?
- (?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/
- */
-
- /* Username (before '@'). */
- while(off < end && (ISALNUM(off) || ISANYOF(off, _T(".!#$%&'*+/=?^_`{|}~-"))))
- off++;
- if(off <= beg)
- return FALSE;
-
- /* '@' */
- if(off >= end || CH(off) != _T('@'))
- return FALSE;
- off++;
-
- /* Labels delimited with '.'; each label is sequence of 1 - 62 alnum
- * characters or '-', but '-' is not allowed as first or last char. */
- label_len = 0;
- while(off < end) {
- if(ISALNUM(off))
- label_len++;
- else if(CH(off) == _T('-') && label_len > 0)
- label_len++;
- else if(CH(off) == _T('.') && label_len > 0 && CH(off-1) != _T('-'))
- label_len = 0;
- else
- return FALSE;
-
- if(label_len > 63)
- return FALSE;
-
- off++;
- }
-
- if(label_len <= 0 || CH(off-1) == _T('-'))
- return FALSE;
-
- return TRUE;
-}
-
-static int
-md_is_autolink(MD_CTX* ctx, OFF beg, OFF end, int* p_missing_mailto)
-{
- MD_ASSERT(CH(beg) == _T('<'));
- MD_ASSERT(CH(end-1) == _T('>'));
-
- beg++;
- end--;
-
- if(md_is_autolink_uri(ctx, beg, end))
- return TRUE;
-
- if(md_is_autolink_email(ctx, beg, end)) {
- *p_missing_mailto = 1;
- return TRUE;
- }
-
- return FALSE;
-}
-
-static void
-md_analyze_lt_gt(MD_CTX* ctx, int mark_index, const MD_LINE* lines, int n_lines)
-{
- MD_MARK* mark = &ctx->marks[mark_index];
- int opener_index;
-
- /* If it is an opener ('<'), remember it. */
- if(mark->flags & MD_MARK_POTENTIAL_OPENER) {
- md_mark_chain_append(ctx, &LOWERTHEN_OPENERS, mark_index);
- return;
- }
-
- /* Otherwise we are potential closer: Try the most recent opener to resolve
- * an autolink. */
- opener_index = LOWERTHEN_OPENERS.head;
- if(opener_index >= 0) {
- MD_MARK* opener = &ctx->marks[opener_index];
- int is_missing_mailto = 0;
-
- if(md_is_autolink(ctx, opener->beg, mark->end, &is_missing_mailto)) {
- if(is_missing_mailto)
- opener->ch = _T('@');
-
- md_rollback(ctx, opener_index, mark_index, MD_ROLLBACK_ALL);
- md_resolve_range(ctx, &LOWERTHEN_OPENERS, opener_index, mark_index);
-
- /* Distinguish it from raw HTML spans. */
- opener->flags |= MD_MARK_AUTOLINK;
- mark->flags |= MD_MARK_AUTOLINK;
- }
- }
-}
-
static void
md_analyze_bracket(MD_CTX* ctx, int mark_index)
{
@@ -3768,8 +3755,6 @@ md_analyze_marks(MD_CTX* ctx, const MD_LINE* lines, int n_lines,
/* Analyze the mark. */
switch(mark->ch) {
- case '<': /* Pass through. */
- case '>': md_analyze_lt_gt(ctx, i, lines, n_lines); break;
case '[': /* Pass through. */
case '!': /* Pass through. */
case ']': md_analyze_bracket(ctx, i); break;
@@ -3801,9 +3786,7 @@ md_analyze_inlines(MD_CTX* ctx, const MD_LINE* lines, int n_lines, int table_mod
/* We analyze marks in few groups to handle their precedence. */
/* (1) Entities; code spans; autolinks; raw HTML. */
- md_analyze_marks(ctx, lines, n_lines, 0, ctx->n_marks, _T("&<>"));
- LOWERTHEN_OPENERS.head = -1;
- LOWERTHEN_OPENERS.tail = -1;
+ md_analyze_marks(ctx, lines, n_lines, 0, ctx->n_marks, _T("&"));
if(table_mode) {
/* (2) Analyze table cell boundaries.