cmark

My personal build of CMark ✏️

Commit
573dd81575b821661fb4aaa6f8c68b513f889f07
Parent
b31224fbe2072b8e2328c9607e8587e79a7f307d
Author
John MacFarlane <jgm@berkeley.edu>
Date

CommonMark renderer: Added 'width' parameter.

This controls column width for hard wrapping. By default it is 0, which means that no wrapping will be done.

Added a width parameter in `cmark_render_commonmark`.

Diffstat

5 files changed, 32 insertions, 9 deletions

Status File Name N° Changes Insertions Deletions
Modified man/man1/cmark.1 5 5 0
Modified man/man3/cmark.3 4 2 2
Modified src/cmark.h 2 1 1
Modified src/commonmark.c 5 2 3
Modified src/main.c 25 22 3
diff --git a/man/man1/cmark.1 b/man/man1/cmark.1
@@ -24,6 +24,11 @@ concatenated before parsing.
 Specify output format (\f[C]html\f[], \f[C]man\f[], \f[C]xml\f[],
 \f[C]commonmark\f[]).
 .TP 12n
+\-\-width \f[I]WIDTH\f[]
+Specify a column width to which to wrap the output. For no wrapping, use
+the value 0 (the default).  This option currently only affects the
+commonmark renderer.
+.TP 12n
 \-\-sourcepos
 Include source position attribute.
 .TP 12n
diff --git a/man/man3/cmark.3 b/man/man3/cmark.3
@@ -1,4 +1,4 @@
-.TH cmark 3 "March 18, 2015" "LOCAL" "Library Functions Manual"
+.TH cmark 3 "March 21, 2015" "LOCAL" "Library Functions Manual"
 .SH
 NAME
 .PP
@@ -474,7 +474,7 @@ to add an appropriate header and footer.
 Render a \f[I]node\f[] tree as a groff man page, without the header.
 
 .PP
-\fIchar *\f[] \fBcmark_render_commonmark\f[](\fIcmark_node *root\f[], \fIint options\f[])
+\fIchar *\f[] \fBcmark_render_commonmark\f[](\fIcmark_node *root\f[], \fIint options\f[], \fIint width\f[])
 
 .PP
 Render a \f[I]node\f[] tree as a commonmark document.
diff --git a/src/cmark.h b/src/cmark.h
@@ -484,7 +484,7 @@ char *cmark_render_man(cmark_node *root, int options);
 /** Render a 'node' tree as a commonmark document.
  */
 CMARK_EXPORT
-char *cmark_render_commonmark(cmark_node *root, int options);
+char *cmark_render_commonmark(cmark_node *root, int options, int width);
 
 /** Default writer options.
  */
diff --git a/src/commonmark.c b/src/commonmark.c
@@ -381,14 +381,13 @@ S_render_node(cmark_node *node, cmark_event_type ev_type,
 	return 1;
 }
 
-// TODO parameter for wrap width or 0 for no wrap
-char *cmark_render_commonmark(cmark_node *root, int options)
+char *cmark_render_commonmark(cmark_node *root, int options, int width)
 {
 	char *result;
 	cmark_strbuf commonmark = GH_BUF_INIT;
 	cmark_strbuf prefix = GH_BUF_INIT;
 	struct render_state state =
-		{ &commonmark, &prefix, 0, 65, 0, 0, true, false };
+		{ &commonmark, &prefix, 0, width, 0, 0, true, false };
 	cmark_node *cur;
 	cmark_event_type ev_type;
 	cmark_iter *iter = cmark_iter_new(root);
diff --git a/src/main.c b/src/main.c
@@ -25,6 +25,7 @@ void print_usage()
 	printf("Usage:   cmark [FILE*]\n");
 	printf("Options:\n");
 	printf("  --to, -t FORMAT  Specify output format (html, xml, man, commonmark)\n");
+	printf("  --width WIDTH    Specify wrap width (default 0 = nowrap)\n");
 	printf("  --sourcepos      Include source position attribute\n");
 	printf("  --hardbreaks     Treat newlines as hard line breaks\n");
 	printf("  --smart          Use smart punctuation\n");
@@ -34,9 +35,10 @@ void print_usage()
 }
 
 static void print_document(cmark_node *document, writer_format writer,
-                           int options)
+                           int options, int width)
 {
 	char *result;
+
 	switch (writer) {
 	case FORMAT_HTML:
 		result = cmark_render_html(document, options);
@@ -48,7 +50,7 @@ static void print_document(cmark_node *document, writer_format writer,
 		result = cmark_render_man(document, options);
 		break;
 	case FORMAT_COMMONMARK:
-		result = cmark_render_commonmark(document, options);
+		result = cmark_render_commonmark(document, options, width);
 		break;
 	default:
 		fprintf(stderr, "Unknown format %d\n", writer);
@@ -66,6 +68,8 @@ int main(int argc, char *argv[])
 	cmark_parser *parser;
 	size_t bytes;
 	cmark_node *document;
+	int width;
+	char *unparsed;
 	writer_format writer = FORMAT_HTML;
 	int options = CMARK_OPT_DEFAULT;
 
@@ -92,6 +96,21 @@ int main(int argc, char *argv[])
 		           (strcmp(argv[i], "-h") == 0)) {
 			print_usage();
 			exit(0);
+		} else if (strcmp(argv[i], "--width") == 0) {
+			i += 1;
+			if (i < argc) {
+				width = (int)strtol(argv[i], &unparsed, 10);
+				if (unparsed && strlen(unparsed) > 0) {
+					fprintf(stderr,
+					"failed parsing width '%s' at '%s'\n",
+						argv[i], unparsed);
+					exit(1);
+				}
+			} else {
+				fprintf(stderr,
+					"--width requires an argument\n");
+				exit(1);
+			}
 		} else if ((strcmp(argv[i], "-t") == 0) ||
 		           (strcmp(argv[i], "--to") == 0)) {
 			i += 1;
@@ -159,7 +178,7 @@ int main(int argc, char *argv[])
 	cmark_parser_free(parser);
 
 	start_timer();
-	print_document(document, writer, options);
+	print_document(document, writer, options, width);
 	end_timer("print_document");
 
 	start_timer();