From 573dd81575b821661fb4aaa6f8c68b513f889f07 Mon Sep 17 00:00:00 2001 From: John MacFarlane Date: Sat, 21 Mar 2015 21:13:36 -0700 Subject: 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`. --- src/cmark.h | 2 +- src/commonmark.c | 5 ++--- src/main.c | 25 ++++++++++++++++++++++--- 3 files changed, 25 insertions(+), 7 deletions(-) (limited to 'src') 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(); -- cgit v1.2.3