Skip to content

Commit

Permalink
Merge pull request #122 from corhere/fix-multi-paragraph-lists
Browse files Browse the repository at this point in the history
Fix rendering of lists
  • Loading branch information
cpuguy83 authored Sep 16, 2024
2 parents 6350b8a + 149c352 commit 54d2e5c
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 12 deletions.
12 changes: 12 additions & 0 deletions go-md2man.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@ written purely in Go so as to reduce dependencies on 3rd party libs.

By default, the input is stdin and the output is stdout.

# OPTIONS

**-in=**_file_
: Path to markdown file to be processed.

Defaults to stdin.

**-out=**_file_
: Path to output processed file.

Defaults to stdout.

# EXAMPLES
Convert the markdown file *go-md2man.1.md* into a manpage:
```
Expand Down
42 changes: 31 additions & 11 deletions md2man/roff.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const (
quoteTag = "\n.PP\n.RS\n"
quoteCloseTag = "\n.RE\n"
listTag = "\n.RS\n"
listCloseTag = "\n.RE\n"
listCloseTag = ".RE\n"
dtTag = "\n.TP\n"
dd2Tag = "\n"
tableStart = "\n.TS\nallbox;\n"
Expand Down Expand Up @@ -150,14 +150,25 @@ func (r *roffRenderer) RenderNode(w io.Writer, node *blackfriday.Node, entering
case blackfriday.Document:
break
case blackfriday.Paragraph:
// roff .PP markers break lists
if r.listDepth > 0 {
return blackfriday.GoToNext
}
if entering && (node.Prev == nil || node.Prev.Type != blackfriday.Heading) {
out(w, paraTag)
if entering {
if r.listDepth > 0 {
// roff .PP markers break lists
if node.Prev != nil { // continued paragraph
if node.Prev.Type == blackfriday.List && node.Prev.ListFlags&blackfriday.ListTypeDefinition == 0 {
out(w, ".IP\n")
} else {
out(w, crTag)
}
}
} else if node.Prev != nil && node.Prev.Type == blackfriday.Heading {
out(w, crTag)
} else {
out(w, paraTag)
}
} else {
out(w, crTag)
if node.Next == nil || node.Next.Type != blackfriday.List {
out(w, crTag)
}
}
case blackfriday.BlockQuote:
if entering {
Expand Down Expand Up @@ -220,6 +231,10 @@ func (r *roffRenderer) handleHeading(w io.Writer, node *blackfriday.Node, enteri
func (r *roffRenderer) handleList(w io.Writer, node *blackfriday.Node, entering bool) {
openTag := listTag
closeTag := listCloseTag
if (entering && r.listDepth == 0) || (!entering && r.listDepth == 1) {
openTag = crTag
closeTag = ""
}
if node.ListFlags&blackfriday.ListTypeDefinition != 0 {
// tags for definition lists handled within Item node
openTag = ""
Expand Down Expand Up @@ -255,13 +270,18 @@ func (r *roffRenderer) handleItem(w io.Writer, node *blackfriday.Node, entering
// subsequent ones, as there should be no vertical
// whitespace between the DT and the first DD.
if node.Prev != nil && node.Prev.ListFlags&(blackfriday.ListTypeTerm|blackfriday.ListTypeDefinition) == blackfriday.ListTypeDefinition {
out(w, dd2Tag)
if node.Prev.Type == blackfriday.Item &&
node.Prev.LastChild != nil &&
node.Prev.LastChild.Type == blackfriday.List &&
node.Prev.LastChild.ListFlags&blackfriday.ListTypeDefinition == 0 {
out(w, ".IP\n")
} else {
out(w, dd2Tag)
}
}
} else {
out(w, ".IP \\(bu 2\n")
}
} else {
out(w, "\n")
}
}

Expand Down
12 changes: 11 additions & 1 deletion md2man/roff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,12 +207,22 @@ func TestCodeSpan(t *testing.T) {
doTestsInline(t, tests)
}

func TestFlatLists(t *testing.T) {
tests := []string{
"Paragraph\n\n- item one\n- item two\n- item three\n",
".nh\n\n.PP\nParagraph\n.IP \\(bu 2\nitem one\n.IP \\(bu 2\nitem two\n.IP \\(bu 2\nitem three\n",
}
doTestsInline(t, tests)
}

func TestListLists(t *testing.T) {
tests := []string{
"\n\n**[grpc]**\n: Section for gRPC socket listener settings. Contains three properties:\n - **address** (Default: \"/run/containerd/containerd.sock\")\n - **uid** (Default: 0)\n - **gid** (Default: 0)",
".nh\n\n.TP\n\\fB[grpc]\\fP\nSection for gRPC socket listener settings. Contains three properties:\n.RS\n.IP \\(bu 2\n\\fBaddress\\fP (Default: \"/run/containerd/containerd.sock\")\n.IP \\(bu 2\n\\fBuid\\fP (Default: 0)\n.IP \\(bu 2\n\\fBgid\\fP (Default: 0)\n\n.RE\n\n",
".nh\n\n.TP\n\\fB[grpc]\\fP\nSection for gRPC socket listener settings. Contains three properties:\n.RS\n.IP \\(bu 2\n\\fBaddress\\fP (Default: \"/run/containerd/containerd.sock\")\n.IP \\(bu 2\n\\fBuid\\fP (Default: 0)\n.IP \\(bu 2\n\\fBgid\\fP (Default: 0)\n.RE\n",
"Definition title\n: Definition description one\n: And two\n: And three\n",
".nh\n\n.TP\nDefinition title\nDefinition description one\n\nAnd two\n\nAnd three\n",
"Definition\n: description\n\n split\n\n over\n\n multiple\n\n paragraphs\n",
".nh\n\n.TP\nDefinition\ndescription\n\nsplit\n\nover\n\nmultiple\n\nparagraphs\n",
}
doTestsParam(t, tests, TestParams{blackfriday.DefinitionLists})
}
Expand Down

0 comments on commit 54d2e5c

Please sign in to comment.