Skip to content

Commit

Permalink
Fix list indents
Browse files Browse the repository at this point in the history
Indent all lists a uniform amount, and also support items with multiple
paragraphs.
  • Loading branch information
baltitenger committed Apr 17, 2024
1 parent d6816bf commit f615bd3
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 23 deletions.
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);

Check failure on line 227 in md2man/roff.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed (gofumpt)
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);

Check failure on line 241 in md2man/roff.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed (gofumpt)
}
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;

Check failure on line 261 in md2man/roff.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed (gofumpt)
} 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

0 comments on commit f615bd3

Please sign in to comment.