Skip to content

Commit b97d094

Browse files
Merge pull request #77 from jmschonfeld/list-markers-api
Expose list marker characters for unordered lists rdar://147511402
2 parents b022b08 + 785b6f5 commit b97d094

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

api_test/main.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ static void accessors(test_batch_runner *runner) {
107107
"get_list_type bullet");
108108
INT_EQ(runner, cmark_node_get_list_tight(bullet_list), 1,
109109
"get_list_tight tight");
110+
INT_EQ(runner, cmark_node_get_list_marker(bullet_list), CMARK_ASTERISK_LIST_MARKER,
111+
"get_list_marker asterisk");
110112

111113
cmark_node *ordered_list = cmark_node_next(bullet_list);
112114
INT_EQ(runner, cmark_node_get_list_type(ordered_list), CMARK_ORDERED_LIST,
@@ -146,6 +148,7 @@ static void accessors(test_batch_runner *runner) {
146148

147149
OK(runner, cmark_node_set_heading_level(heading, 3), "set_heading_level");
148150

151+
OK(runner, cmark_node_set_list_marker(bullet_list, CMARK_PLUS_LIST_MARKER), "set_list_marker plus");
149152
OK(runner, cmark_node_set_list_type(bullet_list, CMARK_ORDERED_LIST),
150153
"set_list_type ordered");
151154
OK(runner, cmark_node_set_list_delim(bullet_list, CMARK_PAREN_DELIM),
@@ -211,6 +214,7 @@ static void accessors(test_batch_runner *runner) {
211214
"get_list_type error");
212215
INT_EQ(runner, cmark_node_get_list_start(code), 0, "get_list_start error");
213216
INT_EQ(runner, cmark_node_get_list_tight(fenced), 0, "get_list_tight error");
217+
INT_EQ(runner, cmark_node_get_list_marker(heading), CMARK_NO_LIST_MARKER, "get_list_marker error");
214218
OK(runner, cmark_node_get_literal(ordered_list) == NULL, "get_literal error");
215219
OK(runner, cmark_node_get_fence_info(paragraph) == NULL,
216220
"get_fence_info error");
@@ -225,6 +229,7 @@ static void accessors(test_batch_runner *runner) {
225229
"set_list_type error");
226230
OK(runner, !cmark_node_set_list_start(code, 3), "set_list_start error");
227231
OK(runner, !cmark_node_set_list_tight(fenced, 0), "set_list_tight error");
232+
OK(runner, !cmark_node_set_list_marker(heading, CMARK_PLUS_LIST_MARKER), "set_list_marker error");
228233
OK(runner, !cmark_node_set_literal(ordered_list, "content\n"),
229234
"set_literal error");
230235
OK(runner, !cmark_node_set_fence_info(paragraph, "lang"),
@@ -240,6 +245,8 @@ static void accessors(test_batch_runner *runner) {
240245
"set_list_type invalid");
241246
OK(runner, !cmark_node_set_list_start(bullet_list, -1),
242247
"set_list_start negative");
248+
OK(runner, !cmark_node_set_list_marker(bullet_list, CMARK_NO_LIST_MARKER),
249+
"set_list_marker invalid");
243250

244251
cmark_node_free(doc);
245252
}

src/include/cmark-gfm.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,13 @@ typedef enum {
9090
CMARK_PAREN_DELIM
9191
} cmark_delim_type;
9292

93+
typedef enum {
94+
CMARK_NO_LIST_MARKER,
95+
CMARK_HYPHEN_LIST_MARKER,
96+
CMARK_PLUS_LIST_MARKER,
97+
CMARK_ASTERISK_LIST_MARKER
98+
} cmark_list_marker_type;
99+
93100
typedef struct cmark_node cmark_node;
94101
typedef struct cmark_parser cmark_parser;
95102
typedef struct cmark_iter cmark_iter;
@@ -395,6 +402,16 @@ CMARK_GFM_EXPORT cmark_list_type cmark_node_get_list_type(cmark_node *node);
395402
CMARK_GFM_EXPORT int cmark_node_set_list_type(cmark_node *node,
396403
cmark_list_type type);
397404

405+
/** Returns the list marker of 'node', or `CMARK_NO_LIST_MARKER` if 'node'
406+
* is not a list.
407+
*/
408+
CMARK_GFM_EXPORT cmark_list_marker_type cmark_node_get_list_marker(cmark_node *node);
409+
410+
/** Sets the list marker of 'node', returning 1 on success and 0 on error.
411+
*/
412+
CMARK_GFM_EXPORT int cmark_node_set_list_marker(cmark_node *node,
413+
cmark_list_marker_type listMarker);
414+
398415
/** Returns the list delimiter type of 'node', or `CMARK_NO_DELIM` if 'node'
399416
* is not a list.
400417
*/
@@ -865,6 +882,9 @@ const char *cmark_version_string(void);
865882
#define ORDERED_LIST CMARK_ORDERED_LIST
866883
#define PERIOD_DELIM CMARK_PERIOD_DELIM
867884
#define PAREN_DELIM CMARK_PAREN_DELIM
885+
#define HYPHEN_LIST_MARKER CMARK_HYPHEN_LIST_MARKER
886+
#define PLUS_LIST_MARKER CMARK_PLUS_LIST_MARKER
887+
#define ASTERISK_LIST_MARKER CMARK_ASTERISK_LIST_MARKER
868888
#endif
869889

870890
typedef int32_t bufsize_t;

src/node.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,43 @@ int cmark_node_set_list_type(cmark_node *node, cmark_list_type type) {
530530
}
531531
}
532532

533+
cmark_list_marker_type cmark_node_get_list_marker(cmark_node *node) {
534+
if (cmark_node_get_list_type(node) != CMARK_BULLET_LIST) {
535+
return CMARK_NO_LIST_MARKER;
536+
}
537+
538+
switch (node->as.list.bullet_char) {
539+
case '-': return CMARK_HYPHEN_LIST_MARKER;
540+
case '+': return CMARK_PLUS_LIST_MARKER;
541+
case '*': return CMARK_ASTERISK_LIST_MARKER;
542+
default: return CMARK_NO_LIST_MARKER;
543+
}
544+
}
545+
546+
int cmark_node_set_list_marker(cmark_node *node, cmark_list_marker_type listMarker) {
547+
if (!(listMarker == CMARK_HYPHEN_LIST_MARKER || listMarker == CMARK_PLUS_LIST_MARKER || listMarker == CMARK_ASTERISK_LIST_MARKER)) {
548+
return 0;
549+
}
550+
551+
if (cmark_node_get_list_type(node) != CMARK_BULLET_LIST) {
552+
return 0;
553+
}
554+
555+
switch (listMarker) {
556+
case CMARK_HYPHEN_LIST_MARKER:
557+
node->as.list.bullet_char = '-';
558+
return 1;
559+
case CMARK_PLUS_LIST_MARKER:
560+
node->as.list.bullet_char = '+';
561+
return 1;
562+
case CMARK_ASTERISK_LIST_MARKER:
563+
node->as.list.bullet_char = '*';
564+
return 1;
565+
default:
566+
return 0;
567+
}
568+
}
569+
533570
cmark_delim_type cmark_node_get_list_delim(cmark_node *node) {
534571
if (node == NULL) {
535572
return CMARK_NO_DELIM;

0 commit comments

Comments
 (0)