summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJohn MacFarlane <jgm@berkeley.edu>2016-04-09 11:44:58 -0700
committerJohn MacFarlane <jgm@berkeley.edu>2016-04-09 12:15:36 -0700
commit9c3a7023014f961197bc27ff8360ce9e1b1d6c29 (patch)
tree12f7238407fb475e026eca84cc01630a3a154a8f /src
parent0b6805c0c544cfd8973f457142434127cac4b3a5 (diff)
Fixed a number of issues relating to line wrapping.
- Extend CMARK_OPT_NOBREAKS to all renderers and add `--nobreaks`. - Do not autowrap, regardless of width parameter, if CMARK_OPT_NOBREAKS is set. - Fixed CMARK_OPT_HARDBREAKS for LaTeX and man renderers. - Ensure that no auto-wrapping occurs if CMARK_OPT_NOBREAKS is enabled, or if output is CommonMark and CMARK_OPT_HARDBREAKS is enabled. - Updated man pages.
Diffstat (limited to 'src')
-rw-r--r--src/cmark.h2
-rw-r--r--src/commonmark.c16
-rw-r--r--src/latex.c10
-rw-r--r--src/main.c3
-rw-r--r--src/man.c14
5 files changed, 31 insertions, 14 deletions
diff --git a/src/cmark.h b/src/cmark.h
index c98e18e..40aa465 100644
--- a/src/cmark.h
+++ b/src/cmark.h
@@ -519,7 +519,7 @@ char *cmark_render_latex(cmark_node *root, int options, int width);
*/
#define CMARK_OPT_SAFE (1 << 3)
-/** Render `softbreak` elements as spaces (HTML only).
+/** Render `softbreak` elements as spaces.
*/
#define CMARK_OPT_NOBREAKS (1 << 4)
diff --git a/src/commonmark.c b/src/commonmark.c
index f5a352d..ed6eff3 100644
--- a/src/commonmark.c
+++ b/src/commonmark.c
@@ -172,6 +172,8 @@ static int S_render_node(cmark_renderer *renderer, cmark_node *node,
char listmarker[LISTMARKER_SIZE];
char *emph_delim;
bufsize_t marker_width;
+ bool allow_wrap = renderer->width > 0 && !(CMARK_OPT_NOBREAKS & options) &&
+ !(CMARK_OPT_HARDBREAKS & options);
// Don't adjust tight list status til we've started the list.
// Otherwise we loose the blank line between a paragraph and
@@ -327,7 +329,7 @@ static int S_render_node(cmark_renderer *renderer, cmark_node *node,
break;
case CMARK_NODE_TEXT:
- OUT(cmark_node_get_literal(node), true, NORMAL);
+ OUT(cmark_node_get_literal(node), allow_wrap, NORMAL);
break;
case CMARK_NODE_LINEBREAK:
@@ -338,10 +340,14 @@ static int S_render_node(cmark_renderer *renderer, cmark_node *node,
break;
case CMARK_NODE_SOFTBREAK:
- if (renderer->width == 0 && !(CMARK_OPT_HARDBREAKS & options)) {
+ if (CMARK_OPT_HARDBREAKS & options) {
+ LIT(" ");
+ CR();
+ } else if (renderer->width == 0 && !(CMARK_OPT_HARDBREAKS & options) &&
+ !(CMARK_OPT_NOBREAKS & options)) {
CR();
} else {
- OUT(" ", true, LITERAL);
+ OUT(" ", allow_wrap, LITERAL);
}
break;
@@ -355,7 +361,7 @@ static int S_render_node(cmark_renderer *renderer, cmark_node *node,
if (code_len == 0 || code[0] == '`') {
LIT(" ");
}
- OUT(cmark_node_get_literal(node), true, LITERAL);
+ OUT(cmark_node_get_literal(node), allow_wrap, LITERAL);
if (code_len == 0 || code[code_len - 1] == '`') {
LIT(" ");
}
@@ -435,7 +441,7 @@ static int S_render_node(cmark_renderer *renderer, cmark_node *node,
OUT(cmark_node_get_url(node), false, URL);
title = cmark_node_get_title(node);
if (safe_strlen(title) > 0) {
- OUT(" \"", true, LITERAL);
+ OUT(" \"", allow_wrap, LITERAL);
OUT(title, false, TITLE);
LIT("\"");
}
diff --git a/src/latex.c b/src/latex.c
index cd2f6f3..904a1b9 100644
--- a/src/latex.c
+++ b/src/latex.c
@@ -223,6 +223,7 @@ static int S_render_node(cmark_renderer *renderer, cmark_node *node,
cmark_list_type list_type;
const char *roman_numerals[] = {"", "i", "ii", "iii", "iv", "v",
"vi", "vii", "viii", "ix", "x"};
+ bool allow_wrap = renderer->width > 0 && !(CMARK_OPT_NOBREAKS & options);
// avoid warning about unused parameter:
(void)(options);
@@ -334,7 +335,7 @@ static int S_render_node(cmark_renderer *renderer, cmark_node *node,
break;
case CMARK_NODE_TEXT:
- OUT(cmark_node_get_literal(node), true, NORMAL);
+ OUT(cmark_node_get_literal(node), allow_wrap, NORMAL);
break;
case CMARK_NODE_LINEBREAK:
@@ -343,10 +344,13 @@ static int S_render_node(cmark_renderer *renderer, cmark_node *node,
break;
case CMARK_NODE_SOFTBREAK:
- if (renderer->width == 0) {
+ if (options & CMARK_OPT_HARDBREAKS) {
+ LIT("\\\\");
+ CR();
+ } else if (renderer->width == 0 && !(CMARK_OPT_NOBREAKS & options)) {
CR();
} else {
- OUT(" ", true, NORMAL);
+ OUT(" ", allow_wrap, NORMAL);
}
break;
diff --git a/src/main.c b/src/main.c
index fa18e03..5716e18 100644
--- a/src/main.c
+++ b/src/main.c
@@ -27,6 +27,7 @@ void print_usage() {
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(" --nobreaks Render soft line breaks as spaces\n");
printf(" --safe Suppress raw HTML and dangerous URLs\n");
printf(" --smart Use smart punctuation\n");
printf(" --normalize Consolidate adjacent text nodes\n");
@@ -90,6 +91,8 @@ int main(int argc, char *argv[]) {
options |= CMARK_OPT_SOURCEPOS;
} else if (strcmp(argv[i], "--hardbreaks") == 0) {
options |= CMARK_OPT_HARDBREAKS;
+ } else if (strcmp(argv[i], "--nobreaks") == 0) {
+ options |= CMARK_OPT_NOBREAKS;
} else if (strcmp(argv[i], "--smart") == 0) {
options |= CMARK_OPT_SMART;
} else if (strcmp(argv[i], "--safe") == 0) {
diff --git a/src/man.c b/src/man.c
index 3d8bc20..1c76f68 100644
--- a/src/man.c
+++ b/src/man.c
@@ -75,6 +75,7 @@ static int S_render_node(cmark_renderer *renderer, cmark_node *node,
cmark_node *tmp;
int list_number;
bool entering = (ev_type == CMARK_EVENT_ENTER);
+ bool allow_wrap = renderer->width > 0 && !(CMARK_OPT_NOBREAKS & options);
// avoid unused parameter error:
(void)(options);
@@ -173,7 +174,7 @@ static int S_render_node(cmark_renderer *renderer, cmark_node *node,
break;
case CMARK_NODE_TEXT:
- OUT(cmark_node_get_literal(node), true, NORMAL);
+ OUT(cmark_node_get_literal(node), allow_wrap, NORMAL);
break;
case CMARK_NODE_LINEBREAK:
@@ -182,16 +183,19 @@ static int S_render_node(cmark_renderer *renderer, cmark_node *node,
break;
case CMARK_NODE_SOFTBREAK:
- if (renderer->width == 0) {
+ if (options & CMARK_OPT_HARDBREAKS) {
+ LIT(".PD 0\n.P\n.PD");
+ CR();
+ } else if (renderer->width == 0 && !(CMARK_OPT_NOBREAKS & options)) {
CR();
} else {
- OUT(" ", true, LITERAL);
+ OUT(" ", allow_wrap, LITERAL);
}
break;
case CMARK_NODE_CODE:
LIT("\\f[C]");
- OUT(cmark_node_get_literal(node), true, NORMAL);
+ OUT(cmark_node_get_literal(node), allow_wrap, NORMAL);
LIT("\\f[]");
break;
@@ -222,7 +226,7 @@ static int S_render_node(cmark_renderer *renderer, cmark_node *node,
case CMARK_NODE_LINK:
if (!entering) {
LIT(" (");
- OUT(cmark_node_get_url(node), true, URL);
+ OUT(cmark_node_get_url(node), allow_wrap, URL);
LIT(")");
}
break;