Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Emit a NAME section properly formatted for whatis parsing #124

Merged
merged 2 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions 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 Expand Up @@ -145,7 +161,7 @@ func (r *roffRenderer) RenderNode(w io.Writer, node *blackfriday.Node, entering
if r.listDepth > 0 {
return blackfriday.GoToNext
}
if entering {
if entering && (node.Prev == nil || node.Prev.Type != blackfriday.Heading) {
out(w, paraTag)
} else {
out(w, crTag)
Expand Down
14 changes: 14 additions & 0 deletions md2man/roff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,20 @@ func TestComments(t *testing.T) {
doTestsInlineParam(t, inlineTests, TestParams{})
}

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",

"# 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)
}

func execRecoverableTestSuite(t *testing.T, tests []string, params TestParams, suite func(candidate *string)) {
// Catch and report panics. This is useful when running 'go test -v' on
// the integration server. When developing, though, crash dump is often
Expand Down
Loading