-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
98 lines (93 loc) · 3.56 KB
/
index.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
const fs = require('fs/promises');
const glob = require('glob');
exports.parseTweeString = (str, extraLinkParsers) => {
let ret = [];
let passageSpit = str.split('\n::');
passageSpit[0] = passageSpit[0].slice(2);
for(let i = 0; i < passageSpit.length; i++) {
let title = passageSpit[i].slice(0, passageSpit[i].indexOf('\n'));
let text = passageSpit[i].substring(title.length + 1).trim();
let start = title.indexOf('[');
let tags = [];
if(start !== -1) {
//Have tags...
let tagStr = title.substr(start+1, title.indexOf(']')-(start+1));
tags = tagStr.split(' ');
title = title.substr(0, start-1);
} else {
//Might still have other stuff...
start = title.indexOf('{');
if(start !== -1) {
title = title.substr(0, start-1);
}
}
let links = getLinks(text);
if(extraLinkParsers !== undefined){
if(!Array.isArray(extraLinkParsers)) {
extraLinkParsers = [extraLinkParsers];
}
for(let j = 0; j < extraLinkParsers.length; j++) {
links = extraLinkParsers[j](text, links);
}
}
title = title.substr(1);
title = title.trim();
ret.push({title: title, tags: tags, links: links});
}
return Promise.resolve(ret);
}
exports.parseTweeFile = (filename, extraLinkParsers) => {
return new Promise((resolve, reject) => {
fs.readFile(filename, 'utf8').then((buff) => {
let str = buff.toString();
exports.parseTweeString(str, extraLinkParsers).then((passages) => {
for(let i = 0; i < passages.length; i++) {
passages[i].filename = filename;
}
resolve(passages);
}).catch(reject);
}).catch(reject);
});
}
exports.parseTweeFolder = (path, extraLinkParsers) => {
let files = glob.sync(path+'/**/*.tw');
let promises = [];
for(let i = 0; i < files.length; i++) {
promises.push(exports.parseTweeFile(files[i], extraLinkParsers));
}
let res = [];
return new Promise((resolve, reject) => {
Promise.allSettled(promises).then((results) => {
for(let i = 0; i < results.length; i++) {
let values = results[i].value;
res = res.concat(values);
}
resolve(res);
});
});
}
const regexes = [
/\[\[([^|\]]+)\|?([^\]]+)?\]\[?([^\]]+)?\]\]?/, /* This matches standard Twine Links */
/<<link "?([^(">)]+)"? "?([^(">)]+)"?>>/, /* This matches SugarCube <<link>>s */
/<<goto "?([^(">)]+)"?>>/, /* This matches SugarCube <<goto>>s */
/<<onetimelink "?([^(">)]+)"? "?([^(">)]+)"?>>/ /* This matches my custom <<onetimelink>>s */
];
function getLinks(str) {
let links = [];
for(let i = 0; i < regexes.length; i++) {
let globRegEx = new RegExp(regexes[i], 'gm');
let globMatches = str.match(globRegEx);
if(globMatches === null) {
continue;
}
for(let j = 0; j < globMatches.length; j++) {
let matches = globMatches[j].match(regexes[i]);
if(matches.length >= 3 && matches[2] !== undefined) {
links.push({text: matches[1], passage: matches[2]});
} else {
links.push({text: matches[1], passage: matches[1]});
}
}
}
return links;
}