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)
68 local constr = self:make_subtype({tag = tag})
69 function constr:new(...)
71 setmetatable(obj, self)
72 self.__index = function(t, k)
76 return getmetatable(t)["tag"]
78 return getmetatable(t)[k]
83 self.constructor = self.constructor or {}
84 self.constructor[tag] = constr
88--- Calls the constructor, creating a new element.
90function Element.__call(t, ...)
94------------------------------------------------------------------------
98--- A complete pandoc document
100-- @tparam {Block,...} blocks document content
101-- @tparam[opt] Meta meta document meta data
102function M.Doc(blocks, meta)
107 ["pandoc-api-version"] = {1,17,0,5},
112------------------------------------------------------------------------
115M.MetaValue = Element:make_subtype{}
116M.MetaValue.__call = function(t, ...)
120-- @function MetaBlocks
121-- @tparam {Block,...} blocks blocks
124-- @function MetaInlines
125-- @tparam {Inline,...} inlines inlines
129-- @tparam {MetaValue,...} meta_values list of meta values
133-- @tparam boolean bool boolean value
137-- @tparam table a string-index map of meta values
140-- @function MetaString
141-- @tparam string str string value
142M.meta_value_types = {
150for i = 1, #M.meta_value_types do
151 M[M.meta_value_types[i]] = M.MetaValue:create_constructor(
152 M.meta_value_types[i],
159------------------------------------------------------------------------
164M.Block = Element:make_subtype{}
165M.Block.__call = function (t, ...)
169--- Creates a block quote element
170-- @function BlockQuote
171-- @tparam {Block,...} content block content
172-- @treturn Block block quote element
173M.BlockQuote = M.Block:create_constructor(
175 function(content) return {c = content} end
178--- Creates a bullet (i.e. unordered) list.
179-- @function BulletList
180-- @tparam {{Block,...},...} content list of items
181-- @treturn Block block quote element
182M.BulletList = M.Block:create_constructor(
184 function(content) return {c = content} end
187--- Creates a code block element
188-- @function CodeBlock
189-- @tparam string code code string
190-- @tparam[opt] Attributes attributes element attributes
191-- @treturn Block code block element
192M.CodeBlock = M.Block:create_constructor(
194 function(code, attributes) return {c = {attributes, code}} end
197--- Creates a definition list, containing terms and their explanation.
198-- @function DefinitionList
199-- @tparam {{{Inline,...},{Block,...}},...} content list of items
200-- @treturn Block block quote element
201M.DefinitionList = M.Block:create_constructor(
203 function(content) return {c = content} end
206--- Creates a div element
208-- @tparam {Block,...} content block content
209-- @tparam[opt] Attributes attributes element attributes
210-- @treturn Block code block element
211M.Div = M.Block:create_constructor(
213 function(content, attributes) return {c = {attributes, content}} end
216--- Creates a block quote element.
218-- @tparam int level header level
219-- @tparam Attributes attributes element attributes
220-- @tparam {Inline,...} content inline content
221-- @treturn Block header element
222M.Header = M.Block:create_constructor(
224 function(level, attributes, content)
225 return {c = {level, attributes, content}}
229--- Creates a horizontal rule.
230-- @function HorizontalRule
231-- @treturn Block horizontal rule
232M.HorizontalRule = M.Block:create_constructor(
234 function() return {} end
237--- Creates a line block element.
238-- @function LineBlock
239-- @tparam {{Inline,...},...} content inline content
240-- @treturn Block block quote element
241M.LineBlock = M.Block:create_constructor(
243 function(content) return {c = content} end
246--- Creates a null element.
248-- @treturn Block null element
249M.Null = M.Block:create_constructor(
251 function() return {} end
254--- Creates an ordered list.
255-- @function OrderedList
256-- @tparam {{Block,...},...} items list items
257-- @param[opt] listAttributes list parameters
259M.OrderedList = M.Block:create_constructor(
261 function(items, listAttributes)
262 return {c = {listAttributes,items}}
266--- Creates a para element.
268-- @tparam {Inline,...} content inline content
269-- @treturn Block block quote element
270M.Para = M.Block:create_constructor(
272 function(content) return {c = content} end
275--- Creates a plain element.
277-- @tparam {Inline,...} content inline content
278-- @treturn Block block quote element
279M.Plain = M.Block:create_constructor(
281 function(content) return {c = content} end
284--- Creates a raw content block of the specified format.
286-- @tparam string format format of content
287-- @tparam string content string content
288-- @treturn Block block quote element
289M.RawBlock = M.Block:create_constructor(
291 function(format, content) return {c = {format, content}} end
294--- Creates a table element.
296-- @tparam {Inline,...} caption table caption
297-- @tparam {AlignDefault|AlignLeft|AlignRight|AlignCenter,...} aligns alignments
298-- @tparam {int,...} widths column widths
299-- @tparam {Block,...} headers header row
300-- @tparam {{Block,...}} rows table rows
301-- @treturn Block block quote element
302M.Table = M.Block:create_constructor(
304 function(caption, aligns, widths, headers, rows)
305 return {c = {caption, aligns, widths, headers, rows}}
310------------------------------------------------------------------------
314--- Inline element class
315M.Inline = Element:make_subtype{}
316M.Inline.__call = function (t, ...)
320--- Creates a Cite inline element
322-- @tparam {Inline,...} content List of inlines
323-- @tparam {Citation,...} citations List of citations
324-- @treturn Inline citations element
325M.Cite = M.Inline:create_constructor(
327 function(content, citations) return {c = {citations, content}} end
330--- Creates a Code inline element
332-- @tparam string code brief image description
333-- @tparam[opt] Attributes attributes additional attributes
334-- @treturn Inline code element
335M.Code = M.Inline:create_constructor(
337 function(code, attributes) return {c = {attributes, code}} end
340--- Creates an inline element representing emphasised text.
342-- @tparam {Inline,..} content inline content
343-- @treturn Inline emphasis element
344M.Emph = M.Inline:create_constructor(
346 function(content) return {c = content} end
349--- Creates a Image inline element
351-- @tparam {Inline,..} caption text used to describe the image
352-- @tparam string src path to the image file
353-- @tparam[opt] string title brief image description
354-- @tparam[opt] Attributes attributes additional attributes
355-- @treturn Inline image element
356M.Image = M.Inline:create_constructor(
358 function(caption, src, title, attributes)
360 attributes = attributes or Attribute.empty
361 return {c = {attributes, caption, {src, title}}}
365--- Create a LineBreak inline element
366-- @function LineBreak
367-- @treturn Inline linebreak element
368M.LineBreak = M.Inline:create_constructor(
370 function() return {} end
373--- Creates a link inline element, usually a hyperlink.
375-- @tparam {Inline,..} content text for this link
376-- @tparam string target the link target
377-- @tparam[opt] string title brief link description
378-- @tparam[opt] Attributes attributes additional attributes
379-- @treturn Inline image element
380M.Link = M.Inline:create_constructor(
382 function(content, target, title, attributes)
384 attributes = attributes or Attribute.empty
385 return {c = {attributes, content, {target, title}}}
389--- Creates a Math element, either inline or displayed. It is usually simpler to
390-- use one of the specialized functions @{InlineMath} or @{DisplayMath} instead.
392-- @tparam "InlineMath"|"DisplayMath" mathtype rendering specifier
393-- @tparam string text Math content
394-- @treturn Inline Math element
395M.Math = M.Inline:create_constructor(
397 function(mathtype, text)
398 return {c = {mathtype, text}}
401--- Creates a DisplayMath element.
402-- @function DisplayMath
403-- @tparam string text Math content
404-- @treturn Inline Math element
405M.DisplayMath = M.Inline:create_constructor(
407 function(text) return M.Math("DisplayMath", text) end
409--- Creates an InlineMath inline element.
410-- @function InlineMath
411-- @tparam string text Math content
412-- @treturn Inline Math element
413M.InlineMath = M.Inline:create_constructor(
415 function(text) return M.Math("InlineMath", text) end
418--- Creates a Note inline element
420-- @tparam {Block,...} content footnote block content
421M.Note = M.Inline:create_constructor(
423 function(contents) return {c = contents} end
426--- Creates a Quoted inline element given the quote type and quoted content. It
427-- is usually simpler to use one of the specialized functions @{SingleQuoted} or
428-- @{DoubleQuoted} instead.
430-- @tparam "DoubleQuote"|"SingleQuote" quotetype type of quotes to be used
431-- @tparam {Inline,..} content inline content
432-- @treturn Inline quoted element
433M.Quoted = M.Inline:create_constructor(
435 function(quotetype, content) return {c = {quotetype, content}} end
437--- Creates a single-quoted inline element.
438-- @function SingleQuoted
439-- @tparam {Inline,..} content inline content
440-- @treturn Inline quoted element
442M.SingleQuoted = M.Inline:create_constructor(
444 function(content) return M.Quoted(M.SingleQuote, content) end
446--- Creates a single-quoted inline element.
447-- @function DoubleQuoted
448-- @tparam {Inline,..} content inline content
449-- @treturn Inline quoted element
451M.DoubleQuoted = M.Inline:create_constructor(
453 function(content) return M.Quoted("DoubleQuote", content) end
456--- Creates a RawInline inline element
457-- @function RawInline
458-- @tparam string format format of the contents
459-- @tparam string text string content
460-- @treturn Inline raw inline element
461M.RawInline = M.Inline:create_constructor(
463 function(format, text) return {c = {format, text}} end
466--- Creates text rendered in small caps
467-- @function SmallCaps
468-- @tparam {Inline,..} content inline content
469-- @treturn Inline smallcaps element
470M.SmallCaps = M.Inline:create_constructor(
472 function(content) return {c = content} end
475--- Creates a SoftBreak inline element.
476-- @function SoftBreak
477-- @treturn Inline softbreak element
478M.SoftBreak = M.Inline:create_constructor(
480 function() return {} end
483--- Create a Space inline element
485-- @treturn Inline space element
486M.Space = M.Inline:create_constructor(
488 function() return {} end
491--- Creates a Span inline element
493-- @tparam {Inline,..} content inline content
494-- @tparam[opt] Attributes attributes additional attributes
495-- @treturn Inline span element
496M.Span = M.Inline:create_constructor(
498 function(content, attributes) return {c = {attributes, content}} end
501--- Creates a Str inline element
503-- @tparam string text content
504-- @treturn Inline string element
505M.Str = M.Inline:create_constructor(
507 function(text) return {c = text} end
510--- Creates text which is striked out.
511-- @function Strikeout
512-- @tparam {Inline,..} content inline content
513-- @treturn Inline strikeout element
514M.Strikeout = M.Inline:create_constructor(
516 function(content) return {c = content} end
519--- Creates a Strong element, whose text is usually displayed in a bold font.
521-- @tparam {Inline,..} content inline content
522-- @treturn Inline strong element
523M.Strong = M.Inline:create_constructor(
525 function(content) return {c = content} end
528--- Creates a Subscript inline element
529-- @function Subscript
530-- @tparam {Inline,..} content inline content
531-- @treturn Inline subscript element
532M.Subscript = M.Inline:create_constructor(
534 function(content) return {c = content} end
537--- Creates a Superscript inline element
538-- @function Superscript
539-- @tparam {Inline,..} content inline content
540-- @treturn Inline strong element
541M.Superscript = M.Inline:create_constructor(
543 function(content) return {c = content} end
547------------------------------------------------------------------------
553--- Create a new set of attributes (Attr).
554-- @function Attributes
555-- @tparam table key_values table containing string keys and values
556-- @tparam[opt] string id element identifier
557-- @tparam[opt] {string,...} classes element classes
558-- @return element attributes
559M.Attributes.__call = function(t, key_values, id, classes)
561 classes = classes or {}
562 local attr = {id, classes, key_values}
563 setmetatable(attr, t)
566M.Attributes.__index = function(t, k)
569 elseif k == "id" then
570 if rawget(t, 1) == '' then
575 elseif k == "class" then
576 if #(rawget(t, 2)) == 0 then
579 return table.concat(t[2], ' ')
582 for _, p in ipairs(t[3]) do
590M.Attributes.__newindex = function(t, k, v)
593 elseif k == "id" then
595 elseif k == "class" then
596 if type(v) == "string" then
602 for _, p in ipairs(rawget(t, 3)) do
613setmetatable(M.Attributes, M.Attributes)
616--- Creates a single citation.
618-- @tparam string id citation identifier (like a bibtex key)
619-- @tparam AuthorInText|SuppressAuthor|NormalCitation mode citation mode
620-- @tparam[opt] {Inline,...} prefix citation prefix
621-- @tparam[opt] {Inline,...} suffix citation suffix
622-- @tparam[opt] int note_num note number
623-- @tparam[opt] int note_num hash number
624M.Citation = function(id, mode, prefix, suffix, note_num, hash)
625 prefix = prefix or {}
626 suffix = suffix or {}
627 note_num = note_num or 0
631 citationPrefix = prefix,
632 citationSuffix = suffix,
634 citationNoteNum = note_num,
640------------------------------------------------------------------------
644--- Author name is mentioned in the text.
647M.AuthorInText = "AuthorInText"
649--- Author name is suppressed.
652M.SuppressAuthor = "SuppressAuthor"
654--- Default citation style is used.
657M.NormalCitation = "NormalCitation"
659--- Table cells aligned left.
661M.AlignLeft = "AlignLeft"
663--- Table cells right-aligned.
665M.AlignRight = "AlignRight"
667--- Table cell content is centered.
669M.AlignCenter = "AlignCenter"
671--- Table cells are alignment is unaltered.
673M.AlignDefault = "AlignDefault"
675--- Default list number delimiters are used.
677M.DefaultDelim = "DefaultDelim"
679--- List numbers are delimited by a period.
683--- List numbers are delimited by a single parenthesis.
685M.OneParen = "OneParen"
687--- List numbers are delimited by a double parentheses.
689M.TwoParens = "TwoParens"
691--- List are numbered in the default style
693M.DefaultStyle = "DefaultStyle"
695--- List items are numbered as examples.
699--- List are numbered using decimal integers.
703--- List are numbered using lower-case roman numerals.
705M.LowerRoman = "LowerRoman"
707--- List are numbered using upper-case roman numerals
709M.UpperRoman = "UpperRoman"
711--- List are numbered using lower-case alphabetic characters.
713M.LowerAlpha = "LowerAlpha"
715--- List are numbered using upper-case alphabetic characters.
717M.UpperAlpha = "UpperAlpha"
720------------------------------------------------------------------------
724--- Use functions defined in the global namespace to create a pandoc filter.
725-- All globally defined functions which have names of pandoc elements are
726-- collected into a new table.
727-- @return A list of filter functions
729-- -- within a file defining a pandoc filter:
731-- return pandoc.Str(utf8.upper(text))
734-- return {pandoc.global_filter()}
735-- -- the above is equivallent to
736-- -- return {{Str = Str}}
737function M.global_filter()
739 for k, v in pairs(_G) do
740 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