Skip to content

Commit

Permalink
Man writer: improvements to code and code blocks.
Browse files Browse the repository at this point in the history
The aim here (see #9020) is to produce more standard and more
portable man pages.  To that end:

- We revert the fanciness introduced in #7506, which employs a
  custom font name V and a macro that makes this act like boldface
  in a terminal and monospace in other formats.  Unfortunately,
  this code uses a mechanism that is not portable (and does not
  work in mandoc).

- Instead of using V for inline code, we simply use CR.
  Note that `\f[CR]` is emitted instead of plain `\f[C]`,
  because there is no C font in man.  (This produces warnings
  in recent versions of groff.)

- For code blocks, we now use the `.EX` and `.EE` macros,
  together with `.IP` for spacing and indentation.  This gives
  more standard code that can be better interpreted e.g. by mandoc.
  • Loading branch information
jgm committed Aug 26, 2023
1 parent 0a298cb commit a6fe02f
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 99 deletions.
14 changes: 0 additions & 14 deletions data/templates/default.man
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,6 @@ $if(pandoc-version)$
.\" Automatically generated by Pandoc $pandoc-version$
.\"
$endif$
.\" Define V font for inline verbatim, using C font in formats
.\" that render this, and otherwise B font.
.ie "\f[CB]x\f[]"x" \{\
. ftr V B
. ftr VI BI
. ftr VB B
. ftr VBI BI
.\}
.el \{\
. ftr V CR
. ftr VI CI
. ftr VB CB
. ftr VBI CBI
.\}
$if(adjusting)$
.ad $adjusting$
$endif$
Expand Down
9 changes: 3 additions & 6 deletions src/Text/Pandoc/Writers/Man.hs
Original file line number Diff line number Diff line change
Expand Up @@ -131,14 +131,12 @@ blockToMan opts (Header level _ inlines) = do
return $ nowrap $ literal heading <> contents
blockToMan opts (CodeBlock _ str) = return $
literal ".IP" $$
literal ".nf" $$
literal "\\f[C]" $$
literal ".EX" $$
((case T.uncons str of
Just ('.',_) -> literal "\\&"
_ -> mempty) <>
literal (escString opts str)) $$
literal "\\f[R]" $$
literal ".fi"
literal ".EE"
blockToMan opts (BlockQuote blocks) = do
contents <- blockListToMan opts blocks
return $ literal ".RS" $$ contents $$ literal ".RE"
Expand Down Expand Up @@ -291,8 +289,7 @@ inlineToMan opts (Quoted DoubleQuote lst) = do
inlineToMan opts (Cite _ lst) =
inlineListToMan opts lst
inlineToMan opts (Code _ str) =
-- note that the V font is specially defined in the default man template
withFontFeature 'V' (return (literal $ escString opts str))
withFontFeature 'C' (return (literal $ escString opts str))
inlineToMan opts (Str str@(T.uncons -> Just ('.',_))) =
return $ afterBreak "\\&" <> literal (escString opts str)
inlineToMan opts (Str str) = return $ literal $ escString opts str
Expand Down
8 changes: 5 additions & 3 deletions src/Text/Pandoc/Writers/Roff.hs
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,11 @@ fontChange = do
fromMaybe False (Map.lookup 'B' features)] ++
['I' | fromMaybe False $ Map.lookup 'I' features]
return $
if null filling
then text "\\f[R]"
else text $ "\\f[" ++ filling ++ "]"
case filling of
[] -> text "\\f[R]"
-- see #9020. C is not a font, use CR.
['C'] -> text "\\f[CR]"
_ -> text $ "\\f[" ++ filling ++ "]"

withFontFeature :: (HasChars a, IsString a, PandocMonad m)
=> Char -> MS m (Doc a) -> MS m (Doc a)
Expand Down
4 changes: 2 additions & 2 deletions test/command/3568.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ normal `code` normal.
normal \f[I]italic \f[BI]bold in the middle\f[I] only italic\f[R]
normal.
.PP
normal \f[B]bold \f[VB]code\f[B] more bold\f[R] normal.
normal \f[B]bold \f[CB]code\f[B] more bold\f[R] normal.
.PP
normal \f[V]code\f[R] normal.
normal \f[CR]code\f[R] normal.
```
4 changes: 2 additions & 2 deletions test/command/5620.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
: Write output to *OUTFILE* instead of `stdout`(3)
^D
.TP
\f[V]-o\f[R], \f[V]--output=\f[R]\f[I]OUTFILE\f[R]
Write output to \f[I]OUTFILE\f[R] instead of \f[V]stdout\f[R](3)
\f[CR]-o\f[R], \f[CR]--output=\f[R]\f[I]OUTFILE\f[R]
Write output to \f[I]OUTFILE\f[R] instead of \f[CR]stdout\f[R](3)
```
94 changes: 29 additions & 65 deletions test/writer.man
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
.\" Define V font for inline verbatim, using C font in formats
.\" that render this, and otherwise B font.
.ie "\f[CB]x\f[]"x" \{\
. ftr V B
. ftr VI BI
. ftr VB B
. ftr VBI BI
.\}
.el \{\
. ftr V CR
. ftr VI CI
. ftr VB CB
. ftr VBI CBI
.\}
.TH "Pandoc Test Suite" "" "July 17, 2006" "" ""
.PP
This is a set of tests for pandoc.
Expand Down Expand Up @@ -65,13 +51,11 @@ It is pretty short.
.PP
Code in a block quote:
.IP
.nf
\f[C]
.EX
sub status {
print \[dq]working\[dq];
}
\f[R]
.fi
.EE
.PP
A list:
.IP "1." 3
Expand Down Expand Up @@ -99,27 +83,23 @@ And a following paragraph.
.PP
Code:
.IP
.nf
\f[C]
.EX
---- (should be four hyphens)
sub status {
print \[dq]working\[dq];
}
this code block is indented by one tab
\f[R]
.fi
.EE
.PP
And:
.IP
.nf
\f[C]
.EX
this code block is indented by two tabs
These should not be escaped: \[rs]$ \[rs]\[rs] \[rs]> \[rs][ \[rs]{
\f[R]
.fi
.EE
.PP
* * * * *
.SH Lists
Expand Down Expand Up @@ -375,11 +355,9 @@ contains seeds, crisp, pleasant to taste
orange fruit
.RS
.IP
.nf
\f[C]
.EX
{ orange code block }
\f[R]
.fi
.EE
.RS
.PP
orange block quote
Expand Down Expand Up @@ -440,21 +418,17 @@ foo
.PP
This should be a code block, though:
.IP
.nf
\f[C]
.EX
<div>
foo
</div>
\f[R]
.fi
.EE
.PP
As should this:
.IP
.nf
\f[C]
.EX
<div>foo</div>
\f[R]
.fi
.EE
.PP
Now, nested:
foo
Expand All @@ -465,21 +439,17 @@ Multiline:
.PP
Code block:
.IP
.nf
\f[C]
.EX
<!-- Comment -->
\f[R]
.fi
.EE
.PP
Just plain comment, with trailing spaces on the line:
.PP
Code:
.IP
.nf
\f[C]
.EX
<hr />
\f[R]
.fi
.EE
.PP
Hr\[cq]s:
.PP
Expand All @@ -500,8 +470,8 @@ So is \f[B]\f[BI]this\f[B]\f[R] word.
.PP
So is \f[B]\f[BI]this\f[B]\f[R] word.
.PP
This is code: \f[V]>\f[R], \f[V]$\f[R], \f[V]\[rs]\f[R], \f[V]\[rs]$\f[R],
\f[V]<html>\f[R].
This is code: \f[CR]>\f[R], \f[CR]$\f[R], \f[CR]\[rs]\f[R], \f[CR]\[rs]$\f[R],
\f[CR]<html>\f[R].
.PP
[STRIKEOUT:This is \f[I]strikeout\f[R].]
.PP
Expand All @@ -525,7 +495,7 @@ So is `pine.'
.PP
`He said, \[lq]I want to go.\[rq]' Were you alive in the 70\[cq]s?
.PP
Here is some quoted `\f[V]code\f[R]' and a \[lq]quoted
Here is some quoted `\f[CR]code\f[R]' and a \[lq]quoted
link (http://example.com/?foo=1&bar=2)\[rq].
.PP
Some dashes: one\[em]two \[em] three\[em]four \[em] five.
Expand Down Expand Up @@ -558,15 +528,15 @@ Here\[cq]s one that has a line break in it:
.PP
These shouldn\[cq]t be math:
.IP \[bu] 2
To get the famous equation, write \f[V]$e = mc\[ha]2$\f[R].
To get the famous equation, write \f[CR]$e = mc\[ha]2$\f[R].
.IP \[bu] 2
$22,000 is a \f[I]lot\f[R] of money.
So is $34,000.
(It worked if \[lq]lot\[rq] is emphasized.)
.IP \[bu] 2
Shoes ($20) and socks ($5).
.IP \[bu] 2
Escaped \f[V]$\f[R]: $73 \f[I]this should be emphasized\f[R] 23$.
Escaped \f[CR]$\f[R]: $73 \f[I]this should be emphasized\f[R] 23$.
.PP
Here\[cq]s a LaTeX table:
.PP
Expand Down Expand Up @@ -664,11 +634,9 @@ Indented thrice.
.PP
This should [not][] be a link.
.IP
.nf
\f[C]
.EX
[not]: /url
\f[R]
.fi
.EE
.PP
Foo bar.
.PP
Expand Down Expand Up @@ -699,13 +667,11 @@ An e-mail address: <[email protected]>
Blockquoted: <http://example.com/>
.RE
.PP
Auto-links should not occur here: \f[V]<http://example.com/>\f[R]
Auto-links should not occur here: \f[CR]<http://example.com/>\f[R]
.IP
.nf
\f[C]
.EX
or here: <http://example.com/>
\f[R]
.fi
.EE
.PP
* * * * *
.SH Images
Expand Down Expand Up @@ -744,18 +710,16 @@ This one contains multiple blocks.
Subsequent blocks are indented to show that they belong to the footnote (as with
list items).
.IP
.nf
\f[C]
.EX
{ <code> }
\f[R]
.fi
.EE
.PP
If you want, you can indent every line, but you can also be lazy and just indent
the first line of each block.
.SS [3]
.PP
This is \f[I]easier\f[R] to type.
Inline notes may contain links (http://google.com) and \f[V]]\f[R] verbatim
Inline notes may contain links (http://google.com) and \f[CR]]\f[R] verbatim
characters, as well as [bracketed text].
.SS [4]
.PP
Expand Down
14 changes: 7 additions & 7 deletions test/writer.ms
Original file line number Diff line number Diff line change
Expand Up @@ -644,8 +644,8 @@ So is \f[B]\f[BI]this\f[B]\f[R] word.
.PP
So is \f[B]\f[BI]this\f[B]\f[R] word.
.PP
This is code: \f[C]>\f[R], \f[C]$\f[R], \f[C]\[rs]\f[R], \f[C]\[rs]$\f[R],
\f[C]<html>\f[R].
This is code: \f[CR]>\f[R], \f[CR]$\f[R], \f[CR]\[rs]\f[R], \f[CR]\[rs]$\f[R],
\f[CR]<html>\f[R].
.PP
\m[strikecolor]This is \f[I]strikeout\f[R].\m[]
.PP
Expand All @@ -671,7 +671,7 @@ So is `pine.'
.PP
`He said, \[lq]I want to go.\[rq]' Were you alive in the 70\[cq]s?
.PP
Here is some quoted `\f[C]code\f[R]' and a \[lq]\c
Here is some quoted `\f[CR]code\f[R]' and a \[lq]\c
.pdfhref W -D "http://example.com/?foo=1&bar=2" -A "\c" \
-- "quoted link"
\&\[rq].
Expand Down Expand Up @@ -707,15 +707,15 @@ Here\[cq]s one that has a line break in it: @alpha + omega times x sup 2@.
.LP
These shouldn\[cq]t be math:
.IP \[bu] 3
To get the famous equation, write \f[C]$e = mc\[ha]2$\f[R].
To get the famous equation, write \f[CR]$e = mc\[ha]2$\f[R].
.IP \[bu] 3
$22,000 is a \f[I]lot\f[R] of money.
So is $34,000.
(It worked if \[lq]lot\[rq] is emphasized.)
.IP \[bu] 3
Shoes ($20) and socks ($5).
.IP \[bu] 3
Escaped \f[C]$\f[R]: $73 \f[I]this should be emphasized\f[R] 23$.
Escaped \f[CR]$\f[R]: $73 \f[I]this should be emphasized\f[R] 23$.
.LP
Here\[cq]s a LaTeX table:
.HLINE
Expand Down Expand Up @@ -938,7 +938,7 @@ Blockquoted: \c
\&
.QE
.LP
Auto-links should not occur here: \f[C]<http://example.com/>\f[R]
Auto-links should not occur here: \f[CR]<http://example.com/>\f[R]
.IP
.nf
\f[C]
Expand Down Expand Up @@ -991,7 +991,7 @@ This is \f[I]easier\f[R] to type.
Inline notes may contain \c
.pdfhref W -D "http://google.com" -A "\c" \
-- "links"
\& and \f[C]]\f[R] verbatim characters, as well as [bracketed text].
\& and \f[CR]]\f[R] verbatim characters, as well as [bracketed text].
.FE
.QS
.LP
Expand Down

0 comments on commit a6fe02f

Please sign in to comment.