building digraph

This commit is contained in:
2025-10-12 17:45:13 +08:00
parent c7e1c1a8dc
commit e133a11d61
3 changed files with 49 additions and 11 deletions

View File

@@ -1,16 +1,43 @@
local label_map = {} -- label has to be unique!
local function collect_labels(div) -- only collect labels in div
if div.identifier and div.identifier ~= "" then
label_map[div.identifier] = div:clone()
local label_map = {} -- label has to be unique!
-- helper functions for debugging
local function show(s)
io.stderr:write("[Debug] " .. s .. "\n")
end
local function collect_labels(blk)
if blk.identifier and blk.identifier ~= "" then
label_map[blk.identifier] = blk:clone()
end
return nil
end
local function dfs(blk, stack) -- depth first search on a top level blk
-- check for identifier and build label_map
if blk.identifier and blk.identifier ~= "" then
label_map[blk.identifier] = blk:clone()
table.insert(stack, blk.identifier)
end
-- recurse into child blocks
-- type matters. see https://hackage-content.haskell.org/package/pandoc-types-1.23.1/docs/Text-Pandoc-Definition.html#t:Block
-- fortunately, we only need to recurse on divs.
if blk.t == 'Div' then
for _, inner in ipairs(blk.content) do
dfs(inner, stack)
end
end
-- pop
if blk.identifier and blk.identifier ~= "" then
table.remove(stack) -- pop
end
end
local function words(s)
local res = {}
for part in s:gmatch("[^,]+") do -- split by commas
local trimmed = part:match("^%s*(.-)%s*$") -- remove leading/trailing spaces
for part in s:gmatch("[^,]+") do -- split by commas
local trimmed = part:match("^%s*(.-)%s*$") -- remove leading/trailing spaces
if trimmed ~= "" then
table.insert(res, trimmed)
end
@@ -26,9 +53,9 @@ local function replace(e)
for _, l in ipairs(words(include)) do
if label_map[l] then
table.insert(blocks, label_map[l]:clone())
io.stderr:write("insert [" .. label_map[l].identifier .. ']\n')
show("insert [" .. label_map[l].identifier .. ']\n')
else
io.stderr:write("Cannot find AST node with label " .. l)
io.stderr:write("Cannot find AST node with label " .. l .. "\n")
end
end
return pandoc.Div(blocks, e.attr)
@@ -41,7 +68,7 @@ return {
Pandoc = function(doc)
-- collect labels
for _, blk in ipairs(doc.blocks) do
collect_labels(blk)
dfs(blk,{})
end
-- replace

View File

@@ -3,12 +3,17 @@
Lorem ipsum dolor sit amet
:::
:::{include="lorem, thm2 , fakelabel , "}
:::{include="lorem, thm2 , fakelabel , ,inthm1 ," #inc}
:::
::: Theorem
test thm1
::: {.Definition #inthm1} :::
something
:::::::::::::::::::::::::::::
:::
:::{.Theorem #thm2}

View File

@@ -1,16 +1,22 @@
<div id="lorem">
<p>Lorem ipsum dolor sit amet</p>
</div>
<div data-include="lorem, thm2 , fakelabel , ">
<div id="inc" data-include="lorem, thm2 , fakelabel , ,inthm1 ,">
<div id="lorem">
<p>Lorem ipsum dolor sit amet</p>
</div>
<div id="thm2" class="Theorem">
<p>test thm2</p>
</div>
<div id="inthm1" class="Definition">
<p>something</p>
</div>
</div>
<div class="Theorem">
<p>test thm1</p>
<div id="inthm1" class="Definition">
<p>something</p>
</div>
</div>
<div id="thm2" class="Theorem">
<p>test thm2</p>