如何修改section-bibliographies.lua过滤器在各参考文献后添加分页符?
修改section-bibliography.lua实现参考文献后自动分页
解决方案代码
直接修改原section-bibliography.lua过滤器,添加分页符生成逻辑并插入到参考文献章节之后。以下是修改后的完整代码(关键修改处已标注注释):
local section_refs = {} local all_refs = pandoc.MetaList{} --- Generate a bibliography for a given list of references. local function make_bibliography(references, doc) local tmp = pandoc.Pandoc({}, doc.meta) tmp.meta.bibliography = nil tmp.meta.references = references return pandoc.utils.run_json_filter(tmp, 'pandoc-citeproc', {FORMAT}) end --- 新增:生成对应格式的分页符节点 local function pagebreak() local fmt = FORMAT:lower() if fmt == 'docx' then return pandoc.RawBlock('openxml', '<w:p><w:r><w:br w:type="page"/></w:r></w:p>') elseif fmt == 'odt' then return pandoc.RawBlock('opendocument', '<text:p text:style-name="PageBreak"/>') elseif fmt:match 'latex' then return pandoc.RawBlock('latex', '\\newpage') elseif fmt:match 'html' then return pandoc.RawBlock('html', '<div style="page-break-after: always;"></div>') else -- 其他格式默认使用水平分隔线作为降级方案 return pandoc.HorizontalRule() end end --- Process a section, collecting references and replacing citations. local function process_section(section, doc, last_section) local refs = pandoc.MetaList{} -- Collect all references in this section. section:walk{ Cite = function(cite) for _, citation in ipairs(cite.citations) do local ref = doc.meta.references and doc.meta.references[citation.id] if ref then refs[#refs + 1] = ref all_refs[#all_refs + 1] = ref end end end } section_refs[section] = refs -- Replace citations with section-local ones. section = section:walk{ Cite = function(cite) local citations = pandoc.List(cite.citations):map(function(cite) cite.note_num = nil cite.properties = cite.properties or {} cite.properties.section = true return cite end) return pandoc.Cite(cite.content, citations) end } -- Add bibliography if there are references. if #refs > 0 then local bib = make_bibliography(refs, doc) local head_level = section.level + 1 local bib_title = doc.meta['section-bibliography-title'] or pandoc.MetaInlines{pandoc.Str('References')} local bib_section = pandoc.Div( {pandoc.Header(head_level, bib_title), table.unpack(bib.blocks)}, {class = 'section-bibliography'} ) section.content[#section.content + 1] = bib_section -- 关键修改:在参考文献章节后添加分页符(非最后一个章节才添加) if not last_section then section.content[#section.content + 1] = pagebreak() end end return section end return { { Meta = function(meta) -- Remove references to avoid duplicates in the global bibliography. meta.references = nil return meta end }, { Pandoc = function(doc) local sections = doc.blocks:filter(function(block) return block.t == 'Div' and block.classes:includes('section') end) -- Process sections in reverse order to handle nested sections correctly. for i = #sections, 1, -1 do sections[i] = process_section(sections[i], doc, i == #sections) end -- Add global bibliography if there are references not in any section. local global_refs = pandoc.MetaList{} local seen = {} for _, ref in ipairs(all_refs) do seen[ref.id] = true end if doc.meta.references then for _, ref in ipairs(doc.meta.references) do if not seen[ref.id] then global_refs[#global_refs + 1] = ref end end end if #global_refs > 0 then local bib = make_bibliography(global_refs, doc) local bib_title = doc.meta['bibliography-title'] or pandoc.MetaInlines{pandoc.Str('References')} doc.blocks[#doc.blocks + 1] = pandoc.Header(1, bib_title) doc.blocks = doc.blocks .. bib.blocks end return doc end } }
修改原理说明
多格式分页适配:新增的
pagebreak()函数针对不同输出格式生成原生分页代码,确保Word、ODT、PDF、HTML等格式都能正确触发分页:- docx:使用OpenXML标准分页标签
- ODT:调用内置分页样式节点
- LaTeX:使用
\newpage命令 - 其他格式:用水平分隔线作为降级处理
分页时机控制:仅对非最后一个章节的参考文献添加分页符,避免文档末尾出现不必要的空白页,同时保证章节间的分页效果。
原逻辑兼容:完全保留原过滤器单bib文件解析、章节独立参考文献生成的核心功能,仅追加分页逻辑,不影响原有引用计数和格式排版。
使用方法
将修改后的文件保存为section-bibliography-pagebreak.lua,编译文档时指定该过滤器即可:
pandoc your-document.md --lua-filter=section-bibliography-pagebreak.lua -o output.docx
内容的提问来源于stack exchange,提问作者streamline
相关产品推荐
相关产品推荐

