Skip to content

Instantly share code, notes, and snippets.

@bpj
Last active January 1, 2019 02:16
Show Gist options
  • Save bpj/c8638d52e2dd21cc40a3cdc47814fdc5 to your computer and use it in GitHub Desktop.
Save bpj/c8638d52e2dd21cc40a3cdc47814fdc5 to your computer and use it in GitHub Desktop.
Pandoc Lua filter which converts docx with poetry as found in the wild to one line block per verse
--[[
Pandoc Lua filter which converts docx with poetry as found in the wild
where each line is a paragraph and each verse is separated by an empty
paragraph into one Pandoc line block per verse, if run like this:
pandoc -r docx+empty_paragraphs --lua-filter pandoc-poetry-line-blocks.lua poetry.docx -so poetry.md
Note the `+empty_paragraphs` extension! It is necessary for this filter to
do its work correctly!
| This software is Copyright (c) 2018 by Benct Philip Jonsson.
|
| This is free software, licensed under:
|
| The MIT (X11) License
|
| The MIT License
|
| Permission is hereby granted, free of charge, to any person
| obtaining a copy of this software and associated
| documentation files (the "Software"), to deal in the Software
| without restriction, including without limitation the rights to
| use, copy, modify, merge, publish, distribute, sublicense,
| and/or sell copies of the Software, and to permit persons to
| whom the Software is furnished to do so, subject to the
| following conditions:
|
| The above copyright notice and this permission notice shall
| be included in all copies or substantial portions of the
| Software.
|
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT
| WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
| INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
| MERCHANTABILITY, FITNESS FOR A PARTICULAR
| PURPOSE AND NONINFRINGEMENT. IN NO EVENT
| SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
| LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
| TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
| CONNECTION WITH THE SOFTWARE OR THE USE OR
| OTHER DEALINGS IN THE SOFTWARE.
]]
function Pandoc (doc)
local old = doc.blocks
local blocks = {{}}
for _,elem in ipairs(old) do
if 'Para' == elem.tag then
local content = elem.content
if #content > 0 then
local block = blocks[#blocks]
block[#block+1] = content
else
blocks[#blocks+1] = {}
end
else
blocks[#blocks+1] = elem
blocks[#blocks+1] = {}
end
end
local new = {}
for _,block in ipairs(blocks) do
if block.t then
new[#new+1] = block
elseif #block > 1 then
new[#new+1] = pandoc.LineBlock(block)
else
new[#new+1] = pandoc.Para(block[1] or {})
end
end
return pandoc.Pandoc(new, doc.meta)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment