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.Doc(blocks, meta)
145 ["pandoc-api-version"] = {1,17,0,5},
150------------------------------------------------------------------------
153M.MetaValue = Element:make_subtype{}
154M.MetaValue.__call = function(t, ...)
158-- @function MetaBlocks
159-- @tparam {Block,...} blocks blocks
162-- @function MetaInlines
163-- @tparam {Inline,...} inlines inlines
167-- @tparam {MetaValue,...} meta_values list of meta values
171-- @tparam table a string-index map of meta values
172M.meta_value_types = {
178for i = 1, #M.meta_value_types do
179 M[M.meta_value_types[i]] = M.MetaValue:create_constructor(
180 M.meta_value_types[i],
187--- Creates string to be used in meta data.
188-- Does nothing, lua strings are meta strings.
189-- @function MetaString
190-- @tparam string str string value
191function M.MetaString(str)
195--- Creates boolean to be used in meta data.
196-- Does nothing, lua booleans are meta booleans.
198-- @tparam boolean bool boolean value
199function M.MetaBool(bool)
203------------------------------------------------------------------------
208M.Block = Element:make_subtype{}
209M.Block.__call = function (t, ...)
213--- Creates a block quote element
214-- @function BlockQuote
215-- @tparam {Block,...} content block content
216-- @treturn Block block quote element
217M.BlockQuote = M.Block:create_constructor(
219 function(content) return {c = content} end,
223--- Creates a bullet (i.e. unordered) list.
224-- @function BulletList
225-- @tparam {{Block,...},...} content list of items
226-- @treturn Block block quote element
227M.BulletList = M.Block:create_constructor(
229 function(content) return {c = content} end,
233--- Creates a code block element
234-- @function CodeBlock
235-- @tparam string text code string
236-- @tparam[opt] Attr attr element attributes
237-- @treturn Block code block element
238M.CodeBlock = M.Block:create_constructor(
240 function(text, attr) return {c = {attr or M.Attr(), text}} end,
241 {{"identifier", "classes", "attributes"}, "text"}
244--- Creates a definition list, containing terms and their explanation.
245-- @function DefinitionList
246-- @tparam {{{Inline,...},{Block,...}},...} content list of items
247-- @treturn Block block quote element
248M.DefinitionList = M.Block:create_constructor(
250 function(content) return {c = content} end,
254--- Creates a div element
256-- @tparam {Block,...} content block content
257-- @tparam[opt] Attr attr element attributes
258-- @treturn Block code block element
259M.Div = M.Block:create_constructor(
261 function(content, attr)
262 return {c = {attr or M.Attr(), content}}
264 {{"identifier", "classes", "attributes"}, "content"}
267--- Creates a block quote element.
269-- @tparam int level header level
270-- @tparam {Inline,...} content inline content
271-- @tparam[opt] Attr attr element attributes
272-- @treturn Block header element
273M.Header = M.Block:create_constructor(
275 function(level, content, attr)
276 return {c = {level, attr or M.Attr(), content}}
278 {"level", {"identifier", "classes", "attributes"}, "content"}
281--- Creates a horizontal rule.
282-- @function HorizontalRule
283-- @treturn Block horizontal rule
284M.HorizontalRule = M.Block:create_constructor(
286 function() return {} end
289--- Creates a line block element.
290-- @function LineBlock
291-- @tparam {{Inline,...},...} content inline content
292-- @treturn Block block quote element
293M.LineBlock = M.Block:create_constructor(
295 function(content) return {c = content} end,
299--- Creates a null element.
301-- @treturn Block null element
302M.Null = M.Block:create_constructor(
304 function() return {} end
307--- Creates an ordered list.
308-- @function OrderedList
309-- @tparam {{Block,...},...} items list items
310-- @param[opt] listAttributes list parameters
312M.OrderedList = M.Block:create_constructor(
314 function(items, listAttributes)
315 return {c = {listAttributes,items}}
317 {{"start", "style", "delimiter"}, "content"}
320--- Creates a para element.
322-- @tparam {Inline,...} content inline content
323-- @treturn Block block quote element
324M.Para = M.Block:create_constructor(
326 function(content) return {c = content} end,
330--- Creates a plain element.
332-- @tparam {Inline,...} content inline content
333-- @treturn Block block quote element
334M.Plain = M.Block:create_constructor(
336 function(content) return {c = content} end,
340--- Creates a raw content block of the specified format.
342-- @tparam string format format of content
343-- @tparam string text string content
344-- @treturn Block block quote element
345M.RawBlock = M.Block:create_constructor(
347 function(format, text) return {c = {format, text}} end,
351--- Creates a table element.
353-- @tparam {Inline,...} caption table caption
354-- @tparam {AlignDefault|AlignLeft|AlignRight|AlignCenter,...} aligns alignments
355-- @tparam {int,...} widths column widths
356-- @tparam {Block,...} headers header row
357-- @tparam {{Block,...}} rows table rows
358-- @treturn Block block quote element
359M.Table = M.Block:create_constructor(
361 function(caption, aligns, widths, headers, rows)
362 return {c = {caption, aligns, widths, headers, rows}}
367------------------------------------------------------------------------
371--- Inline element class
372M.Inline = Element:make_subtype{}
373M.Inline.__call = function (t, ...)
377--- Creates a Cite inline element
379-- @tparam {Inline,...} content List of inlines
380-- @tparam {Citation,...} citations List of citations
381-- @treturn Inline citations element
382M.Cite = M.Inline:create_constructor(
384 function(content, citations) return {c = {citations, content}} end,
385 {"citations", "content"}
388--- Creates a Code inline element
390-- @tparam string text brief image description
391-- @tparam[opt] Attr attr additional attributes
392-- @treturn Inline code element
393M.Code = M.Inline:create_constructor(
395 function(text, attr) return {c = {attr or M.Attr(), text}} end,
396 {{"identifier", "classes", "attributes"}, "text"}
399--- Creates an inline element representing emphasised text.
401-- @tparam {Inline,..} content inline content
402-- @treturn Inline emphasis element
403M.Emph = M.Inline:create_constructor(
405 function(content) return {c = content} end,
409--- Creates a Image inline element
411-- @tparam {Inline,..} caption text used to describe the image
412-- @tparam string src path to the image file
413-- @tparam[opt] string title brief image description
414-- @tparam[opt] Attr attr additional attributes
415-- @treturn Inline image element
416M.Image = M.Inline:create_constructor(
418 function(caption, src, title, attr)
420 attr = attr or M.Attr()
421 return {c = {attr, caption, {src, title}}}
423 {"attributes", "caption", {"src", "title"}}
426--- Create a LineBreak inline element
427-- @function LineBreak
428-- @treturn Inline linebreak element
429M.LineBreak = M.Inline:create_constructor(
431 function() return {} end
434--- Creates a link inline element, usually a hyperlink.
436-- @tparam {Inline,..} content text for this link
437-- @tparam string target the link target
438-- @tparam[opt] string title brief link description
439-- @tparam[opt] Attr attr additional attributes
440-- @treturn Inline image element
441M.Link = M.Inline:create_constructor(
443 function(content, target, title, attr)
445 attr = attr or M.Attr()
446 return {c = {attr, content, {target, title}}}
448 {"attributes", "content", {"target", "title"}}
451--- Creates a Math element, either inline or displayed. It is usually simpler to
452-- use one of the specialized functions @{InlineMath} or @{DisplayMath} instead.
454-- @tparam "InlineMath"|"DisplayMath" mathtype rendering specifier
455-- @tparam string text Math content
456-- @treturn Inline Math element
457M.Math = M.Inline:create_constructor(
459 function(mathtype, text)
460 return {c = {mathtype, text}}
464--- Creates a DisplayMath element.
465-- @function DisplayMath
466-- @tparam string text Math content
467-- @treturn Inline Math element
468M.DisplayMath = M.Inline:create_constructor(
470 function(text) return M.Math("DisplayMath", text) end,
473--- Creates an InlineMath inline element.
474-- @function InlineMath
475-- @tparam string text Math content
476-- @treturn Inline Math element
477M.InlineMath = M.Inline:create_constructor(
479 function(text) return M.Math("InlineMath", text) end,
483--- Creates a Note inline element
485-- @tparam {Block,...} content footnote block content
486M.Note = M.Inline:create_constructor(
488 function(content) return {c = content} end,
492--- Creates a Quoted inline element given the quote type and quoted content. It
493-- is usually simpler to use one of the specialized functions @{SingleQuoted} or
494-- @{DoubleQuoted} instead.
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.
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.
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