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

Fix list indents #116

Closed
wants to merge 1 commit 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 30 additions & 21 deletions md2man/roff.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ type roffRenderer struct {
listCounters []int
firstHeader bool
firstDD bool
listDepth int
itemStarted bool
lastDD bool
}

const (
Expand All @@ -42,9 +43,9 @@ const (
codeCloseTag = ".EE\n" // Do not prepend a newline character since code blocks, by definition, include a newline already (or at least as how blackfriday gives us on).
quoteTag = "\n.PP\n.RS\n"
quoteCloseTag = "\n.RE\n"
listTag = "\n.RS\n"
listCloseTag = "\n.RE\n"
dtTag = "\n.TP\n"
itemTag = "\n.RS 5\n"
itemCloseTag = "\n.RE\n"
dtTag = "\n.TP 5\n"
dd2Tag = "\n"
tableStart = "\n.TS\nallbox;\n"
tableEnd = ".TE\n"
Expand Down Expand Up @@ -141,8 +142,14 @@ 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 {
if r.firstDD {
return blackfriday.GoToNext
}
if r.itemStarted {
if !entering {
r.itemStarted = false
out(w, itemTag)
}
return blackfriday.GoToNext
}
if entering {
Expand Down Expand Up @@ -209,34 +216,31 @@ 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 node.ListFlags&blackfriday.ListTypeDefinition != 0 {
// tags for definition lists handled within Item node
openTag = ""
closeTag = ""
}
if entering {
r.listDepth++
if node.ListFlags&blackfriday.ListTypeOrdered != 0 {
r.listCounters = append(r.listCounters, 1)
}
out(w, openTag)
} else {
if node.ListFlags&blackfriday.ListTypeOrdered != 0 {
r.listCounters = r.listCounters[:len(r.listCounters)-1]
} else if node.ListFlags&blackfriday.ListTypeDefinition != 0 {
out(w, itemCloseTag)
r.lastDD = false
}
out(w, closeTag)
r.listDepth--
}
}

func (r *roffRenderer) handleItem(w io.Writer, node *blackfriday.Node, entering bool) {
if entering {
if node.ListFlags&blackfriday.ListTypeOrdered != 0 {
r.itemStarted = true
out(w, fmt.Sprintf(".IP \"%3d.\" 5\n", r.listCounters[len(r.listCounters)-1]))
r.listCounters[len(r.listCounters)-1]++
} else if node.ListFlags&blackfriday.ListTypeTerm != 0 {
if r.lastDD {
out(w, itemCloseTag)
}
r.itemStarted = true
// DT (definition term): line just before DD (see below).
out(w, dtTag)
r.firstDD = true
Expand All @@ -247,15 +251,20 @@ 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 r.firstDD {
r.firstDD = false
} else {
out(w, dd2Tag)
r.firstDD = false
}
} else {
out(w, ".IP \\(bu 2\n")
r.itemStarted = true
out(w, ".IP \" \\(bu\" 5\n")
}
r.lastDD = false
} else {
out(w, "\n")
if node.ListFlags&(blackfriday.ListTypeTerm|blackfriday.ListTypeDefinition) != 0 {
r.lastDD = true
} else {
out(w, itemCloseTag)
}
}
}

Expand Down
6 changes: 4 additions & 2 deletions md2man/roff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,11 @@ func TestCodeSpan(t *testing.T) {
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 5\n\\fB[grpc]\\fP\nSection for gRPC socket listener settings. Contains three properties:\n.RS 5\n.IP \" \\(bu\" 5\n\\fBaddress\\fP (Default: \"/run/containerd/containerd.sock\")\n.RS 5\n\n.RE\n.IP \" \\(bu\" 5\n\\fBuid\\fP (Default: 0)\n.RS 5\n\n.RE\n.IP \" \\(bu\" 5\n\\fBgid\\fP (Default: 0)\n.RS 5\n\n.RE\n\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",
".nh\n\n.TP 5\nDefinition title\nDefinition description one\n.RS 5\n\n.PP\nAnd two\n\n.PP\nAnd three\n\n.RE\n",
"- item 1\n- item 2\n second par",
".nh\n.IP \" \\(bu\" 5\nitem 1\n.RS 5\n\n.RE\n.IP \" \\(bu\" 5\nitem 2\nsecond par\n.RS 5\n\n.RE\n",
}
doTestsParam(t, tests, TestParams{blackfriday.DefinitionLists})
}
Expand Down
Loading