Skip to content

Commit

Permalink
Do not escape dash, underscore, and ampersand
Browse files Browse the repository at this point in the history
Currently, we escape a few characters, such as

	- (dash) becomes \- (arithmetic minus sign)
	_ (underscore) becomes \_ (underline character)
	& (ampersand) becomes \& (zero-width space)

As a result, we have two bugs:

1. All & characters disappear from the resulting man page.

2. Dashes are quite longer, and underscores becomes underlines (the
   latter are lower). This is not reflected in text output, only in PDF
   etc, and in most cases (like file names, command line options etc)
   does not look good.

In fact, the only character that needs escaping is the backslash itself.
This is what this patch does, together with fixes to expected test
output.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
  • Loading branch information
kolyshkin committed Jul 13, 2021
1 parent 92cd0d0 commit 1c88e95
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 20 deletions.
11 changes: 1 addition & 10 deletions md2man/roff.go
Expand Up @@ -309,15 +309,6 @@ func out(w io.Writer, output string) {
io.WriteString(w, output) // nolint: errcheck
}

func needsBackslash(c byte) bool {
for _, r := range []byte("-_&\\") {
if c == r {
return true
}
}
return false
}

func escapeSpecialChars(w io.Writer, text []byte) {
for i := 0; i < len(text); i++ {
// escape initial apostrophe or period
Expand All @@ -328,7 +319,7 @@ func escapeSpecialChars(w io.Writer, text []byte) {
// directly copy normal characters
org := i

for i < len(text) && !needsBackslash(text[i]) {
for i < len(text) && text[i] != '\\' {
i++
}
if i > org {
Expand Down
20 changes: 10 additions & 10 deletions md2man/roff_test.go
Expand Up @@ -52,13 +52,13 @@ func TestEmphasis(t *testing.T) {
".nh\n\n.PP\nover \\fItwo\nlines\\fP test\n",

"odd _number of_ markers_ here\n",
".nh\n\n.PP\nodd \\fInumber of\\fP markers\\_ here\n",
".nh\n\n.PP\nodd \\fInumber of\\fP markers_ here\n",

"odd _number\nof_ markers_ here\n",
".nh\n\n.PP\nodd \\fInumber\nof\\fP markers\\_ here\n",
".nh\n\n.PP\nodd \\fInumber\nof\\fP markers_ here\n",

"mix of *markers_\n",
".nh\n\n.PP\nmix of *markers\\_\n",
".nh\n\n.PP\nmix of *markers_\n",

"*What is A\\* algorithm?*\n",
".nh\n\n.PP\n\\fIWhat is A* algorithm?\\fP\n",
Expand Down Expand Up @@ -108,13 +108,13 @@ func TestStrong(t *testing.T) {
".nh\n\n.PP\nover \\fBtwo\nlines\\fP test\n",

"odd __number of__ markers__ here\n",
".nh\n\n.PP\nodd \\fBnumber of\\fP markers\\_\\_ here\n",
".nh\n\n.PP\nodd \\fBnumber of\\fP markers__ here\n",

"odd __number\nof__ markers__ here\n",
".nh\n\n.PP\nodd \\fBnumber\nof\\fP markers\\_\\_ here\n",
".nh\n\n.PP\nodd \\fBnumber\nof\\fP markers__ here\n",

"mix of **markers__\n",
".nh\n\n.PP\nmix of **markers\\_\\_\n",
".nh\n\n.PP\nmix of **markers__\n",

"**`/usr`** : this folder is named `usr`\n",
".nh\n\n.PP\n\\fB\\fB\\fC/usr\\fR\\fP : this folder is named \\fB\\fCusr\\fR\n",
Expand All @@ -137,7 +137,7 @@ func TestEmphasisMix(t *testing.T) {
".nh\n\n.PP\n\\fB\\fItriple emphasis\\fP\\fP\n",

"***triple emphasis___\n",
".nh\n\n.PP\n***triple emphasis\\_\\_\\_\n",
".nh\n\n.PP\n***triple emphasis___\n",

"*__triple emphasis__*\n",
".nh\n\n.PP\n\\fI\\fBtriple emphasis\\fP\\fP\n",
Expand Down Expand Up @@ -169,7 +169,7 @@ func TestCodeSpan(t *testing.T) {
".nh\n\n.PP\na `single marker\n",

"a single multi-tick marker with ``` no text\n",
".nh\n\n.PP\na single multi\\-tick marker with ``` no text\n",
".nh\n\n.PP\na single multi-tick marker with ``` no text\n",

"markers with ` ` a space\n",
".nh\n\n.PP\nmarkers with a space\n",
Expand All @@ -178,7 +178,7 @@ func TestCodeSpan(t *testing.T) {
".nh\n\n.PP\n\\fB\\fCsource code\\fR and a `stray\n",

"`source *with* _awkward characters_ in it`\n",
".nh\n\n.PP\n\\fB\\fCsource *with* \\_awkward characters\\_ in it\\fR\n",
".nh\n\n.PP\n\\fB\\fCsource *with* _awkward characters_ in it\\fR\n",

"`split over\ntwo lines`\n",
".nh\n\n.PP\n\\fB\\fCsplit over\ntwo lines\\fR\n",
Expand Down Expand Up @@ -343,7 +343,7 @@ func TestLinks(t *testing.T) {
func TestEscapeCharacters(t *testing.T) {
var tests = []string{
"Test-one_two&three\\four~five",
".nh\n\n.PP\nTest\\-one\\_two\\&three\\\\four~five\n",
".nh\n\n.PP\nTest-one_two&three\\\\four~five\n",
}
doTestsInline(t, tests)
}
Expand Down

0 comments on commit 1c88e95

Please sign in to comment.