-
Notifications
You must be signed in to change notification settings - Fork 0
/
twison.js
executable file
·153 lines (142 loc) · 3.35 KB
/
twison.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
const choiceRegexp = /\[\[.+?\]\]/g
const choiceSyntaxRegExp = /\[\[(.*?)\-\>(.*?)\]\]/
var Twison = {
/**
*
* @param {String} text
*/
extractLinksFromText: function (text) {
var links = text.match(choiceRegexp)
if (links) {
return links.map(function(link) {
var differentName = link.match(choiceSyntaxRegExp);
if (differentName) {
return {
name: differentName[1],
link: differentName[2]
};
} else {
link = link.substring(2, link.length-2)
return {
name: link,
link: link
}
}
});
}
},
/**
*
* @param {String} text
*/
filterText: function (text) {
let retVal = text
const links = text.match(choiceRegexp)
if (links) {
links.forEach(choice => {
retVal = retVal.replace(choice, '')
})
}
if (retVal.endsWith('\n')) {
retVal = retVal.split(" ").filter((k, i, arr) => i !== arr.length - 1).join(' ')
}
return retVal.trim()
},
/**
*
* @param {Object} dict
*/
arrangeKeys: function (dict) {
const retVal = {}
const orderedKeys = ['pid', 'name', 'text', 'tags', 'auto_link', 'links']
orderedKeys.forEach(k => {
if (dict[k]) {
retVal[k] = dict[k]
}
})
return retVal
},
/**
*
* @param {*} passage
*/
convertPassage: function (passage) {
var text = passage.innerHTML
const dict = {}
var linkMatch = text.match(choiceRegexp)
if (linkMatch) {
dict.links = linkMatch
.filter(l => {
const diffName = l.match(choiceSyntaxRegExp)
if (diffName) return true
else {
dict["auto_link"] = l.substring(2, l.length-2)
return false
}
})
.map(l => {
const k = l.match(choiceSyntaxRegExp)
return { name: k[1], link: k[2] }
})
}
if (!dict.links || !dict.links.length) {
delete dict.links
}
const dataArr = ['name', 'pid', 'position', 'tags']
dataArr.forEach(attr => {
const value = passage.attributes[attr].value
if (!value) return
dict[attr] = value
})
if (dict.tags) {
dict.tags = dict.tags.split(" ");
}
dict.text = Twison.filterText(text)
return Twison.arrangeKeys(dict)
},
/**
*
* @param {*} story
*/
convertStory: function(story) {
var passages = story.getElementsByTagName("tw-passagedata")
var convertedPassages = Array.prototype.slice.call(passages).map(Twison.convertPassage)
var dict = {
passages: convertedPassages
};
["name", "startnode", "ifid"].forEach(function(attr) {
var value = story.attributes[attr].value
if (value) {
dict[attr] = value
}
})
// Add PIDs to links
var pidsByName = {};
dict.passages.forEach(passage => {
pidsByName[passage.name] = passage.pid
})
dict.passages.forEach(passage => {
if (passage["auto_link"]) {
passage["auto_link"] = pidsByName[passage["auto_link"]]
}
if (!passage.links) return;
passage.links.forEach(function(link) {
link.pid = pidsByName[link.link];
if (!link.pid) {
link.broken = true;
}
})
})
dict.visited = false
return dict
},
/**
*
*/
convert: function() {
var storyData = document.getElementsByTagName("tw-storydata")[0];
var json = JSON.stringify(Twison.convertStory(storyData), null, 2);
document.getElementById("output").innerHTML = json
}
}
window.Twison = Twison;