-
-
Save AndreyMZ/4b146c2ce5143359dcc4584ff53c2119 to your computer and use it in GitHub Desktop.
pandoc confluence markup writer
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
-- Invoke with: pandoc -t wiki.lua | |
-- | |
-- Note: you need not have lua installed on your system to use this | |
-- custom writer. However, if you do have lua installed, you can | |
-- use it to test changes to the script. 'lua wiki.lua' will | |
-- produce informative error messages if your code contains | |
-- syntax errors. | |
local image_index = 0 | |
-- Character escaping | |
local function escape(s, in_attribute) | |
return s:gsub("[<>&\"']", | |
function(x) | |
if x == '<' then | |
return '<' | |
elseif x == '>' then | |
return '>' | |
elseif x == '&' then | |
return '&' | |
elseif x == '"' then | |
return '"' | |
elseif x == "'" then | |
return ''' | |
else | |
return x | |
end | |
end | |
) | |
end | |
-- Table to store footnotes, so they can be included at the end. | |
local notes = {} | |
-- Blocksep is used to separate block elements. | |
function Blocksep() | |
return "\n" | |
end | |
-- This function is called once for the whole document. Parameters: | |
-- body is a string, metadata is a table, variables is a table. | |
-- This gives you a fragment. You could use the metadata table to | |
-- fill variables in a custom lua template. Or, pass `--template=...` | |
-- to pandoc, and pandoc will add do the template processing as | |
-- usual. | |
function Doc(body, metadata, variables) | |
local buffer = {} | |
local function add(s) | |
table.insert(buffer, s) | |
end | |
add(body) | |
-- TODO: render notes | |
-- if #notes > 0 then | |
-- add('<ol class="footnotes">') | |
-- for _,note in pairs(notes) do | |
-- add(note) | |
-- end | |
-- add('</ol>') | |
-- end | |
return table.concat(buffer, "\n") .. "\n" | |
end | |
-- The functions that follow render corresponding pandoc elements. | |
-- s is always a string, attr is always a table of attributes, and | |
-- items is always an array of strings (the items in a list). | |
-- Comments indicate the types of other variables. | |
function Str(s) | |
return escape(s) | |
end | |
function Space() | |
return " " | |
end | |
function SoftBreak() | |
return " " | |
end | |
function LineBreak() | |
return "\n" | |
end | |
function Para(s) | |
return s .. "\n" | |
end | |
function Plain(s) | |
return s | |
end | |
function Emph(s) | |
return "_" .. s .. "_" | |
end | |
function Strong(s) | |
return "*" .. s .. "*" | |
end | |
function Subscript(s) | |
return "~" .. s .. "~" | |
end | |
function Superscript(s) | |
return "^" .. s .. "^" | |
end | |
function Strikeout(s) | |
return '-' .. s .. '-' | |
end | |
function Link(s, src, tit) | |
if tit:len() > 0 then | |
return "[" .. s .. "|" .. escape(src,true) .. " " .. escape(tit,true) .. "]" | |
else | |
return "[" .. s .. "|" .. escape(src,true) .. "]" | |
end | |
end | |
function CaptionedImage(src, s, tit) | |
image_index = image_index + 1 | |
return "!" .. escape(src, true) .. "!\n" .. 'FIGURE ' .. image_index .. ". " .. tit .. "\n" | |
end | |
function Image(s, src, tit) | |
return "!" .. escape(src,true) .. "!" | |
end | |
-- lev is an integer, the header level. | |
function Header(lev, s, attr) | |
return "h" .. lev .. ". " .. s .. "" | |
end | |
function BlockQuote(s) | |
return "{quote}\n" .. s .. "{quote}\n" | |
end | |
function HorizontalRule() | |
return "----" | |
end | |
function Code(s) | |
return '{{' .. s .. '}}' | |
end | |
function CodeBlock(s, attr) | |
if attr["class"]:len() > 0 then | |
return "{code:" .. attr["class"] .. "}\n" .. s .. "\n{code}\n" | |
else | |
return "{code}\n" .. s .. "\n{code}\n" | |
end | |
end | |
function Note(s) | |
local num = #notes + 1 | |
return s | |
-- TODO: render note back reference | |
-- -- insert the back reference right before the final closing tag. | |
-- s = string.gsub(s, '(.*)</', '%1 <a href="#fnref' .. num .. '">↩</a></') | |
-- -- add a list item with the note to the note table. | |
-- table.insert(notes, '<li id="fn' .. num .. '">' .. s .. '</li>') | |
-- -- return the footnote reference, linked to the note. | |
-- return '<a id="fnref' .. num .. '" href="#fn' .. num .. '"><sup>' .. num .. '</sup></a>' | |
end | |
local function trim(s) | |
return s:match("^%s*(.-)%s*$") | |
end | |
local function List(items, marker) | |
local buffer = {} | |
for _, item in pairs(items) do | |
table.insert(buffer, marker .. " " .. trim(item:gsub("\n+", "\n")) .. "\n") | |
end | |
return table.concat(buffer, "") | |
end | |
function BulletList(items) | |
return List(items, '-') | |
end | |
function OrderedList(items) | |
return List(items, '#') | |
end | |
-- Revisit association list STackValue instance. | |
function DefinitionList(items) | |
local buffer = {} | |
for _,item in pairs(items) do | |
for k, v in pairs(item) do | |
table.insert(buffer, Strong(k) .. "\n" .. BulletList(v)) | |
end | |
end | |
return table.concat(buffer, "\n") | |
end | |
-- Caption is a string, aligns is an array of strings, | |
-- widths is an array of floats, headers is an array of | |
-- strings, rows is an array of arrays of strings. | |
function Table(caption, aligns, widths, headers, rows) | |
local buffer = {} | |
local function add(s) | |
table.insert(buffer, s) | |
end | |
local header_row = {} | |
local empty_header = true | |
for i, h in pairs(headers) do | |
table.insert(header_row, h) | |
empty_header = empty_header and h == "" | |
end | |
if empty_header then | |
head = "" | |
else | |
add('|| ' .. table.concat(header_row, ' || ') .. ' ||') | |
end | |
for _, row in pairs(rows) do | |
add('| ' .. table.concat(row, ' | ') .. ' |') | |
end | |
return "\n" .. table.concat(buffer,'\n') .. "\n" | |
end | |
-- The following code will produce runtime warnings when you haven't defined | |
-- all of the functions you need for the custom writer, so it's useful | |
-- to include when you're working on a writer. | |
local meta = {} | |
meta.__index = function(_, key) | |
io.stderr:write(string.format("WARNING: Undefined function '%s'\n",key)) | |
return function() return "" end | |
end | |
setmetatable(_G, meta) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment