updates to support transclusions

Updates to support transclusions and other features. At some point, we need to refactor all this shared code into a library or the like.
This commit is contained in:
ILindsley 2024-01-29 12:16:27 -05:00
parent 8ddab9a7b3
commit 7c3d3b203c
1 changed files with 94 additions and 81 deletions

View File

@ -1,93 +1,106 @@
-- Filter headers with this function if the target format is LaTeX.
if FORMAT:match 'latex' then
function Pandoc(doc)
return pandoc.Pandoc(filterBlocks(doc), doc.meta)
end
function Header(elem)
local color_to_set = nil
local cli_writer_ops = pandoc.WriterOptions(PANDOC_WRITER_OPTIONS)
local variables = cli_writer_ops['variables']
if (variables == nil) then
return elem
end
if (elem.level == 0) then
color_to_set = variables['color-header-1']
end
if (elem.level == 1) then
color_to_set = variables['color-header-2']
end
if (elem.level == 2) then
color_to_set = variables['color-header-3']
end
if (elem.level == 3) then
color_to_set = variables['color-header-4']
end
if (color_to_set == nil) then
return elem
end
return {
pandoc.RawInline('latex', '\\color{' .. color_to_set .. '}'),
elem,
pandoc.RawInline('latex', '\\color{black}')
}
function filterBlocks(doc)
local hblocks = {}
local cli_writer_ops = pandoc.WriterOptions(PANDOC_WRITER_OPTIONS)
local insideObservationQuestionsSection = false
local hideNextAnswer = false
variables = cli_writer_ops['variables']
vaultBasePath = ''
if (variables['transclusions-directory-path'] ~= nil) then
vaultBasePath = tostring(variables['transclusions-directory-path'])
end
imageFilePath = '.\\'
if (variables['image-base-path'] ~= nil) then
imageFilePath = tostring(variables['image-base-path'])
end
function Pandoc(doc)
local hblocks = {}
local cli_writer_ops = pandoc.WriterOptions(PANDOC_WRITER_OPTIONS)
local variables = cli_writer_ops['variables']
local centerandboldNextElement = false
for i, el in pairs(doc.blocks) do
if (el.t == "RawBlock") then
if (el.text == nil or variables == nil or not variables['treat-comments-as-latex']) then
table.insert(hblocks, el)
else
local _, _, latexCommand = string.find(el.text, "<!%-%-%s*(.+)%s*%-%->")
if (latexCommand == nil) then
table.insert(hblocks, el)
else
latexCommand = trimThisString(latexCommand)
if (latexCommand == 'centerandbold') then
centerandboldNextElement = true
else
local prefix = nil
if (latexCommand ~= 'newpage') then
prefix = '\\'
else
prefix = '\\ \\'
end
table.insert(hblocks, pandoc.RawInline('latex', prefix .. latexCommand))
end
end
end
elseif (el.t == "Para") then
if (centerandboldNextElement) then
centerandboldNextElement = false
local text = ''
for i, inline in pairs(el.content) do
if (inline.text ~= nil ) then
text = text .. inline.text
elseif (inline.t == "Space") then
text = text .. ' '
end
end
table.insert(hblocks, pandoc.RawInline('latex', '\\begin{center}'))
table.insert(hblocks, pandoc.RawInline('latex', '\\textbf{' .. text .. '}'))
table.insert(hblocks, pandoc.RawInline('latex', '\\end{center}'))
for i, el in pairs(doc.blocks) do
if (el.t == "Header") then
if (el.identifier == "observation-questions") then
insideObservationQuestionsSection = true
elseif (el.identifier == "translation-questions") then
insideObservationQuestionsSection = false
end
table.insert(hblocks, el)
elseif (el.t == "RawBlock") then
local _, _, rawBlockText = string.find(el.text, "<!%-%-%s*(.+)%s*%-%->")
if (rawBlockText ~= nil) then
rawBlockText = string.lower(trimThisString(rawBlockText))
if (rawBlockText == 'page break') then
table.insert(hblocks, pandoc.RawInline('latex', '\\newpage'))
elseif (rawBlockText == 'answer') then
hideNextAnswer = true
else
table.insert(hblocks, el)
end
else
table.insert(hblocks, el)
end
elseif (el.t == "Para") then
local t = extractText(el)
local _, _, imageFileName = string.find(t, "%[%[(.+%.jpg)%]%]")
local _, _, fileToTransclude = string.find(t, "%[%[(.+)%]%]")
if (imageFileName ~= nil) then
table.insert(hblocks, pandoc.Image('', imageBasePath .. imageFileName, ''))
elseif (fileToTransclude ~= nil) then
local transcludedPandoc = pandoc.read(extractTransclusionText(fileToTransclude, hblocks))
for k, b in pairs(filterBlocks(transcludedPandoc)) do
table.insert(hblocks, b)
end
elseif (insideObservationQuestionsSection and variables['hide-observation-answers'] and hideNextAnswer) then
hideNextAnswer = false
else
table.insert(hblocks, el)
end
else
table.insert(hblocks, el)
end
return pandoc.Pandoc(hblocks, doc.meta)
end
function trimThisString(s)
return (string.gsub(s, "^%s*(.-)%s*$", "%1"))
end
return hblocks
end
function extractTransclusionText(transclusionFileName, hblocks)
local t = ''
local transclusionFilePath = vaultBasePath .. transclusionFileName .. '.md'
if (file_exists(transclusionFilePath)) then
for line in io.lines(transclusionFilePath) do
t = t .. line .. '\n'
end
else
table.insert(hblocks, pandoc.Plain('Filepath: ' .. transclusionFilePath .. ' NOT FOUND!!'))
end
return t
end
function file_exists(file)
local f = io.open(file, 'r')
if f ~= nil then
io.close(f)
return true
else
return false
end
end
function extractText(el)
local text = ''
if (el.content ~= nil) then
for i, e in pairs(el.content) do
text = text .. extractText(e)
end
elseif (el.text ~= nil) then
text = text .. el.text
elseif (el.t == "Space") then
text = text .. ' '
end
return text
end
function trimThisString(s)
return (string.gsub(s, "^%s*(.-)%s*$", "%1"))
end