|
| 1 | +/** |
| 2 | + * @author Titus Wormer |
| 3 | + * @copyright 2016 Titus Wormer |
| 4 | + * @license MIT |
| 5 | + * @module hast:to-nlcst |
| 6 | + * @fileoverview Transform HAST to NLCST. |
| 7 | + */ |
| 8 | + |
| 9 | +'use strict'; |
| 10 | + |
| 11 | +/* Dependencies. */ |
| 12 | +var vfileLocation = require('vfile-location'); |
| 13 | +var toString = require('nlcst-to-string'); |
| 14 | +var position = require('unist-util-position'); |
| 15 | +var phrasing = require('hast-util-phrasing'); |
| 16 | +var embedded = require('hast-util-embedded'); |
| 17 | +var whitespace = require('hast-util-whitespace'); |
| 18 | +var textContent = require('hast-util-to-string'); |
| 19 | +var is = require('hast-util-is-element'); |
| 20 | + |
| 21 | +/* Expose. */ |
| 22 | +module.exports = toNLCST; |
| 23 | + |
| 24 | +/* Elements representing source. */ |
| 25 | +var SOURCE = ['code']; |
| 26 | +var IGNORE = ['script', 'style', 'svg', 'math', 'del']; |
| 27 | +var EXPLICIT = ['p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6']; |
| 28 | + |
| 29 | +/* Constants. */ |
| 30 | +var FLOW_ACCEPTING = [ |
| 31 | + 'body', |
| 32 | + 'article', |
| 33 | + 'section', |
| 34 | + 'blockquote', |
| 35 | + 'nav', |
| 36 | + 'aside', |
| 37 | + 'header', |
| 38 | + 'footer', |
| 39 | + 'address', |
| 40 | + 'li', |
| 41 | + 'dt', |
| 42 | + 'dd', |
| 43 | + 'figure', |
| 44 | + 'figcaption', |
| 45 | + 'div', |
| 46 | + 'main', |
| 47 | + 'caption', |
| 48 | + 'td', |
| 49 | + 'th', |
| 50 | + 'form', |
| 51 | + 'fieldset', |
| 52 | + 'details', |
| 53 | + 'dialog' |
| 54 | +]; |
| 55 | + |
| 56 | +/** |
| 57 | + * Transform `tree` into `nlcst`. |
| 58 | + * |
| 59 | + * @param {Node} tree - HAST node. |
| 60 | + * @param {File} file - Virtual file. |
| 61 | + * @param {Parser|Function} Parser - (Instance of) NLCST |
| 62 | + * parser. |
| 63 | + * @return {NLCSTNode} - NLCST. |
| 64 | + */ |
| 65 | +function toNLCST(tree, file, Parser) { |
| 66 | + var parser; |
| 67 | + var location; |
| 68 | + var results; |
| 69 | + var doc; |
| 70 | + |
| 71 | + /* Warn for invalid parameters. */ |
| 72 | + if (!tree || !tree.type) { |
| 73 | + throw new Error('hast-util-to-nlcst expected node'); |
| 74 | + } |
| 75 | + |
| 76 | + if (!file || !file.messages) { |
| 77 | + throw new Error('hast-util-to-nlcst expected file'); |
| 78 | + } |
| 79 | + |
| 80 | + /* Construct parser. */ |
| 81 | + if (!Parser) { |
| 82 | + throw new Error('hast-util-to-nlcst expected parser'); |
| 83 | + } |
| 84 | + |
| 85 | + if ( |
| 86 | + !position.start(tree).line || |
| 87 | + !position.start(tree).column |
| 88 | + ) { |
| 89 | + throw new Error('hast-util-to-nlcst expected position on nodes'); |
| 90 | + } |
| 91 | + |
| 92 | + location = vfileLocation(file); |
| 93 | + doc = String(file); |
| 94 | + parser = 'parse' in Parser ? Parser : new Parser(); |
| 95 | + |
| 96 | + /* Transform HAST into NLCST tokens, and pass these |
| 97 | + * into `parser.parse` to insert sentences, paragraphs |
| 98 | + * where needed. */ |
| 99 | + results = []; |
| 100 | + |
| 101 | + find(tree); |
| 102 | + |
| 103 | + return { |
| 104 | + type: 'root', |
| 105 | + children: results, |
| 106 | + position: { |
| 107 | + start: location.toPosition(0), |
| 108 | + end: location.toPosition(doc.length) |
| 109 | + } |
| 110 | + }; |
| 111 | + |
| 112 | + function find(node) { |
| 113 | + var children = node.children; |
| 114 | + |
| 115 | + if (node.type === 'root') { |
| 116 | + findAll(children); |
| 117 | + } else if (is(node) && !is(node, IGNORE)) { |
| 118 | + /* Explicit paragraph. */ |
| 119 | + if (is(node, EXPLICIT)) { |
| 120 | + add(node); |
| 121 | + /* Slightly simplified version of: |
| 122 | + * https://html.spec.whatwg.org/#paragraphs */ |
| 123 | + } else if (is(node, FLOW_ACCEPTING)) { |
| 124 | + implicit(flattenAll(children)); |
| 125 | + /* Dig deeper. */ |
| 126 | + } else { |
| 127 | + findAll(children); |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + function findAll(children) { |
| 133 | + var length = children.length; |
| 134 | + var index = -1; |
| 135 | + |
| 136 | + while (++index < length) { |
| 137 | + find(children[index]); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + function flatten(node) { |
| 142 | + if (is(node, ['a', 'ins', 'del', 'map'])) { |
| 143 | + return flattenAll(node.children); |
| 144 | + } |
| 145 | + |
| 146 | + return node; |
| 147 | + } |
| 148 | + |
| 149 | + function flattenAll(children) { |
| 150 | + var results = []; |
| 151 | + var length = children.length; |
| 152 | + var index = -1; |
| 153 | + |
| 154 | + while (++index < length) { |
| 155 | + results = results.concat(flatten(children[index])); |
| 156 | + } |
| 157 | + |
| 158 | + return results; |
| 159 | + } |
| 160 | + |
| 161 | + function add(node) { |
| 162 | + var result = ('length' in node ? all : one)(node); |
| 163 | + |
| 164 | + if (result.length) { |
| 165 | + results.push(parser.tokenizeParagraph(result)); |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + function implicit(children) { |
| 170 | + var length = children.length + 1; |
| 171 | + var index = -1; |
| 172 | + var viable = false; |
| 173 | + var start = -1; |
| 174 | + var child; |
| 175 | + |
| 176 | + while (++index < length) { |
| 177 | + child = children[index]; |
| 178 | + |
| 179 | + if (child && phrasing(child)) { |
| 180 | + if (start === -1) { |
| 181 | + start = index; |
| 182 | + } |
| 183 | + |
| 184 | + if (!viable && !embedded(child) && !whitespace(child)) { |
| 185 | + viable = true; |
| 186 | + } |
| 187 | + } else if (child && start === -1) { |
| 188 | + find(child); |
| 189 | + } else { |
| 190 | + (viable ? add : findAll)(children.slice(start, index)); |
| 191 | + |
| 192 | + if (child) { |
| 193 | + find(child); |
| 194 | + } |
| 195 | + |
| 196 | + viable = false; |
| 197 | + start = -1; |
| 198 | + } |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | + /* Convert `node` (HAST) to NLCST. */ |
| 203 | + function one(node) { |
| 204 | + var type = node.type; |
| 205 | + var tagName = type === 'element' ? node.tagName : null; |
| 206 | + var change; |
| 207 | + var replacement; |
| 208 | + |
| 209 | + if (type === 'text') { |
| 210 | + change = true; |
| 211 | + replacement = parser.tokenize(node.value); |
| 212 | + } else if (tagName === 'wbr') { |
| 213 | + change = true; |
| 214 | + replacement = [parser.tokenizeWhiteSpace(' ')]; |
| 215 | + } else if (tagName === 'br') { |
| 216 | + change = true; |
| 217 | + replacement = [parser.tokenizeWhiteSpace('\n')]; |
| 218 | + } else if (SOURCE.indexOf(tagName) !== -1) { |
| 219 | + change = true; |
| 220 | + replacement = [parser.tokenizeSource(textContent(node))]; |
| 221 | + } else if (type === 'root' || IGNORE.indexOf(tagName) === -1) { |
| 222 | + replacement = all(node.children); |
| 223 | + } else { |
| 224 | + return; |
| 225 | + } |
| 226 | + |
| 227 | + if (!change) { |
| 228 | + return replacement; |
| 229 | + } |
| 230 | + |
| 231 | + return patch(replacement, location, location.toOffset(position.start(node))); |
| 232 | + } |
| 233 | + |
| 234 | + /* Convert all `children` (HAST) to NLCST. */ |
| 235 | + function all(children) { |
| 236 | + var length = children && children.length; |
| 237 | + var index = -1; |
| 238 | + var result = []; |
| 239 | + var child; |
| 240 | + |
| 241 | + while (++index < length) { |
| 242 | + child = one(children[index]); |
| 243 | + |
| 244 | + if (child) { |
| 245 | + result = result.concat(child); |
| 246 | + } |
| 247 | + } |
| 248 | + |
| 249 | + return result; |
| 250 | + } |
| 251 | + |
| 252 | + /* Patch a position on each node in `nodes`. |
| 253 | + * `offset` is the offset in `file` this run of content |
| 254 | + * starts at. |
| 255 | + * |
| 256 | + * Note that NLCST nodes are concrete, meaning that their |
| 257 | + * starting and ending positions can be inferred from their |
| 258 | + * content. */ |
| 259 | + function patch(nodes, location, offset) { |
| 260 | + var length = nodes.length; |
| 261 | + var index = -1; |
| 262 | + var start = offset; |
| 263 | + var children; |
| 264 | + var node; |
| 265 | + var end; |
| 266 | + |
| 267 | + while (++index < length) { |
| 268 | + node = nodes[index]; |
| 269 | + children = node.children; |
| 270 | + |
| 271 | + if (children) { |
| 272 | + patch(children, location, start); |
| 273 | + } |
| 274 | + |
| 275 | + end = start + toString(node).length; |
| 276 | + |
| 277 | + node.position = { |
| 278 | + start: location.toPosition(start), |
| 279 | + end: location.toPosition(end) |
| 280 | + }; |
| 281 | + |
| 282 | + start = end; |
| 283 | + } |
| 284 | + |
| 285 | + return nodes; |
| 286 | + } |
| 287 | +} |
0 commit comments