summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn MacFarlane <jgm@berkeley.edu>2015-03-21 21:13:36 -0700
committerJohn MacFarlane <jgm@berkeley.edu>2015-03-21 21:30:39 -0700
commit573dd81575b821661fb4aaa6f8c68b513f889f07 (patch)
treeb638bf7d789dc19bfd101caf887faecc4c1fbd78
parentb31224fbe2072b8e2328c9607e8587e79a7f307d (diff)
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`.
-rw-r--r--man/man1/cmark.15
-rw-r--r--man/man3/cmark.34
-rw-r--r--src/cmark.h2
-rw-r--r--src/commonmark.c5
-rw-r--r--src/main.c25
5 files changed, 32 insertions, 9 deletions
diff --git a/man/man1/cmark.1 b/man/man1/cmark.1
index 3db18da..6635be5 100644
--- 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
index 9ebdaf9..5b68ecb 100644
--- 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
index 1c06125..84c6f76 100644
--- 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
index 654afc3..98fef5e 100644
--- 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
index c9b9013..380c488 100644
--- 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();