Skip to content

Commit

Permalink
Make --template escaperoff() escape initial delim.
Browse files Browse the repository at this point in the history
Make escaperoff() escape its roff delimiters whether they appear
after a newline OR at the start of the string.  This is more in line
with how the transform would be used in practice.
  • Loading branch information
kristapsdz committed Dec 25, 2024
1 parent e577fe5 commit c8cdc23
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 6 deletions.
2 changes: 2 additions & 0 deletions man/lowdown-diff.1
Original file line number Diff line number Diff line change
Expand Up @@ -959,6 +959,8 @@ body content.
Escape list items for roff
.Pq Fl t Ns Ar ms , Fl t Ns Ar man ,
either for multiple lines or compressed to a single line.
.Cm escaperoff
also escapes initial roff delimiters and those after newlines.
.It Cm join
Join a list into a singleton list using two spaces as a join delimiter.
.It Cm lowercase
Expand Down
2 changes: 2 additions & 0 deletions man/lowdown.1
Original file line number Diff line number Diff line change
Expand Up @@ -952,6 +952,8 @@ body content.
Escape list items for roff
.Pq Fl t Ns Ar ms , Fl t Ns Ar man ,
either for multiple lines or compressed to a single line.
.Cm escaperoff
also escapes initial roff delimiters and those after newlines.
.It Cm join
Join a list into a singleton list using two spaces as a join delimiter.
.It Cm lowercase
Expand Down
14 changes: 9 additions & 5 deletions nroff_escape.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@
/*
* Escape unsafe text into roff output such that no roff features are
* invoked by the text (macros, escapes, etc.).
* If "oneline" is non-zero, newlines are replaced with spaces.
* If "oneline" is >0, newlines are replaced with spaces; if =0,
* newlines are retained and roff delimiters after the newline are
* escaped; if <0, like =0 except that the first character if a
* delimiter is also escaped.
* If "literal", doesn't strip leading space.
* Return zero on failure, non-zero on success.
*/
Expand Down Expand Up @@ -81,7 +84,7 @@ lowdown_nroff_esc(struct lowdown_buf *ob, const char *data, size_t size,
return 0;
break;
case '\n':
if (!hbuf_putc(ob, oneline ? ' ' : '\n'))
if (!hbuf_putc(ob, oneline > 0 ? ' ' : '\n'))
return 0;
if (literal)
break;
Expand All @@ -99,9 +102,10 @@ lowdown_nroff_esc(struct lowdown_buf *ob, const char *data, size_t size,
break;
case '\'':
case '.':
if (!oneline &&
ob->size > 0 &&
ob->data[ob->size - 1] == '\n' &&
if (((oneline < 0 && i == 0) ||
(oneline <= 0 &&
ob->size > 0 &&
ob->data[ob->size - 1] == '\n')) &&
!HBUF_PUTSL(ob, "\\&"))
return 0;
/* FALLTHROUGH */
Expand Down
3 changes: 3 additions & 0 deletions regress/template/escaperoff.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
\&.HT \e(ac \ee bc
\&.HT foo
.AT hello \eethere .HT multiline
6 changes: 6 additions & 0 deletions regress/template/escaperoff.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
title: .HT \(ac \e bc
.HT foo
author: .AT hello \ethere
.HT multiline

asdf
2 changes: 2 additions & 0 deletions regress/template/escaperoff.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
$title.escaperoff()$
$author.escaperoffline()$
2 changes: 1 addition & 1 deletion template.c
Original file line number Diff line number Diff line change
Expand Up @@ -840,7 +840,7 @@ op_eval_function(struct op_out *out, const char *expr, size_t exprsz,
else if (exprsz == 11 && strncasecmp(expr, "escapelatex", 11) == 0)
nq = op_eval_function_escape_latex(out, input);
else if (exprsz == 10 && strncasecmp(expr, "escaperoff", 10) == 0)
nq = op_eval_function_escape_roff(out, input, 0);
nq = op_eval_function_escape_roff(out, input, -1);
else if (exprsz == 14 && strncasecmp(expr, "escaperoffline", 14) == 0)
nq = op_eval_function_escape_roff(out, input, 1);
else {
Expand Down

0 comments on commit c8cdc23

Please sign in to comment.