-
Notifications
You must be signed in to change notification settings - Fork 2
/
pangamebookgv.lua
101 lines (92 loc) · 2.77 KB
/
pangamebookgv.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
-- pandoc filter to output Graphviz graph from gamebook
-- Copyright 2021-2023 Pelle Nilsson
-- MIT License
-- source: https://github.com/lifelike/pangamebook
-- version: 2.0.1 (2024-08-27)
-- fossil hash: ef2f6a804b30bbc48a2a5398565d7e607af1319b3fb28c346442eec2e77f00b1
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_gamebook_section_header(el)
if not (el.t == "Header"
and el.level == 1) then
return false
end
local first = one_string_from_block(el)
local as_number = tonumber(first)
return as_number ~= null and as_number >= 1
end
function gv_label_for_header(b)
local label = one_string_from_block(b)
local identifier = b.identifier
if not (identifier == "section"
or identifier:sub(1, 8) == "section-") then
label = label .. '\\n' .. identifier
end
root = ''
if tonumber(label) == 1 then
root = ',root=true'
end
return '"' .. identifier .. '" [label=\"' ..label .. '"' .. root .. '];\n'
end
function gv_link(from, to)
return '"' .. from .. '" -> "' .. to .. '";\n'
end
local endstyle = '[shape=doubleoctagon]'
function Pandoc(doc)
if not FORMAT:match "plain" then
return doc
end
local in_header = nil
local links_out = false
local identifiers = {}
local output = "digraph gamebook {\nnode[shape=box];\n\n"
for i,el in pairs(doc.blocks) do
if is_gamebook_section_header(el) then
output = output .. gv_label_for_header(el)
identifiers["#" .. el.identifier] = el.identifier
end
end
for i,el in pairs(doc.blocks) do
if is_gamebook_section_header(el) then
if in_section and not links_out then
output = output .. '"' .. in_section .. '"' .. endstyle .. ';\n'
end
in_section = el.identifier
links_out = false
elseif in_section then
pandoc.walk_block(el, {
Link = function(c)
local target = identifiers[c.target]
if target then
output = output .. gv_link(in_section, target)
links_out = true
end
end
})
end
end
if in_section and not links_out then
output = output .. '"' .. in_section .. '"' .. endstyle .. ';\n'
end
output = output .. "}\n"
local blocks = pandoc.Para(pandoc.Str(output))
return pandoc.Pandoc(blocks, doc.meta)
end
function Blocks(blocks)
return blocks
end
return {
{Pandoc = Pandoc},
}