Skip to content

Commit 426206c

Browse files
committed
Backslash-escape first hyphen in NAME section
This is the required format for man-page parsers across the ecosystem to successfuly parse out whatis information. Signed-off-by: Cory Snider <[email protected]>
1 parent 0f2f6eb commit 426206c

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

md2man/roff.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,23 @@ func (r *roffRenderer) RenderNode(w io.Writer, node *blackfriday.Node, entering
103103

104104
switch node.Type {
105105
case blackfriday.Text:
106-
escapeSpecialChars(w, node.Literal)
106+
// Special case: format the NAME section as required for proper whatis parsing.
107+
// Refer to the lexgrog(1) and groff_man(7) manual pages for details.
108+
if node.Parent != nil &&
109+
node.Parent.Type == blackfriday.Paragraph &&
110+
node.Parent.Prev != nil &&
111+
node.Parent.Prev.Type == blackfriday.Heading &&
112+
node.Parent.Prev.FirstChild != nil &&
113+
bytes.EqualFold(node.Parent.Prev.FirstChild.Literal, []byte("NAME")) {
114+
before, after, found := bytes.Cut(node.Literal, []byte(" - "))
115+
escapeSpecialChars(w, before)
116+
if found {
117+
out(w, ` \- `)
118+
escapeSpecialChars(w, after)
119+
}
120+
} else {
121+
escapeSpecialChars(w, node.Literal)
122+
}
107123
case blackfriday.Softbreak:
108124
out(w, crTag)
109125
case blackfriday.Hardbreak:

md2man/roff_test.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,13 @@ func TestComments(t *testing.T) {
424424
func TestHeadings(t *testing.T) {
425425
tests := []string{
426426
"# title\n\n# NAME\ncommand - description\n\n# SYNOPSIS\nA short description\n\nWhich spans multiple paragraphs\n",
427-
".nh\n.TH title\n\n.SH NAME\ncommand - description\n\n\n.SH SYNOPSIS\nA short description\n\n.PP\nWhich spans multiple paragraphs\n",
427+
".nh\n.TH title\n\n.SH NAME\ncommand \\- description\n\n\n.SH SYNOPSIS\nA short description\n\n.PP\nWhich spans multiple paragraphs\n",
428+
429+
"# title\n\n# Name\nmy-command, other - description - with - hyphens\n",
430+
".nh\n.TH title\n\n.SH Name\nmy-command, other \\- description - with - hyphens\n",
431+
432+
"# title\n\n# Not NAME\nsome - other - text\n",
433+
".nh\n.TH title\n\n.SH Not NAME\nsome - other - text\n",
428434
}
429435
doTestsInline(t, tests)
430436
}

0 commit comments

Comments
 (0)