Last active
February 13, 2019 17:14
-
-
Save bpj/772b93fe58e98e41a4dad61b9e32eec7 to your computer and use it in GitHub Desktop.
Pandoc filter which prevents code blocks containing backticks being mangled in Markdown output
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--[[ | |
Pandoc filter which prevents code blocks containing backticks being mangled in Markdown output. | |
| This software is Copyright (c) 2019 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 function fix_backticks (elem) | |
local code = elem.text:gsub('%s*$','\n') | |
local max = 0 | |
for ticks in code:gmatch('%`+') do | |
local this = ticks:len() | |
if this > max then | |
max = this | |
end | |
end | |
if max > 0 then | |
local ticks = string.rep('`', max * 2) | |
code = code .. ticks .. "\n" | |
classes = elem.classes | |
if 'markdown' == FORMAT then | |
local attr = {} | |
if elem.identifier:len() > 0 then | |
attr[#attr+1] = '#' .. elem.identifier | |
end | |
for _,class in ipairs(classes) do | |
attr[#attr+1] = '.' .. class | |
end | |
for k, v in pairs(elem.attributes) do | |
v = v:gsub('%"', '"') | |
attr[#attr+1] = k .. '="' .. v .. '"' | |
end | |
if #attr > 0 then | |
ticks = ticks .. " {" .. table.concat(attr, " ") .. "}" | |
end | |
elseif 'markdown_github' == FORMAT then | |
if #classes > 0 then | |
ticks = ticks .. classes[1] | |
end | |
else | |
return nil | |
end | |
code = ticks .. "\n" .. code | |
return pandoc.RawBlock(FORMAT, code) | |
end | |
end | |
function Pandoc (doc) | |
if FORMAT == 'markdown' or FORMAT == 'markdown_github' then | |
local filter = { CodeBlock = fix_backticks } | |
local temp = pandoc.Div(doc.blocks) | |
doc.blocks = pandoc.walk_block(temp, filter).content | |
return doc | |
else | |
return nil | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment