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 boolean bool boolean value
175-- @tparam table a string-index map of meta values
178-- @function MetaString
179-- @tparam string str string value
180M.meta_value_types = {
188for i = 1, #M.meta_value_types do
189 M[M.meta_value_types[i]] = M.MetaValue:create_constructor(
190 M.meta_value_types[i],
198------------------------------------------------------------------------
203M.Block = Element:make_subtype{}
204M.Block.__call = function (t, ...)
208--- Creates a block quote element
209-- @function BlockQuote
210-- @tparam {Block,...} content block content
211-- @treturn Block block quote element
212M.BlockQuote = M.Block:create_constructor(
214 function(content) return {c = content} end,
218--- Creates a bullet (i.e. unordered) list.
219-- @function BulletList
220-- @tparam {{Block,...},...} content list of items
221-- @treturn Block block quote element
222M.BulletList = M.Block:create_constructor(
224 function(content) return {c = content} end,
228--- Creates a code block element
229-- @function CodeBlock
230-- @tparam string text code string
231-- @tparam[opt] Attributes attributes element attributes
232-- @treturn Block code block element
233M.CodeBlock = M.Block:create_constructor(
235 function(text, attributes) return {c = {attributes, text}} end,
236 {{"identifier", "classes", "attributes"}, "text"}
239--- Creates a definition list, containing terms and their explanation.
240-- @function DefinitionList
241-- @tparam {{{Inline,...},{Block,...}},...} content list of items
242-- @treturn Block block quote element
243M.DefinitionList = M.Block:create_constructor(
245 function(content) return {c = content} end,
249--- Creates a div element
251-- @tparam {Block,...} content block content
252-- @tparam[opt] Attributes attributes element attributes
253-- @treturn Block code block element
254M.Div = M.Block:create_constructor(
256 function(content, attributes) return {c = {attributes, content}} end,
257 {{"identifier", "classes", "attributes"}, "content"}
260--- Creates a block quote element.
262-- @tparam int level header level
263-- @tparam Attributes attributes element attributes
264-- @tparam {Inline,...} content inline content
265-- @treturn Block header element
266M.Header = M.Block:create_constructor(
268 function(level, attributes, content)
269 return {c = {level, attributes, content}}
271 {"level", {"identifier", "classes", "attributes"}, "content"}
274--- Creates a horizontal rule.
275-- @function HorizontalRule
276-- @treturn Block horizontal rule
277M.HorizontalRule = M.Block:create_constructor(
279 function() return {} end
282--- Creates a line block element.
283-- @function LineBlock
284-- @tparam {{Inline,...},...} content inline content
285-- @treturn Block block quote element
286M.LineBlock = M.Block:create_constructor(
288 function(content) return {c = content} end,
292--- Creates a null element.
294-- @treturn Block null element
295M.Null = M.Block:create_constructor(
297 function() return {} end
300--- Creates an ordered list.
301-- @function OrderedList
302-- @tparam {{Block,...},...} items list items
303-- @param[opt] listAttributes list parameters
305M.OrderedList = M.Block:create_constructor(
307 function(items, listAttributes)
308 return {c = {listAttributes,items}}
310 {{"start", "style", "delimiter"}, "content"}
313--- Creates a para element.
315-- @tparam {Inline,...} content inline content
316-- @treturn Block block quote element
317M.Para = M.Block:create_constructor(
319 function(content) return {c = content} end,
323--- Creates a plain element.
325-- @tparam {Inline,...} content inline content
326-- @treturn Block block quote element
327M.Plain = M.Block:create_constructor(
329 function(content) return {c = content} end,
333--- Creates a raw content block of the specified format.
335-- @tparam string format format of content
336-- @tparam string text string content
337-- @treturn Block block quote element
338M.RawBlock = M.Block:create_constructor(
340 function(format, text) return {c = {format, text}} end,
344--- Creates a table element.
346-- @tparam {Inline,...} caption table caption
347-- @tparam {AlignDefault|AlignLeft|AlignRight|AlignCenter,...} aligns alignments
348-- @tparam {int,...} widths column widths
349-- @tparam {Block,...} headers header row
350-- @tparam {{Block,...}} rows table rows
351-- @treturn Block block quote element
352M.Table = M.Block:create_constructor(
354 function(caption, aligns, widths, headers, rows)
355 return {c = {caption, aligns, widths, headers, rows}}
360------------------------------------------------------------------------
364--- Inline element class
365M.Inline = Element:make_subtype{}
366M.Inline.__call = function (t, ...)
370--- Creates a Cite inline element
372-- @tparam {Inline,...} content List of inlines
373-- @tparam {Citation,...} citations List of citations
374-- @treturn Inline citations element
375M.Cite = M.Inline:create_constructor(
377 function(content, citations) return {c = {citations, content}} end,
378 {"citations", "content"}
381--- Creates a Code inline element
383-- @tparam string text brief image description
384-- @tparam[opt] Attributes attributes additional attributes
385-- @treturn Inline code element
386M.Code = M.Inline:create_constructor(
388 function(text, attributes) return {c = {attributes, text}} end,
389 {{"identifier", "classes", "attributes"}, "text"}
392--- Creates an inline element representing emphasised text.
394-- @tparam {Inline,..} content inline content
395-- @treturn Inline emphasis element
396M.Emph = M.Inline:create_constructor(
398 function(content) return {c = content} end,
402--- Creates a Image inline element
404-- @tparam {Inline,..} caption text used to describe the image
405-- @tparam string src path to the image file
406-- @tparam[opt] string title brief image description
407-- @tparam[opt] Attributes attributes additional attributes
408-- @treturn Inline image element
409M.Image = M.Inline:create_constructor(
411 function(caption, src, title, attributes)
413 attributes = attributes or Attribute.empty
414 return {c = {attributes, caption, {src, title}}}
416 {"attributes", "caption", {"src", "title"}}
419--- Create a LineBreak inline element
420-- @function LineBreak
421-- @treturn Inline linebreak element
422M.LineBreak = M.Inline:create_constructor(
424 function() return {} end
427--- Creates a link inline element, usually a hyperlink.
429-- @tparam {Inline,..} content text for this link
430-- @tparam string target the link target
431-- @tparam[opt] string title brief link description
432-- @tparam[opt] Attributes attributes additional attributes
433-- @treturn Inline image element
434M.Link = M.Inline:create_constructor(
436 function(content, target, title, attributes)
438 attributes = attributes or Attribute.empty
439 return {c = {attributes, content, {target, title}}}
441 {"attributes", "content", {"target", "title"}}
444--- Creates a Math element, either inline or displayed. It is usually simpler to
445-- use one of the specialized functions @{InlineMath} or @{DisplayMath} instead.
447-- @tparam "InlineMath"|"DisplayMath" mathtype rendering specifier
448-- @tparam string text Math content
449-- @treturn Inline Math element
450M.Math = M.Inline:create_constructor(
452 function(mathtype, text)
453 return {c = {mathtype, text}}
457--- Creates a DisplayMath element.
458-- @function DisplayMath
459-- @tparam string text Math content
460-- @treturn Inline Math element
461M.DisplayMath = M.Inline:create_constructor(
463 function(text) return M.Math("DisplayMath", text) end,
466--- Creates an InlineMath inline element.
467-- @function InlineMath
468-- @tparam string text Math content
469-- @treturn Inline Math element
470M.InlineMath = M.Inline:create_constructor(
472 function(text) return M.Math("InlineMath", text) end,
476--- Creates a Note inline element
478-- @tparam {Block,...} content footnote block content
479M.Note = M.Inline:create_constructor(
481 function(content) return {c = content} end,
485--- Creates a Quoted inline element given the quote type and quoted content. It
486-- is usually simpler to use one of the specialized functions @{SingleQuoted} or
487-- @{DoubleQuoted} instead.
489-- @tparam "DoubleQuote"|"SingleQuote" quotetype type of quotes to be used
490-- @tparam {Inline,..} content inline content
491-- @treturn Inline quoted element
492M.Quoted = M.Inline:create_constructor(
494 function(quotetype, content) return {c = {quotetype, content}} end,
495 {"quotetype", "content"}
497--- Creates a single-quoted inline element.
498-- @function SingleQuoted
499-- @tparam {Inline,..} content inline content
500-- @treturn Inline quoted element
502M.SingleQuoted = M.Inline:create_constructor(
504 function(content) return M.Quoted(M.SingleQuote, content) end,
505 {"quotetype", "content"}
507--- Creates a single-quoted inline element.
508-- @function DoubleQuoted
509-- @tparam {Inline,..} content inline content
510-- @treturn Inline quoted element
512M.DoubleQuoted = M.Inline:create_constructor(
514 function(content) return M.Quoted("DoubleQuote", content) end,
515 {"quotetype", "content"}
518--- Creates a RawInline inline element
519-- @function RawInline
520-- @tparam string format format of the contents
521-- @tparam string text string content
522-- @treturn Inline raw inline element
523M.RawInline = M.Inline:create_constructor(
525 function(format, text) return {c = {format, text}} end,
529--- Creates text rendered in small caps
530-- @function SmallCaps
531-- @tparam {Inline,..} content inline content
532-- @treturn Inline smallcaps element
533M.SmallCaps = M.Inline:create_constructor(
535 function(content) return {c = content} end,
539--- Creates a SoftBreak inline element.
540-- @function SoftBreak
541-- @treturn Inline softbreak element
542M.SoftBreak = M.Inline:create_constructor(
544 function() return {} end
547--- Create a Space inline element
549-- @treturn Inline space element
550M.Space = M.Inline:create_constructor(
552 function() return {} end
555--- Creates a Span inline element
557-- @tparam {Inline,..} content inline content
558-- @tparam[opt] Attributes attributes additional attributes
559-- @treturn Inline span element
560M.Span = M.Inline:create_constructor(
562 function(content, attributes) return {c = {attributes, content}} end,
563 {{"identifier", "classes", "attributes"}, "content"}
566--- Creates a Str inline element
568-- @tparam string text content
569-- @treturn Inline string element
570M.Str = M.Inline:create_constructor(
572 function(text) return {c = text} end,
576--- Creates text which is striked out.
577-- @function Strikeout
578-- @tparam {Inline,..} content inline content
579-- @treturn Inline strikeout element
580M.Strikeout = M.Inline:create_constructor(
582 function(content) return {c = content} end,
586--- Creates a Strong element, whose text is usually displayed in a bold font.
588-- @tparam {Inline,..} content inline content
589-- @treturn Inline strong element
590M.Strong = M.Inline:create_constructor(
592 function(content) return {c = content} end,
596--- Creates a Subscript inline element
597-- @function Subscript
598-- @tparam {Inline,..} content inline content
599-- @treturn Inline subscript element
600M.Subscript = M.Inline:create_constructor(
602 function(content) return {c = content} end,
606--- Creates a Superscript inline element
607-- @function Superscript
608-- @tparam {Inline,..} content inline content
609-- @treturn Inline strong element
610M.Superscript = M.Inline:create_constructor(
612 function(content) return {c = content} end,
617------------------------------------------------------------------------
623--- Create a new set of attributes (Attr).
624-- @function Attributes
625-- @tparam table key_values table containing string keys and values
626-- @tparam[opt] string id element identifier
627-- @tparam[opt] {string,...} classes element classes
628-- @return element attributes
629M.Attributes.__call = function(t, key_values, id, classes)
631 classes = classes or {}
632 local attr = {id, classes, key_values}
633 setmetatable(attr, t)
636M.Attributes.__index = function(t, k)
639 elseif k == "id" then
640 if rawget(t, 1) == '' then
645 elseif k == "class" then
646 if #(rawget(t, 2)) == 0 then
649 return table.concat(t[2], ' ')
652 for _, p in ipairs(t[3]) do
660M.Attributes.__newindex = function(t, k, v)
663 elseif k == "id" then
665 elseif k == "class" then
666 if type(v) == "string" then
672 for _, p in ipairs(rawget(t, 3)) do
683setmetatable(M.Attributes, M.Attributes)
686--- Creates a single citation.
688-- @tparam string id citation identifier (like a bibtex key)
689-- @tparam AuthorInText|SuppressAuthor|NormalCitation mode citation mode
690-- @tparam[opt] {Inline,...} prefix citation prefix
691-- @tparam[opt] {Inline,...} suffix citation suffix
692-- @tparam[opt] int note_num note number
693-- @tparam[opt] int note_num hash number
694M.Citation = function(id, mode, prefix, suffix, note_num, hash)
695 prefix = prefix or {}
696 suffix = suffix or {}
697 note_num = note_num or 0
701 citationPrefix = prefix,
702 citationSuffix = suffix,
704 citationNoteNum = note_num,
710------------------------------------------------------------------------
714--- Author name is mentioned in the text.
717M.AuthorInText = "AuthorInText"
719--- Author name is suppressed.
722M.SuppressAuthor = "SuppressAuthor"
724--- Default citation style is used.
727M.NormalCitation = "NormalCitation"
729--- Table cells aligned left.
731M.AlignLeft = "AlignLeft"
733--- Table cells right-aligned.
735M.AlignRight = "AlignRight"
737--- Table cell content is centered.
739M.AlignCenter = "AlignCenter"
741--- Table cells are alignment is unaltered.
743M.AlignDefault = "AlignDefault"
745--- Default list number delimiters are used.
747M.DefaultDelim = "DefaultDelim"
749--- List numbers are delimited by a period.
753--- List numbers are delimited by a single parenthesis.
755M.OneParen = "OneParen"
757--- List numbers are delimited by a double parentheses.
759M.TwoParens = "TwoParens"
761--- List are numbered in the default style
763M.DefaultStyle = "DefaultStyle"
765--- List items are numbered as examples.
769--- List are numbered using decimal integers.
773--- List are numbered using lower-case roman numerals.
775M.LowerRoman = "LowerRoman"
777--- List are numbered using upper-case roman numerals
779M.UpperRoman = "UpperRoman"
781--- List are numbered using lower-case alphabetic characters.
783M.LowerAlpha = "LowerAlpha"
785--- List are numbered using upper-case alphabetic characters.
787M.UpperAlpha = "UpperAlpha"
790------------------------------------------------------------------------
794--- Use functions defined in the global namespace to create a pandoc filter.
795-- All globally defined functions which have names of pandoc elements are
796-- collected into a new table.
797-- @return A list of filter functions
799-- -- within a file defining a pandoc filter:
801-- return pandoc.Str(utf8.upper(text))
804-- return {pandoc.global_filter()}
805-- -- the above is equivallent to
806-- -- return {{Str = Str}}
807function M.global_filter()
809 for k, v in pairs(_G) do
810 if M.Inline.constructor[k] or M.Block.constructor[k] or M.Block.constructors[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