From 4fa2a947590f78160dac3197672e475f433f0e4f Mon Sep 17 00:00:00 2001 From: John MacFarlane Date: Tue, 2 Apr 2013 21:08:38 -0700 Subject: Added `Text.Pandoc.Writers.Custom`, `--print-custom-lua-writer`. pandoc -t data/sample.lua will load the script sample.lua and use it as a custom writer. data/sample.lua is provided as an example. Added `--print-custom-lua-writer` option to print the sample script. --- data/sample.lua | 312 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 312 insertions(+) create mode 100644 data/sample.lua (limited to 'data/sample.lua') diff --git a/data/sample.lua b/data/sample.lua new file mode 100644 index 000000000..fe425b749 --- /dev/null +++ b/data/sample.lua @@ -0,0 +1,312 @@ +-- This is a sample custom writer for pandoc. It produces output +-- that is very similar to that of pandoc's HTML writer. +-- There is one new feature: code blocks marked with class 'dot' +-- are piped through graphviz and images are included in the HTML +-- output using 'data:' URLs. +-- +-- Invoke with: pandoc -t sample.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 sample.lua' will +-- produce informative error messages if your code contains +-- syntax errors. + +-- 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 + +-- Helper function to convert an attributes table into +-- a string that can be put into HTML tags. +local function attributes(attr) + local attr_table = {} + for x,y in pairs(attr) do + if y and y ~= "" then + table.insert(attr_table, ' ' .. x .. '="' .. escape(y,true) .. '"') + end + end + return table.concat(attr_table) +end + +-- Run cmd on a temporary file containing inp and return result. +local function pipe(cmd, inp) + local tmp = os.tmpname() + local tmph = io.open(tmp, "w") + tmph:write(inp) + tmph:close() + local outh = io.popen(cmd .. " " .. tmp,"r") + local result = outh:read("*all") + outh:close() + os.remove(tmp) + return result +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\n" +end + +-- This function is called once for the whole document. Parameters: +-- body, title, date are strings; authors is an array of strings; +-- variables is a table. One could use some kind of templating +-- system here; this just gives you a simple standalone HTML file. +function Doc(body, title, authors, date, variables) + local buffer = {} + local function add(s) + table.insert(buffer, s) + end + add('') + add('') + add('') + add('' .. title .. '') + add('') + add('') + if title ~= "" then + add('

' .. title .. '

') + end + for _, author in pairs(authors) do + add('

' .. author .. '

') + end + if date ~= "" then + add('

' .. date .. '

') + end + add(body) + if #notes > 0 then + add('
    ') + for _,note in pairs(notes) do + add(note) + end + add('
') + end + add('') + add('') + return table.concat(buffer,'\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 LineBreak() + return "
" +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 SmallCaps(s) + return '' .. s .. '' +end + +function Strikeout(s) + return '' .. s .. '' +end + +function Link(s, src, tit) + return "" .. s .. "" +end + +function Image(s, src, tit) + return "" +end + +function Code(s, attr) + return "" .. escape(s) .. "" +end + +function InlineMath(s) + return "\\(" .. escape(s) .. "\\)" +end + +function DisplayMath(s) + return "\\[" .. escape(s) .. "\\]" +end + +function Note(s) + local num = #notes + 1 + -- insert the back reference right before the final closing tag. + s = string.gsub(s, + '(.*)' .. s .. '') + -- return the footnote reference, linked to the note. + return '' .. num .. '' +end + +function Plain(s) + return s +end + +function Para(s) + return "

" .. s .. "

" +end + +-- lev is an integer, the header level. +function Header(lev, s, attr) + return "" .. s .. "" +end + +function BlockQuote(s) + return "
\n" .. s .. "\n
" +end + +function HorizontalRule() + return "
" +end + +function CodeBlock(s, attr) + -- If code block has class 'dot', pipe the contents through dot + -- and base64, and include the base64-encoded png as a data: URL. + if attr.class and string.match(' ' .. attr.class .. ' ',' dot ') then + local png = pipe("base64", pipe("dot -Tpng", s)) + return '' + -- otherwise treat as code (one could pipe through a highlighter) + else + return "
" .. escape(s) ..
+           "
" + end +end + +function BulletList(items) + local buffer = {} + for _, item in pairs(items) do + table.insert(buffer, "
  • " .. item .. "
  • ") + end + return "" +end + +function OrderedList(items) + local buffer = {} + for _, item in pairs(items) do + table.insert(buffer, "
  • " .. item .. "
  • ") + end + return "
      \n" .. table.concat(buffer, "\n") .. "\n
    " +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,"
    " .. k .. "
    \n
    " .. + table.concat(v,"
    \n
    ") .. "
    ") + end + end + return "
    \n" .. table.concat(buffer, "\n") .. "\n
    " +end + +-- Convert pandoc alignment to something HTML can use. +-- align is AlignLeft, AlignRight, AlignCenter, or AlignDefault. +function html_align(align) + if align == 'AlignLeft' then + return 'left' + elseif align == 'AlignRight' then + return 'right' + elseif align == 'AlignCenter' then + return 'center' + else + return 'left' + end +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 + add("") + if caption ~= "" then + add("") + end + if widths and widths[1] ~= 0 then + for _, w in pairs(widths) do + add('') + end + end + local header_row = {} + local empty_header = true + for i, h in pairs(headers) do + local align = html_align(aligns[i]) + table.insert(header_row,'') + empty_header = empty_header and h == "" + end + if empty_header then + head = "" + else + add('') + for _,h in pairs(header_row) do + add(h) + end + add('') + end + local class = "even" + for _, row in pairs(rows) do + class = (class == "even" and "odd") or "even" + add('') + for i,c in pairs(row) do + add('') + end + add('') + end + add('
    " .. caption .. "
    ' .. h .. '
    ' .. c .. '