-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
passage.go
262 lines (226 loc) · 5.7 KB
/
passage.go
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/*
Copyright © 2014–2020 Thomas Michael Edwards. All rights reserved.
Use of this source code is governed by a Simplified BSD License which
can be found in the LICENSE file.
*/
package main
import (
// standard packages
"fmt"
"regexp"
"strings"
// external packages
"golang.org/x/text/unicode/norm"
)
// Info passages are passages which contain solely structural data, metadata,
// and code, rather than any actual story content.
var infoPassages = []string{
// Story formats: Twine 1.4+ vanilla & SugarCube.
"StoryAuthor", "StoryInit", "StoryMenu", "StorySubtitle", "StoryTitle",
// Story formats: SugarCube.
"PassageReady", "PassageDone", "PassageHeader", "PassageFooter", "StoryBanner", "StoryCaption",
// Story formats: SugarCube (v1 only).
"MenuOptions", "MenuShare", "MenuStory",
// Story formats: SugarCube (v2 only).
"StoryInterface", "StoryShare",
// Story formats: Twine 1.4+ vanilla.
// Compilers: Twine/Twee 1.4+, Twee2, & Tweego.
"StorySettings",
// Compilers: Tweego & (whatever Dan Cox's compiler is called).
"StoryData",
// Compilers: Twine/Twee 1.4+ & Twee2.
"StoryIncludes",
}
type passageMetadata struct {
position string // Unused by Tweego. Twine 1 & 2 passage block X and Y coordinates CSV.
size string // Unused by Tweego. Twine 2 passage block width and height CSV.
}
type passage struct {
// Core.
name string
tags []string
text string
// Compiler metadata.
metadata *passageMetadata
}
func newPassage(name string, tags []string, source string) *passage {
return &passage{
name: name,
tags: tags,
text: source,
}
}
func (p *passage) equals(second passage) bool {
return p.text == second.text
}
func (p *passage) tagsHas(needle string) bool {
if len(p.tags) > 0 {
for _, tag := range p.tags {
if tag == needle {
return true
}
}
}
return false
}
func (p *passage) tagsHasAny(needles ...string) bool {
if len(p.tags) > 0 {
for _, tag := range p.tags {
for _, needle := range needles {
if tag == needle {
return true
}
}
}
}
return false
}
func (p *passage) tagsContains(needle string) bool {
if len(p.tags) > 0 {
for _, tag := range p.tags {
if strings.Contains(tag, needle) {
return true
}
}
}
return false
}
func (p *passage) tagsStartsWith(needle string) bool {
if len(p.tags) > 0 {
for _, tag := range p.tags {
if strings.HasPrefix(tag, needle) {
return true
}
}
}
return false
}
func (p *passage) hasMetadataPosition() bool {
return p.metadata != nil && p.metadata.position != ""
}
func (p *passage) hasMetadataSize() bool {
return p.metadata != nil && p.metadata.size != ""
}
func (p *passage) hasAnyMetadata() bool {
return p.metadata != nil && (p.metadata.position != "" || p.metadata.size != "")
}
func (p *passage) hasInfoTags() bool {
return p.tagsHasAny("annotation", "script", "stylesheet", "widget") || p.tagsStartsWith("Twine.")
}
func (p *passage) hasInfoName() bool {
return stringSliceContains(infoPassages, p.name)
}
func (p *passage) isInfoPassage() bool {
return p.hasInfoName() || p.hasInfoTags()
}
func (p *passage) isStoryPassage() bool {
return !p.hasInfoName() && !p.hasInfoTags()
}
func (p *passage) toTwee(outMode outputMode) string {
var output string
if outMode == outModeTwee3 {
output = ":: " + tweeEscapeString(p.name)
if len(p.tags) > 0 {
output += " [" + tweeEscapeString(strings.Join(p.tags, " ")) + "]"
}
if p.hasAnyMetadata() {
output += " " + string(p.marshalMetadata())
}
} else {
output = ":: " + p.name
if len(p.tags) > 0 {
output += " [" + strings.Join(p.tags, " ") + "]"
}
}
output += "\n"
if len(p.text) > 0 {
output += p.text + "\n"
}
output += "\n\n"
return output
}
func (p *passage) toPassagedata(pid uint) string {
var (
position string
size string
)
if p.hasMetadataPosition() {
position = p.metadata.position
} else {
// No position metadata, so generate something sensible on the fly.
x := pid % 10
y := pid / 10
if x == 0 {
x = 10
} else {
y++
}
position = fmt.Sprintf("%d,%d", x*125-25, y*125-25)
}
if p.hasMetadataSize() {
size = p.metadata.size
} else {
// No size metadata, so default to the normal size.
size = "100,100"
}
/*
<tw-passagedata pid="…" name="…" tags="…" position="…" size="…">…</tw-passagedata>
*/
return fmt.Sprintf(`<tw-passagedata pid="%d" name=%q tags=%q position=%q size=%q>%s</tw-passagedata>`,
pid,
attrEscapeString(p.name),
attrEscapeString(strings.Join(p.tags, " ")),
attrEscapeString(position),
attrEscapeString(size),
htmlEscapeString(p.text),
)
}
func (p *passage) toTiddler(pid uint) string {
var position string
if p.hasMetadataPosition() {
position = p.metadata.position
} else {
// No position metadata, so generate something sensible on the fly.
x := pid % 10
y := pid / 10
if x == 0 {
x = 10
} else {
y++
}
position = fmt.Sprintf("%d,%d", x*140-130, y*140-130)
}
/*
<div tiddler="…" tags="…" created="…" modifier="…" twine-position="…">…</div>
*/
return fmt.Sprintf(`<div tiddler=%q tags=%q twine-position=%q>%s</div>`,
attrEscapeString(p.name),
attrEscapeString(strings.Join(p.tags, " ")),
attrEscapeString(position),
tiddlerEscapeString(p.text),
)
}
func (p *passage) countWords() uint64 {
text := p.text
// Strip newlines.
text = strings.Replace(text, "\n", "", -1)
// Strip comments.
re := regexp.MustCompile(`(?s:/%.*?%/|/\*.*?\*/|<!--.*?-->)`)
text = re.ReplaceAllString(text, "")
// Count normalized "characters".
var (
count uint64
ia norm.Iter
)
ia.InitString(norm.NFKD, text)
for !ia.Done() {
count++
ia.Next()
}
// Count "words", typing measurement style—i.e., 5 "characters" per "word".
words := count / 5
if count%5 > 0 {
words++
}
return words
}