Skip to content

Commit 5012125

Browse files
cdervyihui
authored andcommitted
clean the lua filter
1 parent 812f4b6 commit 5012125

File tree

1 file changed

+10
-16
lines changed

1 file changed

+10
-16
lines changed

inst/resources/lua/custom-environment.lua

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
A Pandoc 2 lua filter to deal with custom environment in bookdown
33
--]]
44

5-
-- theroem types available to be used
5+
-- theorem types available to be used
66
local theorem_abbr = {
77
theorem = 'thm',
88
lemma = 'lem',
@@ -14,16 +14,15 @@ local theorem_abbr = {
1414
exercise = 'exr'
1515
}
1616

17-
-- other special envs
17+
-- other special proof envs
1818
local proof_label = {
1919
proof = 'Proof',
2020
remark = 'Remark',
2121
solution = 'Solution'
2222
}
2323

24-
-- just for debuging purpose
24+
-- for debuging purpose
2525
local debug_mode = os.getenv("DEBUG_PANDOC_LUA") == "TRUE"
26-
2726
function print_debug(label,obj,iter)
2827
obj = obj or nil
2928
iter = iter or pairs
@@ -71,16 +70,15 @@ end
7170

7271
-- Modify Pandoc AST for supported custom environment
7372
Div = function (div)
74-
-- checking if classes correponds to a custom one
7573
local classes = div.classes
7674
-- we do nothing if no classes
7775
if (#classes == 0) then
7876
print_debug("No classes in the Div.")
7977
return div
8078
end
81-
8279
print_debug("Div classes -> " , classes)
8380

81+
-- checking if the class is one of the supported custom environment
8482
local env_type = {type = nil, env = nil}
8583
for i,v in ipairs(classes) do
8684
if (theorem_abbr[v] ~= nil) then
@@ -93,13 +91,11 @@ Div = function (div)
9391
break
9492
end
9593
end
96-
9794
-- classes is not a supported one, we return as is
9895
if not env_type.env then
9996
print_debug("Not a bookdown supported custom class")
10097
return div
10198
end
102-
10399
print_debug("Found types -> ", env_type)
104100

105101
-- get the id if it exists - it will we use to build label for reference
@@ -109,6 +105,7 @@ Div = function (div)
109105
print_debug("id -> ", id)
110106
-- remove unwanted identifier on the div, as it will be on the span
111107
div.identifier = ""
108+
112109
-- get the attributes
113110
local options = div.attributes
114111
if (options["data-latex"] ~= nil) then
@@ -135,23 +132,20 @@ Div = function (div)
135132
end
136133

137134
-- create the custom environment
138-
139135
local label
136+
-- Create a label for referencing - only for theorem like env
140137
if (env_type.type == "theorem") then
141138
label = string.format("%s:%s", theorem_abbr[env_type.env], id)
142139
end
143140
print_debug("label for reference -> ", label)
144141

145142
-- TODO: should we support beamer also ?
146143
if (FORMAT:match 'latex') then
147-
148144
local label_part
149145
if label then
150146
label_part = string.format( "\n\\protect\\hypertarget{%s}{}\\label{%s}", label, label)
151147
end
152-
153148
local name = get_name('latex', options)
154-
155149
table.insert(
156150
div.content, 1,
157151
pandoc.RawBlock('tex', string.format('\\begin{%s}%s%s', env_type.env, name, label_part or ""))
@@ -160,15 +154,13 @@ Div = function (div)
160154
div.content,
161155
pandoc.RawBlock('tex', string.format('\\end{%s}', env_type.env))
162156
)
163-
end
164-
165-
if (FORMAT:match 'html') then
157+
elseif (FORMAT:match 'html') then
166158
local name = get_name('html', options)
167159

168160
-- if div is already processed by eng_theorem, it would also modify it.
169161
-- we can ignore knowing how eng_theorem modifies options$html.before2
170162
-- It can be Plain or Para depending if a name was used or not.
171-
-- NOT VERY RELIABLE THOUGH
163+
-- MAYBE NOT VERY RELIABLE THOUGH
172164
if (div.content[1].t == "Plain" or div.content[1].t == "Para") then
173165
for i,el in pairs(div.content[1].content) do
174166
if (el.t == "Span" and el.classes[1] == env_type.env) then
@@ -177,6 +169,8 @@ Div = function (div)
177169
end
178170
end
179171
end
172+
173+
-- inserted the correct span depending on the environment type
180174
local span
181175
if (env_type.type == "theorem") then
182176
span = pandoc.Span(

0 commit comments

Comments
 (0)