cmark

My personal build of CMark ✏️

Commit
a6a7d2f8f47df7144e636ae2875a8d2ffc0173ae
Parent
3bfbee921b34cf3ac4684344255a3d5b14171903
Author
John MacFarlane <jgm@berkeley.edu>
Date

Remove CMARK_NODE_REFERENCE_DEF from API.

Modified finalize in blocks.c to return parent of finalized block, so we can handle the case of reference definitions, when we simply remove the finalized block.

Diffstat

7 files changed, 26 insertions, 36 deletions

Status File Name N° Changes Insertions Deletions
Modified api_test/main.c 5 1 4
Modified man/man3/cmark.3 3 1 2
Modified src/blocks.c 41 23 18
Modified src/cmark.h 4 1 3
Modified src/html.c 3 0 3
Modified src/node.c 2 0 2
Modified src/print.c 4 0 4
diff --git a/api_test/main.c b/api_test/main.c
@@ -23,7 +23,6 @@ static const cmark_node_type node_types[] = {
 	CMARK_NODE_PARAGRAPH,
 	CMARK_NODE_HEADER,
 	CMARK_NODE_HRULE,
-	CMARK_NODE_REFERENCE_DEF,
 	CMARK_NODE_TEXT,
 	CMARK_NODE_SOFTBREAK,
 	CMARK_NODE_LINEBREAK,
@@ -425,8 +424,7 @@ hierarchy(test_batch_runner *runner)
 		(1 << CMARK_NODE_HTML) |
 		(1 << CMARK_NODE_PARAGRAPH) |
 		(1 << CMARK_NODE_HEADER) |
-		(1 << CMARK_NODE_HRULE) |
-		(1 << CMARK_NODE_REFERENCE_DEF);
+		(1 << CMARK_NODE_HRULE);
 	int all_inlines =
 		(1 << CMARK_NODE_TEXT) |
 		(1 << CMARK_NODE_SOFTBREAK) |
@@ -447,7 +445,6 @@ hierarchy(test_batch_runner *runner)
 	test_content(runner, CMARK_NODE_PARAGRAPH,     all_inlines);
 	test_content(runner, CMARK_NODE_HEADER,        all_inlines);
 	test_content(runner, CMARK_NODE_HRULE,         0);
-	test_content(runner, CMARK_NODE_REFERENCE_DEF, 0);
 	test_content(runner, CMARK_NODE_TEXT,	       0);
 	test_content(runner, CMARK_NODE_SOFTBREAK,     0);
 	test_content(runner, CMARK_NODE_LINEBREAK,     0);
diff --git a/man/man3/cmark.3 b/man/man3/cmark.3
@@ -43,10 +43,9 @@ typedef enum {
 	CMARK_NODE_PARAGRAPH,
 	CMARK_NODE_HEADER,
 	CMARK_NODE_HRULE,
-	CMARK_NODE_REFERENCE_DEF,
 
 	CMARK_NODE_FIRST_BLOCK = CMARK_NODE_DOCUMENT,
-	CMARK_NODE_LAST_BLOCK  = CMARK_NODE_REFERENCE_DEF,
+	CMARK_NODE_LAST_BLOCK  = CMARK_NODE_HRULE,
 
 	/* Inline */
 	CMARK_NODE_TEXT,
diff --git a/src/blocks.c b/src/blocks.c
@@ -79,7 +79,8 @@ void cmark_parser_free(cmark_parser *parser)
 	free(parser);
 }
 
-static void finalize(cmark_parser *parser, cmark_node* b, int line_number);
+static cmark_node*
+finalize(cmark_parser *parser, cmark_node* b, int line_number);
 
 // Returns true if line has only space characters, else false.
 static bool is_blank(strbuf *s, int offset)
@@ -166,8 +167,7 @@ static int break_out_of_lists(cmark_parser *parser, cmark_node ** bptr, int line
 	}
 	if (b) {
 		while (container && container != b) {
-			finalize(parser, container, line_number);
-			container = container->parent;
+			container = finalize(parser, container, line_number);
 		}
 		finalize(parser, b, line_number);
 		*bptr = b->parent;
@@ -176,15 +176,20 @@ static int break_out_of_lists(cmark_parser *parser, cmark_node ** bptr, int line
 }
 
 
-static void finalize(cmark_parser *parser, cmark_node* b, int line_number)
+static cmark_node*
+finalize(cmark_parser *parser, cmark_node* b, int line_number)
 {
 	int firstlinelen;
 	int pos;
 	cmark_node* item;
 	cmark_node* subitem;
+	cmark_node* parent;
 
+	parent = b->parent;
+
+	// don't do anything if the cmark_node is already closed
 	if (!b->open)
-		return; // don't do anything if the cmark_node is already closed
+		return parent;
 
 	b->open = false;
 	if (line_number > b->start_line) {
@@ -201,7 +206,8 @@ static void finalize(cmark_parser *parser, cmark_node* b, int line_number)
 				strbuf_drop(&b->string_content, pos);
 			}
 			if (is_blank(&b->string_content, 0)) {
-				b->type = NODE_REFERENCE_DEF;
+				// remove blank node (former reference def)
+				cmark_node_free(b);
 			}
 			break;
 
@@ -260,6 +266,7 @@ static void finalize(cmark_parser *parser, cmark_node* b, int line_number)
 		default:
 			break;
 	}
+	return parent;
 }
 
 // Add a cmark_node as child of another.  Return pointer to child.
@@ -271,8 +278,7 @@ static cmark_node* add_child(cmark_parser *parser, cmark_node* parent,
 	// if 'parent' isn't the kind of cmark_node that can accept this child,
 	// then back up til we hit a cmark_node that can.
 	while (!can_contain(parent->type, block_type)) {
-		finalize(parser, parent, start_line);
-		parent = parent->parent;
+		parent = finalize(parser, parent, start_line);
 	}
 
 	cmark_node* child = make_block(block_type, start_line, start_column);
@@ -415,8 +421,8 @@ static int lists_match(cmark_list *list_data, cmark_list *item_data)
 static cmark_node *finalize_document(cmark_parser *parser)
 {
 	while (parser->current != parser->root) {
-		finalize(parser, parser->current, parser->line_number);
-		parser->current = parser->current->parent;
+		parser->current = finalize(parser, parser->current,
+					   parser->line_number);
 	}
 
 	finalize(parser, parser->root, parser->line_number);
@@ -720,8 +726,8 @@ S_process_line(cmark_parser *parser, const unsigned char *buffer, size_t bytes)
 
 			// it's only now that we know the line is not part of a setext header:
 			container = add_child(parser, container, NODE_HRULE, parser->line_number, first_nonspace + 1);
-			finalize(parser, container, parser->line_number);
-			container = container->parent;
+			container = finalize(parser, container,
+					     parser->line_number);
 			offset = input.len - 1;
 
 		} else if ((matched = parse_list_marker(&input, first_nonspace, &data))) {
@@ -813,8 +819,7 @@ S_process_line(cmark_parser *parser, const unsigned char *buffer, size_t bytes)
 
 		// finalize any blocks that were not matched and set cur to container:
 		while (cur != last_matched_container) {
-			finalize(parser, cur, parser->line_number);
-			cur = cur->parent;
+			cur = finalize(parser, cur, parser->line_number);
 			assert(cur != NULL);
 		}
 
@@ -836,8 +841,8 @@ S_process_line(cmark_parser *parser, const unsigned char *buffer, size_t bytes)
 
 			if (matched) {
 				// if closing fence, don't add line to container; instead, close it:
-				finalize(parser, container, parser->line_number);
-				container = container->parent; // back up to parent
+				container = finalize(parser, container,
+						     parser->line_number);
 			} else {
 				add_line(container, &input, offset);
 			}
@@ -854,8 +859,8 @@ S_process_line(cmark_parser *parser, const unsigned char *buffer, size_t bytes)
 
 			chop_trailing_hashtags(&input);
 			add_line(container, &input, first_nonspace);
-			finalize(parser, container, parser->line_number);
-			container = container->parent;
+			container = finalize(parser, container,
+					     parser->line_number);
 
 		} else if (accepts_lines(container->type)) {
 
diff --git a/src/cmark.h b/src/cmark.h
@@ -47,10 +47,9 @@ typedef enum {
 	CMARK_NODE_PARAGRAPH,
 	CMARK_NODE_HEADER,
 	CMARK_NODE_HRULE,
-	CMARK_NODE_REFERENCE_DEF,
 
 	CMARK_NODE_FIRST_BLOCK = CMARK_NODE_DOCUMENT,
-	CMARK_NODE_LAST_BLOCK  = CMARK_NODE_REFERENCE_DEF,
+	CMARK_NODE_LAST_BLOCK  = CMARK_NODE_HRULE,
 
 	/* Inline */
 	CMARK_NODE_TEXT,
@@ -337,7 +336,6 @@ int cmark_walk(cmark_node *root, cmark_node_handler handler, void *state);
   #define NODE_PARAGRAPH            CMARK_NODE_PARAGRAPH
   #define NODE_HEADER		    CMARK_NODE_HEADER
   #define NODE_HRULE                CMARK_NODE_HRULE
-  #define NODE_REFERENCE_DEF        CMARK_NODE_REFERENCE_DEF
   #define NODE_TEXT                 CMARK_NODE_TEXT
   #define NODE_SOFTBREAK            CMARK_NODE_SOFTBREAK
   #define NODE_LINEBREAK            CMARK_NODE_LINEBREAK
diff --git a/src/html.c b/src/html.c
@@ -163,9 +163,6 @@ S_render_node(cmark_node *node, int entering, void *vstate)
 		strbuf_puts(html, "<hr />\n");
 		break;
 
-	case CMARK_NODE_REFERENCE_DEF:
-		break;
-
 	case CMARK_NODE_PARAGRAPH:
 		parent = cmark_node_parent(node);
 		grandparent = cmark_node_parent(parent);
diff --git a/src/node.c b/src/node.c
@@ -102,7 +102,6 @@ S_type_string(cmark_node *node)
 	case CMARK_NODE_PARAGRAPH:     return "PARAGRAPH";
 	case CMARK_NODE_HEADER:	       return "HEADER";
 	case CMARK_NODE_HRULE:         return "HRULE";
-	case CMARK_NODE_REFERENCE_DEF: return "REFERENCE_DEF";
 	case CMARK_NODE_TEXT:          return "TEXT";
 	case CMARK_NODE_SOFTBREAK:     return "SOFTBREAK";
 	case CMARK_NODE_LINEBREAK:     return "LINEBREAK";
@@ -774,7 +773,6 @@ int S_is_leaf_node(cmark_node *current_node)
 	case CMARK_NODE_HTML:
 	case CMARK_NODE_HRULE:
 	case CMARK_NODE_CODE_BLOCK:
-	case CMARK_NODE_REFERENCE_DEF:
 	case CMARK_NODE_TEXT:
 	case CMARK_NODE_SOFTBREAK:
 	case CMARK_NODE_LINEBREAK:
diff --git a/src/print.c b/src/print.c
@@ -88,10 +88,6 @@ static void render_nodes(strbuf* buffer, cmark_node* node, int indent)
 			print_str(buffer, node->string_content.ptr, -1);
 			strbuf_putc(buffer, '\n');
 			break;
-		case NODE_REFERENCE_DEF:
-			// skip
-			// strbuf_printf(buffer, "reference_def\n");
-			break;
 		case NODE_TEXT:
 			strbuf_printf(buffer, "text ");
 			print_str(buffer, node->as.literal.data, node->as.literal.len);