2017-03-20 15:17:03 +01:00
|
|
|
--[[
|
|
|
|
pandoc.lua
|
|
|
|
|
2017-04-13 19:10:51 +02:00
|
|
|
Copyright © 2017 Albert Krewinkel
|
2017-03-20 15:17:03 +01:00
|
|
|
|
|
|
|
Permission to use, copy, modify, and/or distribute this software for any purpose
|
|
|
|
with or without fee is hereby granted, provided that the above copyright notice
|
|
|
|
and this permission notice appear in all copies.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
|
|
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
|
|
|
FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
|
|
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
|
|
|
|
OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
|
|
|
|
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
|
|
|
|
THIS SOFTWARE.
|
|
|
|
]]
|
|
|
|
|
2017-04-13 19:10:51 +02:00
|
|
|
---
|
|
|
|
-- Lua functions for pandoc scripts.
|
|
|
|
--
|
|
|
|
-- @author Albert Krewinkel
|
|
|
|
-- @copyright © 2017 Albert Krewinkel
|
|
|
|
-- @license MIT
|
2017-03-20 15:17:03 +01:00
|
|
|
local M = {
|
2017-04-13 19:10:51 +02:00
|
|
|
_VERSION = "0.2.0"
|
2017-03-20 15:17:03 +01:00
|
|
|
}
|
|
|
|
|
2017-04-12 21:21:25 +02:00
|
|
|
------------------------------------------------------------------------
|
2017-04-13 19:10:51 +02:00
|
|
|
-- The base class for pandoc's AST elements.
|
|
|
|
-- @type Element
|
|
|
|
-- @local
|
2017-03-20 15:17:03 +01:00
|
|
|
local Element = {}
|
2017-04-12 21:21:25 +02:00
|
|
|
|
2017-03-20 15:17:03 +01:00
|
|
|
--- Create a new element subtype
|
2017-04-13 19:10:51 +02:00
|
|
|
-- @local
|
2017-03-20 15:17:03 +01:00
|
|
|
function Element:make_subtype(o)
|
|
|
|
o = o or {}
|
|
|
|
setmetatable(o, self)
|
|
|
|
self.__index = self
|
|
|
|
return o
|
|
|
|
end
|
2017-04-12 21:21:25 +02:00
|
|
|
|
2017-03-20 15:17:03 +01:00
|
|
|
--- Create a new element given its tag and arguments
|
2017-04-13 19:10:51 +02:00
|
|
|
-- @local
|
2017-03-20 15:17:03 +01:00
|
|
|
function Element:new(tag, ...)
|
|
|
|
local element = { t = tag }
|
|
|
|
local content = {...}
|
|
|
|
-- special case for unary constructors
|
|
|
|
if #content == 1 then
|
|
|
|
element.c = content[1]
|
|
|
|
-- Don't set 'c' field if no further arguments were given. This is important
|
|
|
|
-- for nullary constructors like `Space` and `HorizontalRule`.
|
|
|
|
elseif #content > 0 then
|
|
|
|
element.c = content
|
|
|
|
end
|
|
|
|
setmetatable(element, self)
|
|
|
|
self.__index = self
|
|
|
|
return element
|
|
|
|
end
|
|
|
|
|
2017-04-12 21:21:25 +02:00
|
|
|
--- Create a new constructor
|
2017-04-13 19:10:51 +02:00
|
|
|
-- @local
|
2017-04-12 21:21:25 +02:00
|
|
|
-- @param tag Tag used to identify the constructor
|
|
|
|
-- @param fn Function to be called when constructing a new element
|
|
|
|
-- @return function that constructs a new element
|
2017-04-15 20:00:35 +02:00
|
|
|
function Element:create_constructor(tag, fn, accessors)
|
|
|
|
local constr = self:make_subtype({tag = tag, getters = {}, setters = {}})
|
|
|
|
|
|
|
|
-- Add accessors to the metatable
|
|
|
|
if type(accessors) == "string" then
|
|
|
|
constr.getters[accessors] = function(elem)
|
|
|
|
return elem.c
|
|
|
|
end
|
|
|
|
constr.setters[accessors] = function(elem, v)
|
|
|
|
elem.c = v
|
|
|
|
end
|
|
|
|
else
|
|
|
|
for i = 1, #(accessors or {}) do
|
|
|
|
if type(accessors[i]) == "string" then
|
|
|
|
constr.getters[accessors[i]] = function(elem)
|
|
|
|
return elem.c[i]
|
|
|
|
end
|
|
|
|
constr.setters[accessors[i]] = function(elem, v)
|
|
|
|
elem.c[i] = v
|
|
|
|
end
|
|
|
|
else -- only two levels of nesting are supported
|
|
|
|
for k, v in ipairs(accessors[i]) do
|
|
|
|
constr.getters[v] = function(elem)
|
|
|
|
return elem.c[i][k]
|
|
|
|
end
|
|
|
|
constr.setters[v] = function(elem, v)
|
|
|
|
elem.c[i][k] = v
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-04-12 21:21:25 +02:00
|
|
|
function constr:new(...)
|
|
|
|
local obj = fn(...)
|
|
|
|
setmetatable(obj, self)
|
|
|
|
self.__index = function(t, k)
|
2017-04-15 20:00:35 +02:00
|
|
|
if getmetatable(t).getters[k] then
|
|
|
|
return getmetatable(t).getters[k](t)
|
2017-04-12 21:21:25 +02:00
|
|
|
elseif k == "t" then
|
|
|
|
return getmetatable(t)["tag"]
|
|
|
|
else
|
|
|
|
return getmetatable(t)[k]
|
|
|
|
end
|
|
|
|
end
|
2017-04-15 20:00:35 +02:00
|
|
|
self.__newindex = function(t, k, v)
|
|
|
|
if getmetatable(t).setters[k] then
|
|
|
|
getmetatable(t).setters[k](t, v)
|
|
|
|
else
|
|
|
|
rawset(t, k, v)
|
|
|
|
end
|
|
|
|
end
|
2017-04-12 21:21:25 +02:00
|
|
|
return obj
|
|
|
|
end
|
2017-04-13 19:10:51 +02:00
|
|
|
self.constructor = self.constructor or {}
|
|
|
|
self.constructor[tag] = constr
|
2017-04-12 21:21:25 +02:00
|
|
|
return constr
|
|
|
|
end
|
|
|
|
|
2017-04-13 19:10:51 +02:00
|
|
|
--- Calls the constructor, creating a new element.
|
|
|
|
-- @local
|
2017-04-12 21:21:25 +02:00
|
|
|
function Element.__call(t, ...)
|
|
|
|
return t:new(...)
|
|
|
|
end
|
|
|
|
|
|
|
|
------------------------------------------------------------------------
|
2017-04-13 19:10:51 +02:00
|
|
|
--- Pandoc Document
|
|
|
|
-- @section document
|
|
|
|
|
|
|
|
--- A complete pandoc document
|
|
|
|
-- @function Doc
|
|
|
|
-- @tparam {Block,...} blocks document content
|
|
|
|
-- @tparam[opt] Meta meta document meta data
|
|
|
|
function M.Doc(blocks, meta)
|
|
|
|
meta = meta or {}
|
2017-03-20 15:17:03 +01:00
|
|
|
return {
|
|
|
|
["blocks"] = blocks,
|
|
|
|
["meta"] = meta,
|
|
|
|
["pandoc-api-version"] = {1,17,0,5},
|
|
|
|
}
|
|
|
|
end
|
2017-04-13 19:10:51 +02:00
|
|
|
|
|
|
|
|
2017-04-13 22:57:50 +02:00
|
|
|
------------------------------------------------------------------------
|
|
|
|
-- MetaValue
|
|
|
|
-- @section MetaValue
|
|
|
|
M.MetaValue = Element:make_subtype{}
|
|
|
|
M.MetaValue.__call = function(t, ...)
|
|
|
|
return t:new(...)
|
|
|
|
end
|
|
|
|
--- Meta blocks
|
|
|
|
-- @function MetaBlocks
|
|
|
|
-- @tparam {Block,...} blocks blocks
|
2017-04-14 10:33:38 +02:00
|
|
|
|
2017-04-13 22:57:50 +02:00
|
|
|
--- Meta inlines
|
|
|
|
-- @function MetaInlines
|
|
|
|
-- @tparam {Inline,...} inlines inlines
|
2017-04-14 10:33:38 +02:00
|
|
|
|
2017-04-13 22:57:50 +02:00
|
|
|
--- Meta list
|
|
|
|
-- @function MetaList
|
|
|
|
-- @tparam {MetaValue,...} meta_values list of meta values
|
2017-04-14 10:33:38 +02:00
|
|
|
|
2017-04-13 22:57:50 +02:00
|
|
|
--- Meta map
|
|
|
|
-- @function MetaMap
|
|
|
|
-- @tparam table a string-index map of meta values
|
|
|
|
M.meta_value_types = {
|
|
|
|
"MetaBlocks",
|
|
|
|
"MetaInlines",
|
|
|
|
"MetaList",
|
|
|
|
"MetaMap",
|
|
|
|
}
|
|
|
|
for i = 1, #M.meta_value_types do
|
|
|
|
M[M.meta_value_types[i]] = M.MetaValue:create_constructor(
|
|
|
|
M.meta_value_types[i],
|
|
|
|
function(content)
|
2017-04-16 21:00:01 +02:00
|
|
|
return content
|
|
|
|
end
|
2017-04-13 22:57:50 +02:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2017-04-16 21:00:01 +02:00
|
|
|
--- Creates string to be used in meta data.
|
|
|
|
-- Does nothing, lua strings are meta strings.
|
|
|
|
-- @function MetaString
|
|
|
|
-- @tparam string str string value
|
|
|
|
function M.MetaString(str)
|
|
|
|
return str
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Creates boolean to be used in meta data.
|
|
|
|
-- Does nothing, lua booleans are meta booleans.
|
|
|
|
-- @function MetaBool
|
|
|
|
-- @tparam boolean bool boolean value
|
|
|
|
function M.MetaBool(bool)
|
|
|
|
return bool
|
|
|
|
end
|
|
|
|
|
2017-04-14 10:33:38 +02:00
|
|
|
------------------------------------------------------------------------
|
2017-04-15 00:12:51 +02:00
|
|
|
-- Blocks
|
2017-04-14 10:33:38 +02:00
|
|
|
-- @section Block
|
|
|
|
|
2017-04-15 00:12:51 +02:00
|
|
|
--- Block elements
|
2017-04-14 10:33:38 +02:00
|
|
|
M.Block = Element:make_subtype{}
|
|
|
|
M.Block.__call = function (t, ...)
|
2017-04-12 21:21:25 +02:00
|
|
|
return t:new(...)
|
|
|
|
end
|
|
|
|
|
2017-04-14 10:33:38 +02:00
|
|
|
--- Creates a block quote element
|
|
|
|
-- @function BlockQuote
|
|
|
|
-- @tparam {Block,...} content block content
|
|
|
|
-- @treturn Block block quote element
|
|
|
|
M.BlockQuote = M.Block:create_constructor(
|
|
|
|
"BlockQuote",
|
2017-04-15 20:00:35 +02:00
|
|
|
function(content) return {c = content} end,
|
|
|
|
"content"
|
2017-04-14 10:33:38 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
--- Creates a bullet (i.e. unordered) list.
|
|
|
|
-- @function BulletList
|
|
|
|
-- @tparam {{Block,...},...} content list of items
|
|
|
|
-- @treturn Block block quote element
|
|
|
|
M.BulletList = M.Block:create_constructor(
|
|
|
|
"BulletList",
|
2017-04-15 20:00:35 +02:00
|
|
|
function(content) return {c = content} end,
|
|
|
|
"content"
|
2017-04-14 10:33:38 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
--- Creates a code block element
|
|
|
|
-- @function CodeBlock
|
2017-04-15 20:00:35 +02:00
|
|
|
-- @tparam string text code string
|
2017-05-18 00:04:48 +02:00
|
|
|
-- @tparam[opt] Attr attr element attributes
|
2017-04-14 10:33:38 +02:00
|
|
|
-- @treturn Block code block element
|
|
|
|
M.CodeBlock = M.Block:create_constructor(
|
|
|
|
"CodeBlock",
|
2017-05-18 00:04:48 +02:00
|
|
|
function(text, attr) return {c = {attr or M.Attr(), text}} end,
|
2017-04-15 20:00:35 +02:00
|
|
|
{{"identifier", "classes", "attributes"}, "text"}
|
2017-04-14 10:33:38 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
--- Creates a definition list, containing terms and their explanation.
|
|
|
|
-- @function DefinitionList
|
|
|
|
-- @tparam {{{Inline,...},{Block,...}},...} content list of items
|
|
|
|
-- @treturn Block block quote element
|
|
|
|
M.DefinitionList = M.Block:create_constructor(
|
|
|
|
"DefinitionList",
|
2017-04-15 20:00:35 +02:00
|
|
|
function(content) return {c = content} end,
|
|
|
|
"content"
|
2017-04-14 10:33:38 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
--- Creates a div element
|
|
|
|
-- @function Div
|
|
|
|
-- @tparam {Block,...} content block content
|
2017-05-18 00:04:48 +02:00
|
|
|
-- @tparam[opt] Attr attr element attributes
|
2017-04-14 10:33:38 +02:00
|
|
|
-- @treturn Block code block element
|
|
|
|
M.Div = M.Block:create_constructor(
|
|
|
|
"Div",
|
2017-05-18 00:04:48 +02:00
|
|
|
function(content, attr)
|
|
|
|
return {c = {attr or M.Attr(), content}}
|
|
|
|
end,
|
2017-04-15 20:00:35 +02:00
|
|
|
{{"identifier", "classes", "attributes"}, "content"}
|
2017-04-14 10:33:38 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
--- Creates a block quote element.
|
|
|
|
-- @function Header
|
|
|
|
-- @tparam int level header level
|
|
|
|
-- @tparam {Inline,...} content inline content
|
2017-05-18 00:04:48 +02:00
|
|
|
-- @tparam[opt] Attr attr element attributes
|
2017-04-14 10:33:38 +02:00
|
|
|
-- @treturn Block header element
|
|
|
|
M.Header = M.Block:create_constructor(
|
|
|
|
"Header",
|
2017-05-18 00:04:48 +02:00
|
|
|
function(level, content, attr)
|
|
|
|
return {c = {level, attr or M.Attr(), content}}
|
2017-04-15 20:00:35 +02:00
|
|
|
end,
|
|
|
|
{"level", {"identifier", "classes", "attributes"}, "content"}
|
2017-04-14 10:33:38 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
--- Creates a horizontal rule.
|
|
|
|
-- @function HorizontalRule
|
|
|
|
-- @treturn Block horizontal rule
|
|
|
|
M.HorizontalRule = M.Block:create_constructor(
|
|
|
|
"HorizontalRule",
|
|
|
|
function() return {} end
|
|
|
|
)
|
|
|
|
|
|
|
|
--- Creates a line block element.
|
|
|
|
-- @function LineBlock
|
|
|
|
-- @tparam {{Inline,...},...} content inline content
|
|
|
|
-- @treturn Block block quote element
|
|
|
|
M.LineBlock = M.Block:create_constructor(
|
|
|
|
"LineBlock",
|
2017-04-15 20:00:35 +02:00
|
|
|
function(content) return {c = content} end,
|
|
|
|
"content"
|
2017-04-14 10:33:38 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
--- Creates a null element.
|
|
|
|
-- @function Null
|
|
|
|
-- @treturn Block null element
|
|
|
|
M.Null = M.Block:create_constructor(
|
|
|
|
"Null",
|
|
|
|
function() return {} end
|
|
|
|
)
|
|
|
|
|
|
|
|
--- Creates an ordered list.
|
|
|
|
-- @function OrderedList
|
|
|
|
-- @tparam {{Block,...},...} items list items
|
|
|
|
-- @param[opt] listAttributes list parameters
|
|
|
|
-- @treturn Block
|
|
|
|
M.OrderedList = M.Block:create_constructor(
|
|
|
|
"OrderedList",
|
|
|
|
function(items, listAttributes)
|
|
|
|
return {c = {listAttributes,items}}
|
2017-04-15 20:00:35 +02:00
|
|
|
end,
|
|
|
|
{{"start", "style", "delimiter"}, "content"}
|
2017-04-14 10:33:38 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
--- Creates a para element.
|
|
|
|
-- @function Para
|
|
|
|
-- @tparam {Inline,...} content inline content
|
|
|
|
-- @treturn Block block quote element
|
|
|
|
M.Para = M.Block:create_constructor(
|
|
|
|
"Para",
|
2017-04-15 20:00:35 +02:00
|
|
|
function(content) return {c = content} end,
|
|
|
|
"content"
|
2017-04-14 10:33:38 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
--- Creates a plain element.
|
|
|
|
-- @function Plain
|
|
|
|
-- @tparam {Inline,...} content inline content
|
|
|
|
-- @treturn Block block quote element
|
|
|
|
M.Plain = M.Block:create_constructor(
|
|
|
|
"Plain",
|
2017-04-15 20:00:35 +02:00
|
|
|
function(content) return {c = content} end,
|
|
|
|
"content"
|
2017-04-14 10:33:38 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
--- Creates a raw content block of the specified format.
|
|
|
|
-- @function RawBlock
|
|
|
|
-- @tparam string format format of content
|
2017-04-15 20:00:35 +02:00
|
|
|
-- @tparam string text string content
|
2017-04-14 10:33:38 +02:00
|
|
|
-- @treturn Block block quote element
|
|
|
|
M.RawBlock = M.Block:create_constructor(
|
|
|
|
"RawBlock",
|
2017-04-15 20:00:35 +02:00
|
|
|
function(format, text) return {c = {format, text}} end,
|
|
|
|
{"format", "text"}
|
2017-04-14 10:33:38 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
--- Creates a table element.
|
|
|
|
-- @function Table
|
|
|
|
-- @tparam {Inline,...} caption table caption
|
|
|
|
-- @tparam {AlignDefault|AlignLeft|AlignRight|AlignCenter,...} aligns alignments
|
|
|
|
-- @tparam {int,...} widths column widths
|
|
|
|
-- @tparam {Block,...} headers header row
|
|
|
|
-- @tparam {{Block,...}} rows table rows
|
|
|
|
-- @treturn Block block quote element
|
|
|
|
M.Table = M.Block:create_constructor(
|
|
|
|
"Table",
|
|
|
|
function(caption, aligns, widths, headers, rows)
|
|
|
|
return {c = {caption, aligns, widths, headers, rows}}
|
|
|
|
end
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2017-04-12 21:21:25 +02:00
|
|
|
------------------------------------------------------------------------
|
2017-04-13 19:10:51 +02:00
|
|
|
-- Inline
|
|
|
|
-- @section Inline
|
2017-04-12 21:21:25 +02:00
|
|
|
|
2017-04-14 10:33:38 +02:00
|
|
|
--- Inline element class
|
|
|
|
M.Inline = Element:make_subtype{}
|
|
|
|
M.Inline.__call = function (t, ...)
|
|
|
|
return t:new(...)
|
|
|
|
end
|
|
|
|
|
2017-04-13 19:10:51 +02:00
|
|
|
--- Creates a Cite inline element
|
|
|
|
-- @function Cite
|
|
|
|
-- @tparam {Inline,...} content List of inlines
|
|
|
|
-- @tparam {Citation,...} citations List of citations
|
|
|
|
-- @treturn Inline citations element
|
|
|
|
M.Cite = M.Inline:create_constructor(
|
2017-04-12 21:21:25 +02:00
|
|
|
"Cite",
|
2017-04-15 20:00:35 +02:00
|
|
|
function(content, citations) return {c = {citations, content}} end,
|
|
|
|
{"citations", "content"}
|
2017-04-12 21:21:25 +02:00
|
|
|
)
|
2017-04-13 19:10:51 +02:00
|
|
|
|
|
|
|
--- Creates a Code inline element
|
|
|
|
-- @function Code
|
2017-04-15 20:00:35 +02:00
|
|
|
-- @tparam string text brief image description
|
2017-05-18 00:04:48 +02:00
|
|
|
-- @tparam[opt] Attr attr additional attributes
|
2017-04-13 19:10:51 +02:00
|
|
|
-- @treturn Inline code element
|
|
|
|
M.Code = M.Inline:create_constructor(
|
2017-04-12 21:21:25 +02:00
|
|
|
"Code",
|
2017-05-18 00:04:48 +02:00
|
|
|
function(text, attr) return {c = {attr or M.Attr(), text}} end,
|
2017-04-15 20:00:35 +02:00
|
|
|
{{"identifier", "classes", "attributes"}, "text"}
|
2017-04-12 21:21:25 +02:00
|
|
|
)
|
2017-04-13 19:10:51 +02:00
|
|
|
|
|
|
|
--- Creates an inline element representing emphasised text.
|
|
|
|
-- @function Emph
|
|
|
|
-- @tparam {Inline,..} content inline content
|
|
|
|
-- @treturn Inline emphasis element
|
|
|
|
M.Emph = M.Inline:create_constructor(
|
2017-04-12 21:21:25 +02:00
|
|
|
"Emph",
|
2017-04-15 20:00:35 +02:00
|
|
|
function(content) return {c = content} end,
|
|
|
|
"content"
|
2017-04-12 21:21:25 +02:00
|
|
|
)
|
2017-04-13 19:10:51 +02:00
|
|
|
|
|
|
|
--- Creates a Image inline element
|
|
|
|
-- @function Image
|
|
|
|
-- @tparam {Inline,..} caption text used to describe the image
|
|
|
|
-- @tparam string src path to the image file
|
|
|
|
-- @tparam[opt] string title brief image description
|
2017-05-18 00:04:48 +02:00
|
|
|
-- @tparam[opt] Attr attr additional attributes
|
2017-04-13 19:10:51 +02:00
|
|
|
-- @treturn Inline image element
|
|
|
|
M.Image = M.Inline:create_constructor(
|
2017-04-12 21:21:25 +02:00
|
|
|
"Image",
|
2017-05-18 00:04:48 +02:00
|
|
|
function(caption, src, title, attr)
|
2017-04-13 19:10:51 +02:00
|
|
|
title = title or ""
|
2017-05-18 00:04:48 +02:00
|
|
|
attr = attr or M.Attr()
|
|
|
|
return {c = {attr, caption, {src, title}}}
|
2017-04-15 20:00:35 +02:00
|
|
|
end,
|
|
|
|
{"attributes", "caption", {"src", "title"}}
|
2017-04-12 21:21:25 +02:00
|
|
|
)
|
2017-04-13 19:10:51 +02:00
|
|
|
|
2017-04-12 21:21:25 +02:00
|
|
|
--- Create a LineBreak inline element
|
2017-04-13 19:10:51 +02:00
|
|
|
-- @function LineBreak
|
|
|
|
-- @treturn Inline linebreak element
|
|
|
|
M.LineBreak = M.Inline:create_constructor(
|
2017-04-12 21:21:25 +02:00
|
|
|
"LineBreak",
|
|
|
|
function() return {} end
|
|
|
|
)
|
2017-04-13 19:10:51 +02:00
|
|
|
|
|
|
|
--- Creates a link inline element, usually a hyperlink.
|
|
|
|
-- @function Link
|
|
|
|
-- @tparam {Inline,..} content text for this link
|
|
|
|
-- @tparam string target the link target
|
|
|
|
-- @tparam[opt] string title brief link description
|
2017-05-18 00:04:48 +02:00
|
|
|
-- @tparam[opt] Attr attr additional attributes
|
2017-04-13 19:10:51 +02:00
|
|
|
-- @treturn Inline image element
|
|
|
|
M.Link = M.Inline:create_constructor(
|
2017-04-12 21:21:25 +02:00
|
|
|
"Link",
|
2017-05-18 00:04:48 +02:00
|
|
|
function(content, target, title, attr)
|
2017-04-13 19:10:51 +02:00
|
|
|
title = title or ""
|
2017-05-18 00:04:48 +02:00
|
|
|
attr = attr or M.Attr()
|
|
|
|
return {c = {attr, content, {target, title}}}
|
2017-04-15 20:00:35 +02:00
|
|
|
end,
|
|
|
|
{"attributes", "content", {"target", "title"}}
|
2017-04-12 21:21:25 +02:00
|
|
|
)
|
2017-04-13 19:10:51 +02:00
|
|
|
|
2017-04-15 00:10:33 +02:00
|
|
|
--- Creates a Math element, either inline or displayed. It is usually simpler to
|
|
|
|
-- use one of the specialized functions @{InlineMath} or @{DisplayMath} instead.
|
2017-04-13 19:10:51 +02:00
|
|
|
-- @function Math
|
2017-04-15 00:10:33 +02:00
|
|
|
-- @tparam "InlineMath"|"DisplayMath" mathtype rendering specifier
|
2017-04-13 19:10:51 +02:00
|
|
|
-- @tparam string text Math content
|
|
|
|
-- @treturn Inline Math element
|
|
|
|
M.Math = M.Inline:create_constructor(
|
2017-04-12 21:21:25 +02:00
|
|
|
"Math",
|
2017-04-13 19:10:51 +02:00
|
|
|
function(mathtype, text)
|
|
|
|
return {c = {mathtype, text}}
|
2017-04-15 20:00:35 +02:00
|
|
|
end,
|
|
|
|
{"mathtype", "text"}
|
2017-04-12 21:21:25 +02:00
|
|
|
)
|
2017-04-15 00:10:33 +02:00
|
|
|
--- Creates a DisplayMath element.
|
|
|
|
-- @function DisplayMath
|
|
|
|
-- @tparam string text Math content
|
|
|
|
-- @treturn Inline Math element
|
|
|
|
M.DisplayMath = M.Inline:create_constructor(
|
|
|
|
"DisplayMath",
|
2017-04-15 20:00:35 +02:00
|
|
|
function(text) return M.Math("DisplayMath", text) end,
|
|
|
|
{"mathtype", "text"}
|
2017-04-15 00:10:33 +02:00
|
|
|
)
|
|
|
|
--- Creates an InlineMath inline element.
|
|
|
|
-- @function InlineMath
|
|
|
|
-- @tparam string text Math content
|
|
|
|
-- @treturn Inline Math element
|
|
|
|
M.InlineMath = M.Inline:create_constructor(
|
|
|
|
"InlineMath",
|
2017-04-15 20:00:35 +02:00
|
|
|
function(text) return M.Math("InlineMath", text) end,
|
|
|
|
{"mathtype", "text"}
|
2017-04-15 00:10:33 +02:00
|
|
|
)
|
2017-04-13 19:10:51 +02:00
|
|
|
|
|
|
|
--- Creates a Note inline element
|
|
|
|
-- @function Note
|
|
|
|
-- @tparam {Block,...} content footnote block content
|
|
|
|
M.Note = M.Inline:create_constructor(
|
2017-04-12 21:21:25 +02:00
|
|
|
"Note",
|
2017-04-15 20:00:35 +02:00
|
|
|
function(content) return {c = content} end,
|
|
|
|
"content"
|
2017-04-12 21:21:25 +02:00
|
|
|
)
|
2017-04-13 19:10:51 +02:00
|
|
|
|
2017-04-15 00:10:33 +02:00
|
|
|
--- Creates a Quoted inline element given the quote type and quoted content. It
|
|
|
|
-- is usually simpler to use one of the specialized functions @{SingleQuoted} or
|
|
|
|
-- @{DoubleQuoted} instead.
|
2017-04-13 19:10:51 +02:00
|
|
|
-- @function Quoted
|
2017-04-15 00:10:33 +02:00
|
|
|
-- @tparam "DoubleQuote"|"SingleQuote" quotetype type of quotes to be used
|
2017-04-13 19:10:51 +02:00
|
|
|
-- @tparam {Inline,..} content inline content
|
|
|
|
-- @treturn Inline quoted element
|
|
|
|
M.Quoted = M.Inline:create_constructor(
|
2017-04-12 21:21:25 +02:00
|
|
|
"Quoted",
|
2017-04-15 20:00:35 +02:00
|
|
|
function(quotetype, content) return {c = {quotetype, content}} end,
|
|
|
|
{"quotetype", "content"}
|
2017-04-12 21:21:25 +02:00
|
|
|
)
|
2017-04-15 00:10:33 +02:00
|
|
|
--- Creates a single-quoted inline element.
|
|
|
|
-- @function SingleQuoted
|
|
|
|
-- @tparam {Inline,..} content inline content
|
|
|
|
-- @treturn Inline quoted element
|
|
|
|
-- @see Quoted
|
|
|
|
M.SingleQuoted = M.Inline:create_constructor(
|
|
|
|
"SingleQuoted",
|
2017-04-15 20:00:35 +02:00
|
|
|
function(content) return M.Quoted(M.SingleQuote, content) end,
|
|
|
|
{"quotetype", "content"}
|
2017-04-15 00:10:33 +02:00
|
|
|
)
|
|
|
|
--- Creates a single-quoted inline element.
|
|
|
|
-- @function DoubleQuoted
|
|
|
|
-- @tparam {Inline,..} content inline content
|
|
|
|
-- @treturn Inline quoted element
|
|
|
|
-- @see Quoted
|
|
|
|
M.DoubleQuoted = M.Inline:create_constructor(
|
|
|
|
"DoubleQuoted",
|
2017-04-15 20:00:35 +02:00
|
|
|
function(content) return M.Quoted("DoubleQuote", content) end,
|
|
|
|
{"quotetype", "content"}
|
2017-04-15 00:10:33 +02:00
|
|
|
)
|
|
|
|
|
2017-04-13 19:10:51 +02:00
|
|
|
--- Creates a RawInline inline element
|
|
|
|
-- @function RawInline
|
|
|
|
-- @tparam string format format of the contents
|
|
|
|
-- @tparam string text string content
|
|
|
|
-- @treturn Inline raw inline element
|
|
|
|
M.RawInline = M.Inline:create_constructor(
|
2017-04-12 21:21:25 +02:00
|
|
|
"RawInline",
|
2017-04-15 20:00:35 +02:00
|
|
|
function(format, text) return {c = {format, text}} end,
|
|
|
|
{"format", "text"}
|
2017-04-12 21:21:25 +02:00
|
|
|
)
|
2017-04-13 19:10:51 +02:00
|
|
|
|
|
|
|
--- Creates text rendered in small caps
|
|
|
|
-- @function SmallCaps
|
|
|
|
-- @tparam {Inline,..} content inline content
|
|
|
|
-- @treturn Inline smallcaps element
|
|
|
|
M.SmallCaps = M.Inline:create_constructor(
|
2017-04-12 21:21:25 +02:00
|
|
|
"SmallCaps",
|
2017-04-15 20:00:35 +02:00
|
|
|
function(content) return {c = content} end,
|
|
|
|
"content"
|
2017-04-12 21:21:25 +02:00
|
|
|
)
|
2017-04-13 19:10:51 +02:00
|
|
|
|
|
|
|
--- Creates a SoftBreak inline element.
|
|
|
|
-- @function SoftBreak
|
|
|
|
-- @treturn Inline softbreak element
|
|
|
|
M.SoftBreak = M.Inline:create_constructor(
|
2017-04-12 21:21:25 +02:00
|
|
|
"SoftBreak",
|
|
|
|
function() return {} end
|
|
|
|
)
|
2017-04-13 19:10:51 +02:00
|
|
|
|
2017-04-12 21:21:25 +02:00
|
|
|
--- Create a Space inline element
|
2017-04-13 19:10:51 +02:00
|
|
|
-- @function Space
|
|
|
|
-- @treturn Inline space element
|
|
|
|
M.Space = M.Inline:create_constructor(
|
2017-04-12 21:21:25 +02:00
|
|
|
"Space",
|
|
|
|
function() return {} end
|
|
|
|
)
|
2017-04-13 19:10:51 +02:00
|
|
|
|
|
|
|
--- Creates a Span inline element
|
|
|
|
-- @function Span
|
|
|
|
-- @tparam {Inline,..} content inline content
|
2017-05-18 00:04:48 +02:00
|
|
|
-- @tparam[opt] Attr attr additional attributes
|
2017-04-13 19:10:51 +02:00
|
|
|
-- @treturn Inline span element
|
|
|
|
M.Span = M.Inline:create_constructor(
|
2017-04-12 21:21:25 +02:00
|
|
|
"Span",
|
2017-05-18 00:04:48 +02:00
|
|
|
function(content, attr) return {c = {attr or M.Attr(), content}} end,
|
2017-04-15 20:00:35 +02:00
|
|
|
{{"identifier", "classes", "attributes"}, "content"}
|
2017-04-12 21:21:25 +02:00
|
|
|
)
|
2017-04-13 19:10:51 +02:00
|
|
|
|
|
|
|
--- Creates a Str inline element
|
|
|
|
-- @function Str
|
|
|
|
-- @tparam string text content
|
|
|
|
-- @treturn Inline string element
|
|
|
|
M.Str = M.Inline:create_constructor(
|
2017-04-12 21:21:25 +02:00
|
|
|
"Str",
|
2017-04-15 20:00:35 +02:00
|
|
|
function(text) return {c = text} end,
|
|
|
|
"text"
|
2017-04-12 21:21:25 +02:00
|
|
|
)
|
2017-04-13 19:10:51 +02:00
|
|
|
|
|
|
|
--- Creates text which is striked out.
|
|
|
|
-- @function Strikeout
|
|
|
|
-- @tparam {Inline,..} content inline content
|
|
|
|
-- @treturn Inline strikeout element
|
|
|
|
M.Strikeout = M.Inline:create_constructor(
|
2017-04-12 21:21:25 +02:00
|
|
|
"Strikeout",
|
2017-04-15 20:00:35 +02:00
|
|
|
function(content) return {c = content} end,
|
|
|
|
"content"
|
2017-04-12 21:21:25 +02:00
|
|
|
)
|
2017-04-13 19:10:51 +02:00
|
|
|
|
|
|
|
--- Creates a Strong element, whose text is usually displayed in a bold font.
|
|
|
|
-- @function Strong
|
|
|
|
-- @tparam {Inline,..} content inline content
|
|
|
|
-- @treturn Inline strong element
|
|
|
|
M.Strong = M.Inline:create_constructor(
|
2017-04-12 21:21:25 +02:00
|
|
|
"Strong",
|
2017-04-15 20:00:35 +02:00
|
|
|
function(content) return {c = content} end,
|
|
|
|
"content"
|
2017-04-12 21:21:25 +02:00
|
|
|
)
|
2017-04-13 19:10:51 +02:00
|
|
|
|
|
|
|
--- Creates a Subscript inline element
|
|
|
|
-- @function Subscript
|
|
|
|
-- @tparam {Inline,..} content inline content
|
|
|
|
-- @treturn Inline subscript element
|
|
|
|
M.Subscript = M.Inline:create_constructor(
|
2017-04-12 21:21:25 +02:00
|
|
|
"Subscript",
|
2017-04-15 20:00:35 +02:00
|
|
|
function(content) return {c = content} end,
|
|
|
|
"content"
|
2017-04-12 21:21:25 +02:00
|
|
|
)
|
2017-04-13 19:10:51 +02:00
|
|
|
|
|
|
|
--- Creates a Superscript inline element
|
|
|
|
-- @function Superscript
|
|
|
|
-- @tparam {Inline,..} content inline content
|
|
|
|
-- @treturn Inline strong element
|
|
|
|
M.Superscript = M.Inline:create_constructor(
|
2017-04-12 21:21:25 +02:00
|
|
|
"Superscript",
|
2017-04-15 20:00:35 +02:00
|
|
|
function(content) return {c = content} end,
|
|
|
|
"content"
|
2017-04-12 21:21:25 +02:00
|
|
|
)
|
|
|
|
|
2017-03-20 15:17:03 +01:00
|
|
|
|
2017-04-13 19:10:51 +02:00
|
|
|
------------------------------------------------------------------------
|
2017-04-15 00:12:51 +02:00
|
|
|
-- Helpers
|
|
|
|
-- @section helpers
|
2017-03-20 15:17:03 +01:00
|
|
|
|
2017-04-30 11:50:09 +02:00
|
|
|
-- Attr
|
|
|
|
M.Attr = {}
|
|
|
|
M.Attr._field_names = {identifier = 1, classes = 2, attributes = 3}
|
2017-04-15 00:12:51 +02:00
|
|
|
--- Create a new set of attributes (Attr).
|
2017-04-30 11:50:09 +02:00
|
|
|
-- @function Attr
|
|
|
|
-- @tparam[opt] string identifier element identifier
|
2017-04-15 00:12:51 +02:00
|
|
|
-- @tparam[opt] {string,...} classes element classes
|
2017-04-30 11:50:09 +02:00
|
|
|
-- @tparam[opt] table attributes table containing string keys and values
|
2017-04-15 00:12:51 +02:00
|
|
|
-- @return element attributes
|
2017-04-30 11:50:09 +02:00
|
|
|
M.Attr.__call = function(t, identifier, classes, attributes)
|
|
|
|
identifier = identifier or ''
|
2017-04-15 00:12:51 +02:00
|
|
|
classes = classes or {}
|
2017-04-30 11:50:09 +02:00
|
|
|
attributes = attributes or {}
|
|
|
|
local attr = {identifier, classes, attributes}
|
2017-04-15 00:12:51 +02:00
|
|
|
setmetatable(attr, t)
|
|
|
|
return attr
|
|
|
|
end
|
2017-04-30 11:50:09 +02:00
|
|
|
M.Attr.__index = function(t, k)
|
|
|
|
return rawget(t, k) or
|
|
|
|
rawget(t, M.Attr._field_names[k]) or
|
|
|
|
rawget(getmetatable(t), k)
|
2017-04-15 09:31:09 +02:00
|
|
|
end
|
2017-04-30 11:50:09 +02:00
|
|
|
M.Attr.__newindex = function(t, k, v)
|
|
|
|
if M.Attr._field_names[k] then
|
|
|
|
rawset(t, M.Attr._field_names[k], v)
|
2017-04-15 09:31:09 +02:00
|
|
|
else
|
2017-04-30 11:50:09 +02:00
|
|
|
rawset(t, k, v)
|
2017-04-15 09:31:09 +02:00
|
|
|
end
|
|
|
|
end
|
2017-04-30 11:50:09 +02:00
|
|
|
setmetatable(M.Attr, M.Attr)
|
2017-04-15 09:31:09 +02:00
|
|
|
|
2017-04-13 19:10:51 +02:00
|
|
|
|
2017-04-15 00:12:51 +02:00
|
|
|
--- Creates a single citation.
|
|
|
|
-- @function Citation
|
|
|
|
-- @tparam string id citation identifier (like a bibtex key)
|
|
|
|
-- @tparam AuthorInText|SuppressAuthor|NormalCitation mode citation mode
|
|
|
|
-- @tparam[opt] {Inline,...} prefix citation prefix
|
|
|
|
-- @tparam[opt] {Inline,...} suffix citation suffix
|
|
|
|
-- @tparam[opt] int note_num note number
|
|
|
|
-- @tparam[opt] int note_num hash number
|
|
|
|
M.Citation = function(id, mode, prefix, suffix, note_num, hash)
|
|
|
|
prefix = prefix or {}
|
|
|
|
suffix = suffix or {}
|
|
|
|
note_num = note_num or 0
|
|
|
|
hash = hash or 0
|
|
|
|
return {
|
|
|
|
citationId = id,
|
|
|
|
citationPrefix = prefix,
|
|
|
|
citationSuffix = suffix,
|
|
|
|
citationMode = mode,
|
|
|
|
citationNoteNum = note_num,
|
|
|
|
citationHash = hash,
|
|
|
|
}
|
|
|
|
end
|
2017-03-20 15:17:03 +01:00
|
|
|
|
2017-04-15 00:12:51 +02:00
|
|
|
|
|
|
|
------------------------------------------------------------------------
|
|
|
|
-- Constants
|
|
|
|
-- @section constants
|
2017-04-13 19:10:51 +02:00
|
|
|
|
|
|
|
--- Author name is mentioned in the text.
|
|
|
|
-- @see Citation
|
|
|
|
-- @see Cite
|
2017-04-14 22:32:08 +02:00
|
|
|
M.AuthorInText = "AuthorInText"
|
2017-04-13 19:10:51 +02:00
|
|
|
|
|
|
|
--- Author name is suppressed.
|
|
|
|
-- @see Citation
|
|
|
|
-- @see Cite
|
2017-04-14 22:32:08 +02:00
|
|
|
M.SuppressAuthor = "SuppressAuthor"
|
2017-04-13 19:10:51 +02:00
|
|
|
|
|
|
|
--- Default citation style is used.
|
|
|
|
-- @see Citation
|
|
|
|
-- @see Cite
|
2017-04-14 22:32:08 +02:00
|
|
|
M.NormalCitation = "NormalCitation"
|
2017-04-13 19:10:51 +02:00
|
|
|
|
2017-04-14 10:33:38 +02:00
|
|
|
--- Table cells aligned left.
|
|
|
|
-- @see Table
|
2017-04-14 22:32:08 +02:00
|
|
|
M.AlignLeft = "AlignLeft"
|
2017-04-14 10:33:38 +02:00
|
|
|
|
|
|
|
--- Table cells right-aligned.
|
|
|
|
-- @see Table
|
2017-04-14 22:32:08 +02:00
|
|
|
M.AlignRight = "AlignRight"
|
2017-04-14 10:33:38 +02:00
|
|
|
|
|
|
|
--- Table cell content is centered.
|
|
|
|
-- @see Table
|
2017-04-14 22:32:08 +02:00
|
|
|
M.AlignCenter = "AlignCenter"
|
2017-04-14 10:33:38 +02:00
|
|
|
|
|
|
|
--- Table cells are alignment is unaltered.
|
|
|
|
-- @see Table
|
2017-04-14 22:32:08 +02:00
|
|
|
M.AlignDefault = "AlignDefault"
|
2017-04-14 10:33:38 +02:00
|
|
|
|
|
|
|
--- Default list number delimiters are used.
|
|
|
|
-- @see OrderedList
|
2017-04-14 22:32:08 +02:00
|
|
|
M.DefaultDelim = "DefaultDelim"
|
2017-04-14 10:33:38 +02:00
|
|
|
|
|
|
|
--- List numbers are delimited by a period.
|
|
|
|
-- @see OrderedList
|
2017-04-14 22:32:08 +02:00
|
|
|
M.Period = "Period"
|
2017-04-14 10:33:38 +02:00
|
|
|
|
|
|
|
--- List numbers are delimited by a single parenthesis.
|
|
|
|
-- @see OrderedList
|
2017-04-14 22:32:08 +02:00
|
|
|
M.OneParen = "OneParen"
|
2017-04-14 10:33:38 +02:00
|
|
|
|
|
|
|
--- List numbers are delimited by a double parentheses.
|
|
|
|
-- @see OrderedList
|
2017-04-14 22:32:08 +02:00
|
|
|
M.TwoParens = "TwoParens"
|
2017-04-14 10:33:38 +02:00
|
|
|
|
|
|
|
--- List are numbered in the default style
|
|
|
|
-- @see OrderedList
|
2017-04-14 22:32:08 +02:00
|
|
|
M.DefaultStyle = "DefaultStyle"
|
2017-04-14 10:33:38 +02:00
|
|
|
|
|
|
|
--- List items are numbered as examples.
|
|
|
|
-- @see OrderedList
|
2017-04-14 22:32:08 +02:00
|
|
|
M.Example = "Example"
|
2017-04-14 10:33:38 +02:00
|
|
|
|
|
|
|
--- List are numbered using decimal integers.
|
|
|
|
-- @see OrderedList
|
2017-04-14 22:32:08 +02:00
|
|
|
M.Decimal = "Decimal"
|
2017-04-14 10:33:38 +02:00
|
|
|
|
|
|
|
--- List are numbered using lower-case roman numerals.
|
|
|
|
-- @see OrderedList
|
2017-04-14 22:32:08 +02:00
|
|
|
M.LowerRoman = "LowerRoman"
|
2017-04-14 10:33:38 +02:00
|
|
|
|
|
|
|
--- List are numbered using upper-case roman numerals
|
|
|
|
-- @see OrderedList
|
2017-04-14 22:32:08 +02:00
|
|
|
M.UpperRoman = "UpperRoman"
|
2017-04-14 10:33:38 +02:00
|
|
|
|
|
|
|
--- List are numbered using lower-case alphabetic characters.
|
|
|
|
-- @see OrderedList
|
2017-04-14 22:32:08 +02:00
|
|
|
M.LowerAlpha = "LowerAlpha"
|
2017-04-14 10:33:38 +02:00
|
|
|
|
|
|
|
--- List are numbered using upper-case alphabetic characters.
|
|
|
|
-- @see OrderedList
|
2017-04-14 22:32:08 +02:00
|
|
|
M.UpperAlpha = "UpperAlpha"
|
2017-04-14 10:33:38 +02:00
|
|
|
|
2017-04-13 19:10:51 +02:00
|
|
|
|
|
|
|
------------------------------------------------------------------------
|
|
|
|
-- Helper Functions
|
|
|
|
-- @section helpers
|
|
|
|
|
2017-04-17 11:54:42 +02:00
|
|
|
--- Parse the given string into a Pandoc document.
|
2017-04-30 11:24:48 +02:00
|
|
|
-- The method used to interpret input is specified by *format*. Acceptable
|
|
|
|
-- values for this parameter are equal to those that can be given to the
|
|
|
|
-- `--from` command line option.
|
|
|
|
-- @tparam string markup the markup to be parsed
|
2017-04-17 11:54:42 +02:00
|
|
|
-- @tparam[opt] string format format specification, defaults to "markdown".
|
|
|
|
-- @return Doc pandoc document
|
2017-04-30 11:24:48 +02:00
|
|
|
-- @usage
|
|
|
|
-- local org_markup = "/emphasis/" -- Input to be read
|
|
|
|
-- local document = pandoc.read(org_markup, "org")
|
|
|
|
-- -- Get the first block of the document
|
|
|
|
-- local block = document.blocks[1]
|
|
|
|
-- -- The inline element in that block is an `Emph`
|
|
|
|
-- assert(block.content[1].t == "Emph")
|
2017-04-17 11:54:42 +02:00
|
|
|
function M.read(markup, format)
|
|
|
|
format = format or "markdown"
|
|
|
|
local pd = pandoc.__read(format, markup)
|
|
|
|
if type(pd) == "string" then
|
|
|
|
error(pd)
|
|
|
|
else
|
|
|
|
return pd
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-04-13 19:10:51 +02:00
|
|
|
--- Use functions defined in the global namespace to create a pandoc filter.
|
|
|
|
-- All globally defined functions which have names of pandoc elements are
|
|
|
|
-- collected into a new table.
|
|
|
|
-- @return A list of filter functions
|
|
|
|
-- @usage
|
|
|
|
-- -- within a file defining a pandoc filter:
|
|
|
|
-- function Str(text)
|
|
|
|
-- return pandoc.Str(utf8.upper(text))
|
|
|
|
-- end
|
|
|
|
--
|
|
|
|
-- return {pandoc.global_filter()}
|
|
|
|
-- -- the above is equivallent to
|
|
|
|
-- -- return {{Str = Str}}
|
2017-03-20 15:17:03 +01:00
|
|
|
function M.global_filter()
|
|
|
|
local res = {}
|
|
|
|
for k, v in pairs(_G) do
|
2017-04-30 16:14:33 +02:00
|
|
|
if M.Inline.constructor[k] or M.Block.constructor[k] or k == "Doc" then
|
2017-03-20 15:17:03 +01:00
|
|
|
res[k] = v
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return res
|
|
|
|
end
|
|
|
|
|
|
|
|
return M
|