cmark

My personal build of CMark ✏️

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

Improved end column/end line calculations in finalize.

Diffstat

1 file changed, 18 insertions, 6 deletions

Status File Name N° Changes Insertions Deletions
Modified src/blocks.c 24 18 6
diff --git a/src/blocks.c b/src/blocks.c
@@ -193,14 +193,23 @@ finalize(cmark_parser *parser, cmark_node* b)
 		return parent;
 
 	b->open = false;
-	if (parser->line_number > b->start_line) {
-		b->end_line = parser->line_number - 1;
-	} else {
+
+	if (parser->curline->size == 0) {
+		// end of input - line number has not been incremented
+		b->end_line = parser->line_number;
+		b->end_column = parser->last_line_length;
+	} else if (b->type == NODE_DOCUMENT ||
+	    (b->type == NODE_CODE_BLOCK && b->as.code.fenced) ||
+	    (b->type == NODE_HEADER && b->as.header.setext)) {
 		b->end_line = parser->line_number;
+		b->end_column = parser->curline->size -
+		    (parser->curline->ptr[parser->curline->size - 1] == '\n' ?
+			 1 : 0);
+	} else {
+		b->end_line = parser->line_number - 1;
+		b->end_column = parser->last_line_length;
 	}
 
-	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) == '[' &&
@@ -856,7 +865,10 @@ S_process_line(cmark_parser *parser, const unsigned char *buffer, size_t bytes)
 
 		parser->current = container;
 	}
-	parser->last_line_length = parser->curline->size;
+	parser->last_line_length = parser->curline->size -
+		(parser->curline->ptr[parser->curline->size - 1] == '\n' ?
+		 1 : 0);
+;
 	cmark_strbuf_clear(parser->curline);
 
 }