Skip to content

Commit

Permalink
abstract-to-meta: fix handling of nested blocks
Browse files Browse the repository at this point in the history
Nested blocks are inserted into the abstract in normal order instead of
depth-first order. Pandoc versions 2.9.2 or later accept abstracts
nested in Divs, while earlier versions require the abstract header to be
at the top level.

Fixes: #94
  • Loading branch information
tarleb committed May 20, 2020
1 parent feb6b67 commit 8fc5add
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 17 deletions.
55 changes: 38 additions & 17 deletions abstract-to-meta/abstract-to-meta.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,48 @@ abstract-to-meta – move an "abstract" section into document metadata
Copyright: © 2017–2020 Albert Krewinkel
License: MIT – see LICENSE file for details
]]
local abstract = pandoc.List:new{}

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

function Block (elem)
if looking_at_abstract then
abstract[#abstract + 1] = elem
return {}
for _, block in ipairs(blocks) do
if block.t == 'Header' and block.level == 1 then
if block.identifier == 'abstract' then
looking_at_abstract = true
else
looking_at_abstract = false
body_blocks[#body_blocks + 1] = block
end
elseif looking_at_abstract then
abstract[#abstract + 1] = block
else
body_blocks[#body_blocks + 1] = block
end
end
end

function Header (elem)
if elem.level == 1 and elem.identifier == 'abstract' then
looking_at_abstract = true
return {}
else
looking_at_abstract = looking_at_abstract and elem.level ~= 1
end
return body_blocks
end

function Meta (meta)
meta.abstract = meta.abstract or pandoc.MetaBlocks(abstract)
return meta
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)
meta.abstract = meta.abstract or abstract
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)
meta.abstract = meta.abstract or abstract
return pandoc.Pandoc(other_blocks, meta)
end,
}}
end
15 changes: 15 additions & 0 deletions abstract-to-meta/expected.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,25 @@ abstract: |
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.
---

::: {.frontmatter}
Preface
=======

- 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
===========

Expand Down
14 changes: 14 additions & 0 deletions abstract-to-meta/sample.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
::: {.frontmatter}
# Abstract

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
Expand All @@ -6,9 +7,22 @@ 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.

# Preface

* 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
Expand Down

0 comments on commit 8fc5add

Please sign in to comment.