-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Expand file tree
/
Copy pathexperiment_box.lua
More file actions
63 lines (52 loc) · 1.64 KB
/
Copy pathexperiment_box.lua
File metadata and controls
63 lines (52 loc) · 1.64 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
-- Pandoc Lua filter: wrap special sections in tcolorbox environments.
--
-- 1. "实验 X.Y" headings → experimentbox (description only, until next heading)
-- 2. "思考题" headings → questionbox (until end of chapter or next same/higher heading)
function Pandoc(doc)
local new_blocks = {}
local in_box = false
local box_type = ""
local box_level = 0
local function open_box(name)
table.insert(new_blocks, pandoc.RawBlock("latex", "\\begin{" .. name .. "}"))
in_box = true
box_type = name
end
local function close_box()
table.insert(new_blocks, pandoc.RawBlock("latex", "\\end{" .. box_type .. "}"))
in_box = false
box_type = ""
end
for _, block in ipairs(doc.blocks) do
if block.t == "Header" then
local text = pandoc.utils.stringify(block)
if text:match("^实验%s?%d") then
if in_box then close_box() end
box_level = block.level
block.classes:insert("unnumbered")
open_box("experimentbox")
table.insert(new_blocks, block)
elseif text:match("^思考题") then
if in_box then close_box() end
box_level = block.level
block.classes:insert("unnumbered")
open_box("questionbox")
table.insert(new_blocks, block)
elseif in_box then
if box_type == "experimentbox" then
close_box()
elseif block.level <= box_level then
close_box()
end
table.insert(new_blocks, block)
else
table.insert(new_blocks, block)
end
else
table.insert(new_blocks, block)
end
end
if in_box then close_box() end
doc.blocks = new_blocks
return doc
end