cmark

My personal build of CMark ✏️

Commit
e9e4fa11de887c3e558310d3ef3bc6215510f6f4
Parent
37554bbb7901b7116e1a5006f331968444141b76
Author
John MacFarlane <jgm@berkeley.edu>
Date

Use chunk for fenced code info, instead of strbuf.

Diffstat

5 files changed, 27 insertions, 23 deletions

Status File Name N° Changes Insertions Deletions
Modified src/blocks.c 19 10 9
Modified src/html.c 14 7 7
Modified src/node.c 6 3 3
Modified src/node.h 2 1 1
Modified src/print.c 9 6 3
diff --git a/src/blocks.c b/src/blocks.c
@@ -220,16 +220,17 @@ finalize(cmark_parser *parser, cmark_node* b, int line_number)
 				// first line of contents becomes info
 				firstlinelen = strbuf_strchr(&b->string_content, '\n', 0);
 
+				strbuf tmp = GH_BUF_INIT;
 				houdini_unescape_html_f(
-						&b->as.code.info,
-						b->string_content.ptr,
-						firstlinelen
-						);
+					&tmp,
+					b->string_content.ptr,
+					firstlinelen
+					);
+				strbuf_trim(&tmp);
+				strbuf_unescape(&tmp);
+				b->as.code.info = chunk_buf_detach(&tmp);
 
 				strbuf_drop(&b->string_content, firstlinelen + 1);
-
-				strbuf_trim(&b->as.code.info);
-				strbuf_unescape(&b->as.code.info);
 			}
 			b->as.literal = chunk_buf_detach(&b->string_content);
 			break;
@@ -671,7 +672,7 @@ S_process_line(cmark_parser *parser, const unsigned char *buffer, size_t bytes)
 				container->as.code.fence_char = 0;
 				container->as.code.fence_length = 0;
 				container->as.code.fence_offset = 0;
-				strbuf_init(&container->as.code.info, 0);
+				container->as.code.info = chunk_literal("");
 			} else { // indent > 4 in lazy line
 				break;
 			}
@@ -706,7 +707,7 @@ S_process_line(cmark_parser *parser, const unsigned char *buffer, size_t bytes)
 			container->as.code.fence_char = peek_at(&input, first_nonspace);
 			container->as.code.fence_length = matched;
 			container->as.code.fence_offset = first_nonspace - offset;
-			strbuf_init(&container->as.code.info, 0);
+			container->as.code.info = chunk_literal("");
 			offset = first_nonspace + matched;
 
 		} else if ((matched = scan_html_block_tag(&input, first_nonspace))) {
diff --git a/src/html.c b/src/html.c
@@ -47,7 +47,6 @@ S_render_node(cmark_node *node, cmark_event_type ev_type, void *vstate)
 	strbuf *html = state->html;
 	char start_header[] = "<h0>";
 	char end_header[] = "</h0>";
-	strbuf *info;
 	bool tight;
 
 	bool entering = (ev_type == CMARK_EVENT_ENTER);
@@ -136,20 +135,21 @@ S_render_node(cmark_node *node, cmark_event_type ev_type, void *vstate)
 		break;
 
 	case CMARK_NODE_CODE_BLOCK:
-		info = &node->as.code.info;
 		cr(html);
 
 		if (&node->as.code.fence_length == 0
-		    || strbuf_len(info) == 0) {
+		    || node->as.code.info.len == 0) {
 			strbuf_puts(html, "<pre><code>");
 		}
 		else {
-			int first_tag = strbuf_strchr(info, ' ', 0);
-			if (first_tag < 0)
-				first_tag = strbuf_len(info);
+			int first_tag = 0;
+			while (first_tag < node->as.code.info.len &&
+			       node->as.code.info.data[first_tag] != ' ') {
+				first_tag += 1;
+			}
 
 			strbuf_puts(html, "<pre><code class=\"language-");
-			escape_html(html, info->ptr, first_tag);
+			escape_html(html, node->as.code.info.data, first_tag);
 			strbuf_puts(html, "\">");
 		}
 
diff --git a/src/node.c b/src/node.c
@@ -41,7 +41,7 @@ void S_free_nodes(cmark_node *e)
 		strbuf_free(&e->string_content);
 		switch (e->type){
 		case NODE_CODE_BLOCK:
-			strbuf_free(&e->as.code.info);
+			chunk_free(&e->as.code.info);
 			chunk_free(&e->as.literal);
 			break;
 		case NODE_TEXT:
@@ -352,7 +352,7 @@ cmark_node_get_fence_info(cmark_node *node) {
 	}
 
 	if (node->type == NODE_CODE_BLOCK) {
-		return strbuf_cstr(&node->as.code.info);
+		return chunk_to_cstr(&node->as.code.info);
 	}
 	else {
 		return NULL;
@@ -366,7 +366,7 @@ cmark_node_set_fence_info(cmark_node *node, const char *info) {
 	}
 
 	if (node->type == NODE_CODE_BLOCK) {
-		strbuf_sets(&node->as.code.info, info);
+		chunk_set_cstr(&node->as.code.info, info);
 		return 1;
 	}
 	else {
diff --git a/src/node.h b/src/node.h
@@ -26,7 +26,7 @@ typedef struct {
 	int               fence_length;
 	int               fence_offset;
 	unsigned char     fence_char;
-	strbuf            info;
+	chunk             info;
 } cmark_code;
 
 typedef struct {
diff --git a/src/print.c b/src/print.c
@@ -78,14 +78,17 @@ static void render_nodes(strbuf* buffer, cmark_node* node, int indent)
 			break;
 		case NODE_CODE_BLOCK:
 			strbuf_printf(buffer, "code_block info=");
-			print_str(buffer, node->as.code.info.ptr, -1);
+			print_str(buffer, node->as.code.info.data,
+				  node->as.code.info.len);
 			strbuf_putc(buffer, ' ');
-			print_str(buffer, node->string_content.ptr, -1);
+			print_str(buffer, node->as.literal.data,
+				  node->as.literal.len);
 			strbuf_putc(buffer, '\n');
 			break;
 		case NODE_HTML:
 			strbuf_printf(buffer, "html ");
-			print_str(buffer, node->string_content.ptr, -1);
+			print_str(buffer, node->as.literal.data,
+				  node->as.literal.len);
 			strbuf_putc(buffer, '\n');
 			break;
 		case NODE_TEXT: