Skip to content

Commit 1c88e95

Browse files
committed
Do not escape dash, underscore, and ampersand
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 <[email protected]>
1 parent 92cd0d0 commit 1c88e95

File tree

2 files changed

+11
-20
lines changed

2 files changed

+11
-20
lines changed

md2man/roff.go

+1-10
Original file line numberDiff line numberDiff line change
@@ -309,15 +309,6 @@ func out(w io.Writer, output string) {
309309
io.WriteString(w, output) // nolint: errcheck
310310
}
311311

312-
func needsBackslash(c byte) bool {
313-
for _, r := range []byte("-_&\\") {
314-
if c == r {
315-
return true
316-
}
317-
}
318-
return false
319-
}
320-
321312
func escapeSpecialChars(w io.Writer, text []byte) {
322313
for i := 0; i < len(text); i++ {
323314
// escape initial apostrophe or period
@@ -328,7 +319,7 @@ func escapeSpecialChars(w io.Writer, text []byte) {
328319
// directly copy normal characters
329320
org := i
330321

331-
for i < len(text) && !needsBackslash(text[i]) {
322+
for i < len(text) && text[i] != '\\' {
332323
i++
333324
}
334325
if i > org {

md2man/roff_test.go

+10-10
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,13 @@ func TestEmphasis(t *testing.T) {
5252
".nh\n\n.PP\nover \\fItwo\nlines\\fP test\n",
5353

5454
"odd _number of_ markers_ here\n",
55-
".nh\n\n.PP\nodd \\fInumber of\\fP markers\\_ here\n",
55+
".nh\n\n.PP\nodd \\fInumber of\\fP markers_ here\n",
5656

5757
"odd _number\nof_ markers_ here\n",
58-
".nh\n\n.PP\nodd \\fInumber\nof\\fP markers\\_ here\n",
58+
".nh\n\n.PP\nodd \\fInumber\nof\\fP markers_ here\n",
5959

6060
"mix of *markers_\n",
61-
".nh\n\n.PP\nmix of *markers\\_\n",
61+
".nh\n\n.PP\nmix of *markers_\n",
6262

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

110110
"odd __number of__ markers__ here\n",
111-
".nh\n\n.PP\nodd \\fBnumber of\\fP markers\\_\\_ here\n",
111+
".nh\n\n.PP\nodd \\fBnumber of\\fP markers__ here\n",
112112

113113
"odd __number\nof__ markers__ here\n",
114-
".nh\n\n.PP\nodd \\fBnumber\nof\\fP markers\\_\\_ here\n",
114+
".nh\n\n.PP\nodd \\fBnumber\nof\\fP markers__ here\n",
115115

116116
"mix of **markers__\n",
117-
".nh\n\n.PP\nmix of **markers\\_\\_\n",
117+
".nh\n\n.PP\nmix of **markers__\n",
118118

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

139139
"***triple emphasis___\n",
140-
".nh\n\n.PP\n***triple emphasis\\_\\_\\_\n",
140+
".nh\n\n.PP\n***triple emphasis___\n",
141141

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

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

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

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

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

0 commit comments

Comments
 (0)