forked from dialoa/dialectica-filters
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfirst-line-indent.lua
407 lines (309 loc) · 11.4 KB
/
first-line-indent.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
--[[-- # First-line-indent - First-line idented paragraphs
in Pandoc's markdown.
@author Julien Dutant <[email protected]>
@copyright 2021 Julien Dutant
@license MIT - see LICENSE file for details.
@release 0.2
]]
-- # Options
--- Options map, including defaults.
-- @param set_metadata_variable boolean whether to set the `indent`
-- metadata variable.
-- @param set_header_includes boolean whether to add formatting code in
-- header-includes to handle first-line indent paragraph style.
-- @param auto_remove_indents boolean whether to automatically remove
-- indents after blockquotes and the like.
-- @param remove_after list of strings, Pandoc AST block types
-- after which first-line indents should be automatically removed.
-- @param remove_after_class list of strings, classes of elements
-- after which first-line indents should be automatically removed.
-- @param dont_remove_after_class list of strings, classes of elements
-- after which first-line indents should not be removed. Prevails
-- over remove_after.
-- @param size string a CSS / LaTeX specification of the first line
-- indent length
local options = {
set_metadata_variable = true,
set_header_includes = true,
auto_remove = true,
remove_after = pandoc.List({
'BlockQuote',
'BulletList',
'CodeBlock',
'DefinitionList',
'HorizontalRule',
'OrderedList',
}),
remove_after_class = pandoc.List({
'statement', 'argument',
}),
dont_remove_after_class = pandoc.List:new(),
size = "1em",
}
-- List of pandoc AST block elements that have classes
local types_with_classes = pandoc.List({
'CodeBlock', 'Div', 'Header', 'Table',
})
-- # Filter global variables
-- @utils pandoc's module of utilities functions
-- @param code map of pandoc objects for indent/noindent Raw code
-- @param header_code map of pandoc code to add to header-includes
local utils = pandoc.utils
local code = {
latex = {
indent = pandoc.RawInline('tex', '\\indent '),
noindent = pandoc.RawInline('tex', '\\noindent '),
},
html = {
indent = pandoc.RawBlock('html',
'<div class="first-line-indent-after"></div>'),
noindent = pandoc.RawBlock('html',
'<div class="no-first-line-indent-after"></div>'),
}
}
local header_code = {
html = [[
<style>
p {
text-indent: SIZE;
margin: 0;
}
header + p {
text-indent: ;
}
:is(h1, h2, h3, h4, h5, h6) + p {
text-indent: 0;
}
:is(div.no-first-line-indent-after) + p {
text-indent: 0;
}
:is(div.first-line-indent-after) + p {
text-indent: SIZE;
}
</style>
]],
}
--- type: pandoc-friendly type function
-- pandoc.utils.type is only defined in Pandoc >= 2.17
-- if it isn't, we extend Lua's type function to give the same values
-- as pandoc.utils.type on Meta objects: Inlines, Inline, Blocks, Block,
-- string and booleans
-- Caution: not to be used on non-Meta Pandoc elements, the
-- results will differ (only 'Block', 'Blocks', 'Inline', 'Inlines' in
-- >=2.17, the .t string in <2.17).
local type = pandoc.utils.type or function (obj)
local tag = type(obj) == 'table' and obj.t and obj.t:gsub('^Meta', '')
return tag and tag ~= 'Map' and tag or type(obj)
end
--- add_header_includes: add a block to the document's header-includes
-- meta-data field.
-- @param meta the document's metadata block
-- @param blocks list of Pandoc block elements (e.g. RawBlock or Para)
-- to be added to the header-includes of meta
-- @return meta the modified metadata block
local function add_header_includes(meta, blocks)
local header_includes = pandoc.MetaList( { pandoc.MetaBlocks(blocks) })
-- add any exisiting meta['header-includes']
-- it can be MetaInlines, MetaBlocks or MetaList
if meta['header-includes'] then
if type(meta['header-includes']) == 'List' then
header_includes:extend(meta['header-includes'])
else
header_includes:insert(meta['header-includes'])
end
end
meta['header-includes'] = header_includes
return meta
end
--- classes_include: check if one of an element's class is in a given
-- list. Returns true if match, nil if no match or the element doesn't
-- have classes.
-- @param elem pandoc AST element
-- @param classes pandoc List of strings
local function classes_include(elem,classes)
if elem.classes then
for _,class in ipairs(elem.classes) do
if classes:includes(class) then return true end
end
end
end
-- # Filter functions
--- Filter for the metablock.
-- reads the user options.
-- sets the metadata variable `indent` to `true` unless otherwise specified.
-- adds formatting code to `header-includes` unless otherwise specified.
function process_metadata(meta)
-- read user options
if meta['first-line-indent'] then
local user_options = meta['first-line-indent']
if not(user_options['set-metadata-variable'] == nil)
and user_options['set-metadata-variable'] == false then
options.set_metadata_variable = false
end
if not(user_options['set-header-includes'] == nil)
and user_options['set-header-includes'] == false then
options.set_header_includes = false
end
if not(user_options['auto-remove'] == nil)
and user_options['auto-remove'] == false then
options.auto_remove = false
end
-- (dont-)remove-after, (dont-)remove-after-class accept only
-- MetaInlines and MetaList, we standardize to the latter
for _,key in ipairs({'remove-after','dont-remove-after',
'remove-after-class','dont-remove-after-class'}) do
if user_options[key] and type(user_options[key]) == 'Inlines' then
user_options[key] = pandoc.MetaList({user_options[key]})
end
end
-- for object types we only need to customize one list, remove_after
-- for classes we need a whitelist (remove_after_class) and a
-- blacklist (dont_remove_after_class).
-- We first insert user entries in remove_after, remove_after_class
-- and dont_remove_after_class.
for optname, metakey in pairs({
remove_after = 'remove-after',
remove_after_class = 'remove-after-class',
dont_remove_after_class = 'dont-remove-after-class',
}) do
if user_options[metakey]
and type(user_options[metakey]) == 'List' then
for _,item in ipairs(user_options[metakey]) do
options[optname]:insert(utils.stringify(item))
end
end
end
-- We then remove user entries from remove_after and remove_after_class
for optname, metakey in pairs({
remove_after = 'dont-remove-after',
remove_after_class = 'dont-remove-after-class'
}) do
if user_options[metakey]
and type(user_options[metakey]) == 'List' then
-- list of strings to be removed
local blacklist = pandoc.List:new()
for _,item in ipairs(user_options[metakey]) do
blacklist:insert(utils.stringify(item))
end
-- filter to that returns true iff an item isn't blacklisted
predicate = function (str)
return not(blacklist:includes(str))
end
options[optname] =
options[optname]:filter(predicate)
end
end
-- sil
if not(user_options['size'] == nil) then
-- @todo using stringify means that LaTeX commands in
-- size are erased. But it ensures that the filter gets
-- a string. Improvement: check that we have a string
-- and throw a warning otherwise
options.size = utils.stringify(user_options['size'])
end
end
-- variable to track whether we've changed `meta`
changes = false
-- set the `indent` metadata variable unless otherwise specified or
-- already set to false
if options.set_metadata_variable and not(meta.indent == false) then
meta.indent = true
changes = true
end
-- set the `header-includes` metadata variable
if options.set_header_includes then
if FORMAT:match('html*') then
header_code.html = string.gsub(header_code.html, "SIZE", options.size)
add_header_includes(meta, { pandoc.RawBlock('html', header_code.html) })
elseif FORMAT:match('latex') and not(options.size == "1em") then
add_header_includes(meta, { pandoc.RawBlock('tex',
'\\setlength{\\parindent}{'.. options.size ..'}') })
end
end
if changes then return meta end
end
--- Add format-specific explicit indent markup to a paragraph.
-- @param type string 'indent' or 'noindent', type of markup to add
-- @param elem pandoc AST paragraph
-- @return a list of blocks (containing a single paragraph element or
-- a rawblock and a paragraph element, depending on output format)
local function indent_markup(type, elem)
if FORMAT:match('latex') and (type == 'indent' or type == 'noindent') then
-- in LaTeX, replace any `\indent` or `\noindent` command at
-- the start of the paragraph with the desired one, add it otherwise
local content = pandoc.List(elem.content)
if content[1] and (utils.equals(content[1],
code.latex.indent) or utils.equals(content[1],
code.latex.noindent)) then
content[1] = code.latex[type]
else
content:insert(1, code.latex[type])
end
elem.content = content
return({ elem })
-- in HTML, insert a block (div) before the paragraph
elseif FORMAT:match('html*') and (type == 'indent' or type == 'noindent') then
return({ code.html[type], elem })
else
return({elem})
end
end
--- Process indents in the document's body text.
-- Adds output code for explicitly specified first-line indents,
-- automatically removes first-line indents after blocks of the
-- designed types unless otherwise specified.
local function process_body(doc)
-- result will be the new doc.blocks
local result = pandoc.List({})
local do_not_indent_next_block = false
for _,elem in pairs(doc.blocks) do
-- Paragraphs may already have explicit indentation marking
-- in LaTeX. If so we reproduce it in the relevant output
-- format. Otherwise we remove the first-line indent if
-- needed, provided auto_remove is on.
if elem.t == "Para" then
if elem.content[1] and (utils.equals(elem.content[1],
code.latex.indent) or utils.equals(elem.content[1],
code.latex.noindent)) then
if utils.equals(elem.content[1], code.latex.indent) then
result:extend(indent_markup('indent', elem))
else
result:extend(indent_markup('noindent', elem))
end
elseif do_not_indent_next_block and options.auto_remove then
result:extend(indent_markup('noindent', elem))
else
result:insert(elem)
end
do_not_indent_next_block = false
-- if not paragraph, we check if it is an element after which
-- first-line should be removed because of its type or class
elseif options.remove_after:includes(elem.t) and
not classes_include(elem, options.dont_remove_after_class) then
do_not_indent_next_block = true
result:insert(elem)
elseif types_with_classes:includes(elem.t) and
classes_include(elem, options.remove_after_class) then
do_not_indent_next_block = true
result:insert(elem)
-- otherwise we don't remove indent after this block
else
do_not_indent_next_block = false
result:insert(elem)
end
end
doc.blocks = result
return doc
end
--- Main code
-- Returns the filters in the desired order of execution
local metadata_filter = {
Meta = process_metadata
}
local body_filter = {
Pandoc = process_body
}
return {
metadata_filter,
body_filter
}