|
| 1 | +-- environment.lua |
| 2 | +-- Copyright (C) 2020 by RStudio, PBC |
| 3 | + |
| 4 | +local classEnvironments = pandoc.MetaMap({}) |
| 5 | +local classCommands = pandoc.MetaMap({}) |
| 6 | + |
| 7 | +-- helper that identifies arrays |
| 8 | +local function tisarray(t) |
| 9 | + local i = 0 |
| 10 | + for _ in pairs(t) do |
| 11 | + i = i + 1 |
| 12 | + if t[i] == nil then return false end |
| 13 | + end |
| 14 | + return true |
| 15 | +end |
| 16 | + |
| 17 | +-- reads the environments |
| 18 | +local function readEnvironments(meta) |
| 19 | + local env = meta['environments'] |
| 20 | + if env ~= nil then |
| 21 | + if tisarray(env) then |
| 22 | + -- read an array of strings |
| 23 | + for i, v in ipairs(env) do |
| 24 | + local value = pandoc.utils.stringify(v) |
| 25 | + classEnvironments[value] = value |
| 26 | + end |
| 27 | + else |
| 28 | + -- read key value pairs |
| 29 | + for k, v in pairs(env) do |
| 30 | + local key = pandoc.utils.stringify(k) |
| 31 | + local value = pandoc.utils.stringify(v) |
| 32 | + classEnvironments[key] = value |
| 33 | + end |
| 34 | + end |
| 35 | + end |
| 36 | +end |
| 37 | + |
| 38 | +local function readCommands(meta) |
| 39 | + local env = meta['commands'] |
| 40 | + if env ~= nil then |
| 41 | + if tisarray(env) then |
| 42 | + -- read an array of strings |
| 43 | + for i, v in ipairs(env) do |
| 44 | + local value = pandoc.utils.stringify(v) |
| 45 | + classCommands[value] = value |
| 46 | + end |
| 47 | + else |
| 48 | + -- read key value pairs |
| 49 | + for k, v in pairs(env) do |
| 50 | + local key = pandoc.utils.stringify(k) |
| 51 | + local value = pandoc.utils.stringify(v) |
| 52 | + classCommands[key] = value |
| 53 | + end |
| 54 | + end |
| 55 | + end |
| 56 | +end |
| 57 | + |
| 58 | +local function readEnvsAndCommands(meta) |
| 59 | + readEnvironments(meta) |
| 60 | + readCommands(meta) |
| 61 | +end |
| 62 | + |
| 63 | +-- use the environments from metadata to |
| 64 | +-- emit a custom environment for latex |
| 65 | +local function writeEnvironments(divEl) |
| 66 | + if quarto.doc.is_format("latex") then |
| 67 | + for k, v in pairs(classEnvironments) do |
| 68 | + if divEl.attr.classes:includes(k) then |
| 69 | + -- process this into a latex environment |
| 70 | + local beginEnv = '\\begin' .. '{' .. v .. '}' |
| 71 | + local endEnv = '\n\\end{' .. v .. '}' |
| 72 | + |
| 73 | + -- check if custom options or arguments are present |
| 74 | + -- and add them to the environment accordingly |
| 75 | + local opts = divEl.attr.attributes['options'] |
| 76 | + if opts then |
| 77 | + beginEnv = beginEnv .. '[' .. opts .. ']' |
| 78 | + end |
| 79 | + |
| 80 | + local args = divEl.attr.attributes['arguments'] |
| 81 | + if args then |
| 82 | + beginEnv = beginEnv .. '{' .. args .. '}' |
| 83 | + end |
| 84 | + |
| 85 | + -- if the first and last div blocks are paragraphs then we can |
| 86 | + -- bring the environment begin/end closer to the content |
| 87 | + if #divEl.content > 0 and divEl.content[1].t == "Para" and divEl.content[#divEl.content].t == "Para" then |
| 88 | + table.insert(divEl.content[1].content, 1, pandoc.RawInline('tex', beginEnv .. "\n")) |
| 89 | + table.insert(divEl.content[#divEl.content].content, pandoc.RawInline('tex', "\n" .. endEnv)) |
| 90 | + else |
| 91 | + table.insert(divEl.content, 1, pandoc.RawBlock('tex', beginEnv)) |
| 92 | + table.insert(divEl.content, pandoc.RawBlock('tex', endEnv)) |
| 93 | + end |
| 94 | + return divEl |
| 95 | + end |
| 96 | + end |
| 97 | + end |
| 98 | +end |
| 99 | + |
| 100 | +local function buildCommandArgs(opts, format) |
| 101 | + local function wrap(o) |
| 102 | + return string.format(format, o) |
| 103 | + end |
| 104 | + local t = pandoc.List() |
| 105 | + for str in string.gmatch(opts, "([^"..",".."]+)") do |
| 106 | + t:insert(str) |
| 107 | + end |
| 108 | + return table.concat(t:map(wrap), "") |
| 109 | +end |
| 110 | + |
| 111 | +-- use the environments from metadata to |
| 112 | +-- emit a custom environment for latex |
| 113 | +local function writeCommands(spanEl) |
| 114 | + if quarto.doc.is_format("latex") then |
| 115 | + for k, v in pairs(classCommands) do |
| 116 | + if spanEl.attr.classes:includes(k) then |
| 117 | + |
| 118 | + -- resolve the begin command |
| 119 | + local beginCommand = '\\' .. pandoc.utils.stringify(v) |
| 120 | + local opts = spanEl.attr.attributes['options'] |
| 121 | + local args = spanEl.attr.attributes['arguments'] |
| 122 | + if opts then |
| 123 | + beginCommand = beginCommand .. buildCommandArgs(opts, "[%s]") |
| 124 | + end |
| 125 | + if args then |
| 126 | + beginCommand = beginCommand .. buildCommandArgs(args, "{%s}") |
| 127 | + end |
| 128 | + |
| 129 | + local beginCommandRaw = pandoc.RawInline('latex', beginCommand .. '{') |
| 130 | + |
| 131 | + -- the end command |
| 132 | + local endCommandRaw = pandoc.RawInline('latex', '}') |
| 133 | + |
| 134 | + -- attach the raw inlines to the span contents |
| 135 | + local result = spanEl.content |
| 136 | + table.insert(result, 1, beginCommandRaw) |
| 137 | + table.insert(result, endCommandRaw) |
| 138 | + |
| 139 | + return result |
| 140 | + end |
| 141 | + end |
| 142 | + end |
| 143 | +end |
| 144 | + |
| 145 | +-- Run in two passes so we process metadata |
| 146 | +-- and then process the divs |
| 147 | +return { |
| 148 | + { Meta = readEnvsAndCommands }, |
| 149 | + { Div = writeEnvironments, Span = writeCommands } |
| 150 | +} |
0 commit comments