Skip to content

Commit

Permalink
Add support for multiple abstract-like sections
Browse files Browse the repository at this point in the history
  • Loading branch information
tarleb committed Mar 21, 2023
1 parent 174554b commit 71622f9
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 35 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ DIFF ?= diff
PANDOC ?= pandoc

.PHONY: test
test: test-hrule test-in-div
test: test-hrule test-in-div test-two-sections

test-%: $(FILTER_FILE) test/input-%.md test/test-%.yaml
@$(PANDOC) --defaults test/test-$*.yaml | \
Expand Down
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,34 @@ output:
---
```

Multiple abstracts
------------------------------------------------------------------

It is common for certain works to include two abstracts, one in
English and one in the local language of an academic institution.
E.g., theses published at many German universities must have a
German "Zusammenfassung" in addition to the English "Abstract".

The filter can be configured to support those additional
abstract-like sections as well. The identifiers of the sections
must be listed in the `section-identifiers` field below the
`abstract-section` metadata entry. E.g.:

``` yaml
---
abstract-section:
section-identifiers:
- abstract
- sammanfattning
---
```

This will place the *Abstract* in the `abstract` variable as
before, but will also collect the contents of a *Sammanfattning*
section and place it in the `sammanfattning` field, where it can
be used for further processing, e.g., with a custom template.


License
------------------------------------------------------------------

Expand Down
89 changes: 55 additions & 34 deletions _extensions/abstract-section/abstract-section.lua
Original file line number Diff line number Diff line change
@@ -1,29 +1,35 @@
--[[
abstract-to-meta – move an "abstract" section into document metadata
abstract-section – move an "abstract" section into document metadata
Copyright: © 2017–2021 Albert Krewinkel
Copyright: © 2017–2023 Albert Krewinkel
License: MIT – see LICENSE file for details
]]
local abstract = {}
local stringify = (require 'pandoc.utils').stringify
local section_identifiers = {
abstract = true,
}
local collected = {}

--- Extract abstract from a list of blocks.
function abstract_from_blocklist (blocks)
local function abstract_from_blocklist (blocks)
local body_blocks = {}
local looking_at_abstract = false
local looking_at_section = false

for _, block in ipairs(blocks) do
if block.t == 'Header' and block.level == 1 then
if block.identifier == 'abstract' then
looking_at_abstract = true
if section_identifiers[block.identifier] then
looking_at_section = block.identifier
collected[looking_at_section] = {}
else
looking_at_abstract = false
looking_at_section = false
body_blocks[#body_blocks + 1] = block
end
elseif looking_at_abstract then
elseif looking_at_section then
if block.t == 'HorizontalRule' then
looking_at_abstract = false
looking_at_section = false
else
abstract[#abstract + 1] = block
local collect = collected[looking_at_section]
collect[#collect + 1] = block
end
else
body_blocks[#body_blocks + 1] = block
Expand All @@ -33,27 +39,42 @@ function abstract_from_blocklist (blocks)
return body_blocks
end

if PANDOC_VERSION >= {2,9,2} then
-- Check all block lists with pandoc 2.9.2 or later
return {{
Blocks = abstract_from_blocklist,
Meta = function (meta)
if not meta.abstract and #abstract > 0 then
meta.abstract = pandoc.MetaBlocks(abstract)
end
return meta
end
}}
else
-- otherwise, just check the top-level block-list
return {{
Pandoc = function (doc)
local meta = doc.meta
local other_blocks = abstract_from_blocklist(doc.blocks)
if not meta.abstract and #abstract > 0 then
meta.abstract = pandoc.MetaBlocks(abstract)
end
return pandoc.Pandoc(other_blocks, meta)
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
29 changes: 29 additions & 0 deletions test/expected-two-sections.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
abstract: |
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad
minim veniam, quis nostrud exercitation ullamco laboris nisi ut
aliquip ex ea commodo consequat. Duis aute irure dolor in
reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
pariatur.
- one
- two
- three
Excepteur sint occaecat cupidatat non proident, sunt in culpa qui
officia deserunt mollit anim id est laborum.
sammanfattning: |
- Phasellus purus.
- Praesent fermentum tempor tellus.
- Proin quam nisl, tincidunt et, mattis eget, convallis nec, purus.
- Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
- Curabitur lacinia pulvinar nibh.
---

# Lorem Ipsum

Quo dolore molestiae et laboriosam occaecati explicabo corrupti. Earum
expedita ducimus quaerat est quam ut molestiae. Illum deleniti vel
labore facilis et cum est. Est nemo est vel ad. Assumenda consequatur
rerum officiis atque officia. Est nihil iste cumque ad qui.
37 changes: 37 additions & 0 deletions test/input-two-sections.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
---
abstract-section:
section-identifiers:
- abstract
- sammanfattning
---

# Abstract

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis
nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu
fugiat nulla pariatur.

- one
- two
- three

Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia
deserunt mollit anim id est laborum.

# Sammanfattning {#sammanfattning}

* Phasellus purus.
* Praesent fermentum tempor tellus.
* Proin quam nisl, tincidunt et, mattis eget, convallis nec, purus.
* Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
* Curabitur lacinia pulvinar nibh.

# Lorem Ipsum

Quo dolore molestiae et laboriosam occaecati explicabo corrupti.
Earum expedita ducimus quaerat est quam ut molestiae. Illum
deleniti vel labore facilis et cum est. Est nemo est vel ad.
Assumenda consequatur rerum officiis atque officia. Est nihil iste
cumque ad qui.
4 changes: 4 additions & 0 deletions test/test-two-sections.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
input-files: [test/input-two-sections.md]
standalone: true
filters: [{type: lua, path: abstract-section.lua}]
to: markdown

0 comments on commit 71622f9

Please sign in to comment.