|
| 1 | +// Copyright 2016 Google Inc. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package md |
| 16 | + |
| 17 | +import ( |
| 18 | + "bytes" |
| 19 | + "strings" |
| 20 | + |
| 21 | + "golang.org/x/net/html" |
| 22 | + "golang.org/x/net/html/atom" |
| 23 | +) |
| 24 | + |
| 25 | +var ( |
| 26 | + // headerLevel maps HTML tags to their level in parser.HeaderNode. |
| 27 | + // we -1 as H2 is a new step |
| 28 | + headerLevel = map[atom.Atom]int{ |
| 29 | + atom.H3: 2, |
| 30 | + atom.H4: 3, |
| 31 | + atom.H5: 4, |
| 32 | + atom.H6: 5, |
| 33 | + } |
| 34 | +) |
| 35 | + |
| 36 | +// isHeader returns true if hn is one of secondary headers. |
| 37 | +// Step header is not one of them. |
| 38 | +func isHeader(hn *html.Node) bool { |
| 39 | + _, ok := headerLevel[hn.DataAtom] |
| 40 | + return ok |
| 41 | +} |
| 42 | + |
| 43 | +func isMeta(hn *html.Node) bool { |
| 44 | + elem := strings.ToLower(hn.Data) |
| 45 | + return strings.HasPrefix(elem, metaDuration+metaSep) || strings.HasPrefix(elem, metaEnvironment+metaSep) |
| 46 | +} |
| 47 | + |
| 48 | +func isBold(hn *html.Node) bool { |
| 49 | + if hn.Type == html.TextNode { |
| 50 | + hn = hn.Parent |
| 51 | + } |
| 52 | + return hn.DataAtom == atom.Strong || |
| 53 | + hn.DataAtom == atom.B |
| 54 | +} |
| 55 | + |
| 56 | +func isItalic(hn *html.Node) bool { |
| 57 | + if hn.Type == html.TextNode { |
| 58 | + hn = hn.Parent |
| 59 | + } |
| 60 | + return hn.DataAtom == atom.Em || |
| 61 | + hn.DataAtom == atom.I |
| 62 | +} |
| 63 | + |
| 64 | +func isConsole(hn *html.Node) bool { |
| 65 | + if hn.Type == html.TextNode { |
| 66 | + hn = hn.Parent |
| 67 | + } |
| 68 | + return hn.DataAtom == atom.Code && hn.Parent.DataAtom != atom.Pre |
| 69 | +} |
| 70 | + |
| 71 | +func isCode(hn *html.Node) bool { |
| 72 | + if hn.Type == html.TextNode { |
| 73 | + hn = hn.Parent |
| 74 | + } |
| 75 | + return hn.DataAtom == atom.Code && hn.Parent.DataAtom == atom.Pre |
| 76 | +} |
| 77 | + |
| 78 | +func isButton(hn *html.Node) bool { |
| 79 | + // TODO: implements |
| 80 | + return false |
| 81 | +} |
| 82 | + |
| 83 | +func isInfobox(hn *html.Node) bool { |
| 84 | + if hn.DataAtom != atom.Dt { |
| 85 | + return false |
| 86 | + } |
| 87 | + return strings.ToLower(hn.FirstChild.Data) == "positive" || isInfoboxNegative(hn) |
| 88 | +} |
| 89 | + |
| 90 | +func isInfoboxNegative(hn *html.Node) bool { |
| 91 | + if hn.DataAtom != atom.Dt { |
| 92 | + return false |
| 93 | + } |
| 94 | + return strings.ToLower(hn.FirstChild.Data) == "negative" |
| 95 | +} |
| 96 | + |
| 97 | +func isSurvey(hn *html.Node) bool { |
| 98 | + if hn.DataAtom != atom.Dt { |
| 99 | + return false |
| 100 | + } |
| 101 | + return strings.ToLower(hn.FirstChild.Data) == "survey" |
| 102 | +} |
| 103 | + |
| 104 | +func isTable(hn *html.Node) bool { |
| 105 | + if hn.DataAtom != atom.Table { |
| 106 | + return false |
| 107 | + } |
| 108 | + return countTwo(hn, atom.Tr) > 1 || countTwo(hn, atom.Td) > 1 |
| 109 | +} |
| 110 | + |
| 111 | +func isList(hn *html.Node) bool { |
| 112 | + return hn.DataAtom == atom.Ul || hn.DataAtom == atom.Ol |
| 113 | +} |
| 114 | + |
| 115 | +// countTwo starts counting the number of a Atom children in hn. |
| 116 | +// It returns as soon as the count exceeds 1, so the returned value is inexact. |
| 117 | +// |
| 118 | +// The callers can test for > 1 to verify whether a node contains two |
| 119 | +// or more children of the Atom a. |
| 120 | +func countTwo(hn *html.Node, a atom.Atom) int { |
| 121 | + var count int |
| 122 | + for c := hn.FirstChild; c != nil; c = c.NextSibling { |
| 123 | + if c.DataAtom == a { |
| 124 | + count++ |
| 125 | + } else { |
| 126 | + count += countTwo(c, a) |
| 127 | + } |
| 128 | + if count > 1 { |
| 129 | + break |
| 130 | + } |
| 131 | + } |
| 132 | + return count |
| 133 | +} |
| 134 | + |
| 135 | +// countDirect returns the number of immediate children of hn. |
| 136 | +func countDirect(hn *html.Node) int { |
| 137 | + var count int |
| 138 | + for c := hn.FirstChild; c != nil; c = c.NextSibling { |
| 139 | + count++ |
| 140 | + } |
| 141 | + return count |
| 142 | +} |
| 143 | + |
| 144 | +// findAtom returns first child of root which matches a, nil otherwise. |
| 145 | +// It returns root if it is the same Atom as a. |
| 146 | +func findAtom(root *html.Node, a atom.Atom) *html.Node { |
| 147 | + if root.DataAtom == a { |
| 148 | + return root |
| 149 | + } |
| 150 | + for c := root.FirstChild; c != nil; c = c.NextSibling { |
| 151 | + if v := findAtom(c, a); v != nil { |
| 152 | + return v |
| 153 | + } |
| 154 | + } |
| 155 | + return nil |
| 156 | +} |
| 157 | + |
| 158 | +func findChildAtoms(root *html.Node, a atom.Atom) []*html.Node { |
| 159 | + var nodes []*html.Node |
| 160 | + for hn := root.FirstChild; hn != nil; hn = hn.NextSibling { |
| 161 | + if hn.DataAtom == a { |
| 162 | + nodes = append(nodes, hn) |
| 163 | + } |
| 164 | + nodes = append(nodes, findChildAtoms(hn, a)...) |
| 165 | + } |
| 166 | + return nodes |
| 167 | +} |
| 168 | + |
| 169 | +// findParent is like findAtom but search is in the opposite direction. |
| 170 | +// It is faster to look for parent than child lookup in findAtom. |
| 171 | +func findParent(root *html.Node, a atom.Atom) *html.Node { |
| 172 | + if root.DataAtom == a { |
| 173 | + return root |
| 174 | + } |
| 175 | + for c := root.Parent; c != nil; c = c.Parent { |
| 176 | + if c.DataAtom == a { |
| 177 | + return c |
| 178 | + } |
| 179 | + } |
| 180 | + return nil |
| 181 | +} |
| 182 | + |
| 183 | +var blockParents = map[atom.Atom]struct{}{ |
| 184 | + atom.H1: {}, |
| 185 | + atom.H2: {}, |
| 186 | + atom.H3: {}, |
| 187 | + atom.H4: {}, |
| 188 | + atom.H5: {}, |
| 189 | + atom.H6: {}, |
| 190 | + atom.Li: {}, |
| 191 | + atom.P: {}, |
| 192 | + atom.Div: {}, |
| 193 | +} |
| 194 | + |
| 195 | +// findBlockParent looks up nearest block parent node of hn. |
| 196 | +// For instance, block parent of "text" in <ul><li>text</li></ul> is <li>, |
| 197 | +// while block parent of "text" in <p><span>text</span></p> is <p>. |
| 198 | +func findBlockParent(hn *html.Node) *html.Node { |
| 199 | + for p := hn.Parent; p != nil; p = p.Parent { |
| 200 | + if _, ok := blockParents[p.DataAtom]; ok { |
| 201 | + return p |
| 202 | + } |
| 203 | + } |
| 204 | + return nil |
| 205 | +} |
| 206 | + |
| 207 | +// nodeAttr returns node attribute value of the key name. |
| 208 | +// Attribute keys are case insensitive. |
| 209 | +func nodeAttr(n *html.Node, name string) string { |
| 210 | + name = strings.ToLower(name) |
| 211 | + for _, a := range n.Attr { |
| 212 | + if strings.ToLower(a.Key) == name { |
| 213 | + return a.Val |
| 214 | + } |
| 215 | + } |
| 216 | + return "" |
| 217 | +} |
| 218 | + |
| 219 | +// stringifyNode extracts and concatenates all text nodes starting with root. |
| 220 | +// Line breaks are inserted at <br> and any non-<span> elements. |
| 221 | +func stringifyNode(root *html.Node, trim bool) string { |
| 222 | + if root.Type == html.TextNode { |
| 223 | + s := textCleaner.Replace(root.Data) |
| 224 | + if !trim { |
| 225 | + return s |
| 226 | + } |
| 227 | + return strings.TrimSpace(s) |
| 228 | + } |
| 229 | + if root.DataAtom == atom.Br && !trim { |
| 230 | + return "\n" |
| 231 | + } |
| 232 | + var buf bytes.Buffer |
| 233 | + for c := root.FirstChild; c != nil; c = c.NextSibling { |
| 234 | + if c.DataAtom == atom.Br { |
| 235 | + buf.WriteRune('\n') |
| 236 | + continue |
| 237 | + } |
| 238 | + if c.Type == html.TextNode { |
| 239 | + buf.WriteString(c.Data) |
| 240 | + continue |
| 241 | + } |
| 242 | + if c.DataAtom != atom.Span && c.DataAtom != atom.A { |
| 243 | + buf.WriteRune('\n') |
| 244 | + } |
| 245 | + buf.WriteString(stringifyNode(c, false)) |
| 246 | + } |
| 247 | + s := textCleaner.Replace(buf.String()) |
| 248 | + if !trim { |
| 249 | + return s |
| 250 | + } |
| 251 | + return strings.TrimSpace(s) |
| 252 | +} |
0 commit comments