Repository navigation
Expand file tree
/
Copy pathexpand-howtos.lua
More file actions
140 lines (117 loc) · 3.4 KB
/
Copy pathexpand-howtos.lua
File metadata and controls
140 lines (117 loc) · 3.4 KB
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
-- Expand linked how-to pages under each H2 in the METS How-Tos chapter.
local stringify = (require 'pandoc.utils').stringify
local List = require 'pandoc.List'
local function slugify(text)
local s = text:lower()
s = s:gsub('[^%w%s-]', '')
s = s:gsub('%s+', '-')
s = s:gsub('%-+', '-')
s = s:gsub('^%-', ''):gsub('%-$', '')
return s
end
local function build_h2_index(index_items)
local list_items = List:new()
for _, item in ipairs(index_items) do
local link = pandoc.Link(item.title, '#' .. item.id)
list_items:insert({ pandoc.Plain({ link }) })
end
return List:new({
pandoc.Para({ pandoc.Strong({ pandoc.Str('Index') }) }),
pandoc.BulletList(list_items)
})
end
local function shift_headings(blocks, shift_by)
local filter = {
Header = function(h)
h.level = h.level + shift_by
return h
end
}
return pandoc.walk_block(pandoc.Div(blocks), filter).content
end
local function collect_links_from_bullet_list(bullet_list)
local links = List:new()
for _, item in ipairs(bullet_list.content) do
for _, blk in ipairs(item) do
if blk.t == 'Plain' or blk.t == 'Para' then
for _, inline in ipairs(blk.content) do
if inline.t == 'Link' and inline.target and inline.target:match('%.md$') then
links:insert(inline.target)
end
end
end
end
end
return links
end
local function read_markdown_file(path)
local fh = io.open(path)
if not fh then
io.stderr:write('Cannot open howto file ' .. path .. '\n')
return {}
end
local src = fh:read('*a')
fh:close()
local parsed = pandoc.read(src, 'markdown', PANDOC_READER_OPTIONS)
return parsed.blocks
end
function Pandoc(doc)
local blocks = doc.blocks
local out = List:new()
local howto_h2_index = List:new()
local collecting = false
for _, b in ipairs(blocks) do
if b.t == 'Header' and b.level == 1 then
collecting = (stringify(b.content) == 'METS How-Tos')
elseif collecting and b.t == 'Header' and b.level == 2 then
local title = stringify(b.content)
local id = b.identifier
if not id or id == '' then
id = slugify(title)
end
howto_h2_index:insert({ title = title, id = id })
elseif collecting and b.t == 'Header' and b.level == 1 then
collecting = false
end
end
local in_howtos = false
local index_inserted = false
local i = 1
while i <= #blocks do
local b = blocks[i]
if b.t == 'Header' and b.level == 1 then
in_howtos = (stringify(b.content) == 'METS How-Tos')
out:insert(b)
i = i + 1
elseif in_howtos and b.t == 'Header' and b.level == 2 then
if not index_inserted and #howto_h2_index > 0 then
out:extend(build_h2_index(howto_h2_index))
index_inserted = true
end
out:insert(b)
local j = i + 1
local section_links = List:new()
while j <= #blocks do
local nxt = blocks[j]
if nxt.t == 'Header' and nxt.level <= 2 then
break
end
out:insert(nxt)
if nxt.t == 'BulletList' then
section_links:extend(collect_links_from_bullet_list(nxt))
end
j = j + 1
end
for _, link_target in ipairs(section_links) do
local included = read_markdown_file(link_target)
out:extend(shift_headings(included, 2))
end
i = j
else
out:insert(b)
i = i + 1
end
end
doc.blocks = out
return doc
end