Skip to content

Commit 9414b4c

Browse files
committedJul 13, 2021
Fix missing carriage return before .TE if last table cell is empty
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>
1 parent 9654f2a commit 9414b4c

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed
 

‎md2man/roff.go

+12-4
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ func (r *roffRenderer) handleItem(w io.Writer, node *blackfriday.Node, entering
259259
func (r *roffRenderer) handleTable(w io.Writer, node *blackfriday.Node, entering bool) {
260260
if entering {
261261
out(w, tableStart)
262-
//call walker to count cells (and rows?) so format section can be produced
262+
// call walker to count cells (and rows?) so format section can be produced
263263
columns := countColumns(node)
264264
out(w, strings.Repeat("l ", columns)+"\n")
265265
out(w, strings.Repeat("l ", columns)+".\n")
@@ -283,9 +283,17 @@ func (r *roffRenderer) handleTableCell(w io.Writer, node *blackfriday.Node, ente
283283
out(w, start)
284284
}
285285
} else {
286-
// need to carriage return if we are at the end of the header row
287-
if node.IsHeader && node.Next == nil {
288-
end = end + crTag
286+
if node.Next == nil {
287+
if node.IsHeader {
288+
// need to carriage return if we are at the end of the header row
289+
end = end + crTag
290+
} else if node.FirstChild == nil {
291+
// empty cell: need to carriage return if we are at the end of
292+
// the table, because handleText() will not be called if there's
293+
// no text to preocess (which would otherwise add the trailing
294+
// carriage return)
295+
end = crTag
296+
}
289297
}
290298
out(w, end)
291299
}

‎md2man/roff_test.go

+23
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,29 @@ robin red.
272272
doTestsInlineParam(t, tests, TestParams{blackfriday.Tables})
273273
}
274274

275+
func TestTableWithEmptyCell(t *testing.T) {
276+
var tests = []string{
277+
`
278+
| Col1 | Col2 | Col3 |
279+
|:---------|:-----:|:----:|
280+
| row one | | |
281+
| row two | x | |
282+
`,
283+
`.nh
284+
285+
.TS
286+
allbox;
287+
l l l
288+
l l l .
289+
\fB\fCCol1\fR \fB\fCCol2\fR \fB\fCCol3\fR
290+
row one
291+
row two x
292+
.TE
293+
`,
294+
}
295+
doTestsInlineParam(t, tests, TestParams{blackfriday.Tables})
296+
}
297+
275298
func TestLinks(t *testing.T) {
276299
var tests = []string{
277300
"See [docs](https://docs.docker.com/) for\nmore",

0 commit comments

Comments
 (0)
Please sign in to comment.