-
Notifications
You must be signed in to change notification settings - Fork 2
/
pangamebook.lua
288 lines (265 loc) · 7.3 KB
/
pangamebook.lua
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
-- pandoc filter to turn headers and links into numbers
-- Copyright 2021-2024 Pelle Nilsson
-- MIT License
-- source: https://github.com/lifelike/pangamebook
-- version: 2.0.1 (2024-08-27)
-- fossil hash: ef2f6a804b30bbc48a2a5398565d7e607af1319b3fb28c346442eec2e77f00b1
local nr = 1
local mapped = {}
function get_nr_for_header(text, identifier)
local key = "#" .. identifier
local name_nr = tonumber(text)
if name_nr ~= nil then
if name_nr >= nr then
mapped[key] = name_nr
nr = name_nr + 1
else
io.stderr:write("ERROR: Section number " .. name_nr
.. " too low (expected >= " .. nr .. ")")
os.exit(1)
end
else
mapped[key] = nr
nr = nr + 1
end
return mapped[key]
end
function one_string_from_block(b)
local result = ""
local found_text = false
pandoc.walk_block(b, {
Str = function(s)
if found_text then
return ""
else
result = s.text
end
end
})
return result
end
function is_valid_section_name(el)
if gap >= 1 and is_number_section_name(el) then
return true
end
local first = one_string_from_block(el)
local s,e = string.find(first, "[a-z][a-z_0-9]*")
return (s == 1 and e == string.len(first))
end
function is_number_section_name(el)
local first = one_string_from_block(el)
local as_number = tonumber(first)
return as_number ~= nil and as_number >= 1
end
function shuffle_insert(target, sections)
if gap < 1 then
return shuffle_insert_random(target, sections)
else
return shuffle_insert_gap(target, sections)
end
end
function shuffle_insert_gap(target, sections)
local shuffled = {}
local max = #sections
for i,section in pairs(sections) do
if is_number_section_name(section[1]) then
local number = tonumber(one_string_from_block(section[1]))
if shuffled[number] then
io.stderr:write("ERROR: Two sections numbered " .. number .. "?")
os.exit(1)
end
shuffled[number] = section
table.remove(sections, i)
end
end
if not shuffled[1] and #sections > 1 then
local first = table.remove(sections, 1)
shuffled[1] = first
end
local next_index = 1
for _,section in pairs(sections) do
next_index = get_free_index(shuffled, next_index + gap, max)
shuffled[next_index] = section
if next_index > max then
next_index = get_free_index(shuffled, 1, max)
end
end
for _,section in pairs(shuffled) do
for _,v in ipairs(section) do
table.insert(target, v)
end
end
end
function get_free_index(list, from, max)
if from > max then
from = 1
end
local i = from
while i ~= from - 1 or (from == 1 and i == max) do
if list[i] == nil then
return i
end
i = i + 1
if i > max then
i = 1
end
end
io.stderr:write("ERROR: Failed to find free index from " .. from
.. "(max " .. max .. ") (should never happen)")
os.exit(1)
end
function shuffle_insert_random(target, sections)
while #sections > 0 do
local i = math.random(1, #sections)
local section = table.remove(sections, i)
for _,v in ipairs(section) do
table.insert(target, v)
end
end
end
function insert_sections(sections,
section_start,
section_end,
blocks)
local section = {}
for j=section_start,section_end-1 do
table.insert(section, blocks[j])
end
table.insert(sections, section)
end
function from_meta_bool(meta, name, default)
local value = meta[name]
if type(value) == 'boolean' then
return value
end
if value ~= nil and value.t == "MetaBool" then
return value.c
end
return default
end
function from_meta_string(meta, name, default)
local value = meta[name]
if value ~= nil then
return pandoc.utils.stringify(value)
end
return default
end
function from_meta_int(meta, name, default)
if meta[name] then
local value = pandoc.utils.stringify(meta[name])
return tonumber(value)
end
return default
end
function shuffle_blocks(doc)
local sections = {}
local first_section_i = 0
local current_section_start = -1
local blocks = {}
for i,el in pairs(doc.blocks) do
if (el.t == "Header"
and el.level == 1) then
if current_section_start >= 0 then
insert_sections(sections,
current_section_start,
i,
doc.blocks)
end
if is_valid_section_name(el) then
current_section_start = i
else
if #sections > 0 then
shuffle_insert(blocks, sections)
sections = {}
end
table.insert(blocks, el)
current_section_start = -1
end
else
if current_section_start < 0 then
table.insert(blocks, el)
end
end
end
if current_section_start >= 0 then
insert_sections(sections,
current_section_start,
#doc.blocks + 1,
doc.blocks)
end
if #sections > 0 then
shuffle_insert(blocks, sections)
end
return blocks
end
function Pandoc(doc)
number_sections = from_meta_bool(doc.meta, "gamebook-numbers", true)
strong_links = from_meta_bool(doc.meta, "gamebook-strong-links", true)
link_pre = from_meta_string(doc.meta, "gamebook-pre-link", "")
link_post = from_meta_string(doc.meta, "gamebook-post-link", "")
gap = from_meta_int(doc.meta, "gamebook-gap", 23)
if from_meta_bool(doc.meta, "gamebook-shuffle", true) then
local seed_number = from_meta_int(doc.meta, "gamebook-randomseed", 2023)
math.randomseed(seed_number)
return pandoc.Pandoc(shuffle_blocks(doc), doc.meta)
else
return doc
end
end
function Header(el)
if (el.level ~= 1
or not number_sections
or not (is_valid_section_name(el)
or is_number_section_name(el))) then
return el
end
local first = one_string_from_block(el)
local as_number
local found = false
local replaced = false
local identifier = el.identifier
return pandoc.walk_block(el, {
Str = function(b)
if replaced then
return pandoc.Str("")
else
replaced = true
local nr = get_nr_for_header(b.text, identifier)
return pandoc.Str(nr)
end
end
})
end
function Link(el)
if string.find(el.target, "://") ~= nil then
return
end
local nr = mapped[el.target]
local content
if nr == nil then
content = pandoc.Str(link_pre .. el.target .. link_post)
else
content = pandoc.Str(link_pre .. nr .. link_post)
end
if strong_links then
content = pandoc.Strong(content)
end
return pandoc.Link(content, el.target)
end
function Blocks(blocks)
return blocks
end
function Meta(meta)
local mapped_meta = {}
for k, v in pairs(mapped) do
mapped_meta[k] = tostring(v)
end
meta["pangamebook-mapping"] = mapped_meta
return meta
end
return {
{Pandoc = Pandoc},
{Header = Header},
{Link = Link},
{Meta = Meta}
}