Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add exposing list markers *, -, +, or numeric to AST and API. #226

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions api_test/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ static void accessors(test_batch_runner *runner) {
cmark_node *bullet_list = cmark_node_next(heading);
INT_EQ(runner, cmark_node_get_list_type(bullet_list), CMARK_BULLET_LIST,
"get_list_type bullet");
INT_EQ(runner, cmark_node_get_list_marker(bullet_list), CMARK_ASTERISK_MARKER,
"get_list_marker asterisk");
INT_EQ(runner, cmark_node_get_list_tight(bullet_list), 1,
"get_list_tight tight");

Expand All @@ -111,6 +113,8 @@ static void accessors(test_batch_runner *runner) {
"get_list_type ordered");
INT_EQ(runner, cmark_node_get_list_delim(ordered_list), CMARK_PERIOD_DELIM,
"get_list_delim ordered");
INT_EQ(runner, cmark_node_get_list_marker(ordered_list), CMARK_NUMERIC_MARKER,
"get_list_marker numeric");
INT_EQ(runner, cmark_node_get_list_start(ordered_list), 2, "get_list_start");
INT_EQ(runner, cmark_node_get_list_tight(ordered_list), 0,
"get_list_tight loose");
Expand Down Expand Up @@ -144,10 +148,14 @@ static void accessors(test_batch_runner *runner) {

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

OK(runner, cmark_node_set_list_marker(bullet_list, CMARK_HYPHEN_MARKER),
"set_list_marker hyphen");
OK(runner, cmark_node_set_list_type(bullet_list, CMARK_ORDERED_LIST),
"set_list_type ordered");
OK(runner, cmark_node_set_list_delim(bullet_list, CMARK_PAREN_DELIM),
"set_list_delim paren");
OK(runner, cmark_node_set_list_marker(bullet_list, CMARK_NUMERIC_MARKER),
"set_list_marker numeric");
OK(runner, cmark_node_set_list_start(bullet_list, 3), "set_list_start");
OK(runner, cmark_node_set_list_tight(bullet_list, 0), "set_list_tight loose");

Expand Down
8 changes: 8 additions & 0 deletions src/blocks.c
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,13 @@ static bufsize_t parse_list_marker(cmark_mem *mem, cmark_chunk *input,
data->marker_offset = 0; // will be adjusted later
data->list_type = CMARK_BULLET_LIST;
data->bullet_char = c;
if (c == '*') {
data->marker = CMARK_ASTERISK_MARKER;
} else if (c == '-') {
data->marker = CMARK_HYPHEN_MARKER;
} else if (c == '+') {
data->marker = CMARK_PLUS_MARKER;
}
data->start = 0;
data->delimiter = CMARK_NO_DELIM;
data->tight = false;
Expand Down Expand Up @@ -454,6 +461,7 @@ static bufsize_t parse_list_marker(cmark_mem *mem, cmark_chunk *input,
data->marker_offset = 0; // will be adjusted later
data->list_type = CMARK_ORDERED_LIST;
data->bullet_char = 0;
data->marker = CMARK_NUMERIC_MARKER;
data->start = start;
data->delimiter = (c == '.' ? CMARK_PERIOD_DELIM : CMARK_PAREN_DELIM);
data->tight = false;
Expand Down
24 changes: 24 additions & 0 deletions src/cmark.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ typedef enum {
CMARK_PAREN_DELIM
} cmark_delim_type;

typedef enum {
CMARK_NO_MARKER,
CMARK_NUMERIC_MARKER,
CMARK_ASTERISK_MARKER,
CMARK_HYPHEN_MARKER,
CMARK_PLUS_MARKER
} cmark_marker_type;

typedef struct cmark_node cmark_node;
typedef struct cmark_parser cmark_parser;
typedef struct cmark_iter cmark_iter;
Expand Down Expand Up @@ -308,6 +316,16 @@ CMARK_EXPORT cmark_delim_type cmark_node_get_list_delim(cmark_node *node);
CMARK_EXPORT int cmark_node_set_list_delim(cmark_node *node,
cmark_delim_type delim);

/** Returns the marker type of 'node' or CMARK_NO_MARKER if 'node' is not a
* list.
*/
CMARK_EXPORT cmark_marker_type cmark_node_get_list_marker(cmark_node *node);

/** Sets the marker type of 'node', returning 1 on success and 0 on error.
*/
CMARK_EXPORT int cmark_node_set_list_marker(cmark_node *node,
cmark_marker_type marker);

/** Returns starting number of 'node', if it is an ordered list, otherwise 0.
*/
CMARK_EXPORT int cmark_node_get_list_start(cmark_node *node);
Expand Down Expand Up @@ -633,8 +651,14 @@ const char *cmark_version_string(void);
#define NODE_IMAGE CMARK_NODE_IMAGE
#define BULLET_LIST CMARK_BULLET_LIST
#define ORDERED_LIST CMARK_ORDERED_LIST
#define NO_DELIM CMARK_NO_DELIM
#define PERIOD_DELIM CMARK_PERIOD_DELIM
#define PAREN_DELIM CMARK_PAREN_DELIM
#define NO_MARKER CMARK_PERIOD_DELIM
#define NUMERIC_MARKER CMARK_NUMERIC_MARKER
#define ASTERISK_MARKER CMARK_ASTERISK_MARKER
#define HYPHEN_MARKER CMARK_HYPHEN_MARKER
#define PLUS_MARKER CMARK_PLUS_MARKER
#endif

#ifdef __cplusplus
Expand Down
17 changes: 10 additions & 7 deletions src/commonmark.c
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,14 @@ static int S_render_node(cmark_renderer *renderer, cmark_node *node,
case CMARK_NODE_ITEM:
if (cmark_node_get_list_type(node->parent) == CMARK_BULLET_LIST) {
marker_width = 4;
if (cmark_node_get_list_marker(node->parent) == CMARK_ASTERISK_MARKER) {
snprintf(listmarker, LISTMARKER_SIZE, " %s ", "*");
} else if (cmark_node_get_list_marker(node->parent) ==
CMARK_PLUS_MARKER) {
snprintf(listmarker, LISTMARKER_SIZE, " %s ", "+");
} else {
snprintf(listmarker, LISTMARKER_SIZE, " %s ", "-");
}
} else {
list_number = cmark_node_get_list_start(node->parent);
list_delim = cmark_node_get_list_delim(node->parent);
Expand All @@ -237,13 +245,8 @@ static int S_render_node(cmark_renderer *renderer, cmark_node *node,
marker_width = strlen(listmarker);
}
if (entering) {
if (cmark_node_get_list_type(node->parent) == CMARK_BULLET_LIST) {
LIT(" - ");
renderer->begin_content = true;
} else {
LIT(listmarker);
renderer->begin_content = true;
}
LIT(listmarker);
renderer->begin_content = true;
for (i = marker_width; i--;) {
cmark_strbuf_putc(renderer->prefix, ' ');
}
Expand Down
46 changes: 46 additions & 0 deletions src/node.c
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,52 @@ int cmark_node_set_list_delim(cmark_node *node, cmark_delim_type delim) {
}
}

cmark_marker_type cmark_node_get_list_marker(cmark_node *node) {
if (node == NULL) {
return CMARK_NO_MARKER;
}

if (node->type == CMARK_NODE_LIST) {
return node->as.list.marker;
} else {
return CMARK_NO_MARKER;
}
}

int cmark_node_set_list_marker(cmark_node *node, cmark_marker_type marker) {
if (node == NULL) {
return 0;
}

if (node->type != CMARK_NODE_LIST) {
return 0;
}

if (cmark_node_get_list_type(node) == CMARK_ORDERED_LIST &&
marker == CMARK_NUMERIC_MARKER) {
node->as.list.bullet_char = 0;
node->as.list.marker = CMARK_NUMERIC_MARKER;
return 1;
} else if (cmark_node_get_list_type(node) == CMARK_BULLET_LIST &&
marker == CMARK_ASTERISK_MARKER) {
node->as.list.bullet_char = '*';
node->as.list.marker = marker;
return 1;
} else if (cmark_node_get_list_type(node) == CMARK_BULLET_LIST &&
marker == CMARK_HYPHEN_MARKER) {
node->as.list.bullet_char = '-';
node->as.list.marker = marker;
return 1;
} else if (cmark_node_get_list_type(node) == CMARK_BULLET_LIST &&
marker == CMARK_PLUS_MARKER) {
node->as.list.bullet_char = '+';
node->as.list.marker = marker;
return 1;
} else {
return 0;
}
}

int cmark_node_get_list_start(cmark_node *node) {
if (node == NULL) {
return 0;
Expand Down
1 change: 1 addition & 0 deletions src/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ typedef struct {
cmark_delim_type delimiter;
unsigned char bullet_char;
bool tight;
cmark_marker_type marker;
} cmark_list;

typedef struct {
Expand Down
13 changes: 13 additions & 0 deletions src/xml.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ static int S_render_node(cmark_node *node, cmark_event_type ev_type,
cmark_strbuf *xml = state->xml;
bool literal = false;
cmark_delim_type delim;
cmark_marker_type marker;
bool entering = (ev_type == CMARK_EVENT_ENTER);
char buffer[BUFFER_SIZE];

Expand Down Expand Up @@ -79,9 +80,21 @@ static int S_render_node(cmark_node *node, cmark_event_type ev_type,
} else if (delim == CMARK_PERIOD_DELIM) {
cmark_strbuf_puts(xml, " delim=\"period\"");
}
marker = cmark_node_get_list_marker(node);
if (marker == CMARK_NUMERIC_MARKER) {
cmark_strbuf_puts(xml, " marker=\"numeric\"");
}
break;
case CMARK_BULLET_LIST:
cmark_strbuf_puts(xml, " type=\"bullet\"");
marker = cmark_node_get_list_marker(node);
if (marker == CMARK_ASTERISK_MARKER) {
cmark_strbuf_puts(xml, " marker=\"asterisk\"");
} else if (marker == CMARK_HYPHEN_MARKER) {
cmark_strbuf_puts(xml, " marker=\"hyphen\"");
} else if (marker == CMARK_PLUS_MARKER) {
cmark_strbuf_puts(xml, " marker=\"plus\"");
}
break;
default:
break;
Expand Down
6 changes: 5 additions & 1 deletion wrappers/wrapper.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
(_enum '(no_list bullet_list ordered_list)))
(define _cmark_delim_type
(_enum '(no_delim period_delim paren_delim)))
(define _cmark_marker_type
(_enum '(no_marker numeric_marker asterisk_marker hyphen_marker plus_marker)))
(define _cmark_opts
(_bitmask '(sourcepos = 1 hardbreaks = 2 normalize = 4 smart = 8)))

Expand Down Expand Up @@ -62,6 +64,8 @@
(defcmark cmark_node_set_list_type (_fun _node _cmark_list_type -> _bool))
(defcmark cmark_node_get_list_delim (_fun _node -> _cmark_delim_type))
(defcmark cmark_node_set_list_delim (_fun _node _cmark_delim_type -> _bool))
(defcmark cmark_node_get_list_marker (_fun _node -> _cmark_marker_type))
(defcmark cmark_node_set_list_marker (_fun _node _cmark_marker_type -> _bool))
(defcmark cmark_node_get_list_start (_fun _node -> _int))
(defcmark cmark_node_set_list_start (_fun _node _int -> _bool))
(defcmark cmark_node_get_list_tight (_fun _node -> _bool))
Expand Down Expand Up @@ -110,7 +114,7 @@
(define-getters+setters getters+setters
[header header_level] [code-block fence_info]
[link url title] [image url title]
[list list_type list_delim list_start list_tight])
[list list_type list_delim list_marker list_start list_tight])

(provide cmark->sexpr)
(define (cmark->sexpr node)
Expand Down