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 return {c = {listAttributes,items}}
320 {{"start", "style", "delimiter"}, "content"}
323--- Creates a para element.
325-- @tparam {Inline,...} content inline content
326-- @treturn Block block quote element
327M.Para = M.Block:create_constructor(
329 function(content) return {c = content} end,
333--- Creates a plain element.
335-- @tparam {Inline,...} content inline content
336-- @treturn Block block quote element
337M.Plain = M.Block:create_constructor(
339 function(content) return {c = content} end,
343--- Creates a raw content block of the specified format.
345-- @tparam string format format of content
346-- @tparam string text string content
347-- @treturn Block block quote element
348M.RawBlock = M.Block:create_constructor(
350 function(format, text) return {c = {format, text}} end,
354--- Creates a table element.
356-- @tparam {Inline,...} caption table caption
357-- @tparam {AlignDefault|AlignLeft|AlignRight|AlignCenter,...} aligns alignments
358-- @tparam {int,...} widths column widths
359-- @tparam {Block,...} headers header row
360-- @tparam {{Block,...}} rows table rows
361-- @treturn Block block quote element
362M.Table = M.Block:create_constructor(
364 function(caption, aligns, widths, headers, rows)
365 return {c = {caption, aligns, widths, headers, rows}}
367 {"caption", "aligns", "widths", "headers", "rows"}
371------------------------------------------------------------------------
375--- Inline element class
376M.Inline = Element:make_subtype{}
377M.Inline.__call = function (t, ...)
381--- Creates a Cite inline element
383-- @tparam {Inline,...} content List of inlines
384-- @tparam {Citation,...} citations List of citations
385-- @treturn Inline citations element
386M.Cite = M.Inline:create_constructor(
388 function(content, citations) return {c = {citations, content}} end,
389 {"citations", "content"}
392--- Creates a Code inline element
394-- @tparam string text brief image description
395-- @tparam[opt] Attr attr additional attributes
396-- @treturn Inline code element
397M.Code = M.Inline:create_constructor(
399 function(text, attr) return {c = {attr or M.Attr(), text}} end,
400 {{"identifier", "classes", "attributes"}, "text"}
403--- Creates an inline element representing emphasised text.
405-- @tparam {Inline,..} content inline content
406-- @treturn Inline emphasis element
407M.Emph = M.Inline:create_constructor(
409 function(content) return {c = content} end,
413--- Creates a Image inline element
415-- @tparam {Inline,..} caption text used to describe the image
416-- @tparam string src path to the image file
417-- @tparam[opt] string title brief image description
418-- @tparam[opt] Attr attr additional attributes
419-- @treturn Inline image element
420M.Image = M.Inline:create_constructor(
422 function(caption, src, title, attr)
424 attr = attr or M.Attr()
425 return {c = {attr, caption, {src, title}}}
427 {"attributes", "caption", {"src", "title"}}
430--- Create a LineBreak inline element
431-- @function LineBreak
432-- @treturn Inline linebreak element
433M.LineBreak = M.Inline:create_constructor(
435 function() return {} end
438--- Creates a link inline element, usually a hyperlink.
440-- @tparam {Inline,..} content text for this link
441-- @tparam string target the link target
442-- @tparam[opt] string title brief link description
443-- @tparam[opt] Attr attr additional attributes
444-- @treturn Inline image element
445M.Link = M.Inline:create_constructor(
447 function(content, target, title, attr)
449 attr = attr or M.Attr()
450 return {c = {attr, content, {target, title}}}
452 {"attributes", "content", {"target", "title"}}
455--- Creates a Math element, either inline or displayed.
457-- @tparam "InlineMath"|"DisplayMath" mathtype rendering specifier
458-- @tparam string text Math content
459-- @treturn Inline Math element
460M.Math = M.Inline:create_constructor(
462 function(mathtype, text)
463 return {c = {mathtype, text}}
467--- Creates a DisplayMath element (DEPRECATED).
468-- @function DisplayMath
469-- @tparam string text Math content
470-- @treturn Inline Math element
471M.DisplayMath = M.Inline:create_constructor(
473 function(text) return M.Math("DisplayMath", text) end,
476--- Creates an InlineMath inline element (DEPRECATED).
477-- @function InlineMath
478-- @tparam string text Math content
479-- @treturn Inline Math element
480M.InlineMath = M.Inline:create_constructor(
482 function(text) return M.Math("InlineMath", text) end,
486--- Creates a Note inline element
488-- @tparam {Block,...} content footnote block content
489M.Note = M.Inline:create_constructor(
491 function(content) return {c = content} end,
495--- Creates a Quoted inline element given the quote type and quoted content.
497-- @tparam "DoubleQuote"|"SingleQuote" quotetype type of quotes to be used
498-- @tparam {Inline,..} content inline content
499-- @treturn Inline quoted element
500M.Quoted = M.Inline:create_constructor(
502 function(quotetype, content) return {c = {quotetype, content}} end,
503 {"quotetype", "content"}
505--- Creates a single-quoted inline element (DEPRECATED).
506-- @function SingleQuoted
507-- @tparam {Inline,..} content inline content
508-- @treturn Inline quoted element
510M.SingleQuoted = M.Inline:create_constructor(
512 function(content) return M.Quoted(M.SingleQuote, content) end,
513 {"quotetype", "content"}
515--- Creates a single-quoted inline element (DEPRECATED).
516-- @function DoubleQuoted
517-- @tparam {Inline,..} content inline content
518-- @treturn Inline quoted element
520M.DoubleQuoted = M.Inline:create_constructor(
522 function(content) return M.Quoted("DoubleQuote", content) end,
523 {"quotetype", "content"}
526--- Creates a RawInline inline element
527-- @function RawInline
528-- @tparam string format format of the contents
529-- @tparam string text string content
530-- @treturn Inline raw inline element
531M.RawInline = M.Inline:create_constructor(
533 function(format, text) return {c = {format, text}} end,
537--- Creates text rendered in small caps
538-- @function SmallCaps
539-- @tparam {Inline,..} content inline content
540-- @treturn Inline smallcaps element
541M.SmallCaps = M.Inline:create_constructor(
543 function(content) return {c = content} end,
547--- Creates a SoftBreak inline element.
548-- @function SoftBreak
549-- @treturn Inline softbreak element
550M.SoftBreak = M.Inline:create_constructor(
552 function() return {} end
555--- Create a Space inline element
557-- @treturn Inline space element
558M.Space = M.Inline:create_constructor(
560 function() return {} end
563--- Creates a Span inline element
565-- @tparam {Inline,..} content inline content
566-- @tparam[opt] Attr attr additional attributes
567-- @treturn Inline span element
568M.Span = M.Inline:create_constructor(
570 function(content, attr) return {c = {attr or M.Attr(), content}} end,
571 {{"identifier", "classes", "attributes"}, "content"}
574--- Creates a Str inline element
576-- @tparam string text content
577-- @treturn Inline string element
578M.Str = M.Inline:create_constructor(
580 function(text) return {c = text} end,
584--- Creates text which is striked out.
585-- @function Strikeout
586-- @tparam {Inline,..} content inline content
587-- @treturn Inline strikeout element
588M.Strikeout = M.Inline:create_constructor(
590 function(content) return {c = content} end,
594--- Creates a Strong element, whose text is usually displayed in a bold font.
596-- @tparam {Inline,..} content inline content
597-- @treturn Inline strong element
598M.Strong = M.Inline:create_constructor(
600 function(content) return {c = content} end,
604--- Creates a Subscript inline element
605-- @function Subscript
606-- @tparam {Inline,..} content inline content
607-- @treturn Inline subscript element
608M.Subscript = M.Inline:create_constructor(
610 function(content) return {c = content} end,
614--- Creates a Superscript inline element
615-- @function Superscript
616-- @tparam {Inline,..} content inline content
617-- @treturn Inline strong element
618M.Superscript = M.Inline:create_constructor(
620 function(content) return {c = content} end,
625------------------------------------------------------------------------
631M.Attr._field_names = {identifier = 1, classes = 2, attributes = 3}
632--- Create a new set of attributes (Attr).
634-- @tparam[opt] string identifier element identifier
635-- @tparam[opt] {string,...} classes element classes
636-- @tparam[opt] table attributes table containing string keys and values
637-- @return element attributes
638M.Attr.__call = function(t, identifier, classes, attributes)
639 identifier = identifier or ''
640 classes = classes or {}
641 attributes = attributes or {}
642 local attr = {identifier, classes, attributes}
643 setmetatable(attr, t)
646M.Attr.__index = function(t, k)
647 return rawget(t, k) or
648 rawget(t, M.Attr._field_names[k]) or
649 rawget(getmetatable(t), k)
651M.Attr.__newindex = function(t, k, v)
652 if M.Attr._field_names[k] then
653 rawset(t, M.Attr._field_names[k], v)
658setmetatable(M.Attr, M.Attr)
661--- Creates a single citation.
663-- @tparam string id citation identifier (like a bibtex key)
664-- @tparam AuthorInText|SuppressAuthor|NormalCitation mode citation mode
665-- @tparam[opt] {Inline,...} prefix citation prefix
666-- @tparam[opt] {Inline,...} suffix citation suffix
667-- @tparam[opt] int note_num note number
668-- @tparam[opt] int hash hash number
669M.Citation = function(id, mode, prefix, suffix, note_num, hash)
670 prefix = prefix or {}
671 suffix = suffix or {}
672 note_num = note_num or 0
676 citationPrefix = prefix,
677 citationSuffix = suffix,
679 citationNoteNum = note_num,
685------------------------------------------------------------------------
689--- Author name is mentioned in the text.
692M.AuthorInText = "AuthorInText"
694--- Author name is suppressed.
697M.SuppressAuthor = "SuppressAuthor"
699--- Default citation style is used.
702M.NormalCitation = "NormalCitation"
704--- Table cells aligned left.
706M.AlignLeft = "AlignLeft"
708--- Table cells right-aligned.
710M.AlignRight = "AlignRight"
712--- Table cell content is centered.
714M.AlignCenter = "AlignCenter"
716--- Table cells are alignment is unaltered.
718M.AlignDefault = "AlignDefault"
720--- Default list number delimiters are used.
722M.DefaultDelim = "DefaultDelim"
724--- List numbers are delimited by a period.
728--- List numbers are delimited by a single parenthesis.
730M.OneParen = "OneParen"
732--- List numbers are delimited by a double parentheses.
734M.TwoParens = "TwoParens"
736--- List are numbered in the default style
738M.DefaultStyle = "DefaultStyle"
740--- List items are numbered as examples.
744--- List are numbered using decimal integers.
748--- List are numbered using lower-case roman numerals.
750M.LowerRoman = "LowerRoman"
752--- List are numbered using upper-case roman numerals
754M.UpperRoman = "UpperRoman"
756--- List are numbered using lower-case alphabetic characters.
758M.LowerAlpha = "LowerAlpha"
760--- List are numbered using upper-case alphabetic characters.
762M.UpperAlpha = "UpperAlpha"
765------------------------------------------------------------------------
769--- Parse the given string into a Pandoc document.
770-- The method used to interpret input is specified by *format*. Acceptable
771-- values for this parameter are equal to those that can be given to the
772-- `--from` command line option.
773-- @tparam string markup the markup to be parsed
774-- @tparam[opt] string format format specification, defaults to "markdown".
775-- @return Doc pandoc document
777-- local org_markup = "/emphasis/" -- Input to be read
778-- local document = pandoc.read(org_markup, "org")
779-- -- Get the first block of the document
780-- local block = document.blocks[1]
781-- -- The inline element in that block is an `Emph`
782-- assert(block.content[1].t == "Emph")
783function M.read(markup, format)
784 format = format or "markdown"
785 local pd = pandoc.__read(format, markup)
786 if type(pd) == "string" then
793--- Use functions defined in the global namespace to create a pandoc filter.
794-- All globally defined functions which have names of pandoc elements are
795-- collected into a new table.
796-- @return A list of filter functions
798-- -- within a file defining a pandoc filter:
800-- return pandoc.Str(utf8.upper(text))
803-- return {pandoc.global_filter()}
804-- -- the above is equivallent to
805-- -- return {{Str = Str}}
806function M.global_filter()
808 function is_filter_function(k)
809 return M.Inline.constructor[k] or
810 M.Block.constructor[k] or
811 k == "Meta" or k == "Doc" or k == "Pandoc"
813 for k, v in pairs(_G) do
814 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