/src/mupdf/thirdparty/cmark-gfm/extensions/tasklist.c
Line | Count | Source |
1 | | #include "tasklist.h" |
2 | | #include <parser.h> |
3 | | #include <render.h> |
4 | | #include <html.h> |
5 | | #include "ext_scanners.h" |
6 | | |
7 | | typedef enum { |
8 | | CMARK_TASKLIST_NOCHECKED, |
9 | | CMARK_TASKLIST_CHECKED, |
10 | | } cmark_tasklist_type; |
11 | | |
12 | | // Local constants |
13 | | static const char *TYPE_STRING = "tasklist"; |
14 | | |
15 | 0 | static const char *get_type_string(cmark_syntax_extension *extension, cmark_node *node) { |
16 | 0 | return TYPE_STRING; |
17 | 0 | } |
18 | | |
19 | | |
20 | | // Return 1 if state was set, 0 otherwise |
21 | 0 | int cmark_gfm_extensions_set_tasklist_item_checked(cmark_node *node, bool is_checked) { |
22 | | // The node has to exist, and be an extension, and actually be the right type in order to get the value. |
23 | 0 | if (!node || !node->extension || strcmp(cmark_node_get_type_string(node), TYPE_STRING)) |
24 | 0 | return 0; |
25 | | |
26 | 0 | node->as.list.checked = is_checked; |
27 | 0 | return 1; |
28 | 0 | } |
29 | | |
30 | 0 | bool cmark_gfm_extensions_get_tasklist_item_checked(cmark_node *node) { |
31 | 0 | if (!node || !node->extension || strcmp(cmark_node_get_type_string(node), TYPE_STRING)) |
32 | 0 | return false; |
33 | | |
34 | 0 | if (node->as.list.checked) { |
35 | 0 | return true; |
36 | 0 | } |
37 | 0 | else { |
38 | 0 | return false; |
39 | 0 | } |
40 | 0 | } |
41 | | |
42 | | static bool parse_node_item_prefix(cmark_parser *parser, const char *input, |
43 | 0 | cmark_node *container) { |
44 | 0 | bool res = false; |
45 | |
|
46 | 0 | if (parser->indent >= |
47 | 0 | container->as.list.marker_offset + container->as.list.padding) { |
48 | 0 | cmark_parser_advance_offset(parser, input, container->as.list.marker_offset + |
49 | 0 | container->as.list.padding, |
50 | 0 | true); |
51 | 0 | res = true; |
52 | 0 | } else if (parser->blank && container->first_child != NULL) { |
53 | | // if container->first_child is NULL, then the opening line |
54 | | // of the list item was blank after the list marker; in this |
55 | | // case, we are done with the list item. |
56 | 0 | cmark_parser_advance_offset(parser, input, parser->first_nonspace - parser->offset, |
57 | 0 | false); |
58 | 0 | res = true; |
59 | 0 | } |
60 | 0 | return res; |
61 | 0 | } |
62 | | |
63 | | static int matches(cmark_syntax_extension *self, cmark_parser *parser, |
64 | | unsigned char *input, int len, |
65 | 0 | cmark_node *parent_container) { |
66 | 0 | return parse_node_item_prefix(parser, (const char*)input, parent_container); |
67 | 0 | } |
68 | | |
69 | | static int can_contain(cmark_syntax_extension *extension, cmark_node *node, |
70 | 0 | cmark_node_type child_type) { |
71 | 0 | return (node->type == CMARK_NODE_ITEM) ? 1 : 0; |
72 | 0 | } |
73 | | |
74 | | static cmark_node *open_tasklist_item(cmark_syntax_extension *self, |
75 | | int indented, cmark_parser *parser, |
76 | | cmark_node *parent_container, |
77 | 54.8k | unsigned char *input, int len) { |
78 | 54.8k | cmark_node_type node_type = cmark_node_get_type(parent_container); |
79 | 54.8k | if (node_type != CMARK_NODE_ITEM) { |
80 | 54.8k | return NULL; |
81 | 54.8k | } |
82 | | |
83 | 2 | bufsize_t matched = scan_tasklist(input, len, 0); |
84 | 2 | if (!matched) { |
85 | 2 | return NULL; |
86 | 2 | } |
87 | | |
88 | 0 | cmark_node_set_syntax_extension(parent_container, self); |
89 | 0 | cmark_parser_advance_offset(parser, (char *)input, 3, false); |
90 | | |
91 | | // Either an upper or lower case X means the task is completed. |
92 | 0 | parent_container->as.list.checked = (strstr((char*)input, "[x]") || strstr((char*)input, "[X]")); |
93 | |
|
94 | 0 | return NULL; |
95 | 2 | } |
96 | | |
97 | | static void commonmark_render(cmark_syntax_extension *extension, |
98 | | cmark_renderer *renderer, cmark_node *node, |
99 | 0 | cmark_event_type ev_type, int options) { |
100 | 0 | bool entering = (ev_type == CMARK_EVENT_ENTER); |
101 | 0 | if (entering) { |
102 | 0 | renderer->cr(renderer); |
103 | 0 | if (node->as.list.checked) { |
104 | 0 | renderer->out(renderer, node, "- [x] ", false, LITERAL); |
105 | 0 | } else { |
106 | 0 | renderer->out(renderer, node, "- [ ] ", false, LITERAL); |
107 | 0 | } |
108 | 0 | cmark_strbuf_puts(renderer->prefix, " "); |
109 | 0 | } else { |
110 | 0 | cmark_strbuf_truncate(renderer->prefix, renderer->prefix->size - 2); |
111 | 0 | renderer->cr(renderer); |
112 | 0 | } |
113 | 0 | } |
114 | | |
115 | | static void html_render(cmark_syntax_extension *extension, |
116 | | cmark_html_renderer *renderer, cmark_node *node, |
117 | 0 | cmark_event_type ev_type, int options) { |
118 | 0 | bool entering = (ev_type == CMARK_EVENT_ENTER); |
119 | 0 | if (entering) { |
120 | 0 | cmark_html_render_cr(renderer->html); |
121 | 0 | cmark_strbuf_puts(renderer->html, "<li"); |
122 | 0 | cmark_html_render_sourcepos(node, renderer->html, options); |
123 | 0 | cmark_strbuf_putc(renderer->html, '>'); |
124 | 0 | if (node->as.list.checked) { |
125 | 0 | cmark_strbuf_puts(renderer->html, "<input type=\"checkbox\" checked=\"\" disabled=\"\" /> "); |
126 | 0 | } else { |
127 | 0 | cmark_strbuf_puts(renderer->html, "<input type=\"checkbox\" disabled=\"\" /> "); |
128 | 0 | } |
129 | 0 | } else { |
130 | 0 | cmark_strbuf_puts(renderer->html, "</li>\n"); |
131 | 0 | } |
132 | 0 | } |
133 | | |
134 | | static const char *xml_attr(cmark_syntax_extension *extension, |
135 | 0 | cmark_node *node) { |
136 | 0 | if (node->as.list.checked) { |
137 | 0 | return " completed=\"true\""; |
138 | 0 | } else { |
139 | 0 | return " completed=\"false\""; |
140 | 0 | } |
141 | 0 | } |
142 | | |
143 | 1 | cmark_syntax_extension *create_tasklist_extension(void) { |
144 | 1 | cmark_syntax_extension *ext = cmark_syntax_extension_new("tasklist"); |
145 | | |
146 | 1 | cmark_syntax_extension_set_match_block_func(ext, matches); |
147 | 1 | cmark_syntax_extension_set_get_type_string_func(ext, get_type_string); |
148 | 1 | cmark_syntax_extension_set_open_block_func(ext, open_tasklist_item); |
149 | 1 | cmark_syntax_extension_set_can_contain_func(ext, can_contain); |
150 | 1 | cmark_syntax_extension_set_commonmark_render_func(ext, commonmark_render); |
151 | 1 | cmark_syntax_extension_set_plaintext_render_func(ext, commonmark_render); |
152 | 1 | cmark_syntax_extension_set_html_render_func(ext, html_render); |
153 | 1 | cmark_syntax_extension_set_xml_attr_func(ext, xml_attr); |
154 | | |
155 | 1 | return ext; |
156 | 1 | } |