-
Notifications
You must be signed in to change notification settings - Fork 1
/
pdfs.js
187 lines (148 loc) · 5.36 KB
/
pdfs.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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
const pdfjsLib = require('pdfjs-dist');
const fs = require('fs');
const PDF_PATH = './pdf/';
const TXT_PATH = './documents/';
/*** READ PDF *************************************************************************************/
function readPage (doc, i) {
return doc.getPage(i)
.then(page => page.getTextContent())
.then(content => content.items.map(item => item.str));
}
function readPages (doc, file) {
const promises = Array
.from({ length: doc.numPages }, (_, i) => i + 1)
.map(i => readPage(doc, i));
return Promise.all(promises);
}
function concatPages (pages) {
return pages.reduce((a, b) => a.concat(b)).join('\n');
}
function extract (file) {
return pdfjsLib
.getDocument({ url: `${PDF_PATH}${file.name}.pdf`, useSystemFonts: true })
.promise
.then(doc => readPages(doc, file))
.then(concatPages)
.then(txt => cleanup(txt, file))
.then(txt => save(file, txt));
}
/*** READ PDF *************************************************************************************/
/*** SAVE OUTPUT **********************************************************************************/
function save (file, text) {
fs.writeFile(`${TXT_PATH}${file.name}.txt`, text, err => {
if (err) return console.log(err);
console.log(`- ${file.name} saved`);
});
}
/*** SAVE OUTPUT **********************************************************************************/
/*** CLEANUP FUNCTIONS ****************************************************************************/
function removeDates (text) {
return text
.replace(/(\d{1,2}\/\d{1,2}\/\d{4})/g, '') // dd/mm/yyyy
.replace(/(\d{4}\-\d{2}\-\d{2})/g, ''); // yyyy-mm-dd
}
function removeEmptyLines (text) {
const lines = text.split('\n');
const newLines = [];
lines.forEach(line => {
line = line.trim();
if (line.length >= 2) newLines.push(line);
else if (line.match(/[=<>{}()]/)) newLines.push(line);
});
return newLines.join('\n');
}
function correctMultiline (text) {
return text
.replace(/\/\n\//g, '//') // remove line breaks between / and /
.replace(/\n\./g, '.') // remove line breaks before .
.replace(/->\n/g, '-> ') // remove line breaks after ->
.replace(/\)\n{/g, ') {') // remove line breaks between ) and {
.replace(/\(\n/g, '(').replace(/\n\)/g, ')') // remove line breaks around ()
.replace(/\-\n/g, '-').replace(/\n\-/g, '-') // remove line breaks around -
.replace(/\n,/g, ',').replace(/,\n/g, ', ') // remove line breaks around ,
.replace(/\n:/g, ':').replace(/:\n/g, ': ') // remove line breaks around :
.replace(/\n=/g, ' =').replace(/=\n/g, '= ') // remove line breaks around =
}
function removeWeirdCharacters (text) {
return text.replace(/ /g, 'fi');
}
function trimText (text, file) {
const start = file.startFrom ? text.indexOf(file.startFrom) : 0;
const end = file.endAt ? text.indexOf(file.endAt, start) : text.length;
return text.substring(start, end).trim();
}
function dropWords (text, file) {
file.dropWords = file.dropWords || [];
const drops = [
'This is custom documentation. For more information, please visit the\nSAP Help Portal\n',
...file.dropWords
];
drops.forEach(word => {
text = text.replace(new RegExp(word, 'g'), '');
});
return text.trim();
}
function cleanup (text, file) {
text = text.normalize();
text = removeEmptyLines(text);
text = correctMultiline(text);
text = removeDates(text);
text = removeWeirdCharacters(text);
text = trimText(text, file);
text = dropWords(text, file);
text = removeEmptyLines(text);
return text;
}
/*** CLEANUP FUNCTIONS ****************************************************************************/
const files = [
{
name: 'SBASSupportComponents',
startFrom: 'CA-BAS',
endAt: 'This is custom documentation',
dropWords: ['Description', 'Component']
},
{
name: 'SBASDevGuide',
startFrom: 'Developer Guide\nWith SAP Business Application Studio',
endAt: 'Troubleshooting\nYou can find solutions for common troubleshooting issues in SAP Business Application Studio',
},
{
name: 'SBAS_components',
startFrom: 'BAS - SNOW Components',
dropWords: [
'Root\nComponent\n',
'Sub Component\n',
'Description\n',
'Selectable/Not\nSelectable\n',
'Not selectable\n',
'internal\n',
]
},
{
name: 'SAPBTPConnectivityandSupport',
startFrom: 'Overview\nSAP',
dropWords: [
'Back to T\nasks\n',
'Back to Procedure\n',
]
},
{
name: 'FioriToolsDocs',
startFrom: 'Develop SAP Fiori Applications with SAP Fiori Tools',
},
{
name: 'CloudConnector',
startFrom: 'Overview\nSAP',
dropWords: [
'Back to T\nasks\n',
'Back to Procedure\n',
'Back to\nOperations\n',
'Back to\nop\n',
]
},
{
name: 'BTPSupportComponents',
startFrom: 'Support Components\n',
},
];
files.forEach(extract);