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

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

As a result, we have two bugs:

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

2. Dash and underscore become quite longer (not in text output, only in
   PDF etc), which in many cases (like file names, command line options
   etc) look ugly.

In fact, the only character that needs escaping is the backslash itself.

Signed-off-by: Kir Kolyshkin <[email protected]>
  • Loading branch information
kolyshkin committed Jul 13, 2021
1 parent 817886f commit eb69b48
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 19 deletions.
11 changes: 1 addition & 10 deletions md2man/roff.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,15 +317,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 @@ -336,7 +327,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
18 changes: 9 additions & 9 deletions md2man/roff_test.go
Original file line number Diff line number Diff line change
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

0 comments on commit eb69b48

Please sign in to comment.