Skip to content

Commit

Permalink
Backslash-escape first hyphen in NAME section
Browse files Browse the repository at this point in the history
This is the required format for man-page parsers across the ecosystem to
successfuly parse out whatis information.

Signed-off-by: Cory Snider <csnider@mirantis.com>
  • Loading branch information
corhere committed Aug 9, 2024
1 parent 0f2f6eb commit 426206c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
18 changes: 17 additions & 1 deletion md2man/roff.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,23 @@ func (r *roffRenderer) RenderNode(w io.Writer, node *blackfriday.Node, entering

switch node.Type {
case blackfriday.Text:
escapeSpecialChars(w, node.Literal)
// Special case: format the NAME section as required for proper whatis parsing.
// Refer to the lexgrog(1) and groff_man(7) manual pages for details.
if node.Parent != nil &&
node.Parent.Type == blackfriday.Paragraph &&
node.Parent.Prev != nil &&
node.Parent.Prev.Type == blackfriday.Heading &&
node.Parent.Prev.FirstChild != nil &&
bytes.EqualFold(node.Parent.Prev.FirstChild.Literal, []byte("NAME")) {
before, after, found := bytes.Cut(node.Literal, []byte(" - "))
escapeSpecialChars(w, before)
if found {
out(w, ` \- `)
escapeSpecialChars(w, after)
}
} else {
escapeSpecialChars(w, node.Literal)
}
case blackfriday.Softbreak:
out(w, crTag)
case blackfriday.Hardbreak:
Expand Down
8 changes: 7 additions & 1 deletion md2man/roff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,13 @@ func TestComments(t *testing.T) {
func TestHeadings(t *testing.T) {
tests := []string{
"# title\n\n# NAME\ncommand - description\n\n# SYNOPSIS\nA short description\n\nWhich spans multiple paragraphs\n",
".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",
".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",

"# title\n\n# Name\nmy-command, other - description - with - hyphens\n",
".nh\n.TH title\n\n.SH Name\nmy-command, other \\- description - with - hyphens\n",

"# title\n\n# Not NAME\nsome - other - text\n",
".nh\n.TH title\n\n.SH Not NAME\nsome - other - text\n",
}
doTestsInline(t, tests)
}
Expand Down

0 comments on commit 426206c

Please sign in to comment.