-
Notifications
You must be signed in to change notification settings - Fork 0
/
xml2html.js
105 lines (90 loc) · 2.8 KB
/
xml2html.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
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
var jsdom = require('jsdom').jsdom,
myWindow = jsdom().createWindow(),
$ = require('jquery').create(),
jQuery = require('jquery').create(myWindow),
fs = require('fs'),
xml2js = require('xml2js');
var parser = new xml2js.Parser();
var args = process.argv.slice(2);
var xmlFile = args.shift();
if (typeof xmlFile == 'undefined') {
console.log('Please specify an XML file in argument 1.' + "\n");
console.log('USAGE: node xml2html.js input.xml output.html output2.html' + "\n");
process.exit();
}
if (!args.length) {
console.log('Please specify at least one HTML file to process.' + "\n");
console.log('USAGE: node xml2html.js input.xml output.html output2.html' + "\n");
process.exit();
}
readXML(xmlFile, function ($xml) {
args.forEach(function (htmlFile, index, array) {
readHTML(htmlFile, function ($html) {
writeHTML(htmlFile, compileNewHTML($html, $xml));
});
});
});
function compileNewHTML($html, $xml) {
var html = '<!DOCTYPE html>';
$html = loadMorphs($html, $xml);
$html.each(function () {
if (typeof $(this).html() != 'undefined') {
html += $(this).html();
}
});
return html;
}
function loadMorphs($html, $xml) {
//Each verse
$html.find('.verse').each(function (verseIndex) {
var $verse = $(this);
// console.log($verse.attr('data-osis'));
var $verseXML = $xml.find('verse[osisID="' + $verse.attr('data-osis') + '"]');
// console.log($verseXML.attr('osisID'));
//Each word
$verse.find('span.w[data-lemma^="H"]').each(function (wordIndex) {
var $word = $(this);
// console.log($word.attr('data-lemma'));
var $wordXML = $verseXML.find('w').filter(function(index) {
return index == wordIndex;
});
// console.log($wordXML.attr('morph'));
$word.attr('data-morph', $wordXML.attr('morph'));
}); //end word
// console.log($verse.html());
}); //end verse
console.log('Morphs applied.');
return $html;
}
function readXML(file, callback) {
fs.readFile(file, 'utf8', function (err, data) {
if (err) {
console.log('An error has occurred!' + "\n");
return console.log(err);
}
return parser.parseString(data, function (err, result) {
console.log(file + ' loaded and parsed.');
return callback($(data).find('div'));
});
});
}
function readHTML(file, callback) {
fs.readFile(file, 'utf8', function (err, data) {
if (err) {
console.log('An error has occurred!' + "\n");
return console.log(err);
}
console.log(file + ' loaded and parsed.');
return callback($(data));
});
}
function writeHTML(file, data, callback) {
fs.writeFile(file, data, function(err) {
if(err) {
console.log('An error has occurred!' + "\n");
console.log(err);
} else {
console.log(file + " saved.");
}
});
}