Implement task lists. (#50) Fixes #30.
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
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 2421b3f..b2be03c 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -6,6 +6,14 @@
New features:
+ * Add extension for GitHub-style task lists:
+
+ ```
+ * [x] foo
+ * [x] bar
+ * [ ] baz
+ ```
+
* Renamed structure `MD_RENDERER` to `MD_PARSER` and refactorize its contents
a little bit. Note this is source-level incompatible and initialization code
in apps may need to be updated.
diff --git a/README.md b/README.md
index 3c95d66..abce6ce 100644
--- a/README.md
+++ b/README.md
@@ -88,6 +88,8 @@ extensions and/or deviations from the specification.
* With the flag `MD_FLAG_TABLES`, GitHub-style tables are supported.
+ * With the flag `MD_FLAG_TASKLISTS`, GitHub-style task lists are supported.
+
* With the flag `MD_FLAG_STRIKETHROUGH`, strike-through spans are enabled
(text enclosed in tilde marks, e.g. `~foo bar~`).
diff --git a/md2html/md2html.c b/md2html/md2html.c
index bad4be5..0d5b38b 100644
--- a/md2html/md2html.c
+++ b/md2html/md2html.c
@@ -208,6 +208,7 @@ static const option cmdline_options[] = {
{ "fcollapse-whitespace", 0, 'W', OPTION_ARG_NONE },
{ "ftables", 0, 'T', OPTION_ARG_NONE },
{ "fstrikethrough", 0, 'S', OPTION_ARG_NONE },
+ { "ftasklists", 0, 'X', OPTION_ARG_NONE },
{ 0 }
};
@@ -255,6 +256,7 @@ usage(void)
" --fno-html Same as --fno-html-blocks --fno-html-spans\n"
" --ftables Enable tables\n"
" --fstrikethrough Enable strikethrough spans\n"
+ " --ftasklists Enable task lists\n"
);
}
@@ -302,6 +304,7 @@ cmdline_callback(int opt, char const* value, void* data)
case 'V': parser_flags |= MD_FLAG_PERMISSIVEAUTOLINKS; break;
case 'T': parser_flags |= MD_FLAG_TABLES; break;
case 'S': parser_flags |= MD_FLAG_STRIKETHROUGH; break;
+ case 'X': parser_flags |= MD_FLAG_TASKLISTS; break;
default:
fprintf(stderr, "Illegal option: %s\n", value);
diff --git a/md2html/render_html.c b/md2html/render_html.c
index f93b5cf..9fc62f8 100644
--- a/md2html/render_html.c
+++ b/md2html/render_html.c
@@ -268,6 +268,20 @@ render_open_ol_block(MD_RENDER_HTML* r, const MD_BLOCK_OL_DETAIL* det)
}
static void
+render_open_li_block(MD_RENDER_HTML* r, const MD_BLOCK_LI_DETAIL* det)
+{
+ if(det->is_task) {
+ RENDER_LITERAL(r, "<li class=\"task-list-item\">"
+ "<input type=\"checkbox\" class=\"task-list-item-checkbox\" disabled");
+ if(det->task_mark == 'x' || det->task_mark == 'X')
+ RENDER_LITERAL(r, " checked");
+ RENDER_LITERAL(r, ">");
+ } else {
+ RENDER_LITERAL(r, "<li>");
+ }
+}
+
+static void
render_open_code_block(MD_RENDER_HTML* r, const MD_BLOCK_CODE_DETAIL* det)
{
RENDER_LITERAL(r, "<pre><code");
@@ -350,7 +364,7 @@ enter_block_callback(MD_BLOCKTYPE type, void* detail, void* userdata)
case MD_BLOCK_QUOTE: RENDER_LITERAL(r, "<blockquote>\n"); break;
case MD_BLOCK_UL: RENDER_LITERAL(r, "<ul>\n"); break;
case MD_BLOCK_OL: render_open_ol_block(r, (const MD_BLOCK_OL_DETAIL*)detail); break;
- case MD_BLOCK_LI: RENDER_LITERAL(r, "<li>"); break;
+ case MD_BLOCK_LI: render_open_li_block(r, (const MD_BLOCK_LI_DETAIL*)detail); break;
case MD_BLOCK_HR: RENDER_LITERAL(r, "<hr>\n"); break;
case MD_BLOCK_H: RENDER_LITERAL(r, head[((MD_BLOCK_H_DETAIL*)detail)->level - 1]); break;
case MD_BLOCK_CODE: render_open_code_block(r, (const MD_BLOCK_CODE_DETAIL*) detail); break;
diff --git a/md4c/md4c.c b/md4c/md4c.c
index 959acd9..e73d996 100644
--- a/md4c/md4c.c
+++ b/md4c/md4c.c
@@ -4285,11 +4285,13 @@ struct MD_BLOCK_tag {
/* MD_BLOCK_H: Header level (1 - 6)
* MD_BLOCK_CODE: Non-zero if fenced, zero if indented.
- * MD_BLOCK_TABLE: Column count (as determined by the table underline)
+ * MD_BLOCK_LI: Task mark character (0 if not task list item, 'x', 'X' or ' ').
+ * MD_BLOCK_TABLE: Column count (as determined by the table underline).
*/
unsigned data : 16;
/* Leaf blocks: Count of lines (MD_LINE or MD_VERBATIMLINE) on the block.
+ * MD_BLOCK_LI: Task mark offset in the input doc.
* MD_BLOCK_OL: Start item number.
*/
unsigned n_lines;
@@ -4298,10 +4300,12 @@ struct MD_BLOCK_tag {
struct MD_CONTAINER_tag {
CHAR ch;
unsigned is_loose : 8;
+ unsigned is_task : 8;
unsigned start;
unsigned mark_indent;
unsigned contents_indent;
OFF block_byte_off;
+ OFF task_mark_off;
};
@@ -4515,6 +4519,7 @@ md_process_all_blocks(MD_CTX* ctx)
union {
MD_BLOCK_UL_DETAIL ul;
MD_BLOCK_OL_DETAIL ol;
+ MD_BLOCK_LI_DETAIL li;
} det;
switch(block->type) {
@@ -4529,6 +4534,12 @@ md_process_all_blocks(MD_CTX* ctx)
det.ol.mark_delimiter = (CHAR) block->data;
break;
+ case MD_BLOCK_LI:
+ det.li.is_task = (block->data != 0);
+ det.li.task_mark = (CHAR) block->data;
+ det.li.task_mark_offset = (OFF) block->n_lines;
+ break;
+
default:
/* noop */
break;
@@ -5240,11 +5251,13 @@ md_enter_child_containers(MD_CTX* ctx, int n_children, unsigned data)
MD_CHECK(md_push_container_bytes(ctx,
(is_ordered_list ? MD_BLOCK_OL : MD_BLOCK_UL),
c->start, data, MD_BLOCK_CONTAINER_OPENER));
- MD_CHECK(md_push_container_bytes(ctx, MD_BLOCK_LI, 0, data, MD_BLOCK_CONTAINER_OPENER));
+ MD_CHECK(md_push_container_bytes(ctx, MD_BLOCK_LI,
+ c->task_mark_off, (c->is_task ? CH(c->task_mark_off) : 0),
+ MD_BLOCK_CONTAINER_OPENER));
break;
case _T('>'):
- MD_CHECK(md_push_container_bytes(ctx, MD_BLOCK_QUOTE, 0, data, MD_BLOCK_CONTAINER_OPENER));
+ MD_CHECK(md_push_container_bytes(ctx, MD_BLOCK_QUOTE, 0, 0, MD_BLOCK_CONTAINER_OPENER));
break;
default:
@@ -5275,8 +5288,9 @@ md_leave_child_containers(MD_CTX* ctx, int n_keep)
case _T('-'):
case _T('+'):
case _T('*'):
- MD_CHECK(md_push_container_bytes(ctx, MD_BLOCK_LI, 0,
- 0, MD_BLOCK_CONTAINER_CLOSER));
+ MD_CHECK(md_push_container_bytes(ctx, MD_BLOCK_LI,
+ c->task_mark_off, (c->is_task ? CH(c->task_mark_off) : 0),
+ MD_BLOCK_CONTAINER_CLOSER));
MD_CHECK(md_push_container_bytes(ctx,
(is_ordered_list ? MD_BLOCK_OL : MD_BLOCK_UL), 0,
c->ch, MD_BLOCK_CONTAINER_CLOSER));
@@ -5310,6 +5324,7 @@ md_is_container_mark(MD_CTX* ctx, unsigned indent, OFF beg, OFF* p_end, MD_CONTA
off++;
p_container->ch = _T('>');
p_container->is_loose = FALSE;
+ p_container->is_task = FALSE;
p_container->mark_indent = indent;
p_container->contents_indent = indent + 1;
*p_end = off;
@@ -5320,9 +5335,10 @@ md_is_container_mark(MD_CTX* ctx, unsigned indent, OFF beg, OFF* p_end, MD_CONTA
if(off+1 < ctx->size && ISANYOF(off, _T("-+*")) && (ISBLANK(off+1) || ISNEWLINE(off+1))) {
p_container->ch = CH(off);
p_container->is_loose = FALSE;
+ p_container->is_task = FALSE;
p_container->mark_indent = indent;
p_container->contents_indent = indent + 1;
- *p_end = off+1;
+ *p_end = off + 1;
return TRUE;
}
@@ -5338,9 +5354,10 @@ md_is_container_mark(MD_CTX* ctx, unsigned indent, OFF beg, OFF* p_end, MD_CONTA
if(off+1 < ctx->size && (CH(off) == _T('.') || CH(off) == _T(')')) && (ISBLANK(off+1) || ISNEWLINE(off+1))) {
p_container->ch = CH(off);
p_container->is_loose = FALSE;
+ p_container->is_task = FALSE;
p_container->mark_indent = indent;
p_container->contents_indent = indent + off - beg + 1;
- *p_end = off+1;
+ *p_end = off + 1;
return TRUE;
}
@@ -5726,6 +5743,28 @@ redo:
n_parents = ctx->n_containers;
}
+ /* Check for task mark. */
+ if((ctx->parser.flags & MD_FLAG_TASKLISTS) && n_brothers + n_children > 0 &&
+ ISANYOF_(ctx->containers[ctx->n_containers-1].ch, _T("-+*.)")))
+ {
+ OFF tmp = off;
+
+ while(tmp < ctx->size && tmp < off + 3 && ISBLANK(tmp))
+ tmp++;
+ if(tmp + 2 < ctx->size && CH(tmp) == _T('[') &&
+ ISANYOF(tmp+1, _T("xX ")) && CH(tmp+2) == _T(']') &&
+ (tmp + 3 == ctx->size || ISBLANK(tmp+3) || ISNEWLINE(tmp+3)))
+ {
+ MD_CONTAINER* task_container = (n_children > 0 ? &ctx->containers[ctx->n_containers-1] : &container);
+ task_container->is_task = TRUE;
+ task_container->task_mark_off = tmp + 1;
+ off = tmp + 3;
+ while(ISWHITESPACE(off))
+ off++;
+ line->beg = off;
+ }
+ }
+
done:
/* Scan for end of the line.
*
@@ -5787,8 +5826,16 @@ done_on_eol:
/* Enter any container we found a mark for. */
if(n_brothers > 0) {
MD_ASSERT(n_brothers == 1);
- MD_CHECK(md_push_container_bytes(ctx, MD_BLOCK_LI, 0, 0,
- MD_BLOCK_CONTAINER_CLOSER | MD_BLOCK_CONTAINER_OPENER));
+ MD_CHECK(md_push_container_bytes(ctx, MD_BLOCK_LI,
+ ctx->containers[n_parents].task_mark_off,
+ (ctx->containers[n_parents].is_task ? CH(ctx->containers[n_parents].task_mark_off) : 0),
+ MD_BLOCK_CONTAINER_CLOSER));
+ MD_CHECK(md_push_container_bytes(ctx, MD_BLOCK_LI,
+ container.task_mark_off,
+ (container.is_task ? CH(container.task_mark_off) : 0),
+ MD_BLOCK_CONTAINER_OPENER));
+ ctx->containers[n_parents].is_task = container.is_task;
+ ctx->containers[n_parents].task_mark_off = container.task_mark_off;
}
if(n_children > 0)
diff --git a/md4c/md4c.h b/md4c/md4c.h
index 7caba5d..10cba67 100644
--- a/md4c/md4c.h
+++ b/md4c/md4c.h
@@ -47,7 +47,8 @@ typedef unsigned MD_OFFSET;
/* Block represents a part of document hierarchy structure like a paragraph
- * or list item. */
+ * or list item.
+ */
typedef enum MD_BLOCKTYPE {
/* <body>...</body> */
MD_BLOCK_DOC = 0,
@@ -63,7 +64,8 @@ typedef enum MD_BLOCKTYPE {
* Detail: Structure MD_BLOCK_OL_DETAIL. */
MD_BLOCK_OL,
- /* <li>...</li> */
+ /* <li>...</li>
+ * Detail: Structure MD_BLOCK_LI_DETAIL. */
MD_BLOCK_LI,
/* <hr> */
@@ -186,7 +188,7 @@ typedef enum MD_ALIGN {
* So, for example, lets consider an image has a title attribute string
* set to "foo " bar". (Note the string size is 14.)
*
- * Then:
+ * Then the attribute MD_SPAN_IMG_DETAIL::title shall provide the following:
* -- [0]: "foo " (substr_types[0] == MD_TEXT_NORMAL; substr_offsets[0] == 0)
* -- [1]: """ (substr_types[1] == MD_TEXT_ENTITY; substr_offsets[1] == 4)
* -- [2]: " bar" (substr_types[2] == MD_TEXT_NORMAL; substr_offsets[2] == 10)
@@ -207,17 +209,24 @@ typedef struct MD_ATTRIBUTE {
/* Detailed info for MD_BLOCK_UL. */
typedef struct MD_BLOCK_UL_DETAIL {
- int is_tight; /* Non-zero if tight list, zero of loose. */
+ int is_tight; /* Non-zero if tight list, zero if loose. */
MD_CHAR mark; /* Item bullet character in MarkDown source of the list, e.g. '-', '+', '*'. */
} MD_BLOCK_UL_DETAIL;
/* Detailed info for MD_BLOCK_OL. */
typedef struct MD_BLOCK_OL_DETAIL {
unsigned start; /* Start index of the ordered list. */
- int is_tight; /* Non-zero if tight list, zero of loose. */
+ int is_tight; /* Non-zero if tight list, zero if loose. */
MD_CHAR mark_delimiter; /* Character delimiting the item marks in MarkDown source, e.g. '.' or ')' */
} MD_BLOCK_OL_DETAIL;
+/* Detailed info for MD_BLOCK_LI. */
+typedef struct MD_BLOCK_LI_DETAIL {
+ int is_task; /* Can be non-zero only with MD_FLAG_TASKLISTS */
+ MD_CHAR task_mark; /* If is_task, then one of 'x', 'X' or ' '. Undefined otherwise. */
+ MD_OFFSET task_mark_offset; /* If is_task, then offset in the input of the char between '[' and ']'. */
+} MD_BLOCK_LI_DETAIL;
+
/* Detailed info for MD_BLOCK_H. */
typedef struct MD_BLOCK_H_DETAIL {
unsigned level; /* Header level (1 - 6) */
@@ -262,6 +271,7 @@ typedef struct MD_SPAN_IMG_DETAIL {
#define MD_FLAG_TABLES 0x0100 /* Enable tables extension. */
#define MD_FLAG_STRIKETHROUGH 0x0200 /* Enable strikethrough extension. */
#define MD_FLAG_PERMISSIVEWWWAUTOLINKS 0x0400 /* Enable WWW autolinks (even without any scheme prefix, if they begin with 'www.') */
+#define MD_FLAG_TASKLISTS 0x0800 /* Enable task list extension. */
#define MD_FLAG_PERMISSIVEAUTOLINKS (MD_FLAG_PERMISSIVEEMAILAUTOLINKS | MD_FLAG_PERMISSIVEURLAUTOLINKS | MD_FLAG_PERMISSIVEWWWAUTOLINKS)
#define MD_FLAG_NOHTML (MD_FLAG_NOHTMLBLOCKS | MD_FLAG_NOHTMLSPANS)
@@ -276,7 +286,7 @@ typedef struct MD_SPAN_IMG_DETAIL {
* extensions, bringing the dialect closer to the original, are implemented.
*/
#define MD_DIALECT_COMMONMARK 0
-#define MD_DIALECT_GITHUB (MD_FLAG_PERMISSIVEAUTOLINKS | MD_FLAG_TABLES | MD_FLAG_STRIKETHROUGH)
+#define MD_DIALECT_GITHUB (MD_FLAG_PERMISSIVEAUTOLINKS | MD_FLAG_TABLES | MD_FLAG_STRIKETHROUGH | MD_FLAG_TASKLISTS)
/* Renderer structure.
*/
diff --git a/scripts/run-tests.sh b/scripts/run-tests.sh
index cc62111..7f5324f 100755
--- a/scripts/run-tests.sh
+++ b/scripts/run-tests.sh
@@ -55,5 +55,9 @@ echo "Strikethrough extension:"
$PYTHON "$TEST_DIR/spec_tests.py" -s "$TEST_DIR/strikethrough.txt" -p "$PROGRAM --fstrikethrough"
echo
+echo "Task lists extension:"
+$PYTHON "$TEST_DIR/spec_tests.py" -s "$TEST_DIR/tasklists.txt" -p "$PROGRAM --ftasklists"
+
+echo
echo "Pathological input:"
$PYTHON "$TEST_DIR/pathological_tests.py" -p "$PROGRAM"
diff --git a/test/tasklists.txt b/test/tasklists.txt
new file mode 100644
index 0000000..378d520
--- /dev/null
+++ b/test/tasklists.txt
@@ -0,0 +1,113 @@
+
+# Tasklists
+
+With the flag `MD_FLAG_TASKLISTS`, MD4C enables extension for recognition of
+task lists.
+
+Basic task list may look as follows:
+
+```````````````````````````````` example
+ * [x] foo
+ * [X] bar
+ * [ ] baz
+.
+<ul>
+<li class="task-list-item"><input type="checkbox" class="task-list-item-checkbox" disabled checked>foo</li>
+<li class="task-list-item"><input type="checkbox" class="task-list-item-checkbox" disabled checked>bar</li>
+<li class="task-list-item"><input type="checkbox" class="task-list-item-checkbox" disabled>baz</li>
+</ul>
+````````````````````````````````
+
+Task lists can also be in ordered lists:
+```````````````````````````````` example
+ 1. [x] foo
+ 2. [X] bar
+ 3. [ ] baz
+.
+<ol>
+<li class="task-list-item"><input type="checkbox" class="task-list-item-checkbox" disabled checked>foo</li>
+<li class="task-list-item"><input type="checkbox" class="task-list-item-checkbox" disabled checked>bar</li>
+<li class="task-list-item"><input type="checkbox" class="task-list-item-checkbox" disabled>baz</li>
+</ol>
+````````````````````````````````
+
+Task lists can also be nested in ordinary lists:
+```````````````````````````````` example
+ * xxx:
+ * [x] foo
+ * [x] bar
+ * [ ] baz
+ * yyy:
+ * [ ] qux
+ * [x] quux
+ * [ ] quuz
+.
+<ul>
+<li>xxx:
+<ul>
+<li class="task-list-item"><input type="checkbox" class="task-list-item-checkbox" disabled checked>foo</li>
+<li class="task-list-item"><input type="checkbox" class="task-list-item-checkbox" disabled checked>bar</li>
+<li class="task-list-item"><input type="checkbox" class="task-list-item-checkbox" disabled>baz</li>
+</ul></li>
+<li>yyy:
+<ul>
+<li class="task-list-item"><input type="checkbox" class="task-list-item-checkbox" disabled>qux</li>
+<li class="task-list-item"><input type="checkbox" class="task-list-item-checkbox" disabled checked>quux</li>
+<li class="task-list-item"><input type="checkbox" class="task-list-item-checkbox" disabled>quuz</li>
+</ul></li>
+</ul>
+````````````````````````````````
+
+Or in a parent task list:
+```````````````````````````````` example
+ 1. [x] xxx:
+ * [x] foo
+ * [x] bar
+ * [ ] baz
+ 2. [ ] yyy:
+ * [ ] qux
+ * [x] quux
+ * [ ] quuz
+.
+<ol>
+<li class="task-list-item"><input type="checkbox" class="task-list-item-checkbox" disabled checked>xxx:
+<ul>
+<li class="task-list-item"><input type="checkbox" class="task-list-item-checkbox" disabled checked>foo</li>
+<li class="task-list-item"><input type="checkbox" class="task-list-item-checkbox" disabled checked>bar</li>
+<li class="task-list-item"><input type="checkbox" class="task-list-item-checkbox" disabled>baz</li>
+</ul></li>
+<li class="task-list-item"><input type="checkbox" class="task-list-item-checkbox" disabled>yyy:
+<ul>
+<li class="task-list-item"><input type="checkbox" class="task-list-item-checkbox" disabled>qux</li>
+<li class="task-list-item"><input type="checkbox" class="task-list-item-checkbox" disabled checked>quux</li>
+<li class="task-list-item"><input type="checkbox" class="task-list-item-checkbox" disabled>quuz</li>
+</ul></li>
+</ol>
+````````````````````````````````
+
+Also, ordinary lists can be nested in the task lists.
+```````````````````````````````` example
+ * [x] xxx:
+ * foo
+ * bar
+ * baz
+ * [ ] yyy:
+ * qux
+ * quux
+ * quuz
+.
+<ul>
+<li class="task-list-item"><input type="checkbox" class="task-list-item-checkbox" disabled checked>xxx:
+<ul>
+<li>foo</li>
+<li>bar</li>
+<li>baz</li>
+</ul></li>
+<li class="task-list-item"><input type="checkbox" class="task-list-item-checkbox" disabled>yyy:
+<ul>
+<li>qux</li>
+<li>quux</li>
+<li>quuz</li>
+</ul></li>
+</ul>
+````````````````````````````````