-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConvertToProperJSON.js
98 lines (90 loc) · 3.65 KB
/
ConvertToProperJSON.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
var fs = require("fs");
var filename = "agt2";
var sorted = require(`./export/${filename}.json`);
var reg = /(\s{2,})/g;
const trimSpaces = param =>
new Promise((resolve, reject) => {
var res = param.replace(reg, " ");
resolve(res);
});
// fs.writeFileSync('/lastversion.json', sorted.)
var book = [];
var section = { title: "", text: "" };
for (let index = 0; index < sorted.length; index++) {
for (let y = 0; y < sorted[index].texts.length; y++) {
//firts check fontName of title, whenever title comes, we add it first with "---" to find and manupilate later easily.
if (sorted[index].texts[y].fontSize === 21.011116) {
if (!((sorted[index].texts[y - 1]
? sorted[index].texts[y - 1].fontSize
: 24.763102) === 21.011116 &&
(sorted[index].texts[y + 1]
? sorted[index].texts[y + 1].fontSize
: 24.763102 === 21.011116)
)) {
book.push(section);
section = { title: "", text: "" };
section.title = sorted[index].texts[y].text;
} else {
if (
sorted[index].texts[y + 1] &&
sorted[index].texts[y + 1].fontSize === 21.011116
) {
section.title =section.title + sorted[index].texts[y].text+' '; //
} else {
section.title = section.title + sorted[index].texts[y].text+' ';
}
}
} else if (sorted[index].texts[y].fontName === "g_d0_f6") {
section.text += sorted[index].texts[y].text;
} else {
section.text += sorted[index].texts[y].text + " ";
}
}
}
// console.log((book[12].slice(0,book[12].indexOf('-end')+4)).slice(0,book[12].indexOf('-end')));
// console.log(book[12].slice(book[12].indexOf('-end')+4,book[12].length));
//
fs.writeFileSync(
`./export/${filename}-Ready.json`,
JSON.stringify(
book.map(part => {
part.text = part.text.replace(reg, " ");
part.title = part.title.replace(reg, " ");
return part;
})
)
);
//---------------------------------------------------------------------------------------------------------------------------------
// var fs = require('fs');
// var filename="ff5sorted"
// var sorted = require(`./${filename}.json`);
// // fs.writeFileSync('/lastversion.json', sorted.)
// var book = [];
// var section = '';
// var reg =/(\s{2,})/g
// for (let index = 0; index < sorted.length; index++) {
// // for (let index = 0; index <10; index++) {
// for (let y = 0; y < sorted[index].texts.length; y++) {
// //firts check fontName of title, whenever title comes, we add it first with "---" to find and manupilate later easily.
// if (sorted[index].texts[y].fontName === "g_d0_f3") {
// if ((sorted[index].texts[y-1]&& sorted[index].texts[y-1].fontName !== "g_d0_f3")&& (sorted[index].texts[y+1]&& sorted[index].texts[y+1].fontName !== "g_d0_f3")) {
// book.push(section);
// section =sorted[index].texts[y].text+"-end";
// }else{
// if(sorted[index].texts[y+1]&& sorted[index].texts[y+1].fontName === "g_d0_f3"){
// section =sorted[index].texts[y].text; //
// }else{
// section =section+ sorted[index].texts[y].text+"-end";
// }
// }
// } else if(sorted[index].texts[y].fontName === "g_d0_f5") {
// section += sorted[index].texts[y].text;
// }else{
// section += sorted[index].texts[y].text+' ';
// }
// }
// }
// // console.log((book[12].slice(0,book[12].indexOf('-end')+4)).slice(0,book[12].indexOf('-end')));
// // console.log(book[12].slice(book[12].indexOf('-end')+4,book[12].length));
// //
// fs.writeFileSync(`./${filename}ready.json`, JSON.stringify(book.map(section=>section.replace(reg,' '))));