Skip to content

Commit c2c0656

Browse files
authored
Merge pull request #111 from cpuguy83/tbl_preprocessor
Prepend table preprocessor
2 parents b597a58 + b262760 commit c2c0656

File tree

2 files changed

+45
-31
lines changed

2 files changed

+45
-31
lines changed

md2man/roff.go

Lines changed: 39 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -21,34 +21,35 @@ type roffRenderer struct {
2121
}
2222

2323
const (
24-
titleHeader = ".TH "
25-
topLevelHeader = "\n\n.SH "
26-
secondLevelHdr = "\n.SH "
27-
otherHeader = "\n.SS "
28-
crTag = "\n"
29-
emphTag = "\\fI"
30-
emphCloseTag = "\\fP"
31-
strongTag = "\\fB"
32-
strongCloseTag = "\\fP"
33-
breakTag = "\n.br\n"
34-
paraTag = "\n.PP\n"
35-
hruleTag = "\n.ti 0\n\\l'\\n(.lu'\n"
36-
linkTag = "\n\\[la]"
37-
linkCloseTag = "\\[ra]"
38-
codespanTag = "\\fB"
39-
codespanCloseTag = "\\fR"
40-
codeTag = "\n.EX\n"
41-
codeCloseTag = "\n.EE\n"
42-
quoteTag = "\n.PP\n.RS\n"
43-
quoteCloseTag = "\n.RE\n"
44-
listTag = "\n.RS\n"
45-
listCloseTag = "\n.RE\n"
46-
dtTag = "\n.TP\n"
47-
dd2Tag = "\n"
48-
tableStart = "\n.TS\nallbox;\n"
49-
tableEnd = ".TE\n"
50-
tableCellStart = "T{\n"
51-
tableCellEnd = "\nT}\n"
24+
titleHeader = ".TH "
25+
topLevelHeader = "\n\n.SH "
26+
secondLevelHdr = "\n.SH "
27+
otherHeader = "\n.SS "
28+
crTag = "\n"
29+
emphTag = "\\fI"
30+
emphCloseTag = "\\fP"
31+
strongTag = "\\fB"
32+
strongCloseTag = "\\fP"
33+
breakTag = "\n.br\n"
34+
paraTag = "\n.PP\n"
35+
hruleTag = "\n.ti 0\n\\l'\\n(.lu'\n"
36+
linkTag = "\n\\[la]"
37+
linkCloseTag = "\\[ra]"
38+
codespanTag = "\\fB"
39+
codespanCloseTag = "\\fR"
40+
codeTag = "\n.EX\n"
41+
codeCloseTag = "\n.EE\n"
42+
quoteTag = "\n.PP\n.RS\n"
43+
quoteCloseTag = "\n.RE\n"
44+
listTag = "\n.RS\n"
45+
listCloseTag = "\n.RE\n"
46+
dtTag = "\n.TP\n"
47+
dd2Tag = "\n"
48+
tableStart = "\n.TS\nallbox;\n"
49+
tableEnd = ".TE\n"
50+
tableCellStart = "T{\n"
51+
tableCellEnd = "\nT}\n"
52+
tablePreprocessor = `'\" t`
5253
)
5354

5455
// NewRoffRenderer creates a new blackfriday Renderer for generating roff documents
@@ -75,6 +76,16 @@ func (r *roffRenderer) GetExtensions() blackfriday.Extensions {
7576

7677
// RenderHeader handles outputting the header at document start
7778
func (r *roffRenderer) RenderHeader(w io.Writer, ast *blackfriday.Node) {
79+
// We need to walk the tree to check if there are any tables.
80+
// If there are, we need to enable the roff table preprocessor.
81+
ast.Walk(func(node *blackfriday.Node, entering bool) blackfriday.WalkStatus {
82+
if node.Type == blackfriday.Table {
83+
out(w, tablePreprocessor+"\n")
84+
return blackfriday.Terminate
85+
}
86+
return blackfriday.GoToNext
87+
})
88+
7889
// disable hyphenation
7990
out(w, ".nh\n")
8091
}

md2man/roff_test.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,8 @@ func TestTable(t *testing.T) {
263263
| zebra | Sometimes black and sometimes white, depending on the stripe. |
264264
| robin | red. |
265265
`,
266-
`.nh
266+
`'\" t
267+
.nh
267268
268269
.TS
269270
allbox;
@@ -292,7 +293,8 @@ func TestTableWithEmptyCell(t *testing.T) {
292293
| row one | | |
293294
| row two | x | |
294295
`,
295-
`.nh
296+
`'\" t
297+
.nh
296298
297299
.TS
298300
allbox;
@@ -320,7 +322,8 @@ func TestTableWrapping(t *testing.T) {
320322
| row six | A line that's longer than 30 characters with inline ` + "`code markup`" + ` or _cursive_ should not wrap. |
321323
| row seven | Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent eu ipsum eget tortor aliquam accumsan. Quisque ac turpis convallis, sagittis urna ac, tempor est. Mauris nibh arcu, hendrerit id eros sed, sodales lacinia ex. Suspendisse sed condimentum urna, vitae mattis lectus. Mauris imperdiet magna vel purus pretium, id interdum libero. |
322324
`,
323-
`.nh
325+
`'\" t
326+
.nh
324327
325328
.TS
326329
allbox;

0 commit comments

Comments
 (0)