depth 1 replace

This commit is contained in:
2025-10-11 11:49:38 +08:00
parent 40f6d927cd
commit c7e1c1a8dc
4 changed files with 56 additions and 80 deletions

View File

@@ -6,4 +6,10 @@ This is a pandoc filter manipulating [fenced_divs](https://pandoc.org/MANUAL.htm
Inspired by [this joke](https://www.zhihu.com/question/586807834/answer/2973535981).
useful refs:
- https://pandoc.org/lua-filters.html
- https://pandoc.org/lua-filters.html
# How does this work?
`filter.lua` walks the AST. There are some special leaf nodes with a `include` attr which is a string containing labels of some other divs to be included here.
For each of `include` node `i`, we maintain a list `fa[i]` containing nodes with labels on the path `i -> root`. Then for each label `l` in `i`'s include list, one can see that every node in `fa[i]` depends on `l`. Thus we build a digraph G on labels. If G is a DAG, then we can use topological sort to rank its vertices and thus decide the order of substitution; Otherwise, G contains a directed cycle, the filter reports an error.