From e01ed83975933e4c8eafcda7950db98342ddfd63 Mon Sep 17 00:00:00 2001 From: Case Duckworth Date: Fri, 6 Mar 2015 22:08:20 -0700 Subject: Switch to compile.lua for reasons --- lua/allwords.lua | 17 +++ lua/jttm.lua | 193 +++++++++++++++++++++++++++++++++ lua/river.lua | 226 ++++++++++++++++++++++++++++++++++++++ lua/sample.lua | 324 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ lua/test.lua | 304 +++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 1064 insertions(+) create mode 100644 lua/allwords.lua create mode 100644 lua/jttm.lua create mode 100644 lua/river.lua create mode 100644 lua/sample.lua create mode 100644 lua/test.lua (limited to 'lua') diff --git a/lua/allwords.lua b/lua/allwords.lua new file mode 100644 index 0000000..b87f08a --- /dev/null +++ b/lua/allwords.lua @@ -0,0 +1,17 @@ +function allwords () + local line = io.read() + local pos = 1 + return function () + while line do + local s, e = string.find(line, "%w+", pos) + if s then + pos = e + 1 + return string.sub(line, s, e) + else + line = io.read() + pos = 1 + end + end + return nil + end +end diff --git a/lua/jttm.lua b/lua/jttm.lua new file mode 100644 index 0000000..2ba101a --- /dev/null +++ b/lua/jttm.lua @@ -0,0 +1,193 @@ +-- Pandoc "Just the text, Ma'am" +-- (JTTM): a custom writer that +-- strips everything except for +-- the TEXT from a pandoc source +-- vim: fdm=marker +-- invoke with: pandoc -t jttm/jttm.lua + +-- Table to store footnotes so they are at the END of the document +local notes = {} + +-- This function is called once for the whole document. Parameters: +-- body is a string, metadata is a table, 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, metadata, variables) + local buffer = {} + local function add(s) + table.insert(buffer, s) + end + if metadata['title'] and metadata['title'] ~= "" then + add(string.upper(metadata['title'])) + end + if metadata['subtitle'] and metadata['subtitle'] ~= "" then + add(": " .. metadata['subtitle']) + end + add("\n") + -- TODO: epigraph.content, epigraph.attrib, dedication, other metadata? + add(body) + -- TODO: add notes at the end. + return table.concat(buffer, '\n') +end + +-- TODOs {{{ +function align(align) + -- TODO: is this necessary? +end + +function Note(s) + -- TODO +end + +-- convert Tables to csv? or tab-separated? +function Table(caption, aligns, widths, headers, rows) + local buffer = {} + local function add(s) + table.insert(buffer, s) + end + add("\n\n") + if caption ~= "" then + add("[" .. caption .. "]") + end + -- TODO: finish +end + +-- }}} +-- Remove all formatting {{{ +function Blocksep() + return "\n\n" +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 Code(s, attr) + return s +end + +function CodeBlock(s, attr) + return s +end + +function InlineMath(s) + return s +end + +function DisplayMath(s) + return s +end + +function Span(s, attr) + return s +end + +function Cite(s) + return s +end + +function Plain(s) + return s +end + +-- Links only include the link text +function Link(s, src, tit) + return s +end + +-- Images have nothing to give us +-- (but add a space just in case) +function Image(s, src, tit) + return " " +end + +function Str(s) + return s +end + +function Div(s, attr) + return s +end + +function Space(s) + return " " +end + +function LineBreak() + return "\n" +end + +function Para(s) + -- add paragraphing + return s .. "\n\n" +end +-- }}} +-- Leave just a little formatting {{{ +function Header(lev, s, attr) + if lev == 1 then + return "\n\n " .. string.upper(s) .. "\n\n" + elseif lev == 2 then + return "\n " .. string.upper(s) .. "\n" + else + return s + end +end + +function Blockquote(s) + return "\n\n" .. string.gsub(s, "*\n", " %0") +end + +function HorizontalRule(s) + return "\n\n\n" +end + +function BulletList(items) + local buffer = {} + for _, item in pairs(items) do + table.insert(buffer, "- " .. item .. "\n") + end + return "\n\n" .. table.concat(buffer, "\n") .. "\n\n" +end + +function DefinitionList(items) + local buffer = {} + for _, item in pairs(items) do + for k, v in pairs(item) do + table.insert(buffer, "\n" .. k .. ":\n " .. + table.concat(v, "\n ")) + end + end + return "\n\n" .. table.concat(buffer, "\n") .. "\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) + diff --git a/lua/river.lua b/lua/river.lua new file mode 100644 index 0000000..d060ba9 --- /dev/null +++ b/lua/river.lua @@ -0,0 +1,226 @@ +-- Pandoc River writer +-- it takes out all formatting, leaving only a river of text +-- running down the page: one word per line +-- vim: fdm=marker +-- invoke with: pandoc -t river.lua + +local function flow(s) + return s:gsub("%s+", "\n") +end + +local function nude(s) + -- Expand contractions + s = s:gsub("'%a+%s", function (x) + if x == "'ll" then + return " will " + elseif x == "'ve" then + return " have " + elseif x == "'re" then + return " are " + else + return x + end + end) + -- Get rid of quotes around words + s = s:gsub('"', ' ') + s = s:gsub("%s+'", ' ') + s = s:gsub("'%s+", ' ') + -- Remove HTML entities + s = s:gsub('&.-;', ' ') + s = s:gsub('%b<>', ' ') + -- Remove end-of-line backslashes + s = s:gsub('%s+\\$', ' ') + -- Remove dashes (not hyphens) + s = s:gsub('%-%-+', ' ') + s = s:gsub('%-%s', ' ') + -- Remove general punctuation + s = s:gsub('[%.!%?:;,%[%]%(%)<>]', ' ') + -- Remove extra spaces + s = s:gsub('%s+', ' ') + return s:lower() +end + +-- This function is called once for the whole document. Parameters: +-- body is a string, metadata is a table, 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, metadata, variables) + local buffer = "" + local function add(s) + buffer = buffer .. nude(s) .. "\n" + end + if metadata['title'] then + add(metadata['title']) + end + if metadata['subtitle'] then + add(metadata['subtitle']) + end + -- TODO: epigraph.content, epigraph.attrib, dedication, other metadata? + add(body) + return flow(buffer) +end + +-- Remove all formatting {{{ +function Note(s) + return nude(s) +end + +function Blocksep() + return "\n" +end +function Emph(s) + return nude(s) +end + +function Strong(s) + return nude(s) +end + +function Subscript(s) + return nude(s) +end + +function Superscript(s) + return nude(s) +end + +function SmallCaps(s) + return nude(s) +end + +function Strikeout(s) + return nude(s) +end + +function Code(s, attr) + return nude(s) +end + +function CodeBlock(s, attr) + return nude(s) +end + +function InlineMath(s) + return nude(s) +end + +function DisplayMath(s) + return nude(s) +end + +function Span(s, attr) + return nude(s) +end + +function Cite(s) + return nude(s) +end + +function Plain(s) + return nude(s) +end + +-- Links only include the link text +function Link(s, src, tit) + return nude(s) +end + +-- Images have nothing to give us +-- (but add a space just in case) +function Image(s, src, tit) + return "\n" +end + +function CaptionedImage(s, src, tit) + return "\n" +end + +function Str(s) + return nude(s) +end + +function Div(s, attr) + return nude(s) +end + +function Space(s) + return "\n" +end + +function LineBreak() + return "\n" +end + +function Para(s) + return nude(s) +end + +function Header(lev, s, attr) + return nude(s) +end + +function BlockQuote(s) + return nude(s) +end + +function HorizontalRule() + return "\n" +end + +function BulletList(items) + local buffer = "" + for _, item in pairs(items) do + buffer = buffer .. nude(item) .. "\n" + end + return buffer .. "\n" +end + +function OrderedList(items) + local buffer = "" + for _, item in pairs(items) do + buffer = buffer .. nude(item) .. "\n" + end + return buffer .. "\n" +end + +function DefinitionList(items) + local buffer = "" + for _, item in pairs(items) do + for k, v in pairs(item) do + buffer = buffer .. nude(k) .. "\n" .. nude(v) .. "\n" + end + end + return buffer .. "\n" +end + +function Table(caption, aligns, widths, headers, rows) + local buffer = "" + local function add(s) + buffer = buffer .. nude(s) .. "\n" + end + if caption ~= "" then + add(caption) + end + for _,h in pairs(headers) do + add(h) + end + for _, row in pairs(rows) do + for _, cell in pairs(row) do + add(cell) + end + end + return buffer +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) + diff --git a/lua/sample.lua b/lua/sample.lua new file mode 100644 index 0000000..a0c3c29 --- /dev/null +++ b/lua/sample.lua @@ -0,0 +1,324 @@ +-- 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 is a string, metadata is a table, 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, metadata, variables) + local buffer = {} + local function add(s) + table.insert(buffer, s) + end + add('') + add('') + add('') + add('' .. (metadata['title'] or '') .. '') + add('') + add('') + if metadata['title'] and metadata['title'] ~= "" then + add('

' .. metadata['title'] .. '

') + end + for _, author in pairs(metadata['author'] or {}) do + add('

' .. author .. '

') + end + if metadata['date'] and metadata['date'] ~= "" then + add('

' .. metadata.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 Span(s, attr) + return "" .. s .. "" +end + +function Cite(s) + return "" .. s .. "" +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('\n" .. s .. "" +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) + diff --git a/lua/test.lua b/lua/test.lua new file mode 100644 index 0000000..bfbafc2 --- /dev/null +++ b/lua/test.lua @@ -0,0 +1,304 @@ +-- 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 is a string, metadata is a table, 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, metadata, variables) + local buffer = {} + for k, v in pairs(metadata) do + if type(v) == "string" then + table.insert(buffer, string.upper(k) .. ': ' .. v) + else + table.insert(buffer, string.upper(k) .. ': ' .. table.concat(v)) + end + end + 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 Span(s, attr) + return "" .. s .. "" +end + +function Cite(s) + return "" .. s .. "" +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 "
      \n" .. table.concat(buffer, "\n") .. "\n
    " +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("
    " .. caption .. "
    ' .. h .. '
    ' .. c .. '
    ") + 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('\n" .. s .. "" +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) + + -- cgit 1.4.1-21-gabe81
    " .. caption .. "
    ' .. h .. '
    ' .. c .. '