From 7e681492040da157c293b8262d6b5fd67e921d98 Mon Sep 17 00:00:00 2001 From: Jonas Smedegaard Date: Sat, 15 Mar 2025 07:36:30 +0100 Subject: embed filter extensions in format extension --- .../pandoc-ext/abstract-section/_extension.yaml | 6 -- .../abstract-section/abstract-section.lua | 85 ---------------------- .../ruc-play/stylish-report/_extension.yaml | 3 + .../pandoc-ext/abstract-section/_extension.yaml | 6 ++ .../abstract-section/abstract-section.lua | 85 ++++++++++++++++++++++ .../ruc-play/stylish-roles/_extension.yaml | 6 ++ .../ruc-play/stylish-roles/stylish-roles.lua | 54 ++++++++++++++ _extensions/ruc-play/stylish-roles/_extension.yaml | 6 -- .../ruc-play/stylish-roles/stylish-roles.lua | 54 -------------- _quarto.yml | 4 - 10 files changed, 154 insertions(+), 155 deletions(-) delete mode 100644 _extensions/pandoc-ext/abstract-section/_extension.yaml delete mode 100644 _extensions/pandoc-ext/abstract-section/abstract-section.lua create mode 100644 _extensions/ruc-play/stylish-report/_extensions/pandoc-ext/abstract-section/_extension.yaml create mode 100644 _extensions/ruc-play/stylish-report/_extensions/pandoc-ext/abstract-section/abstract-section.lua create mode 100644 _extensions/ruc-play/stylish-report/_extensions/ruc-play/stylish-roles/_extension.yaml create mode 100644 _extensions/ruc-play/stylish-report/_extensions/ruc-play/stylish-roles/stylish-roles.lua delete mode 100644 _extensions/ruc-play/stylish-roles/_extension.yaml delete mode 100644 _extensions/ruc-play/stylish-roles/stylish-roles.lua diff --git a/_extensions/pandoc-ext/abstract-section/_extension.yaml b/_extensions/pandoc-ext/abstract-section/_extension.yaml deleted file mode 100644 index f6a946b..0000000 --- a/_extensions/pandoc-ext/abstract-section/_extension.yaml +++ /dev/null @@ -1,6 +0,0 @@ -name: abstract-section -author: Albert Krewinkel -version: 1.2.0 -contributes: - filters: - - abstract-section.lua diff --git a/_extensions/pandoc-ext/abstract-section/abstract-section.lua b/_extensions/pandoc-ext/abstract-section/abstract-section.lua deleted file mode 100644 index 4badeb4..0000000 --- a/_extensions/pandoc-ext/abstract-section/abstract-section.lua +++ /dev/null @@ -1,85 +0,0 @@ ---[[ -abstract-section – move an "abstract" section into document metadata - -Copyright: © 2017–2023 Albert Krewinkel -License: MIT – see LICENSE file for details -]] -local stringify = (require 'pandoc.utils').stringify -local section_identifiers = { - abstract = true, -} -local collected = {} ---- The level of the highest heading that was seen so far. Abstracts ---- must be at or above this level to prevent nested sections from being ---- treated as metadata. Only top-level sections should become metadata. -local toplevel = 6 - ---- Extract abstract from a list of blocks. -local function abstract_from_blocklist (blocks) - local body_blocks = {} - local looking_at_section = false - - for _, block in ipairs(blocks) do - if block.t == 'Header' and block.level <= toplevel then - toplevel = block.level - if section_identifiers[block.identifier] then - looking_at_section = block.identifier - collected[looking_at_section] = {} - else - looking_at_section = false - body_blocks[#body_blocks + 1] = block - end - elseif looking_at_section then - if block.t == 'HorizontalRule' then - looking_at_section = false - else - local collect = collected[looking_at_section] - collect[#collect + 1] = block - end - else - body_blocks[#body_blocks + 1] = block - end - end - - return body_blocks -end - -Pandoc = function (doc) - local meta = doc.meta - - -- configure - section_identifiers_list = - (doc.meta['abstract-section'] or {})['section-identifiers'] - if section_identifiers_list and #section_identifiers_list > 0 then - section_identifiers = {} - for i, ident in ipairs(section_identifiers_list) do - section_identifiers[stringify(ident)] = true - end - end - -- unset config in meta - doc.meta['abstract-section'] = nil - - local blocks = {} - if PANDOC_VERSION >= {2,17} then - -- Walk all block lists by default - blocks = doc.blocks:walk{Blocks = abstract_from_blocklist} - elseif PANDOC_VERSION >= {2,9,2} then - -- Do the same with pandoc versions that don't have walk methods but the - -- `walk_block` function. - blocks = pandoc.utils.walk_block( - pandoc.Div(doc.blocks), - {Blocks = abstract_from_blocklist} - ).content - else - -- otherwise, just check the top-level block-list - blocks = abstract_from_blocklist(doc.blocks) - end - for metakey in pairs(section_identifiers) do - metakey = stringify(metakey) - local abstract = collected[metakey] - if not meta[metakey] and abstract and #abstract > 0 then - meta[metakey] = pandoc.MetaBlocks(abstract) - end - end - return pandoc.Pandoc(blocks, meta) -end diff --git a/_extensions/ruc-play/stylish-report/_extension.yaml b/_extensions/ruc-play/stylish-report/_extension.yaml index 507eeaa..58eb145 100644 --- a/_extensions/ruc-play/stylish-report/_extension.yaml +++ b/_extensions/ruc-play/stylish-report/_extension.yaml @@ -4,6 +4,9 @@ version: 0.0.3 contributes: formats: common: + filters: + - stylish-roles + - abstract-section pdf: number-sections: true toc: true diff --git a/_extensions/ruc-play/stylish-report/_extensions/pandoc-ext/abstract-section/_extension.yaml b/_extensions/ruc-play/stylish-report/_extensions/pandoc-ext/abstract-section/_extension.yaml new file mode 100644 index 0000000..f6a946b --- /dev/null +++ b/_extensions/ruc-play/stylish-report/_extensions/pandoc-ext/abstract-section/_extension.yaml @@ -0,0 +1,6 @@ +name: abstract-section +author: Albert Krewinkel +version: 1.2.0 +contributes: + filters: + - abstract-section.lua diff --git a/_extensions/ruc-play/stylish-report/_extensions/pandoc-ext/abstract-section/abstract-section.lua b/_extensions/ruc-play/stylish-report/_extensions/pandoc-ext/abstract-section/abstract-section.lua new file mode 100644 index 0000000..4badeb4 --- /dev/null +++ b/_extensions/ruc-play/stylish-report/_extensions/pandoc-ext/abstract-section/abstract-section.lua @@ -0,0 +1,85 @@ +--[[ +abstract-section – move an "abstract" section into document metadata + +Copyright: © 2017–2023 Albert Krewinkel +License: MIT – see LICENSE file for details +]] +local stringify = (require 'pandoc.utils').stringify +local section_identifiers = { + abstract = true, +} +local collected = {} +--- The level of the highest heading that was seen so far. Abstracts +--- must be at or above this level to prevent nested sections from being +--- treated as metadata. Only top-level sections should become metadata. +local toplevel = 6 + +--- Extract abstract from a list of blocks. +local function abstract_from_blocklist (blocks) + local body_blocks = {} + local looking_at_section = false + + for _, block in ipairs(blocks) do + if block.t == 'Header' and block.level <= toplevel then + toplevel = block.level + if section_identifiers[block.identifier] then + looking_at_section = block.identifier + collected[looking_at_section] = {} + else + looking_at_section = false + body_blocks[#body_blocks + 1] = block + end + elseif looking_at_section then + if block.t == 'HorizontalRule' then + looking_at_section = false + else + local collect = collected[looking_at_section] + collect[#collect + 1] = block + end + else + body_blocks[#body_blocks + 1] = block + end + end + + return body_blocks +end + +Pandoc = function (doc) + local meta = doc.meta + + -- configure + section_identifiers_list = + (doc.meta['abstract-section'] or {})['section-identifiers'] + if section_identifiers_list and #section_identifiers_list > 0 then + section_identifiers = {} + for i, ident in ipairs(section_identifiers_list) do + section_identifiers[stringify(ident)] = true + end + end + -- unset config in meta + doc.meta['abstract-section'] = nil + + local blocks = {} + if PANDOC_VERSION >= {2,17} then + -- Walk all block lists by default + blocks = doc.blocks:walk{Blocks = abstract_from_blocklist} + elseif PANDOC_VERSION >= {2,9,2} then + -- Do the same with pandoc versions that don't have walk methods but the + -- `walk_block` function. + blocks = pandoc.utils.walk_block( + pandoc.Div(doc.blocks), + {Blocks = abstract_from_blocklist} + ).content + else + -- otherwise, just check the top-level block-list + blocks = abstract_from_blocklist(doc.blocks) + end + for metakey in pairs(section_identifiers) do + metakey = stringify(metakey) + local abstract = collected[metakey] + if not meta[metakey] and abstract and #abstract > 0 then + meta[metakey] = pandoc.MetaBlocks(abstract) + end + end + return pandoc.Pandoc(blocks, meta) +end diff --git a/_extensions/ruc-play/stylish-report/_extensions/ruc-play/stylish-roles/_extension.yaml b/_extensions/ruc-play/stylish-report/_extensions/ruc-play/stylish-roles/_extension.yaml new file mode 100644 index 0000000..190c698 --- /dev/null +++ b/_extensions/ruc-play/stylish-report/_extensions/ruc-play/stylish-roles/_extension.yaml @@ -0,0 +1,6 @@ +name: stylish-roles +author: Jonas Smedegaard +version: 0.0.1 +contributes: + filters: + - stylish-roles.lua diff --git a/_extensions/ruc-play/stylish-report/_extensions/ruc-play/stylish-roles/stylish-roles.lua b/_extensions/ruc-play/stylish-report/_extensions/ruc-play/stylish-roles/stylish-roles.lua new file mode 100644 index 0000000..50caf71 --- /dev/null +++ b/_extensions/ruc-play/stylish-report/_extensions/ruc-play/stylish-roles/stylish-roles.lua @@ -0,0 +1,54 @@ +--[[ +stylish-roles - provide existence hints for attributes and CrediT roles +as authors[*].metadata.credit[role], by-author[*].metadata.credit[role], +some-author-has[*].credit[role] and some-author-has.attributes[attribute]. + +Copyright: © 2024 Jonas Smedegaard