cmark

My personal build of CMark ✏️

Commit
9219938929274df6398dfe6b9356fd8bbb3d565b
Parent
ff2c9dc143b730a0fa5cfeddec0c355edba72e51
Author
John MacFarlane <jgm@berkeley.edu>
Date

Removed options field from renderer struct.

Added options argument to render_node function, and rearrange argument order.

Diffstat

4 files changed, 27 insertions, 20 deletions

Status File Name N° Changes Insertions Deletions
Modified src/commonmark.c 15 11 4
Modified src/latex.c 9 7 2
Modified src/render.c 16 6 10
Modified src/render.h 7 3 4
diff --git a/src/commonmark.c b/src/commonmark.c
@@ -163,8 +163,10 @@ get_containing_block(cmark_node *node)
 }
 
 static int
-S_render_node(cmark_node *node, cmark_event_type ev_type,
-              cmark_renderer *renderer)
+S_render_node(cmark_renderer *renderer,
+	      cmark_node *node,
+	      cmark_event_type ev_type,
+	      int options)
 {
 	cmark_node *tmp;
 	int list_number;
@@ -337,14 +339,14 @@ S_render_node(cmark_node *node, cmark_event_type ev_type,
 		break;
 
 	case CMARK_NODE_LINEBREAK:
-		if (!(CMARK_OPT_HARDBREAKS & renderer->options)) {
+		if (!(CMARK_OPT_HARDBREAKS & options)) {
 			LIT("\\");
 		}
 		CR();
 		break;
 
 	case CMARK_NODE_SOFTBREAK:
-		if (renderer->width == 0) {
+		if (CMARK_OPT_HARDBREAKS & options) {
 			CR();
 		} else {
 			OUT(" ", true, LITERAL);
@@ -455,5 +457,10 @@ S_render_node(cmark_node *node, cmark_event_type ev_type,
 
 char *cmark_render_commonmark(cmark_node *root, int options, int width)
 {
+	if (options & CMARK_OPT_HARDBREAKS) {
+		// disable breaking on width, since it has
+		// a different meaning with OPT_HARDBREAKS
+		width = 0;
+	}
 	return cmark_render(root, options, width, outc, S_render_node);
 }
diff --git a/src/latex.c b/src/latex.c
@@ -258,8 +258,10 @@ S_get_enumlevel(cmark_node *node)
 }
 
 static int
-S_render_node(cmark_node *node, cmark_event_type ev_type,
-              cmark_renderer *renderer)
+S_render_node(cmark_renderer *renderer,
+	      cmark_node *node,
+	      cmark_event_type ev_type,
+	      int options)
 {
 	int list_number;
 	char list_number_string[20];
@@ -269,6 +271,9 @@ S_render_node(cmark_node *node, cmark_event_type ev_type,
 	                                 "vi", "vii", "viii", "ix", "x"
 	                               };
 
+	// avoid warning about unused parameter:
+	(void)(options);
+
 	switch (node->type) {
 	case CMARK_NODE_DOCUMENT:
 		break;
diff --git a/src/render.c b/src/render.c
@@ -124,9 +124,10 @@ cmark_render(cmark_node *root,
 			  cmark_escaping,
 			  int32_t,
 			  unsigned char),
-	     int (*render_node)(cmark_node *node,
-				 cmark_event_type ev_type,
-				 cmark_renderer *renderer))
+	     int (*render_node)(cmark_renderer *renderer,
+				cmark_node *node,
+				cmark_event_type ev_type,
+				int options))
 {
 	cmark_strbuf pref = GH_BUF_INIT;
 	cmark_strbuf buf = GH_BUF_INIT;
@@ -135,18 +136,13 @@ cmark_render(cmark_node *root,
 	char *result;
 	cmark_iter *iter = cmark_iter_new(root);
 
-
-	if (CMARK_OPT_HARDBREAKS & options) {
-		width = 0;
-	}
-
-	cmark_renderer renderer = { options, &buf, &pref, 0, width,
+	cmark_renderer renderer = { &buf, &pref, 0, width,
 				    0, 0, true, false, false,
 	                            outc, S_cr, S_blankline, S_out };
 
 	while ((ev_type = cmark_iter_next(iter)) != CMARK_EVENT_DONE) {
 		cur = cmark_iter_get_node(iter);
-		if (!render_node(cur, ev_type, &renderer)) {
+		if (!render_node(&renderer, cur, ev_type, options)) {
 			// a false value causes us to skip processing
 			// the node's contents.  this is used for
 			// autolinks.
diff --git a/src/render.h b/src/render.h
@@ -17,7 +17,6 @@ typedef enum  {
 } cmark_escaping;
 
 struct cmark_renderer {
-	int options;
 	cmark_strbuf* buffer;
 	cmark_strbuf* prefix;
 	int column;
@@ -49,10 +48,10 @@ cmark_render(cmark_node *root,
 			  cmark_escaping,
 			  int32_t,
 			  unsigned char),
-	     int (*render_node)(cmark_node *node,
+	     int (*render_node)(cmark_renderer *renderer,
+				cmark_node *node,
 				cmark_event_type ev_type,
-				cmark_renderer *renderer));
-
+				int options));
 
 #ifdef __cplusplus
 }