From 7f298175918c16b3bc467b47bde976def0650d68 Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Sun, 27 Jan 2019 11:08:03 -0500 Subject: [PATCH] Use a more robust approach for in-word-underscores --- include/util.h | 1 - src/main.c | 16 +++++++++------- src/util.c | 22 ++++++++++------------ test/inline-formatting | 16 ++++++++++++++++ diff --git a/include/util.h b/include/util.h index d9a1a5cdde6af9b1af04b38f4c10297e08318ac2..fe50986736a4108788fa34ceee6fb21698bb2292 100644 --- a/include/util.h +++ b/include/util.h @@ -12,7 +12,6 @@ uint32_t queue[32]; uint32_t flags; const char *str; int fmt_line, fmt_col; - uint32_t last[2]; }; enum formatting { diff --git a/src/main.c b/src/main.c index d240ea47d6cf921e7b08fead8a7ff487b8501207..44e1bb43f5f819f1debc56600f8be2af8a635e98 100644 --- a/src/main.c +++ b/src/main.c @@ -163,11 +163,6 @@ parser_fatal(p, error); } fprintf(p->output, "\\fR"); } else { - if (fmt == FORMAT_UNDERLINE && !isspace(p->last[0])) { - // Ignore underscores in the middle of words - utf8_fputch(p->output, '_'); - return; - } fprintf(p->output, "\\f%c", formats[fmt]); p->fmt_line = p->line; p->fmt_col = p->col; @@ -199,7 +194,7 @@ fprintf(p->output, "\n.br\n"); } static void parse_text(struct parser *p) { - uint32_t ch; + uint32_t ch, last = ' '; int i = 0; while ((ch = parser_getch(p)) != UTF8_INVALID) { switch (ch) { @@ -217,7 +212,13 @@ case '*': parse_format(p, FORMAT_BOLD); break; case '_': - parse_format(p, FORMAT_UNDERLINE); + if ((p->flags & FORMAT_UNDERLINE)) { + parse_format(p, FORMAT_UNDERLINE); + } else if (!p->flags && isspace(last)) { + parse_format(p, FORMAT_UNDERLINE); + } else { + utf8_fputch(p->output, ch); + } break; case '+': parse_linebreak(p); @@ -233,6 +234,7 @@ break; } /* fallthrough */ default: + last = ch; utf8_fputch(p->output, ch); break; } diff --git a/src/util.c b/src/util.c index 834a9cec1f8d64d53829bc76d93410069790976e..ec55abbe07ca9c08cc0ce7c80034e04729bca31e 100644 --- a/src/util.c +++ b/src/util.c @@ -14,26 +14,24 @@ exit(1); } uint32_t parser_getch(struct parser *parser) { - uint32_t ch = 0; if (parser->qhead) { - ch = parser->queue[--parser->qhead]; - } else if (parser->str) { + return parser->queue[--parser->qhead]; + } + if (parser->str) { uint32_t ch = utf8_decode(&parser->str); if (!ch || ch == UTF8_INVALID) { parser->str = NULL; return UTF8_INVALID; } + return ch; + } + uint32_t ch = utf8_fgetch(parser->input); + if (ch == '\n') { + parser->col = 0; + ++parser->line; } else { - ch = utf8_fgetch(parser->input); - if (ch == '\n') { - parser->col = 0; - ++parser->line; - } else { - ++parser->col; - } + ++parser->col; } - parser->last[0] = parser->last[1]; - parser->last[1] = ch; return ch; } diff --git a/test/inline-formatting b/test/inline-formatting index f595d918112cc25c3819c70a3eddf61084ac1f00..7f61ba6a94d77049baa372bbd7991f80bf84b09c 100755 --- a/test/inline-formatting +++ b/test/inline-formatting @@ -9,6 +9,22 @@ _hello *world*_ EOF end 1 +begin "Ignores underscores in words" +scdoc </dev/null +test(8) + +hello_world +EOF +end 0 + +begin "Allows underscores in bolded words" +scdoc </dev/null +test(8) + +*hello_world* +EOF +end 0 + begin "Emits bold text" scdoc </dev/null test(8) -- 2.48.1