Skip to content

Commit

Permalink
Add support for MarkDown headings formatted as "COMMAND(1) ..."
Browse files Browse the repository at this point in the history
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
  • Loading branch information
thaJeztah committed Oct 17, 2020
1 parent 1029f53 commit a546ea0
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
21 changes: 15 additions & 6 deletions md2man/roff.go
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"io"
"os"
"regexp"
"strings"

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

var title = regexp.MustCompile(`^(\s*\w+)\((\d)\)`)
func (r *roffRenderer) handleText(w io.Writer, node *blackfriday.Node, entering bool) {
var (
start, end string
)
// handle special roff table cell text encapsulation
if node.Parent.Type == blackfriday.TableCell {
var start, end string
if len(node.Literal) > 30 {
start = tableCellStart
end = tableCellEnd
Expand All @@ -178,10 +178,19 @@ func (r *roffRenderer) handleText(w io.Writer, node *blackfriday.Node, entering
end = crTag
}
}
out(w, start)
escapeSpecialChars(w, node.Literal)
out(w, end)
return
}
literal := node.Literal
if r.firstHeader && title.Match(node.Literal){
if r.firstHeader && title.Match(literal){
// "COMMAND(1) SOMETHING" => "COMMAND 1 SOMETHING"
literal = title.ReplaceAll(literal, []byte("${1} ${2}"))
}
}
out(w, start)
escapeSpecialChars(w, node.Literal)
out(w, end)
escapeSpecialChars(w, literal)
}

func (r *roffRenderer) handleHeading(w io.Writer, node *blackfriday.Node, entering bool) {
Expand Down
15 changes: 15 additions & 0 deletions md2man/roff_test.go
Expand Up @@ -10,6 +10,21 @@ 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"`,
}
doTestsInline(t, tests)
}

func TestEmphasis(t *testing.T) {
var tests = []string{
"nothing inline\n",
Expand Down

0 comments on commit a546ea0

Please sign in to comment.