forked from certsimple/rosetta-stone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhtml-to-json.js
executable file
·65 lines (58 loc) · 1.75 KB
/
html-to-json.js
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
#!/usr/bin/env node
var marked = require('marked'),
fs = require('fs'),
jsdom = require("jsdom"),
multiline = require('multiline');
require('es6-shim')
var DEBUG = false
var log;
if ( DEBUG ) {
log = console.log.bind(console);
} else {
log = function(){}
}
module.exports = function(originalHTML, cb){
jsdom.env(originalHTML, ["http://code.jquery.com/jquery.js"], function (errors, window) {
var $ = window.$;
var data = {}
var firstSection = $('h2').each(function(sectionIndex, sectionElement){
var $sectionElement = $(sectionElement)
var sectionTitle = $sectionElement.text()
log('\n\nMAKING SECTION AND TABLE FOR', sectionTitle)
data[sectionTitle] = {}
var currentRow = 0
var currentCell = 0
var rowName
var previousCellContents;
$sectionElement.nextUntil('h2').each(function(nonSectionIndex, element){
var $element = $(element)
var elementText = $element.text()
if ( $element.is('h3') ) {
// We're done with previous row, so add it
rowName = elementText
data[sectionTitle][rowName] = {}
log(' MAKING ROW FOR', rowName)
currentRow++
} else {
if ( $element.is('h4') ) {
cellName = elementText
data[sectionTitle][rowName][cellName] = []
currentCell++
} else {
if ( elementText.toLowerCase().includes('as above') ) {
data[sectionTitle][rowName][cellName] = previousCellContents
return
}
log(' ADDING TO CELL', currentCell, ':', elementText)
data[sectionTitle][rowName][cellName].push('<p>'+$element.html()+'</p>')
// Commands might be the same, but don't copy references
if ( ! $element.is('a') ) {
previousCellContents = data[sectionTitle][rowName][cellName]
}
}
}
})
})
cb(data)
});
}