cmark

My personal build of CMark ✏️

Commit
19a3a5ea4f73e8757b6d208f3be003bc2828e752
Parent
d19558961c1b579ddb2f5c62f1b6a65be3324333
Author
John MacFarlane <jgm@berkeley.edu>
Date

Added end_column to cmark_node struct.

API exports cmark_node_get_column.

XML writer indicates start and end line and column for block-level nodes.

Diffstat

6 files changed, 22 insertions, 2 deletions

Status File Name N° Changes Insertions Deletions
Modified src/blocks.c 4 4 0
Modified src/cmark.h 5 5 0
Modified src/node.c 8 8 0
Modified src/node.h 1 1 0
Modified src/parser.h 1 1 0
Modified src/xml.c 5 3 2
diff --git a/src/blocks.c b/src/blocks.c
@@ -64,6 +64,7 @@ cmark_parser *cmark_parser_new()
 	parser->current = document;
 	parser->line_number = 0;
 	parser->curline = line;
+	parser->last_line_length = 0;
 	parser->linebuf = buf;
 
 	return parser;
@@ -198,6 +199,8 @@ finalize(cmark_parser *parser, cmark_node* b)
 		b->end_line = parser->line_number;
 	}
 
+	b->end_column = parser->last_line_length - 1;  // -1 because of newline
+
 	switch (b->type) {
 		case NODE_PARAGRAPH:
 			while (cmark_strbuf_at(&b->string_content, 0) == '[' &&
@@ -853,6 +856,7 @@ S_process_line(cmark_parser *parser, const unsigned char *buffer, size_t bytes)
 
 		parser->current = container;
 	}
+	parser->last_line_length = parser->curline->size;
 	cmark_strbuf_clear(parser->curline);
 
 }
diff --git a/src/cmark.h b/src/cmark.h
@@ -306,6 +306,11 @@ cmark_node_get_start_column(cmark_node *node);
 CMARK_EXPORT int
 cmark_node_get_end_line(cmark_node *node);
 
+/** Returns the column at which 'node' ends.
+ */
+CMARK_EXPORT int
+cmark_node_get_end_column(cmark_node *node);
+
 /**
  * ## Tree Manipulation
  */
diff --git a/src/node.c b/src/node.c
@@ -475,6 +475,14 @@ cmark_node_get_end_line(cmark_node *node) {
 	return node->end_line;
 }
 
+int
+cmark_node_get_end_column(cmark_node *node) {
+	if (node == NULL) {
+		return 0;
+	}
+	return node->end_column;
+}
+
 static inline bool
 S_is_block(cmark_node *node) {
 	if (node == NULL) {
diff --git a/src/node.h b/src/node.h
@@ -52,6 +52,7 @@ struct cmark_node {
 	int start_line;
 	int start_column;
 	int end_line;
+	int end_column;
 	bool open;
 	bool last_line_blank;
 
diff --git a/src/parser.h b/src/parser.h
@@ -17,6 +17,7 @@ struct cmark_parser {
 	struct cmark_node* current;
 	int line_number;
 	cmark_strbuf *curline;
+	int last_line_length;
 	cmark_strbuf *linebuf;
 };
 
diff --git a/src/xml.c b/src/xml.c
@@ -49,10 +49,11 @@ S_render_node(cmark_node *node, cmark_event_type ev_type, void *vstate)
 				    cmark_node_get_type_string(node));
 
 		if (node->start_line != 0) {
-			cmark_strbuf_printf(xml, " sourcepos=\"%d:%d-%d\"",
+			cmark_strbuf_printf(xml, " sourcepos=\"%d:%d-%d:%d\"",
 					    node->start_line,
 					    node->start_column,
-					    node->end_line);
+					    node->end_line,
+					    node->end_column);
 		}
 
 		literal = false;