Implement emphasis and strong emphasis.
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 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509
diff --git a/README.md b/README.md
index eac74ad..e7c0157 100644
--- a/README.md
+++ b/README.md
@@ -100,7 +100,7 @@ more or less forms our to do list.
- [x] 6.1 Backslash escapes
- [x] 6.2 Entity and numeric character references
- [x] 6.3 Code spans
- - [ ] 6.4 Emphasis and strong emphasis
+ - [x] 6.4 Emphasis and strong emphasis
- [ ] 6.5 Links
- [ ] 6.6 Images
- [x] 6.7 Autolinks
diff --git a/md2html/md2html.c b/md2html/md2html.c
index 2c3025e..7368b4e 100644
--- a/md2html/md2html.c
+++ b/md2html/md2html.c
@@ -293,6 +293,8 @@ enter_span_callback(MD_SPANTYPE type, void* detail, void* userdata)
struct membuffer* out = (struct membuffer*) userdata;
switch(type) {
+ case MD_SPAN_EM: MEMBUF_APPEND_LITERAL(out, "<em>"); break;
+ case MD_SPAN_STRONG: MEMBUF_APPEND_LITERAL(out, "<strong>"); break;
case MD_SPAN_A: open_a_span(out, (MD_SPAN_A_DETAIL*) detail); break;
case MD_SPAN_CODE: MEMBUF_APPEND_LITERAL(out, "<code>"); break;
}
@@ -306,6 +308,8 @@ leave_span_callback(MD_SPANTYPE type, void* detail, void* userdata)
struct membuffer* out = (struct membuffer*) userdata;
switch(type) {
+ case MD_SPAN_EM: MEMBUF_APPEND_LITERAL(out, "</em>"); break;
+ case MD_SPAN_STRONG: MEMBUF_APPEND_LITERAL(out, "</strong>"); break;
case MD_SPAN_A: MEMBUF_APPEND_LITERAL(out, "</a>"); break;
case MD_SPAN_CODE: MEMBUF_APPEND_LITERAL(out, "</code>"); break;
}
diff --git a/md4c/md4c.c b/md4c/md4c.c
index 5a83332..b4bc70b 100644
--- a/md4c/md4c.c
+++ b/md4c/md4c.c
@@ -97,11 +97,15 @@ struct MD_CTX_tag {
unsigned n_marks;
unsigned alloc_marks;
- MD_MARKCHAIN mark_chains[2];
+ MD_MARKCHAIN mark_chains[4];
/* For md_analyze_backtick(). */
#define BACKTICK_OPENERS ctx->mark_chains[0]
/* For md_analyze_lt_gt(). */
#define LT_GT_OPENERS ctx->mark_chains[1]
+ /* For md_analyze_asterisk(). */
+ #define ASTERISK_OPENERS ctx->mark_chains[2]
+ /* For md_analyze_underscore(). */
+ #define UNDERSCORE_OPENERS ctx->mark_chains[3]
/* For MD_BLOCK_QUOTE */
unsigned quote_level; /* Nesting level. */
@@ -172,7 +176,6 @@ md_log(MD_CTX* ctx, const char* fmt, ...)
if(!(cond)) { \
md_log(ctx, "%s:%d: Assertion '" #cond "' failed.", \
__FILE__, (int)__LINE__); \
- ret = -2; \
exit(1); \
} \
} while(0)
@@ -770,12 +773,13 @@ md_is_autolink(MD_CTX* ctx, OFF beg, OFF end)
/* The mark structure.
*
* '\\': Maybe escape sequence.
+ * '*': Maybe (strong) emphasis start/end.
* '`': Maybe code span start/end.
* '&': Maybe start of entity.
* ';': Maybe end of entity.
- * '<': Maybe start of raw HTML.
- * '>': Maybe end of raw HTML.
- * '0': NULL char (need replacement).
+ * '<': Maybe start of raw HTML or autolink.
+ * '>': Maybe end of raw HTML or autolink.
+ * '0': NULL char.
*
* Note that not all instances of these chars in the text imply creation of the
* structure. Only those which have (or may have, after we see more context)
@@ -803,9 +807,10 @@ struct MD_MARK_tag {
/* Mark flags. */
#define MD_MARK_POTENTIAL_OPENER 0x01 /* Maybe opener. */
#define MD_MARK_POTENTIAL_CLOSER 0x02 /* Maybe closer. */
+#define MD_MARK_AUTOLINK 0x04 /* Distinguisher for '<', '>'. */
#define MD_MARK_RESOLVED 0x10 /* Yes, the special meaning is indeed recognized. */
-#define MD_MARK_OPENER 0x20 /* This opens (or potentially may open) a span. */
-#define MD_MARK_CLOSER 0x40 /* This closes (or potentially may close) a span. */
+#define MD_MARK_OPENER 0x20 /* This opens a span. */
+#define MD_MARK_CLOSER 0x40 /* This closes a span. */
static MD_MARK*
@@ -890,8 +895,27 @@ md_resolve_range(MD_CTX* ctx, MD_MARKCHAIN* chain, int opener_index, int closer_
closer->flags |= MD_MARK_CLOSER | MD_MARK_RESOLVED;
}
+
+#define MD_ROLLBACK_ALL 0
+#define MD_ROLLBACK_CROSSING 1
+
+/* Undo some or all resolvings of ctx->marks[opener_index] ... [closer_index]:
+ *
+ * (1) If 'how' is MD_ROLLBACK_ALL, then
+ * (1.1) all resolved closers within the range, corresponding to openers
+ * BEFORE the range are discarded and those openers before the range
+ * are again made ready for future resolving to closers after the
+ * range; and
+ * (1.2) ALL resolved marks within the range, including all closers and
+ * any non-paired marks like entities, are discarded.
+ *
+ * (2) If 'how' is MD_ROLLBACK_CROSSING, then
+ * (2.1) Ditto as (1.1); and
+ * (2.2) all unresolved openers INSIDE the range are discarded from
+ * openers their respective openers chain.
+ */
static void
-md_rollback(MD_CTX* ctx, int opener_index, int closer_index)
+md_rollback(MD_CTX* ctx, int opener_index, int closer_index, int how)
{
int i;
@@ -909,11 +933,8 @@ md_rollback(MD_CTX* ctx, int opener_index, int closer_index)
}
/* Go backwards so that un-resolved openers are re-added into their
- * respective chains in the right order. */
+ * respective chains, in the right order. */
for(i = closer_index - 1; i > opener_index; i--) {
- /* If the close has an opening counter before the range, we have to
- * un-resolve the opener and add it back to the list of unresolved
- * openers. */
if(ctx->marks[i].flags & MD_MARK_CLOSER) {
int index = ctx->marks[i].prev;
@@ -921,22 +942,47 @@ md_rollback(MD_CTX* ctx, int opener_index, int closer_index)
MD_MARK* opener = &ctx->marks[index];
MD_MARKCHAIN* chain;
+ /* (1.1) + (2.1) */
switch(opener->ch) {
+ case '*': chain = &ASTERISK_OPENERS; break;
+ case '_': chain = &UNDERSCORE_OPENERS; break;
case '`': chain = &BACKTICK_OPENERS; break;
case '<': chain = <_GT_OPENERS; break;
default: MD_UNREACHABLE(); break;
}
-
md_mark_chain_append(ctx, chain, index);
opener->flags &= ~(MD_MARK_OPENER | MD_MARK_CLOSER | MD_MARK_RESOLVED);
+
+ /* (1.2) */
+ ctx->marks[i].flags &= ~(MD_MARK_OPENER | MD_MARK_CLOSER | MD_MARK_RESOLVED);
}
}
- /* Reset any "resolved" flags in the range. */
- ctx->marks[i].flags &= ~(MD_MARK_OPENER | MD_MARK_CLOSER | MD_MARK_RESOLVED);
+ /* (2.2) */
+ if(how == MD_ROLLBACK_ALL)
+ ctx->marks[i].flags &= ~(MD_MARK_OPENER | MD_MARK_CLOSER | MD_MARK_RESOLVED);
}
}
+/* Split a longer mark into two. The new mark takes the given count of characters.
+ * May only be called if a dummy 'D' mark follows.
+ */
+static int
+md_split_mark(MD_CTX* ctx, int mark_index, SZ n)
+{
+ MD_MARK* mark = &ctx->marks[mark_index];
+ MD_MARK* dummy = &ctx->marks[mark_index + 1];
+
+ MD_ASSERT(mark->end - mark->beg > n);
+ MD_ASSERT(dummy->ch == 'D');
+
+ memcpy(dummy, mark, sizeof(MD_MARK));
+ mark->end -= n;
+ dummy->beg = mark->end;
+
+ return mark_index + 1;
+}
+
static int
md_collect_marks(MD_CTX* ctx, const MD_LINE* lines, int n_lines)
{
@@ -947,9 +993,9 @@ md_collect_marks(MD_CTX* ctx, const MD_LINE* lines, int n_lines)
for(i = 0; i < n_lines; i++) {
const MD_LINE* line = &lines[i];
OFF off = line->beg;
- OFF end = line->end;
+ OFF line_end = line->end;
- while(off < end) {
+ while(off < line_end) {
CHAR ch = CH(off);
/* A backslash escape.
* It can go beyond line->end as it may involve escaped new
@@ -972,16 +1018,69 @@ md_collect_marks(MD_CTX* ctx, const MD_LINE* lines, int n_lines)
if((ctx->r.flags & MD_FLAG_COLLAPSEWHITESPACE) && ISWHITESPACE_(ch)) {
OFF tmp = off+1;
- while(tmp < end && ISWHITESPACE(tmp))
+ while(tmp < line_end && ISWHITESPACE(tmp))
tmp++;
- if(tmp - end > 1 || ch != _T(' ')) {
+ if(tmp - off > 1 || ch != _T(' ')) {
PUSH_MARK(ch, off, tmp, MD_MARK_RESOLVED);
off = tmp;
continue;
}
}
+ /* A potential (string) emphasis start/end. */
+ if(ch == _T('*') || ch == _T('_')) {
+ OFF tmp = off+1;
+ int left_level; /* What precedes: 0 = whitespace; 1 = punctuation; 2 = other char. */
+ int right_level; /* What follows: 0 = whitespace; 1 = punctuation; 2 = other char. */
+ unsigned flags = 0;
+
+ while(tmp < line_end && CH(tmp) == ch)
+ tmp++;
+
+ if(off == line->beg || ISWHITESPACE(off-1))
+ left_level = 0;
+ else if(ISPUNCT(off-1))
+ left_level = 1;
+ else
+ left_level = 2;
+
+ if(tmp == line_end || ISWHITESPACE(tmp))
+ right_level = 0;
+ else if(ISPUNCT(tmp))
+ right_level = 1;
+ else
+ right_level = 2;
+
+ /* Intra-word underscore doesn't have special meaning. */
+ if(ch == _T('_') && left_level == 2 && right_level == 2) {
+ left_level = 0;
+ right_level = 0;
+ }
+
+ if(left_level > 0 && left_level >= right_level)
+ flags |= MD_MARK_POTENTIAL_CLOSER;
+ if(right_level > 0 && right_level >= left_level)
+ flags |= MD_MARK_POTENTIAL_OPENER;
+
+ if(flags != 0) {
+ PUSH_MARK(ch, off, tmp, flags);
+
+ /* During resolving, multiple asterisks may have to be
+ * split into independent span start/ends. Consider e.g.
+ * "**foo* bar*". Therefore we push also some empty dummy
+ * marks to have enough space for that. */
+ off++;
+ while(off < tmp) {
+ PUSH_MARK('D', off, off, 0);
+ off++;
+ }
+ continue;
+ }
+
+ off = tmp;
+ }
+
/* A potential code span start/end. */
if(ch == _T('`')) {
unsigned flags;
@@ -993,7 +1092,7 @@ md_collect_marks(MD_CTX* ctx, const MD_LINE* lines, int n_lines)
else
flags = MD_MARK_POTENTIAL_OPENER | MD_MARK_POTENTIAL_CLOSER;
- while(tmp < end && CH(tmp) == _T('`'))
+ while(tmp < line_end && CH(tmp) == _T('`'))
tmp++;
PUSH_MARK(ch, off, tmp, flags);
@@ -1010,7 +1109,7 @@ md_collect_marks(MD_CTX* ctx, const MD_LINE* lines, int n_lines)
/* A potential entity end. */
if(ch == _T(';')) {
- /* We surely cannot be entity unless previous mark is '&'. */
+ /* We surely cannot be entity unless the previous mark is '&'. */
if(ctx->n_marks > 0 && ctx->marks[ctx->n_marks-1].ch == _T('&')) {
PUSH_MARK(ch, off, off+1, MD_MARK_POTENTIAL_CLOSER);
off++;
@@ -1018,7 +1117,7 @@ md_collect_marks(MD_CTX* ctx, const MD_LINE* lines, int n_lines)
}
}
- /* A potential raw HTML start/end. */
+ /* A potential autolink or raw HTML start/end. */
if(ch == _T('<') || ch == _T('>')) {
if(!(ctx->r.flags & MD_FLAG_NOHTMLSPANS)) {
PUSH_MARK(ch, off, off+1, (ch == _T('<') ? MD_MARK_POTENTIAL_OPENER : MD_MARK_POTENTIAL_CLOSER));
@@ -1064,7 +1163,7 @@ md_analyze_backtick(MD_CTX* ctx, int mark_index)
/* Rollback anything found inside it.
* (e.g. the code span contains some back-ticks or other special
* chars we misinterpreted.) */
- md_rollback(ctx, opener_index, mark_index);
+ md_rollback(ctx, opener_index, mark_index, MD_ROLLBACK_ALL);
/* Resolve the span. */
md_resolve_range(ctx, &BACKTICK_OPENERS, opener_index, mark_index);
@@ -1131,7 +1230,7 @@ md_analyze_lt_gt(MD_CTX* ctx, int mark_index, const MD_LINE* lines, int n_lines)
* to resolve the opener. */
MD_ASSERT(detected_end == mark->end);
- md_rollback(ctx, opener_index, mark_index);
+ md_rollback(ctx, opener_index, mark_index, MD_ROLLBACK_ALL);
md_resolve_range(ctx, <_GT_OPENERS, opener_index, mark_index);
if(is_raw_html) {
@@ -1139,11 +1238,12 @@ md_analyze_lt_gt(MD_CTX* ctx, int mark_index, const MD_LINE* lines, int n_lines)
* contents. */
opener->end = opener->beg;
mark->beg = mark->end;
+
+ opener->flags &= ~MD_MARK_AUTOLINK;
+ mark->flags &= ~MD_MARK_AUTOLINK;
} else {
- /* Hack: This is to distinguish the autolink from raw HTML in
- * md_process_inlines(). */
- opener->ch = 'A';
- mark->ch = 'B';
+ opener->flags |= MD_MARK_AUTOLINK;
+ mark->flags |= MD_MARK_AUTOLINK;
}
/* And we are done. */
@@ -1218,14 +1318,56 @@ md_analyze_entity(MD_CTX* ctx, int mark_index)
/* Mark us as an entity.
* As entity has no span, we may just turn the range into a single mark.
* (This also causes we do not get called for ';'. */
+ md_resolve_range(ctx, NULL, mark_index, mark_index+1);
opener->end = closer->end;
- opener->flags |= MD_MARK_OPENER | MD_MARK_RESOLVED;
+}
+
+static void
+md_analyze_simple_pairing_mark(MD_CTX* ctx, MD_MARKCHAIN* chain, int mark_index)
+{
+ MD_MARK* mark = &ctx->marks[mark_index];
+
+ /* If we can be a closer, try to resolve with the preceding opener. */
+ if((mark->flags & MD_MARK_POTENTIAL_CLOSER) && chain->tail >= 0) {
+ int opener_index = chain->tail;
+ MD_MARK* opener = &ctx->marks[opener_index];
+ SZ opener_size = opener->end - opener->beg;
+ SZ closer_size = mark->end - mark->beg;
+
+ if(opener_size > closer_size) {
+ opener_index = md_split_mark(ctx, opener_index, closer_size);
+ md_mark_chain_append(ctx, chain, opener_index);
+ } else if(opener_size < closer_size) {
+ md_split_mark(ctx, mark_index, closer_size - opener_size);
+ }
+
+ md_rollback(ctx, opener_index, mark_index, MD_ROLLBACK_CROSSING);
+ md_resolve_range(ctx, chain, opener_index, mark_index);
+ return;
+ }
+
+ /* If not resolved, and we can be an opener, remember the mark for
+ * the future. */
+ if(mark->flags & MD_MARK_POTENTIAL_OPENER)
+ md_mark_chain_append(ctx, chain, mark_index);
+}
+
+static inline void
+md_analyze_asterisk(MD_CTX* ctx, int mark_index)
+{
+ md_analyze_simple_pairing_mark(ctx, &ASTERISK_OPENERS, mark_index);
+}
+
+static inline void
+md_analyze_underscore(MD_CTX* ctx, int mark_index)
+{
+ md_analyze_simple_pairing_mark(ctx, &UNDERSCORE_OPENERS, mark_index);
}
/* Table of precedence of various span types. */
static const CHAR* md_precedence_table[] = {
- _T("`<>"), /* Code spans; raw HTML. */
- _T("&"), /* Entities. */
+ _T("&`<>"), /* Code spans; autolinks; raw HTML. */
+ _T("*_") /* Emphasis and string emphasis. */
};
static void
@@ -1239,10 +1381,12 @@ md_analyze_marks(MD_CTX* ctx, const MD_LINE* lines, int n_lines, int precedence_
/* Skip resolved spans. */
if(mark->flags & MD_MARK_RESOLVED) {
- if(mark->flags & MD_MARK_OPENER)
+ if(mark->flags & MD_MARK_OPENER) {
+ MD_ASSERT(i < mark->next);
i = mark->next + 1;
- else
+ } else {
i++;
+ }
continue;
}
@@ -1254,18 +1398,12 @@ md_analyze_marks(MD_CTX* ctx, const MD_LINE* lines, int n_lines, int precedence_
/* Analyze the mark. */
switch(mark->ch) {
- case '`':
- md_analyze_backtick(ctx, i);
- break;
-
- case '<':
- case '>':
- md_analyze_lt_gt(ctx, i, lines, n_lines);
- break;
-
- case '&':
- md_analyze_entity(ctx, i);
- break;
+ case '`': md_analyze_backtick(ctx, i); break;
+ case '<': /* Pass through. */
+ case '>': md_analyze_lt_gt(ctx, i, lines, n_lines); break;
+ case '&': md_analyze_entity(ctx, i); break;
+ case '*': md_analyze_asterisk(ctx, i); break;
+ case '_': md_analyze_underscore(ctx, i); break;
}
i++;
@@ -1363,23 +1501,45 @@ md_process_inlines(MD_CTX* ctx, const MD_LINE* lines, int n_lines)
}
break;
- case 'A': /* Autolink. */
- det.a.href = STR(mark->end);
- det.a.href_size = ctx->marks[mark->next].beg - mark->end;
- MD_ENTER_SPAN(MD_SPAN_A, (void*) &det);
- break;
- case 'B':
- /* The detail already has to be initialized: There cannot
- * be any resolved mark between the autlink opener and
- * closer. */
- MD_LEAVE_SPAN(MD_SPAN_A, (void*) &det);
+ case '_':
+ case '*': /* Emphasis, strong emphasis. */
+ if(mark->flags & MD_MARK_OPENER) {
+ while(off + 1 < mark->end) {
+ MD_ENTER_SPAN(MD_SPAN_STRONG, NULL);
+ off += 2;
+ }
+ if(off < mark->end)
+ MD_ENTER_SPAN(MD_SPAN_EM, NULL);
+ } else {
+ if((mark->end - off) & 0x01) {
+ MD_LEAVE_SPAN(MD_SPAN_EM, NULL);
+ off++;
+ }
+ while(off < mark->end) {
+ MD_LEAVE_SPAN(MD_SPAN_STRONG, NULL);
+ off += 2;
+ }
+ }
break;
- case '<': /* Raw HTML. */
- text_type = MD_TEXT_HTML;
+ case '<': /* Autolink or raw HTML. */
+ if(mark->flags & MD_MARK_AUTOLINK) {
+ det.a.href = STR(mark->end);
+ det.a.href_size = ctx->marks[mark->next].beg - mark->end;
+ MD_ENTER_SPAN(MD_SPAN_A, (void*) &det);
+ } else {
+ text_type = MD_TEXT_HTML;
+ }
break;
+
case '>':
- text_type = MD_TEXT_NORMAL;
+ if(mark->flags & MD_MARK_AUTOLINK)
+ /* The detail already has to be initialized: There cannot
+ * be any resolved mark between the autlink opener and
+ * closer. */
+ MD_LEAVE_SPAN(MD_SPAN_A, (void*) &det);
+ else
+ text_type = MD_TEXT_NORMAL;
break;
case '&': /* Entity. */
diff --git a/md4c/md4c.h b/md4c/md4c.h
index d69d40b..7f59cf4 100644
--- a/md4c/md4c.h
+++ b/md4c/md4c.h
@@ -89,6 +89,12 @@ enum MD_BLOCKTYPE_tag {
* like paragraph or list item. */
typedef enum MD_SPANTYPE_tag MD_SPANTYPE;
enum MD_SPANTYPE_tag {
+ /* <em>...</em> */
+ MD_SPAN_EM,
+
+ /* <strong>...</strong> */
+ MD_SPAN_STRONG,
+
/* <a href="xxx">...</a>
* Detail: See structure MD_SPAN_A_DETAIL. */
MD_SPAN_A,