forked from JohannesKaufmann/html-to-markdown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtable.go
184 lines (159 loc) · 4.47 KB
/
table.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package plugin
import (
"strings"
md "github.com/JohannesKaufmann/html-to-markdown"
"github.com/PuerkitoBio/goquery"
)
// TableCompat is a compatibility plugon for environments where
// only commonmark markdown (without Tables) is supported.
//
// Note: In an environment that supports "real" Tables, like GitHub's Flavored Markdown
// use `plugin.Table()` instead.
func TableCompat() md.Plugin {
return func(c *md.Converter) []md.Rule {
return []md.Rule{
{
Filter: []string{"td", "th"},
Replacement: func(content string, selec *goquery.Selection, opt *md.Options) *string {
content = strings.TrimSpace(content)
if content == "" {
return &content
}
next := selec.Next()
nextIsEmpty := strings.TrimSpace(next.Text()) == ""
if (next.Is("td") || next.Is("th")) && !nextIsEmpty {
content = content + " · "
}
return &content
},
},
{
Filter: []string{"tr"},
Replacement: func(content string, selec *goquery.Selection, opt *md.Options) *string {
content = content + "\n\n"
return &content
},
},
}
}
}
// Table converts a html table (using hyphens and pipe characters) to a
// visuall representation in markdown.
//
// Note: This Plugin overrides the default compatibility rules from `commonmark.go`.
// Only use this Plugin in an environment that has extendeded the normal syntax,
// like GitHub's Flavored Markdown.
func Table() md.Plugin {
return func(c *md.Converter) []md.Rule {
c.Before(func(selec *goquery.Selection) {
selec.Find("caption").Each(func(i int, s *goquery.Selection) {
parent := s.Parent()
if !parent.Is("table") {
return
}
// move the caption from inside the table to after the table
parent.AfterSelection(s)
})
})
return []md.Rule{
{
Filter: []string{"table"},
Replacement: func(content string, selec *goquery.Selection, opt *md.Options) *string {
noHeader := selec.Find("thead").Length() == 0 && selec.Find("th").Length() == 0
if noHeader {
var maxCount int
selec.Find("tr").Each(func(i int, s *goquery.Selection) {
count := s.Children().Length()
if count > maxCount {
maxCount = count
}
})
// add an empty header, so that the table is recognized.
header := "|" + strings.Repeat(" |", maxCount)
divider := "|" + strings.Repeat(" --- |", maxCount)
content = header + "\n" + divider + content
}
content = "\n\n" + content + "\n\n"
return &content
},
},
{ // TableCell
Filter: []string{"th", "td"},
Replacement: func(content string, selec *goquery.Selection, opt *md.Options) *string {
return md.String(getCellContent(content, selec))
},
},
{ // TableRow
Filter: []string{"tr"},
Replacement: func(content string, selec *goquery.Selection, opt *md.Options) *string {
borderCells := ""
if isHeadingRow(selec) {
selec.Children().Each(func(i int, s *goquery.Selection) {
border := "---"
if align, ok := s.Attr("align"); ok {
switch align {
case "left":
border = ":--"
case "right":
border = "--:"
case "center":
border = ":-:"
}
}
borderCells += getCellContent(border, s)
})
}
text := "\n" + content
if borderCells != "" {
text += "\n" + borderCells
}
return &text
},
},
}
}
}
// A tr is a heading row if:
// - the parent is a THEAD
// - or if its the first child of the TABLE or the first TBODY (possibly
// following a blank THEAD)
// - and every cell is a TH
func isHeadingRow(s *goquery.Selection) bool {
parent := s.Parent()
if goquery.NodeName(parent) == "thead" {
return true
}
isTableOrBody := parent.Is("table") || isFirstTbody(parent)
everyTH := true
s.Children().Each(func(i int, s *goquery.Selection) {
if goquery.NodeName(s) != "th" {
everyTH = false
}
})
if parent.Children().First().IsSelection(s) && isTableOrBody && everyTH {
return true
}
return false
}
func isFirstTbody(s *goquery.Selection) bool {
firstSibling := s.Siblings().Eq(0) // TODO: previousSibling
if s.Is("tbody") && firstSibling.Length() == 0 {
return true
}
return false
}
func getCellContent(content string, s *goquery.Selection) string {
content = strings.TrimSpace(content)
index := -1
for i, node := range s.Parent().Children().Nodes {
if s.IsNodes(node) {
index = i
break
}
}
prefix := " "
if index == 0 {
prefix = "| "
}
return prefix + content + " |"
}