Skip to content

Instantly share code, notes, and snippets.

@bpj
Last active December 31, 2020 16:58
Show Gist options
  • Save bpj/dd32a2243a099283895bbee2795bdb42 to your computer and use it in GitHub Desktop.
Save bpj/dd32a2243a099283895bbee2795bdb42 to your computer and use it in GitHub Desktop.
Pandoc filter which converts select raw LaTeX environments into Pandoc native divs
--[==============================[
Pandoc filter which converts select raw LaTeX environments into Pandoc native divs,
with the evironment name as first/only class and a possible
mandatory argument after the environment name converted to a first
paragraph in the div. This was in response to a question on how to
convert certain environments into ReStructuredText directives, so some
of the comments relate to that.
To use this filter run pandoc like this:
$ pandoc -L env2div.lua -r latex+raw_tex -o output.rst input.ltx
where you in principle at least can replace `.rst` with `.md` and
get divs with classes instead of rst directives, although then you
must write your own filter or CSS to do something sensible with those
divs.
This software is Copyright (c) 2020 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.
--]==============================]
-- local dump = require"pl.pretty".dump -- for debugging
-- Patterns for matching a raw environment block.
local env_pats = {
-- With one mandatory arg following the environment name.
'^%s*\\begin{(%a+)}(%b{})%s+(.*)\\end{%1}%s*$',
-- Without any arg(s) following the environment name.
'^%s*\\begin{(%a+)}(%s+)(.*)\\end{%1}%s*$',
}
-- Mapping environment names to div classes aka directive names or
-- handler functions.
-- When the names are identical the value can be a boolean `true`.
-- If the value is a function, it will be called to get the return value.
-- Such functions are called with the environment body and the environment
-- argument (raw, with braces around, or just whitespace if there was no argument)
-- as function arguments.
local known_env = {
note = 'note',
warning = true, -- equivalent to warning = 'warning'
tab = true,
admonition = true,
}
local filter = {}
filter.RawBlock = function (raw)
for _,pat in ipairs(env_pats) do
local env, arg, body = raw.text:match(pat)
-- dump{ env = env, arg = arg, body = body }
if env and known_env[env] then
if 'string' == type(known_env[env]) then
env = known_env[env]
elseif 'function' == type(known_env[env]) then
return known_env[env](body, arg)
end
if arg and tostring(arg):match'%b{}' then -- got an arg
-- Remove braces around arg
arg = arg:sub(2,-2)
-- Parse arg contents.
-- The return value is a full doc but we are only interested in the body
local blocks = pandoc.read(arg, 'latex').blocks
-- The body is a list of blocks so we extract the inlines from it
local separator = {pandoc.Space()}
local inlines = pandoc.utils.blocks_to_inlines(blocks, separator)
-- Wrap the inlines in a para
arg = pandoc.Para(inlines)
else -- got no arg
arg = nil
end
-- Parse environment body into an AST.
local blocks = pandoc.Div(pandoc.read(body, 'latex').blocks)
-- Run filter recursively to resolve nested environments
local content = pandoc.walk_block(blocks, filter).content
if arg then
-- I'm inserting the arg/title as a top para here. Quite
-- possibly the right thing to do is to insert it as some
-- (string) attribute. I don't know! Possibly you will need to
-- do some manual editing of the rst file...
table.insert(content, 1, arg)
end
return pandoc.Div(content, {class = env})
end
end
-- else do nothing
return nil
end
function Pandoc (doc)
local blocks = pandoc.Div(doc.blocks)
doc.blocks = pandoc.walk_block(blocks, filter).content
return doc
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment