forked from rsheldon3ayers/insert-page-breaks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
136 lines (97 loc) · 4.45 KB
/
Copy pathindex.js
File metadata and controls
136 lines (97 loc) · 4.45 KB
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
"use strict";
const path = require('path');
const fs = require("fs");
const fse = require('fs-extra')
const cheerio = require('cheerio');
const natural = require('natural');
const list = require("./lib/unfoldList");
const lib = path.resolve('./', 'lib');
const project = path.resolve(lib, 'project');
let isbn = '9781535922791'
let pdf = `./pdfs/${isbn}/`
let dir = `../../EPUB Projects/DefenseDoctrine/dev/epub/${isbn}/OEBPS/text`
const isDirectory = source => fs.lstatSync(source).isDirectory()
const getDirectories = source =>
fs.readdirSync(source).map(name => path.join(source, name)).filter(isDirectory)
let html;
let files = fs.readdirSync(dir)
files.forEach(file => {
let html = fs.readFileSync(path.join(dir, file), 'utf-8')
if (html) insertPageBreaks(html, file)
});
function insertPageBreaks(html, fileName) {
const $ = cheerio.load(html, {
xmlMode: true,
decodeEntities: false
});
let highestScore = 0;
let finalNode;
list.firstSentences.forEach((testString) => {
$('span,p,h\d+,li').filter(function () {
if (formatStrings($(this).text()).match(new RegExp(formatStrings(testString), "gi"))) {
writePageBreak($(this), testString);
} else return;
})
})
function writePageBreak(pageBreakLoc, testString) {
let isAtextNode = 3;
let textNodes = pageBreakLoc.contents().filter(function () {
return this.nodeType == isAtextNode;
});
let contentsOfTextNodes = [];
textNodes.each((index, node) => {
contentsOfTextNodes[index] = node.data;
})
contentsOfTextNodes.forEach((str, i, item) => {
if (item.toString().length <= testString.length) {
if (formatStrings(testString).match(new RegExp(formatStrings(item.toString()), "gi"))) {
getScore(natural.JaroWinklerDistance(testString, item.toString()), textNodes[i])
highestScore = natural.JaroWinklerDistance(testString, item.toString())
}
} else if (item.toString().length >= testString.length) {
if (formatStrings(item.toString()).match(new RegExp(formatStrings(testString), "gi"))) {
getScore(natural.JaroWinklerDistance(testString, item.toString()), textNodes[i])
highestScore = natural.JaroWinklerDistance(testString, item.toString())
}
} else return;
})
if (finalNode) writeResults(finalNode, highestScore, testString, pageBreakLoc)
}
function getScore(score, node) {
highestScore < score ? finalNode = node : finalNode = finalNode;
}
function writeResults(i, results, testString, pageBreakLoc) {
let pageNumIndex = list.firstSentences.indexOf(testString);
let pageIndex = pageBreakLoc.html().indexOf(testString);
let placeNum = `${list.pageNumbers[pageNumIndex]}`;
let nodeString = [];
if (pageBreakLoc.html().indexOf(`${placeNum}`) > -1) {
console.log(pageBreakLoc.html().indexOf(`${placeNum}`), `${fileName}`)
return false;
} else {
console.log(`placing into ${fileName}`)
let nodeString = [];
if (finalNode.data.indexOf(testString) > -1) {
nodeString = [finalNode.data.slice(0, finalNode.data.indexOf(testString)), ` ${placeNum}`, finalNode.data.slice(finalNode.data.indexOf(testString))].join('')
finalNode.data = nodeString;
} else if (testString.indexOf(finalNode.data) > -1) {
let finalString = [pageBreakLoc.html().slice(0, pageIndex), ` ${placeNum}`, pageBreakLoc.html().slice(pageIndex)].join('');
pageBreakLoc.html(finalString);
} else {
if (pageBreakLoc.html().match(formatStrings(testString)) > -1) {
let finalString = [pageBreakLoc.html().slice(0, pageIndex), `${placeNum}`, pageBreakLoc.html().slice(pageIndex)].join('');
pageBreakLoc.html(finalString);
}
}
}
let newHTML = $.html();
fs.writeFileSync(`${pdf}/finish/${fileName}`, newHTML, {
encoding: 'utf8'
});
}
function formatStrings(str) {
str = str.replace(/(\(|\?|\)|\“|\”|~|`|\[|\]|!|@|#|\$|%|\^|–|&|\*|\(|\)|\)|\{|\}|\[|\]|;|:|\"|’|<|,|\.|>|\?|\/|\\|\||-|_|\+|=|\s)/g, "")
return str;
}
}
fse.emptyDir('./sanitized')