Debian Codegraph @

inst/rmarkdown/lua/latex-div.lua

raw symbols SCIP
1 --[[
2 A Pandoc 2 Lua filter converting Pandoc native divs to LaTeX environments
3 Author: Romain Lesur, Christophe Dervieux, Atsushi Yasumoto and Yihui Xie
4 License: Public domain
5 --]]
6
7 -- REQUIREMENTS: Load shared lua filter - see `shared.lua` for more details.
8 dofile(os.getenv 'RMARKDOWN_LUA_SHARED')
9
10 --[[
11 About the requirement:
12 * PANDOC_VERSION -> 2.1
13 ]]
14 if (not pandocAvailable {2,1}) then
15 io.stderr:write("[WARNING] (latex-div.lua) requires at least Pandoc 2.1. Lua Filter skipped.\n")
16 return {}
17 end
18
19 -- START OF THE FILTER'S FUNCTIONS --
20
21 text = require 'text'
22
23 Div = function (div)
24 -- look for 'latex' or 'data-latex' and at least 1 class
25 local options = div.attributes['latex']
26 if not options then
27 options = div.attributes['data-latex']
28 end
29 if not options or #div.attr.classes == 0 then
30 return nil
31 end
32
33 -- if the output format is not latex, remove the attr and return
34 if FORMAT ~= 'latex' and FORMAT ~= 'beamer' then
35 if div.attributes['latex'] then div.attributes['latex'] = nil end
36 if div.attributes['data-latex'] then div.attributes['data-latex'] = nil end
37 return div
38 end
39
40 -- if it's "1" or "true" then just set it to empty string
41 if options == "1" or text.lower(options) == "true" then
42 options = ""
43 end
44
45 -- environment begin/end
46 local env = div.classes[1]
47 local beginEnv = '\\begin' .. '{' .. env .. '}' .. options
48 local endEnv = '\n\\end{' .. env .. '}'
49
50 -- if the first and last div blocks are paragraphs then we can
51 -- bring the environment begin/end closer to the content
52 if div.content[1].t == "Para" and div.content[#div.content].t == "Para" then
53 table.insert(div.content[1].content, 1, pandoc.RawInline('tex', beginEnv .. "\n"))
54 table.insert(div.content[#div.content].content, pandoc.RawInline('tex', "\n" .. endEnv))
55 else
56 table.insert(div.content, 1, pandoc.RawBlock('tex', beginEnv))
57 table.insert(div.content, pandoc.RawBlock('tex', endEnv))
58 end
59 return div
60 end