Skip to content

Commit

Permalink
Fix missing carriage return before .TE if last table cell is empty
Browse files Browse the repository at this point in the history
This is an alternative to 1fc61ec, which
tried to address this same issue, but introduced a regression.

Before this, tables ending with an empty cell would miss a carriage return
before the table closing marker (.TE), causing the table to not be closed,
and rendering to be broken;

       ┌──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┬─────────────────────┬─────────┐
       │Col1                                                                                                                                                                                          │ Co2                │ Col3    │
       ├──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┼─────────────────────┼─────────┤
       │row one                                                                                                                                                                                       │                     │ row two │
       ├──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┼─────────────────────┼─────────┤
       │                                                                                                                                                                                              │                     │         │
       ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┼─────────────────────┼─────────┤
       │                                                                                                                                                                                              │                     │         │
       ├──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┼─────

After this patch, tables ending with an empty cell get a carriage return added
before the closing marker:

       ┌────────┬──────┬──────┐
       │Col1    │ Col2 │ Col3 │
       ├────────┼──────┼──────┤
       │row one │      │      │
       ├────────┼──────┼──────┤
       │row two │ x    │      │
       └────────┴──────┴──────┘

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
  • Loading branch information
thaJeztah committed Jul 13, 2021
1 parent 9654f2a commit 9414b4c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
16 changes: 12 additions & 4 deletions md2man/roff.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ func (r *roffRenderer) handleItem(w io.Writer, node *blackfriday.Node, entering
func (r *roffRenderer) handleTable(w io.Writer, node *blackfriday.Node, entering bool) {
if entering {
out(w, tableStart)
//call walker to count cells (and rows?) so format section can be produced
// call walker to count cells (and rows?) so format section can be produced
columns := countColumns(node)
out(w, strings.Repeat("l ", columns)+"\n")
out(w, strings.Repeat("l ", columns)+".\n")
Expand All @@ -283,9 +283,17 @@ func (r *roffRenderer) handleTableCell(w io.Writer, node *blackfriday.Node, ente
out(w, start)
}
} else {
// need to carriage return if we are at the end of the header row
if node.IsHeader && node.Next == nil {
end = end + crTag
if node.Next == nil {
if node.IsHeader {
// need to carriage return if we are at the end of the header row
end = end + crTag
} else if node.FirstChild == nil {
// empty cell: need to carriage return if we are at the end of
// the table, because handleText() will not be called if there's
// no text to preocess (which would otherwise add the trailing
// carriage return)
end = crTag
}
}
out(w, end)
}
Expand Down
23 changes: 23 additions & 0 deletions md2man/roff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,29 @@ robin red.
doTestsInlineParam(t, tests, TestParams{blackfriday.Tables})
}

func TestTableWithEmptyCell(t *testing.T) {
var tests = []string{
`
| Col1 | Col2 | Col3 |
|:---------|:-----:|:----:|
| row one | | |
| row two | x | |
`,
`.nh

.TS
allbox;
l l l
l l l .
\fB\fCCol1\fR \fB\fCCol2\fR \fB\fCCol3\fR
row one
row two x
.TE
`,
}
doTestsInlineParam(t, tests, TestParams{blackfriday.Tables})
}

func TestLinks(t *testing.T) {
var tests = []string{
"See [docs](https://docs.docker.com/) for\nmore",
Expand Down

0 comments on commit 9414b4c

Please sign in to comment.