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

Add support for MarkDown headings formatted as "COMMAND(1) ..." #65

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 27 additions & 2 deletions md2man/roff.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package md2man

import (
"bytes"
"fmt"
"io"
"os"
"regexp"
"strings"

"github.com/russross/blackfriday/v2"
Expand Down Expand Up @@ -163,12 +165,16 @@ func (r *roffRenderer) RenderNode(w io.Writer, node *blackfriday.Node, entering
return walkAction
}

var title = regexp.MustCompile(`^\s*"?(\w+)\((\d)\)"?\s*`)

func (r *roffRenderer) handleText(w io.Writer, node *blackfriday.Node, entering bool) {
var (
start, end string
literal = node.Literal
)
// handle special roff table cell text encapsulation
if node.Parent.Type == blackfriday.TableCell {
switch node.Parent.Type {
case blackfriday.TableCell:
if len(node.Literal) > 30 {
start = tableCellStart
end = tableCellEnd
Expand All @@ -178,9 +184,28 @@ func (r *roffRenderer) handleText(w io.Writer, node *blackfriday.Node, entering
end = crTag
}
}
case blackfriday.Heading:
if r.firstHeader {
if title.Match(literal) {
// COMMAND(1) SOMETHING\nSOMETHING 2\nSOMETHING 3 => "COMMAND" "1"\nSOMETHING\nSOMETHING 2\nSOMETHING 3
literal = title.ReplaceAll(literal, []byte("\"${1}\" \"${2}\"\n"))
}

// BlackFriday preserves newlines in the pandoc header. We already quoted
// the command itself. If there's remaining lines, quote and append each.
parts := bytes.Split(literal, []byte("\n"))
literal = parts[0]
if len(parts) > 1 {
for _, line := range parts[1:] {
literal = append(literal, []byte(` "`)...)
literal = append(literal, bytes.Trim(line, `" `)...)
literal = append(literal, []byte(`"`)...)
}
}
}
}
out(w, start)
escapeSpecialChars(w, node.Literal)
escapeSpecialChars(w, literal)
out(w, end)
}

Expand Down
69 changes: 68 additions & 1 deletion md2man/roff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,69 @@ type TestParams struct {
extensions blackfriday.Extensions
}

func TestFirstHeading(t *testing.T) {
var tests = []string{
`DOCKER 1 "JUNE 2014" "Docker Community" "Docker User Manuals"
====
`,
`.nh
.TH DOCKER 1 "JUNE 2014" "Docker Community" "Docker User Manuals"`,

`# DOCKER(1) "JUNE 2014" "Docker Community" "Docker User Manuals"`,
`.nh
.TH "DOCKER" "1" "JUNE 2014" "Docker Community" "Docker User Manuals"`,

`# "DOCKER(1)" "JUNE 2014" "Docker Community" "Docker User Manuals"`,
`.nh
.TH "DOCKER" "1" "JUNE 2014" "Docker Community" "Docker User Manuals"`,

`% DOCKER(1) Docker User Manuals
% Docker Community
% JUNE 2014
`,
`.nh
.TH "DOCKER" "1" "Docker User Manuals" "Docker Community" "JUNE 2014"`,

`% DOCKER(1) "Docker User Manuals"
% "Docker Community"
% "JUNE 2014"
`,
`.nh
.TH "DOCKER" "1" "Docker User Manuals" "Docker Community" "JUNE 2014"`,

`% DOCKER(1)
% JUNE 2014
`,
`.nh
.TH "DOCKER" "1" "JUNE 2014"`,

`% DOCKER 1
% JUNE 2014
`,
`.nh
.TH DOCKER 1 "JUNE 2014"`,

`# DOCKER(1) JUNE 2014

Here's a paragraph, following the first header.

# SEE ALSO
**docker-run(1)**
`,
`.nh
.TH "DOCKER" "1" "JUNE 2014"
.PP
Here's a paragraph, following the first header.


.SH SEE ALSO
.PP
\fBdocker\-run(1)\fP
`,
}
doTestsParam(t, tests, TestParams{})
}

func TestEmphasis(t *testing.T) {
var tests = []string{
"nothing inline\n",
Expand Down Expand Up @@ -305,8 +368,12 @@ func execRecoverableTestSuite(t *testing.T, tests []string, params TestParams, s

func runMarkdown(input string, params TestParams) string {
renderer := NewRoffRenderer()
extensions := params.extensions
if extensions == 0 {
extensions = renderer.extensions
}
Comment on lines +371 to +374
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, after digging why it didn't work with the % xxx pandoc headers, this seemed to be the problem; the tests were running without the blackfriday.Titleblock extension enabled, so blackfriday was not converting them to H1 blocks.

return string(blackfriday.Run([]byte(input), blackfriday.WithRenderer(renderer),
blackfriday.WithExtensions(params.extensions)))
blackfriday.WithExtensions(extensions)))
}

func doTestsParam(t *testing.T, tests []string, params TestParams) {
Expand Down