4Copyright © 2017 Albert Krewinkel
6Permission to use, copy, modify, and/or distribute this software for any purpose
7with or without fee is hereby granted, provided that the above copyright notice
8and this permission notice appear in all copies.
10THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
11REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
12FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
13INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
14OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
15TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
20-- Lua functions for pandoc scripts.
22-- @author Albert Krewinkel
23-- @copyright © 2017 Albert Krewinkel
29------------------------------------------------------------------------
30-- The base class for pandoc's AST elements.
35--- Create a new element subtype
37function Element:make_subtype(o)
44--- Create a new element given its tag and arguments
46function Element:new(tag, ...)
47 local element = { t = tag }
49 -- special case for unary constructors
51 element.c = content[1]
52 -- Don't set 'c' field if no further arguments were given. This is important
53 -- for nullary constructors like `Space` and `HorizontalRule`.
54 elseif #content > 0 then
57 setmetatable(element, self)
62--- Create a new constructor
64-- @param tag Tag used to identify the constructor
65-- @param fn Function to be called when constructing a new element
66-- @param accessors names to use as accessors for numerical fields
67-- @return function that constructs a new element
68function Element:create_constructor(tag, fn, accessors)
69 local constr = self:make_subtype({tag = tag, getters = {}, setters = {}})
71 -- Add accessors to the metatable
72 if type(accessors) == "string" then
73 constr.getters[accessors] = function(elem)
76 constr.setters[accessors] = function(elem, v)
80 for i = 1, #(accessors or {}) do
81 if type(accessors[i]) == "string" then
82 constr.getters[accessors[i]] = function(elem)
85 constr.setters[accessors[i]] = function(elem, v)
88 else -- only two levels of nesting are supported
89 for k, v in ipairs(accessors[i]) do
90 constr.getters[v] = function(elem)
93 constr.setters[v] = function(elem, v)
101 function constr:new(...)
103 setmetatable(obj, self)
104 self.__index = function(t, k)
105 if getmetatable(t).getters[k] then
106 return getmetatable(t).getters[k](t)
108 return getmetatable(t)["tag"]
110 return getmetatable(t)[k]
113 self.__newindex = function(t, k, v)
114 if getmetatable(t).setters[k] then
115 getmetatable(t).setters[k](t, v)
122 self.constructor = self.constructor or {}
123 self.constructor[tag] = constr
127--- Calls the constructor, creating a new element.
129function Element.__call(t, ...)
133------------------------------------------------------------------------
137--- A complete pandoc document
139-- @tparam {Block,...} blocks document content
140-- @tparam[opt] Meta meta document meta data
141function M.Pandoc(blocks, meta)
146 ["pandoc-api-version"] = {1,17,0,5},
150-- DEPRECATED synonym:
153------------------------------------------------------------------------
156M.MetaValue = Element:make_subtype{}
157M.MetaValue.__call = function(t, ...)
161-- @function MetaBlocks
162-- @tparam {Block,...} blocks blocks
165-- @function MetaInlines
166-- @tparam {Inline,...} inlines inlines
170-- @tparam {MetaValue,...} meta_values list of meta values
174-- @tparam table key_value_map a string-indexed map of meta values
175M.meta_value_types = {
181for i = 1, #M.meta_value_types do
182 M[M.meta_value_types[i]] = M.MetaValue:create_constructor(
183 M.meta_value_types[i],
190--- Creates string to be used in meta data.
191-- Does nothing, lua strings are meta strings.
192-- @function MetaString
193-- @tparam string str string value
194function M.MetaString(str)
198--- Creates boolean to be used in meta data.
199-- Does nothing, lua booleans are meta booleans.
201-- @tparam boolean bool boolean value
202function M.MetaBool(bool)
206------------------------------------------------------------------------
211M.Block = Element:make_subtype{}
212M.Block.__call = function (t, ...)
216--- Creates a block quote element
217-- @function BlockQuote
218-- @tparam {Block,...} content block content
219-- @treturn Block block quote element
220M.BlockQuote = M.Block:create_constructor(
222 function(content) return {c = content} end,
226--- Creates a bullet (i.e. unordered) list.
227-- @function BulletList
228-- @tparam {{Block,...},...} content list of items
229-- @treturn Block block quote element
230M.BulletList = M.Block:create_constructor(
232 function(content) return {c = content} end,
236--- Creates a code block element
237-- @function CodeBlock
238-- @tparam string text code string
239-- @tparam[opt] Attr attr element attributes
240-- @treturn Block code block element
241M.CodeBlock = M.Block:create_constructor(
243 function(text, attr) return {c = {attr or M.Attr(), text}} end,
244 {{"identifier", "classes", "attributes"}, "text"}
247--- Creates a definition list, containing terms and their explanation.
248-- @function DefinitionList
249-- @tparam {{{Inline,...},{Block,...}},...} content list of items
250-- @treturn Block block quote element
251M.DefinitionList = M.Block:create_constructor(
253 function(content) return {c = content} end,
257--- Creates a div element
259-- @tparam {Block,...} content block content
260-- @tparam[opt] Attr attr element attributes
261-- @treturn Block code block element
262M.Div = M.Block:create_constructor(
264 function(content, attr)
265 return {c = {attr or M.Attr(), content}}
267 {{"identifier", "classes", "attributes"}, "content"}
270--- Creates a block quote element.
272-- @tparam int level header level
273-- @tparam {Inline,...} content inline content
274-- @tparam[opt] Attr attr element attributes
275-- @treturn Block header element
276M.Header = M.Block:create_constructor(
278 function(level, content, attr)
279 return {c = {level, attr or M.Attr(), content}}
281 {"level", {"identifier", "classes", "attributes"}, "content"}
284--- Creates a horizontal rule.
285-- @function HorizontalRule
286-- @treturn Block horizontal rule
287M.HorizontalRule = M.Block:create_constructor(
289 function() return {} end
292--- Creates a line block element.
293-- @function LineBlock
294-- @tparam {{Inline,...},...} content inline content
295-- @treturn Block block quote element
296M.LineBlock = M.Block:create_constructor(
298 function(content) return {c = content} end,
302--- Creates a null element.
304-- @treturn Block null element
305M.Null = M.Block:create_constructor(
307 function() return {} end
310--- Creates an ordered list.
311-- @function OrderedList
312-- @tparam {{Block,...},...} items list items
313-- @param[opt] listAttributes list parameters
315M.OrderedList = M.Block:create_constructor(
317 function(items, listAttributes)
318 listAttributes = listAttributes or {1, M.DefaultStyle, M.DefaultDelim}
319 return {c = {listAttributes, items}}
321 {{"start", "style", "delimiter"}, "content"}
324--- Creates a para element.
326-- @tparam {Inline,...} content inline content
327-- @treturn Block block quote element
328M.Para = M.Block:create_constructor(
330 function(content) return {c = content} end,
334--- Creates a plain element.
336-- @tparam {Inline,...} content inline content
337-- @treturn Block block quote element
338M.Plain = M.Block:create_constructor(
340 function(content) return {c = content} end,
344--- Creates a raw content block of the specified format.
346-- @tparam string format format of content
347-- @tparam string text string content
348-- @treturn Block block quote element
349M.RawBlock = M.Block:create_constructor(
351 function(format, text) return {c = {format, text}} end,
355--- Creates a table element.
357-- @tparam {Inline,...} caption table caption
358-- @tparam {AlignDefault|AlignLeft|AlignRight|AlignCenter,...} aligns alignments
359-- @tparam {int,...} widths column widths
360-- @tparam {Block,...} headers header row
361-- @tparam {{Block,...}} rows table rows
362-- @treturn Block block quote element
363M.Table = M.Block:create_constructor(
365 function(caption, aligns, widths, headers, rows)
366 return {c = {caption, aligns, widths, headers, rows}}
368 {"caption", "aligns", "widths", "headers", "rows"}
372------------------------------------------------------------------------
376--- Inline element class
377M.Inline = Element:make_subtype{}
378M.Inline.__call = function (t, ...)
382--- Creates a Cite inline element
384-- @tparam {Inline,...} content List of inlines
385-- @tparam {Citation,...} citations List of citations
386-- @treturn Inline citations element
387M.Cite = M.Inline:create_constructor(
389 function(content, citations) return {c = {citations, content}} end,
390 {"citations", "content"}
393--- Creates a Code inline element
395-- @tparam string text brief image description
396-- @tparam[opt] Attr attr additional attributes
397-- @treturn Inline code element
398M.Code = M.Inline:create_constructor(
400 function(text, attr) return {c = {attr or M.Attr(), text}} end,
401 {{"identifier", "classes", "attributes"}, "text"}
404--- Creates an inline element representing emphasised text.
406-- @tparam {Inline,..} content inline content
407-- @treturn Inline emphasis element
408M.Emph = M.Inline:create_constructor(
410 function(content) return {c = content} end,
414--- Creates a Image inline element
416-- @tparam {Inline,..} caption text used to describe the image
417-- @tparam string src path to the image file
418-- @tparam[opt] string title brief image description
419-- @tparam[opt] Attr attr additional attributes
420-- @treturn Inline image element
421M.Image = M.Inline:create_constructor(
423 function(caption, src, title, attr)
425 attr = attr or M.Attr()
426 return {c = {attr, caption, {src, title}}}
428 {{"identifier", "classes", "attributes"}, "caption", {"src", "title"}}
431--- Create a LineBreak inline element
432-- @function LineBreak
433-- @treturn Inline linebreak element
434M.LineBreak = M.Inline:create_constructor(
436 function() return {} end
439--- Creates a link inline element, usually a hyperlink.
441-- @tparam {Inline,..} content text for this link
442-- @tparam string target the link target
443-- @tparam[opt] string title brief link description
444-- @tparam[opt] Attr attr additional attributes
445-- @treturn Inline image element
446M.Link = M.Inline:create_constructor(
448 function(content, target, title, attr)
450 attr = attr or M.Attr()
451 return {c = {attr, content, {target, title}}}
453 {{"identifier", "classes", "attributes"}, "content", {"target", "title"}}
456--- Creates a Math element, either inline or displayed.
458-- @tparam "InlineMath"|"DisplayMath" mathtype rendering specifier
459-- @tparam string text Math content
460-- @treturn Inline Math element
461M.Math = M.Inline:create_constructor(
463 function(mathtype, text)
464 return {c = {mathtype, text}}
468--- Creates a DisplayMath element (DEPRECATED).
469-- @function DisplayMath
470-- @tparam string text Math content
471-- @treturn Inline Math element
472M.DisplayMath = M.Inline:create_constructor(
474 function(text) return M.Math("DisplayMath", text) end,
477--- Creates an InlineMath inline element (DEPRECATED).
478-- @function InlineMath
479-- @tparam string text Math content
480-- @treturn Inline Math element
481M.InlineMath = M.Inline:create_constructor(
483 function(text) return M.Math("InlineMath", text) end,
487--- Creates a Note inline element
489-- @tparam {Block,...} content footnote block content
490M.Note = M.Inline:create_constructor(
492 function(content) return {c = content} end,
496--- Creates a Quoted inline element given the quote type and quoted content.
498-- @tparam "DoubleQuote"|"SingleQuote" quotetype type of quotes to be used
499-- @tparam {Inline,..} content inline content
500-- @treturn Inline quoted element
501M.Quoted = M.Inline:create_constructor(
503 function(quotetype, content) return {c = {quotetype, content}} end,
504 {"quotetype", "content"}
506--- Creates a single-quoted inline element (DEPRECATED).
507-- @function SingleQuoted
508-- @tparam {Inline,..} content inline content
509-- @treturn Inline quoted element
511M.SingleQuoted = M.Inline:create_constructor(
513 function(content) return M.Quoted(M.SingleQuote, content) end,
514 {"quotetype", "content"}
516--- Creates a single-quoted inline element (DEPRECATED).
517-- @function DoubleQuoted
518-- @tparam {Inline,..} content inline content
519-- @treturn Inline quoted element
521M.DoubleQuoted = M.Inline:create_constructor(
523 function(content) return M.Quoted("DoubleQuote", content) end,
524 {"quotetype", "content"}
527--- Creates a RawInline inline element
528-- @function RawInline
529-- @tparam string format format of the contents
530-- @tparam string text string content
531-- @treturn Inline raw inline element
532M.RawInline = M.Inline:create_constructor(
534 function(format, text) return {c = {format, text}} end,
538--- Creates text rendered in small caps
539-- @function SmallCaps
540-- @tparam {Inline,..} content inline content
541-- @treturn Inline smallcaps element
542M.SmallCaps = M.Inline:create_constructor(
544 function(content) return {c = content} end,
548--- Creates a SoftBreak inline element.
549-- @function SoftBreak
550-- @treturn Inline softbreak element
551M.SoftBreak = M.Inline:create_constructor(
553 function() return {} end
556--- Create a Space inline element
558-- @treturn Inline space element
559M.Space = M.Inline:create_constructor(
561 function() return {} end
564--- Creates a Span inline element
566-- @tparam {Inline,..} content inline content
567-- @tparam[opt] Attr attr additional attributes
568-- @treturn Inline span element
569M.Span = M.Inline:create_constructor(
571 function(content, attr) return {c = {attr or M.Attr(), content}} end,
572 {{"identifier", "classes", "attributes"}, "content"}
575--- Creates a Str inline element
577-- @tparam string text content
578-- @treturn Inline string element
579M.Str = M.Inline:create_constructor(
581 function(text) return {c = text} end,
585--- Creates text which is striked out.
586-- @function Strikeout
587-- @tparam {Inline,..} content inline content
588-- @treturn Inline strikeout element
589M.Strikeout = M.Inline:create_constructor(
591 function(content) return {c = content} end,
595--- Creates a Strong element, whose text is usually displayed in a bold font.
597-- @tparam {Inline,..} content inline content
598-- @treturn Inline strong element
599M.Strong = M.Inline:create_constructor(
601 function(content) return {c = content} end,
605--- Creates a Subscript inline element
606-- @function Subscript
607-- @tparam {Inline,..} content inline content
608-- @treturn Inline subscript element
609M.Subscript = M.Inline:create_constructor(
611 function(content) return {c = content} end,
615--- Creates a Superscript inline element
616-- @function Superscript
617-- @tparam {Inline,..} content inline content
618-- @treturn Inline strong element
619M.Superscript = M.Inline:create_constructor(
621 function(content) return {c = content} end,
626------------------------------------------------------------------------
632M.Attr._field_names = {identifier = 1, classes = 2, attributes = 3}
633--- Create a new set of attributes (Attr).
635-- @tparam[opt] string identifier element identifier
636-- @tparam[opt] {string,...} classes element classes
637-- @tparam[opt] table attributes table containing string keys and values
638-- @return element attributes
639M.Attr.__call = function(t, identifier, classes, attributes)
640 identifier = identifier or ''
641 classes = classes or {}
642 attributes = attributes or {}
643 local attr = {identifier, classes, attributes}
644 setmetatable(attr, t)
647M.Attr.__index = function(t, k)
648 return rawget(t, k) or
649 rawget(t, M.Attr._field_names[k]) or
650 rawget(getmetatable(t), k)
652M.Attr.__newindex = function(t, k, v)
653 if M.Attr._field_names[k] then
654 rawset(t, M.Attr._field_names[k], v)
659setmetatable(M.Attr, M.Attr)
662--- Creates a single citation.
664-- @tparam string id citation identifier (like a bibtex key)
665-- @tparam AuthorInText|SuppressAuthor|NormalCitation mode citation mode
666-- @tparam[opt] {Inline,...} prefix citation prefix
667-- @tparam[opt] {Inline,...} suffix citation suffix
668-- @tparam[opt] int note_num note number
669-- @tparam[opt] int hash hash number
670M.Citation = function(id, mode, prefix, suffix, note_num, hash)
671 prefix = prefix or {}
672 suffix = suffix or {}
673 note_num = note_num or 0
677 citationPrefix = prefix,
678 citationSuffix = suffix,
680 citationNoteNum = note_num,
686------------------------------------------------------------------------
690--- Author name is mentioned in the text.
693M.AuthorInText = "AuthorInText"
695--- Author name is suppressed.
698M.SuppressAuthor = "SuppressAuthor"
700--- Default citation style is used.
703M.NormalCitation = "NormalCitation"
705--- Table cells aligned left.
707M.AlignLeft = "AlignLeft"
709--- Table cells right-aligned.
711M.AlignRight = "AlignRight"
713--- Table cell content is centered.
715M.AlignCenter = "AlignCenter"
717--- Table cells are alignment is unaltered.
719M.AlignDefault = "AlignDefault"
721--- Default list number delimiters are used.
723M.DefaultDelim = "DefaultDelim"
725--- List numbers are delimited by a period.
729--- List numbers are delimited by a single parenthesis.
731M.OneParen = "OneParen"
733--- List numbers are delimited by a double parentheses.
735M.TwoParens = "TwoParens"
737--- List are numbered in the default style
739M.DefaultStyle = "DefaultStyle"
741--- List items are numbered as examples.
745--- List are numbered using decimal integers.
749--- List are numbered using lower-case roman numerals.
751M.LowerRoman = "LowerRoman"
753--- List are numbered using upper-case roman numerals
755M.UpperRoman = "UpperRoman"
757--- List are numbered using lower-case alphabetic characters.
759M.LowerAlpha = "LowerAlpha"
761--- List are numbered using upper-case alphabetic characters.
763M.UpperAlpha = "UpperAlpha"
766------------------------------------------------------------------------
770--- Parse the given string into a Pandoc document.
771-- The method used to interpret input is specified by *format*. Acceptable
772-- values for this parameter are equal to those that can be given to the
773-- `--from` command line option.
774-- @tparam string markup the markup to be parsed
775-- @tparam[opt] string format format specification, defaults to "markdown".
776-- @treturn Pandoc pandoc document
778-- local org_markup = "/emphasis/" -- Input to be read
779-- local document = pandoc.read(org_markup, "org")
780-- -- Get the first block of the document
781-- local block = document.blocks[1]
782-- -- The inline element in that block is an `Emph`
783-- assert(block.content[1].t == "Emph")
784function M.read(markup, format)
785 format = format or "markdown"
786 local pd = pandoc._read(format, markup)
787 if type(pd) == "string" then
794--- Runs command with arguments, passing it some input, and returns the output.
795-- @treturn string Output of command.
796-- @raise A table containing the keys `command`, `error_code`, and `output` is
797-- thrown if the command exits with a non-zero error code.
799-- local ec, output = pandoc.pipe("sed", {"-e","s/a/b/"}, "abc")
800function M.pipe (command, args, input)
801 local ec, output = pandoc._pipe(command, args, input)
804 { command = command, error_code = ec, output = output},
805 { __tostring = function(e)
806 return "Error running " .. e.command
807 .. " (error code " .. e.error_code .. "): "
817--- Use functions defined in the global namespace to create a pandoc filter.
818-- All globally defined functions which have names of pandoc elements are
819-- collected into a new table.
820-- @return A list of filter functions
822-- -- within a file defining a pandoc filter:
824-- return pandoc.Str(utf8.upper(text))
827-- return {pandoc.global_filter()}
828-- -- the above is equivallent to
829-- -- return {{Str = Str}}
830function M.global_filter()
832 function is_filter_function(k)
833 return M.Inline.constructor[k] or
834 M.Block.constructor[k] or
835 k == "Meta" or k == "Doc" or k == "Pandoc" or
836 k == "Block" or k == "Inline"
838 for k, v in pairs(_G) do
839 if is_filter_function(k) then
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list