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-- @return function that constructs a new element
67function Element:create_constructor(tag, fn, accessors)
68 local constr = self:make_subtype({tag = tag, getters = {}, setters = {}})
70 -- Add accessors to the metatable
71 if type(accessors) == "string" then
72 constr.getters[accessors] = function(elem)
75 constr.setters[accessors] = function(elem, v)
79 for i = 1, #(accessors or {}) do
80 if type(accessors[i]) == "string" then
81 constr.getters[accessors[i]] = function(elem)
84 constr.setters[accessors[i]] = function(elem, v)
87 else -- only two levels of nesting are supported
88 for k, v in ipairs(accessors[i]) do
89 constr.getters[v] = function(elem)
92 constr.setters[v] = function(elem, v)
100 function constr:new(...)
102 setmetatable(obj, self)
103 self.__index = function(t, k)
104 if getmetatable(t).getters[k] then
105 return getmetatable(t).getters[k](t)
107 return getmetatable(t)["tag"]
109 return getmetatable(t)[k]
112 self.__newindex = function(t, k, v)
113 if getmetatable(t).setters[k] then
114 getmetatable(t).setters[k](t, v)
121 self.constructor = self.constructor or {}
122 self.constructor[tag] = constr
126--- Calls the constructor, creating a new element.
128function Element.__call(t, ...)
132------------------------------------------------------------------------
136--- A complete pandoc document
138-- @tparam {Block,...} blocks document content
139-- @tparam[opt] Meta meta document meta data
140function M.Pandoc(blocks, meta)
145 ["pandoc-api-version"] = {1,17,0,5},
149-- DEPRECATED synonym:
152------------------------------------------------------------------------
155M.MetaValue = Element:make_subtype{}
156M.MetaValue.__call = function(t, ...)
160-- @function MetaBlocks
161-- @tparam {Block,...} blocks blocks
164-- @function MetaInlines
165-- @tparam {Inline,...} inlines inlines
169-- @tparam {MetaValue,...} meta_values list of meta values
173-- @tparam table a string-index map of meta values
174M.meta_value_types = {
180for i = 1, #M.meta_value_types do
181 M[M.meta_value_types[i]] = M.MetaValue:create_constructor(
182 M.meta_value_types[i],
189--- Creates string to be used in meta data.
190-- Does nothing, lua strings are meta strings.
191-- @function MetaString
192-- @tparam string str string value
193function M.MetaString(str)
197--- Creates boolean to be used in meta data.
198-- Does nothing, lua booleans are meta booleans.
200-- @tparam boolean bool boolean value
201function M.MetaBool(bool)
205------------------------------------------------------------------------
210M.Block = Element:make_subtype{}
211M.Block.__call = function (t, ...)
215--- Creates a block quote element
216-- @function BlockQuote
217-- @tparam {Block,...} content block content
218-- @treturn Block block quote element
219M.BlockQuote = M.Block:create_constructor(
221 function(content) return {c = content} end,
225--- Creates a bullet (i.e. unordered) list.
226-- @function BulletList
227-- @tparam {{Block,...},...} content list of items
228-- @treturn Block block quote element
229M.BulletList = M.Block:create_constructor(
231 function(content) return {c = content} end,
235--- Creates a code block element
236-- @function CodeBlock
237-- @tparam string text code string
238-- @tparam[opt] Attr attr element attributes
239-- @treturn Block code block element
240M.CodeBlock = M.Block:create_constructor(
242 function(text, attr) return {c = {attr or M.Attr(), text}} end,
243 {{"identifier", "classes", "attributes"}, "text"}
246--- Creates a definition list, containing terms and their explanation.
247-- @function DefinitionList
248-- @tparam {{{Inline,...},{Block,...}},...} content list of items
249-- @treturn Block block quote element
250M.DefinitionList = M.Block:create_constructor(
252 function(content) return {c = content} end,
256--- Creates a div element
258-- @tparam {Block,...} content block content
259-- @tparam[opt] Attr attr element attributes
260-- @treturn Block code block element
261M.Div = M.Block:create_constructor(
263 function(content, attr)
264 return {c = {attr or M.Attr(), content}}
266 {{"identifier", "classes", "attributes"}, "content"}
269--- Creates a block quote element.
271-- @tparam int level header level
272-- @tparam {Inline,...} content inline content
273-- @tparam[opt] Attr attr element attributes
274-- @treturn Block header element
275M.Header = M.Block:create_constructor(
277 function(level, content, attr)
278 return {c = {level, attr or M.Attr(), content}}
280 {"level", {"identifier", "classes", "attributes"}, "content"}
283--- Creates a horizontal rule.
284-- @function HorizontalRule
285-- @treturn Block horizontal rule
286M.HorizontalRule = M.Block:create_constructor(
288 function() return {} end
291--- Creates a line block element.
292-- @function LineBlock
293-- @tparam {{Inline,...},...} content inline content
294-- @treturn Block block quote element
295M.LineBlock = M.Block:create_constructor(
297 function(content) return {c = content} end,
301--- Creates a null element.
303-- @treturn Block null element
304M.Null = M.Block:create_constructor(
306 function() return {} end
309--- Creates an ordered list.
310-- @function OrderedList
311-- @tparam {{Block,...},...} items list items
312-- @param[opt] listAttributes list parameters
314M.OrderedList = M.Block:create_constructor(
316 function(items, listAttributes)
317 return {c = {listAttributes,items}}
319 {{"start", "style", "delimiter"}, "content"}
322--- Creates a para element.
324-- @tparam {Inline,...} content inline content
325-- @treturn Block block quote element
326M.Para = M.Block:create_constructor(
328 function(content) return {c = content} end,
332--- Creates a plain element.
334-- @tparam {Inline,...} content inline content
335-- @treturn Block block quote element
336M.Plain = M.Block:create_constructor(
338 function(content) return {c = content} end,
342--- Creates a raw content block of the specified format.
344-- @tparam string format format of content
345-- @tparam string text string content
346-- @treturn Block block quote element
347M.RawBlock = M.Block:create_constructor(
349 function(format, text) return {c = {format, text}} end,
353--- Creates a table element.
355-- @tparam {Inline,...} caption table caption
356-- @tparam {AlignDefault|AlignLeft|AlignRight|AlignCenter,...} aligns alignments
357-- @tparam {int,...} widths column widths
358-- @tparam {Block,...} headers header row
359-- @tparam {{Block,...}} rows table rows
360-- @treturn Block block quote element
361M.Table = M.Block:create_constructor(
363 function(caption, aligns, widths, headers, rows)
364 return {c = {caption, aligns, widths, headers, rows}}
366 {"caption", "aligns", "widths", "headers", "rows"}
370------------------------------------------------------------------------
374--- Inline element class
375M.Inline = Element:make_subtype{}
376M.Inline.__call = function (t, ...)
380--- Creates a Cite inline element
382-- @tparam {Inline,...} content List of inlines
383-- @tparam {Citation,...} citations List of citations
384-- @treturn Inline citations element
385M.Cite = M.Inline:create_constructor(
387 function(content, citations) return {c = {citations, content}} end,
388 {"citations", "content"}
391--- Creates a Code inline element
393-- @tparam string text brief image description
394-- @tparam[opt] Attr attr additional attributes
395-- @treturn Inline code element
396M.Code = M.Inline:create_constructor(
398 function(text, attr) return {c = {attr or M.Attr(), text}} end,
399 {{"identifier", "classes", "attributes"}, "text"}
402--- Creates an inline element representing emphasised text.
404-- @tparam {Inline,..} content inline content
405-- @treturn Inline emphasis element
406M.Emph = M.Inline:create_constructor(
408 function(content) return {c = content} end,
412--- Creates a Image inline element
414-- @tparam {Inline,..} caption text used to describe the image
415-- @tparam string src path to the image file
416-- @tparam[opt] string title brief image description
417-- @tparam[opt] Attr attr additional attributes
418-- @treturn Inline image element
419M.Image = M.Inline:create_constructor(
421 function(caption, src, title, attr)
423 attr = attr or M.Attr()
424 return {c = {attr, caption, {src, title}}}
426 {"attributes", "caption", {"src", "title"}}
429--- Create a LineBreak inline element
430-- @function LineBreak
431-- @treturn Inline linebreak element
432M.LineBreak = M.Inline:create_constructor(
434 function() return {} end
437--- Creates a link inline element, usually a hyperlink.
439-- @tparam {Inline,..} content text for this link
440-- @tparam string target the link target
441-- @tparam[opt] string title brief link description
442-- @tparam[opt] Attr attr additional attributes
443-- @treturn Inline image element
444M.Link = M.Inline:create_constructor(
446 function(content, target, title, attr)
448 attr = attr or M.Attr()
449 return {c = {attr, content, {target, title}}}
451 {"attributes", "content", {"target", "title"}}
454--- Creates a Math element, either inline or displayed.
456-- @tparam "InlineMath"|"DisplayMath" mathtype rendering specifier
457-- @tparam string text Math content
458-- @treturn Inline Math element
459M.Math = M.Inline:create_constructor(
461 function(mathtype, text)
462 return {c = {mathtype, text}}
466--- Creates a DisplayMath element (DEPRECATED).
467-- @function DisplayMath
468-- @tparam string text Math content
469-- @treturn Inline Math element
470M.DisplayMath = M.Inline:create_constructor(
472 function(text) return M.Math("DisplayMath", text) end,
475--- Creates an InlineMath inline element (DEPRECATED).
476-- @function InlineMath
477-- @tparam string text Math content
478-- @treturn Inline Math element
479M.InlineMath = M.Inline:create_constructor(
481 function(text) return M.Math("InlineMath", text) end,
485--- Creates a Note inline element
487-- @tparam {Block,...} content footnote block content
488M.Note = M.Inline:create_constructor(
490 function(content) return {c = content} end,
494--- Creates a Quoted inline element given the quote type and quoted content.
496-- @tparam "DoubleQuote"|"SingleQuote" quotetype type of quotes to be used
497-- @tparam {Inline,..} content inline content
498-- @treturn Inline quoted element
499M.Quoted = M.Inline:create_constructor(
501 function(quotetype, content) return {c = {quotetype, content}} end,
502 {"quotetype", "content"}
504--- Creates a single-quoted inline element (DEPRECATED).
505-- @function SingleQuoted
506-- @tparam {Inline,..} content inline content
507-- @treturn Inline quoted element
509M.SingleQuoted = M.Inline:create_constructor(
511 function(content) return M.Quoted(M.SingleQuote, content) end,
512 {"quotetype", "content"}
514--- Creates a single-quoted inline element (DEPRECATED).
515-- @function DoubleQuoted
516-- @tparam {Inline,..} content inline content
517-- @treturn Inline quoted element
519M.DoubleQuoted = M.Inline:create_constructor(
521 function(content) return M.Quoted("DoubleQuote", content) end,
522 {"quotetype", "content"}
525--- Creates a RawInline inline element
526-- @function RawInline
527-- @tparam string format format of the contents
528-- @tparam string text string content
529-- @treturn Inline raw inline element
530M.RawInline = M.Inline:create_constructor(
532 function(format, text) return {c = {format, text}} end,
536--- Creates text rendered in small caps
537-- @function SmallCaps
538-- @tparam {Inline,..} content inline content
539-- @treturn Inline smallcaps element
540M.SmallCaps = M.Inline:create_constructor(
542 function(content) return {c = content} end,
546--- Creates a SoftBreak inline element.
547-- @function SoftBreak
548-- @treturn Inline softbreak element
549M.SoftBreak = M.Inline:create_constructor(
551 function() return {} end
554--- Create a Space inline element
556-- @treturn Inline space element
557M.Space = M.Inline:create_constructor(
559 function() return {} end
562--- Creates a Span inline element
564-- @tparam {Inline,..} content inline content
565-- @tparam[opt] Attr attr additional attributes
566-- @treturn Inline span element
567M.Span = M.Inline:create_constructor(
569 function(content, attr) return {c = {attr or M.Attr(), content}} end,
570 {{"identifier", "classes", "attributes"}, "content"}
573--- Creates a Str inline element
575-- @tparam string text content
576-- @treturn Inline string element
577M.Str = M.Inline:create_constructor(
579 function(text) return {c = text} end,
583--- Creates text which is striked out.
584-- @function Strikeout
585-- @tparam {Inline,..} content inline content
586-- @treturn Inline strikeout element
587M.Strikeout = M.Inline:create_constructor(
589 function(content) return {c = content} end,
593--- Creates a Strong element, whose text is usually displayed in a bold font.
595-- @tparam {Inline,..} content inline content
596-- @treturn Inline strong element
597M.Strong = M.Inline:create_constructor(
599 function(content) return {c = content} end,
603--- Creates a Subscript inline element
604-- @function Subscript
605-- @tparam {Inline,..} content inline content
606-- @treturn Inline subscript element
607M.Subscript = M.Inline:create_constructor(
609 function(content) return {c = content} end,
613--- Creates a Superscript inline element
614-- @function Superscript
615-- @tparam {Inline,..} content inline content
616-- @treturn Inline strong element
617M.Superscript = M.Inline:create_constructor(
619 function(content) return {c = content} end,
624------------------------------------------------------------------------
630M.Attr._field_names = {identifier = 1, classes = 2, attributes = 3}
631--- Create a new set of attributes (Attr).
633-- @tparam[opt] string identifier element identifier
634-- @tparam[opt] {string,...} classes element classes
635-- @tparam[opt] table attributes table containing string keys and values
636-- @return element attributes
637M.Attr.__call = function(t, identifier, classes, attributes)
638 identifier = identifier or ''
639 classes = classes or {}
640 attributes = attributes or {}
641 local attr = {identifier, classes, attributes}
642 setmetatable(attr, t)
645M.Attr.__index = function(t, k)
646 return rawget(t, k) or
647 rawget(t, M.Attr._field_names[k]) or
648 rawget(getmetatable(t), k)
650M.Attr.__newindex = function(t, k, v)
651 if M.Attr._field_names[k] then
652 rawset(t, M.Attr._field_names[k], v)
657setmetatable(M.Attr, M.Attr)
660--- Creates a single citation.
662-- @tparam string id citation identifier (like a bibtex key)
663-- @tparam AuthorInText|SuppressAuthor|NormalCitation mode citation mode
664-- @tparam[opt] {Inline,...} prefix citation prefix
665-- @tparam[opt] {Inline,...} suffix citation suffix
666-- @tparam[opt] int note_num note number
667-- @tparam[opt] int note_num hash number
668M.Citation = function(id, mode, prefix, suffix, note_num, hash)
669 prefix = prefix or {}
670 suffix = suffix or {}
671 note_num = note_num or 0
675 citationPrefix = prefix,
676 citationSuffix = suffix,
678 citationNoteNum = note_num,
684------------------------------------------------------------------------
688--- Author name is mentioned in the text.
691M.AuthorInText = "AuthorInText"
693--- Author name is suppressed.
696M.SuppressAuthor = "SuppressAuthor"
698--- Default citation style is used.
701M.NormalCitation = "NormalCitation"
703--- Table cells aligned left.
705M.AlignLeft = "AlignLeft"
707--- Table cells right-aligned.
709M.AlignRight = "AlignRight"
711--- Table cell content is centered.
713M.AlignCenter = "AlignCenter"
715--- Table cells are alignment is unaltered.
717M.AlignDefault = "AlignDefault"
719--- Default list number delimiters are used.
721M.DefaultDelim = "DefaultDelim"
723--- List numbers are delimited by a period.
727--- List numbers are delimited by a single parenthesis.
729M.OneParen = "OneParen"
731--- List numbers are delimited by a double parentheses.
733M.TwoParens = "TwoParens"
735--- List are numbered in the default style
737M.DefaultStyle = "DefaultStyle"
739--- List items are numbered as examples.
743--- List are numbered using decimal integers.
747--- List are numbered using lower-case roman numerals.
749M.LowerRoman = "LowerRoman"
751--- List are numbered using upper-case roman numerals
753M.UpperRoman = "UpperRoman"
755--- List are numbered using lower-case alphabetic characters.
757M.LowerAlpha = "LowerAlpha"
759--- List are numbered using upper-case alphabetic characters.
761M.UpperAlpha = "UpperAlpha"
764------------------------------------------------------------------------
768--- Parse the given string into a Pandoc document.
769-- The method used to interpret input is specified by *format*. Acceptable
770-- values for this parameter are equal to those that can be given to the
771-- `--from` command line option.
772-- @tparam string markup the markup to be parsed
773-- @tparam[opt] string format format specification, defaults to "markdown".
774-- @return Doc pandoc document
776-- local org_markup = "/emphasis/" -- Input to be read
777-- local document = pandoc.read(org_markup, "org")
778-- -- Get the first block of the document
779-- local block = document.blocks[1]
780-- -- The inline element in that block is an `Emph`
781-- assert(block.content[1].t == "Emph")
782function M.read(markup, format)
783 format = format or "markdown"
784 local pd = pandoc.__read(format, markup)
785 if type(pd) == "string" then
792--- Use functions defined in the global namespace to create a pandoc filter.
793-- All globally defined functions which have names of pandoc elements are
794-- collected into a new table.
795-- @return A list of filter functions
797-- -- within a file defining a pandoc filter:
799-- return pandoc.Str(utf8.upper(text))
802-- return {pandoc.global_filter()}
803-- -- the above is equivallent to
804-- -- return {{Str = Str}}
805function M.global_filter()
807 for k, v in pairs(_G) do
808 if M.Inline.constructor[k] or M.Block.constructor[k] or k == "Doc" 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