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 attributes element attributes
237-- @treturn Block code block element
238M.CodeBlock = M.Block:create_constructor(
240 function(text, attributes) return {c = {attributes, 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 attributes element attributes
258-- @treturn Block code block element
259M.Div = M.Block:create_constructor(
261 function(content, attributes) return {c = {attributes, content}} end,
262 {{"identifier", "classes", "attributes"}, "content"}
265--- Creates a block quote element.
267-- @tparam int level header level
268-- @tparam {Inline,...} content inline content
269-- @tparam Attr attributes element attributes
270-- @treturn Block header element
271M.Header = M.Block:create_constructor(
273 function(level, content, attributes)
274 return {c = {level, attributes, content}}
276 {"level", {"identifier", "classes", "attributes"}, "content"}
279--- Creates a horizontal rule.
280-- @function HorizontalRule
281-- @treturn Block horizontal rule
282M.HorizontalRule = M.Block:create_constructor(
284 function() return {} end
287--- Creates a line block element.
288-- @function LineBlock
289-- @tparam {{Inline,...},...} content inline content
290-- @treturn Block block quote element
291M.LineBlock = M.Block:create_constructor(
293 function(content) return {c = content} end,
297--- Creates a null element.
299-- @treturn Block null element
300M.Null = M.Block:create_constructor(
302 function() return {} end
305--- Creates an ordered list.
306-- @function OrderedList
307-- @tparam {{Block,...},...} items list items
308-- @param[opt] listAttributes list parameters
310M.OrderedList = M.Block:create_constructor(
312 function(items, listAttributes)
313 return {c = {listAttributes,items}}
315 {{"start", "style", "delimiter"}, "content"}
318--- Creates a para element.
320-- @tparam {Inline,...} content inline content
321-- @treturn Block block quote element
322M.Para = M.Block:create_constructor(
324 function(content) return {c = content} end,
328--- Creates a plain element.
330-- @tparam {Inline,...} content inline content
331-- @treturn Block block quote element
332M.Plain = M.Block:create_constructor(
334 function(content) return {c = content} end,
338--- Creates a raw content block of the specified format.
340-- @tparam string format format of content
341-- @tparam string text string content
342-- @treturn Block block quote element
343M.RawBlock = M.Block:create_constructor(
345 function(format, text) return {c = {format, text}} end,
349--- Creates a table element.
351-- @tparam {Inline,...} caption table caption
352-- @tparam {AlignDefault|AlignLeft|AlignRight|AlignCenter,...} aligns alignments
353-- @tparam {int,...} widths column widths
354-- @tparam {Block,...} headers header row
355-- @tparam {{Block,...}} rows table rows
356-- @treturn Block block quote element
357M.Table = M.Block:create_constructor(
359 function(caption, aligns, widths, headers, rows)
360 return {c = {caption, aligns, widths, headers, rows}}
365------------------------------------------------------------------------
369--- Inline element class
370M.Inline = Element:make_subtype{}
371M.Inline.__call = function (t, ...)
375--- Creates a Cite inline element
377-- @tparam {Inline,...} content List of inlines
378-- @tparam {Citation,...} citations List of citations
379-- @treturn Inline citations element
380M.Cite = M.Inline:create_constructor(
382 function(content, citations) return {c = {citations, content}} end,
383 {"citations", "content"}
386--- Creates a Code inline element
388-- @tparam string text brief image description
389-- @tparam[opt] Attr attributes additional attributes
390-- @treturn Inline code element
391M.Code = M.Inline:create_constructor(
393 function(text, attributes) return {c = {attributes, text}} end,
394 {{"identifier", "classes", "attributes"}, "text"}
397--- Creates an inline element representing emphasised text.
399-- @tparam {Inline,..} content inline content
400-- @treturn Inline emphasis element
401M.Emph = M.Inline:create_constructor(
403 function(content) return {c = content} end,
407--- Creates a Image inline element
409-- @tparam {Inline,..} caption text used to describe the image
410-- @tparam string src path to the image file
411-- @tparam[opt] string title brief image description
412-- @tparam[opt] Attr attributes additional attributes
413-- @treturn Inline image element
414M.Image = M.Inline:create_constructor(
416 function(caption, src, title, attributes)
418 attributes = attributes or Attribute.empty
419 return {c = {attributes, caption, {src, title}}}
421 {"attributes", "caption", {"src", "title"}}
424--- Create a LineBreak inline element
425-- @function LineBreak
426-- @treturn Inline linebreak element
427M.LineBreak = M.Inline:create_constructor(
429 function() return {} end
432--- Creates a link inline element, usually a hyperlink.
434-- @tparam {Inline,..} content text for this link
435-- @tparam string target the link target
436-- @tparam[opt] string title brief link description
437-- @tparam[opt] Attr attributes additional attributes
438-- @treturn Inline image element
439M.Link = M.Inline:create_constructor(
441 function(content, target, title, attributes)
443 attributes = attributes or Attribute.empty
444 return {c = {attributes, content, {target, title}}}
446 {"attributes", "content", {"target", "title"}}
449--- Creates a Math element, either inline or displayed. It is usually simpler to
450-- use one of the specialized functions @{InlineMath} or @{DisplayMath} instead.
452-- @tparam "InlineMath"|"DisplayMath" mathtype rendering specifier
453-- @tparam string text Math content
454-- @treturn Inline Math element
455M.Math = M.Inline:create_constructor(
457 function(mathtype, text)
458 return {c = {mathtype, text}}
462--- Creates a DisplayMath element.
463-- @function DisplayMath
464-- @tparam string text Math content
465-- @treturn Inline Math element
466M.DisplayMath = M.Inline:create_constructor(
468 function(text) return M.Math("DisplayMath", text) end,
471--- Creates an InlineMath inline element.
472-- @function InlineMath
473-- @tparam string text Math content
474-- @treturn Inline Math element
475M.InlineMath = M.Inline:create_constructor(
477 function(text) return M.Math("InlineMath", text) end,
481--- Creates a Note inline element
483-- @tparam {Block,...} content footnote block content
484M.Note = M.Inline:create_constructor(
486 function(content) return {c = content} end,
490--- Creates a Quoted inline element given the quote type and quoted content. It
491-- is usually simpler to use one of the specialized functions @{SingleQuoted} or
492-- @{DoubleQuoted} instead.
494-- @tparam "DoubleQuote"|"SingleQuote" quotetype type of quotes to be used
495-- @tparam {Inline,..} content inline content
496-- @treturn Inline quoted element
497M.Quoted = M.Inline:create_constructor(
499 function(quotetype, content) return {c = {quotetype, content}} end,
500 {"quotetype", "content"}
502--- Creates a single-quoted inline element.
503-- @function SingleQuoted
504-- @tparam {Inline,..} content inline content
505-- @treturn Inline quoted element
507M.SingleQuoted = M.Inline:create_constructor(
509 function(content) return M.Quoted(M.SingleQuote, content) end,
510 {"quotetype", "content"}
512--- Creates a single-quoted inline element.
513-- @function DoubleQuoted
514-- @tparam {Inline,..} content inline content
515-- @treturn Inline quoted element
517M.DoubleQuoted = M.Inline:create_constructor(
519 function(content) return M.Quoted("DoubleQuote", content) end,
520 {"quotetype", "content"}
523--- Creates a RawInline inline element
524-- @function RawInline
525-- @tparam string format format of the contents
526-- @tparam string text string content
527-- @treturn Inline raw inline element
528M.RawInline = M.Inline:create_constructor(
530 function(format, text) return {c = {format, text}} end,
534--- Creates text rendered in small caps
535-- @function SmallCaps
536-- @tparam {Inline,..} content inline content
537-- @treturn Inline smallcaps element
538M.SmallCaps = M.Inline:create_constructor(
540 function(content) return {c = content} end,
544--- Creates a SoftBreak inline element.
545-- @function SoftBreak
546-- @treturn Inline softbreak element
547M.SoftBreak = M.Inline:create_constructor(
549 function() return {} end
552--- Create a Space inline element
554-- @treturn Inline space element
555M.Space = M.Inline:create_constructor(
557 function() return {} end
560--- Creates a Span inline element
562-- @tparam {Inline,..} content inline content
563-- @tparam[opt] Attr attributes additional attributes
564-- @treturn Inline span element
565M.Span = M.Inline:create_constructor(
567 function(content, attributes) return {c = {attributes, content}} end,
568 {{"identifier", "classes", "attributes"}, "content"}
571--- Creates a Str inline element
573-- @tparam string text content
574-- @treturn Inline string element
575M.Str = M.Inline:create_constructor(
577 function(text) return {c = text} end,
581--- Creates text which is striked out.
582-- @function Strikeout
583-- @tparam {Inline,..} content inline content
584-- @treturn Inline strikeout element
585M.Strikeout = M.Inline:create_constructor(
587 function(content) return {c = content} end,
591--- Creates a Strong element, whose text is usually displayed in a bold font.
593-- @tparam {Inline,..} content inline content
594-- @treturn Inline strong element
595M.Strong = M.Inline:create_constructor(
597 function(content) return {c = content} end,
601--- Creates a Subscript inline element
602-- @function Subscript
603-- @tparam {Inline,..} content inline content
604-- @treturn Inline subscript element
605M.Subscript = M.Inline:create_constructor(
607 function(content) return {c = content} end,
611--- Creates a Superscript inline element
612-- @function Superscript
613-- @tparam {Inline,..} content inline content
614-- @treturn Inline strong element
615M.Superscript = M.Inline:create_constructor(
617 function(content) return {c = content} end,
622------------------------------------------------------------------------
628M.Attr._field_names = {identifier = 1, classes = 2, attributes = 3}
629--- Create a new set of attributes (Attr).
631-- @tparam[opt] string identifier element identifier
632-- @tparam[opt] {string,...} classes element classes
633-- @tparam[opt] table attributes table containing string keys and values
634-- @return element attributes
635M.Attr.__call = function(t, identifier, classes, attributes)
636 identifier = identifier or ''
637 classes = classes or {}
638 attributes = attributes or {}
639 local attr = {identifier, classes, attributes}
640 setmetatable(attr, t)
643M.Attr.__index = function(t, k)
644 return rawget(t, k) or
645 rawget(t, M.Attr._field_names[k]) or
646 rawget(getmetatable(t), k)
648M.Attr.__newindex = function(t, k, v)
649 if M.Attr._field_names[k] then
650 rawset(t, M.Attr._field_names[k], v)
655setmetatable(M.Attr, M.Attr)
658--- Creates a single citation.
660-- @tparam string id citation identifier (like a bibtex key)
661-- @tparam AuthorInText|SuppressAuthor|NormalCitation mode citation mode
662-- @tparam[opt] {Inline,...} prefix citation prefix
663-- @tparam[opt] {Inline,...} suffix citation suffix
664-- @tparam[opt] int note_num note number
665-- @tparam[opt] int note_num hash number
666M.Citation = function(id, mode, prefix, suffix, note_num, hash)
667 prefix = prefix or {}
668 suffix = suffix or {}
669 note_num = note_num or 0
673 citationPrefix = prefix,
674 citationSuffix = suffix,
676 citationNoteNum = note_num,
682------------------------------------------------------------------------
686--- Author name is mentioned in the text.
689M.AuthorInText = "AuthorInText"
691--- Author name is suppressed.
694M.SuppressAuthor = "SuppressAuthor"
696--- Default citation style is used.
699M.NormalCitation = "NormalCitation"
701--- Table cells aligned left.
703M.AlignLeft = "AlignLeft"
705--- Table cells right-aligned.
707M.AlignRight = "AlignRight"
709--- Table cell content is centered.
711M.AlignCenter = "AlignCenter"
713--- Table cells are alignment is unaltered.
715M.AlignDefault = "AlignDefault"
717--- Default list number delimiters are used.
719M.DefaultDelim = "DefaultDelim"
721--- List numbers are delimited by a period.
725--- List numbers are delimited by a single parenthesis.
727M.OneParen = "OneParen"
729--- List numbers are delimited by a double parentheses.
731M.TwoParens = "TwoParens"
733--- List are numbered in the default style
735M.DefaultStyle = "DefaultStyle"
737--- List items are numbered as examples.
741--- List are numbered using decimal integers.
745--- List are numbered using lower-case roman numerals.
747M.LowerRoman = "LowerRoman"
749--- List are numbered using upper-case roman numerals
751M.UpperRoman = "UpperRoman"
753--- List are numbered using lower-case alphabetic characters.
755M.LowerAlpha = "LowerAlpha"
757--- List are numbered using upper-case alphabetic characters.
759M.UpperAlpha = "UpperAlpha"
762------------------------------------------------------------------------
766--- Parse the given string into a Pandoc document.
767-- The method used to interpret input is specified by *format*. Acceptable
768-- values for this parameter are equal to those that can be given to the
769-- `--from` command line option.
770-- @tparam string markup the markup to be parsed
771-- @tparam[opt] string format format specification, defaults to "markdown".
772-- @return Doc pandoc document
774-- local org_markup = "/emphasis/" -- Input to be read
775-- local document = pandoc.read(org_markup, "org")
776-- -- Get the first block of the document
777-- local block = document.blocks[1]
778-- -- The inline element in that block is an `Emph`
779-- assert(block.content[1].t == "Emph")
780function M.read(markup, format)
781 format = format or "markdown"
782 local pd = pandoc.__read(format, markup)
783 if type(pd) == "string" then
790--- Use functions defined in the global namespace to create a pandoc filter.
791-- All globally defined functions which have names of pandoc elements are
792-- collected into a new table.
793-- @return A list of filter functions
795-- -- within a file defining a pandoc filter:
797-- return pandoc.Str(utf8.upper(text))
800-- return {pandoc.global_filter()}
801-- -- the above is equivallent to
802-- -- return {{Str = Str}}
803function M.global_filter()
805 for k, v in pairs(_G) do
806 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