git.iko.soy/typeable/pandoc
on newlatex help sign in
typeable/pandoc/data/pandoc.lua
815 lines 23 kB history raw
1--[[
2pandoc.lua
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
16THIS SOFTWARE.
17]]
19---
20-- Lua functions for pandoc scripts.
21--
22-- @author Albert Krewinkel
23-- @copyright © 2017 Albert Krewinkel
24-- @license MIT
25local M = {
26 _VERSION = "0.3.0"
29------------------------------------------------------------------------
30-- The base class for pandoc's AST elements.
31-- @type Element
32-- @local
33local Element = {}
35--- Create a new element subtype
36-- @local
37function Element:make_subtype(o)
38 o = o or {}
39 setmetatable(o, self)
40 self.__index = self
41 return o
42end
44--- Create a new element given its tag and arguments
45-- @local
46function Element:new(tag, ...)
47 local element = { t = tag }
48 local content = {...}
49 -- special case for unary constructors
50 if #content == 1 then
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
55 element.c = content
56 end
57 setmetatable(element, self)
58 self.__index = self
59 return element
60end
62--- Create a new constructor
63-- @local
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)
73 return elem.c
74 end
75 constr.setters[accessors] = function(elem, v)
76 elem.c = v
77 end
78 else
79 for i = 1, #(accessors or {}) do
80 if type(accessors[i]) == "string" then
81 constr.getters[accessors[i]] = function(elem)
82 return elem.c[i]
83 end
84 constr.setters[accessors[i]] = function(elem, v)
85 elem.c[i] = v
86 end
87 else -- only two levels of nesting are supported
88 for k, v in ipairs(accessors[i]) do
89 constr.getters[v] = function(elem)
90 return elem.c[i][k]
91 end
92 constr.setters[v] = function(elem, v)
93 elem.c[i][k] = v
94 end
95 end
96 end
97 end
98 end
100 function constr:new(...)
101 local obj = fn(...)
102 setmetatable(obj, self)
103 self.__index = function(t, k)
104 if getmetatable(t).getters[k] then
105 return getmetatable(t).getters[k](t)
106 elseif k == "t" then
107 return getmetatable(t)["tag"]
108 else
109 return getmetatable(t)[k]
110 end
111 end
112 self.__newindex = function(t, k, v)
113 if getmetatable(t).setters[k] then
114 getmetatable(t).setters[k](t, v)
115 else
116 rawset(t, k, v)
117 end
118 end
119 return obj
120 end
121 self.constructor = self.constructor or {}
122 self.constructor[tag] = constr
123 return constr
124end
126--- Calls the constructor, creating a new element.
127-- @local
128function Element.__call(t, ...)
129 return t:new(...)
130end
132------------------------------------------------------------------------
133--- Pandoc Document
134-- @section document
136--- A complete pandoc document
137-- @function Doc
138-- @tparam {Block,...} blocks document content
139-- @tparam[opt] Meta meta document meta data
140function M.Pandoc(blocks, meta)
141 meta = meta or {}
142 return {
143 ["blocks"] = blocks,
144 ["meta"] = meta,
145 ["pandoc-api-version"] = {1,17,0,5},
146 }
147end
149-- DEPRECATED synonym:
150M.Doc = M.Pandoc
152------------------------------------------------------------------------
153-- MetaValue
154-- @section MetaValue
155M.MetaValue = Element:make_subtype{}
156M.MetaValue.__call = function(t, ...)
157 return t:new(...)
158end
159--- Meta blocks
160-- @function MetaBlocks
161-- @tparam {Block,...} blocks blocks
163--- Meta inlines
164-- @function MetaInlines
165-- @tparam {Inline,...} inlines inlines
167--- Meta list
168-- @function MetaList
169-- @tparam {MetaValue,...} meta_values list of meta values
171--- Meta map
172-- @function MetaMap
173-- @tparam table a string-index map of meta values
174M.meta_value_types = {
175 "MetaBlocks",
176 "MetaInlines",
177 "MetaList",
178 "MetaMap",
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],
183 function(content)
184 return content
185 end
186 )
187end
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)
194 return str
195end
197--- Creates boolean to be used in meta data.
198-- Does nothing, lua booleans are meta booleans.
199-- @function MetaBool
200-- @tparam boolean bool boolean value
201function M.MetaBool(bool)
202 return bool
203end
205------------------------------------------------------------------------
206-- Blocks
207-- @section Block
209--- Block elements
210M.Block = Element:make_subtype{}
211M.Block.__call = function (t, ...)
212 return t:new(...)
213end
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(
220 "BlockQuote",
221 function(content) return {c = content} end,
222 "content"
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(
230 "BulletList",
231 function(content) return {c = content} end,
232 "content"
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(
241 "CodeBlock",
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(
251 "DefinitionList",
252 function(content) return {c = content} end,
253 "content"
256--- Creates a div element
257-- @function Div
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(
262 "Div",
263 function(content, attr)
264 return {c = {attr or M.Attr(), content}}
265 end,
266 {{"identifier", "classes", "attributes"}, "content"}
269--- Creates a block quote element.
270-- @function Header
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(
276 "Header",
277 function(level, content, attr)
278 return {c = {level, attr or M.Attr(), content}}
279 end,
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(
287 "HorizontalRule",
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(
296 "LineBlock",
297 function(content) return {c = content} end,
298 "content"
301--- Creates a null element.
302-- @function Null
303-- @treturn Block null element
304M.Null = M.Block:create_constructor(
305 "Null",
306 function() return {} end
309--- Creates an ordered list.
310-- @function OrderedList
311-- @tparam {{Block,...},...} items list items
312-- @param[opt] listAttributes list parameters
313-- @treturn Block
314M.OrderedList = M.Block:create_constructor(
315 "OrderedList",
316 function(items, listAttributes)
317 return {c = {listAttributes,items}}
318 end,
319 {{"start", "style", "delimiter"}, "content"}
322--- Creates a para element.
323-- @function Para
324-- @tparam {Inline,...} content inline content
325-- @treturn Block block quote element
326M.Para = M.Block:create_constructor(
327 "Para",
328 function(content) return {c = content} end,
329 "content"
332--- Creates a plain element.
333-- @function Plain
334-- @tparam {Inline,...} content inline content
335-- @treturn Block block quote element
336M.Plain = M.Block:create_constructor(
337 "Plain",
338 function(content) return {c = content} end,
339 "content"
342--- Creates a raw content block of the specified format.
343-- @function RawBlock
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(
348 "RawBlock",
349 function(format, text) return {c = {format, text}} end,
350 {"format", "text"}
353--- Creates a table element.
354-- @function Table
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(
362 "Table",
363 function(caption, aligns, widths, headers, rows)
364 return {c = {caption, aligns, widths, headers, rows}}
365 end,
366 {"caption", "aligns", "widths", "headers", "rows"}
370------------------------------------------------------------------------
371-- Inline
372-- @section Inline
374--- Inline element class
375M.Inline = Element:make_subtype{}
376M.Inline.__call = function (t, ...)
377 return t:new(...)
378end
380--- Creates a Cite inline element
381-- @function Cite
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(
386 "Cite",
387 function(content, citations) return {c = {citations, content}} end,
388 {"citations", "content"}
391--- Creates a Code inline element
392-- @function Code
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(
397 "Code",
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.
403-- @function Emph
404-- @tparam {Inline,..} content inline content
405-- @treturn Inline emphasis element
406M.Emph = M.Inline:create_constructor(
407 "Emph",
408 function(content) return {c = content} end,
409 "content"
412--- Creates a Image inline element
413-- @function Image
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(
420 "Image",
421 function(caption, src, title, attr)
422 title = title or ""
423 attr = attr or M.Attr()
424 return {c = {attr, caption, {src, title}}}
425 end,
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(
433 "LineBreak",
434 function() return {} end
437--- Creates a link inline element, usually a hyperlink.
438-- @function Link
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(
445 "Link",
446 function(content, target, title, attr)
447 title = title or ""
448 attr = attr or M.Attr()
449 return {c = {attr, content, {target, title}}}
450 end,
451 {"attributes", "content", {"target", "title"}}
454--- Creates a Math element, either inline or displayed.
455-- @function Math
456-- @tparam "InlineMath"|"DisplayMath" mathtype rendering specifier
457-- @tparam string text Math content
458-- @treturn Inline Math element
459M.Math = M.Inline:create_constructor(
460 "Math",
461 function(mathtype, text)
462 return {c = {mathtype, text}}
463 end,
464 {"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(
471 "DisplayMath",
472 function(text) return M.Math("DisplayMath", text) end,
473 {"mathtype", "text"}
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(
480 "InlineMath",
481 function(text) return M.Math("InlineMath", text) end,
482 {"mathtype", "text"}
485--- Creates a Note inline element
486-- @function Note
487-- @tparam {Block,...} content footnote block content
488M.Note = M.Inline:create_constructor(
489 "Note",
490 function(content) return {c = content} end,
491 "content"
494--- Creates a Quoted inline element given the quote type and quoted content.
495-- @function Quoted
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(
500 "Quoted",
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
508-- @see Quoted
509M.SingleQuoted = M.Inline:create_constructor(
510 "SingleQuoted",
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
518-- @see Quoted
519M.DoubleQuoted = M.Inline:create_constructor(
520 "DoubleQuoted",
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(
531 "RawInline",
532 function(format, text) return {c = {format, text}} end,
533 {"format", "text"}
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(
541 "SmallCaps",
542 function(content) return {c = content} end,
543 "content"
546--- Creates a SoftBreak inline element.
547-- @function SoftBreak
548-- @treturn Inline softbreak element
549M.SoftBreak = M.Inline:create_constructor(
550 "SoftBreak",
551 function() return {} end
554--- Create a Space inline element
555-- @function Space
556-- @treturn Inline space element
557M.Space = M.Inline:create_constructor(
558 "Space",
559 function() return {} end
562--- Creates a Span inline element
563-- @function Span
564-- @tparam {Inline,..} content inline content
565-- @tparam[opt] Attr attr additional attributes
566-- @treturn Inline span element
567M.Span = M.Inline:create_constructor(
568 "Span",
569 function(content, attr) return {c = {attr or M.Attr(), content}} end,
570 {{"identifier", "classes", "attributes"}, "content"}
573--- Creates a Str inline element
574-- @function Str
575-- @tparam string text content
576-- @treturn Inline string element
577M.Str = M.Inline:create_constructor(
578 "Str",
579 function(text) return {c = text} end,
580 "text"
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(
588 "Strikeout",
589 function(content) return {c = content} end,
590 "content"
593--- Creates a Strong element, whose text is usually displayed in a bold font.
594-- @function Strong
595-- @tparam {Inline,..} content inline content
596-- @treturn Inline strong element
597M.Strong = M.Inline:create_constructor(
598 "Strong",
599 function(content) return {c = content} end,
600 "content"
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(
608 "Subscript",
609 function(content) return {c = content} end,
610 "content"
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(
618 "Superscript",
619 function(content) return {c = content} end,
620 "content"
624------------------------------------------------------------------------
625-- Helpers
626-- @section helpers
628-- Attr
629M.Attr = {}
630M.Attr._field_names = {identifier = 1, classes = 2, attributes = 3}
631--- Create a new set of attributes (Attr).
632-- @function 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)
643 return attr
644end
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)
649end
650M.Attr.__newindex = function(t, k, v)
651 if M.Attr._field_names[k] then
652 rawset(t, M.Attr._field_names[k], v)
653 else
654 rawset(t, k, v)
655 end
656end
657setmetatable(M.Attr, M.Attr)
660--- Creates a single citation.
661-- @function 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
672 hash = hash or 0
673 return {
674 citationId = id,
675 citationPrefix = prefix,
676 citationSuffix = suffix,
677 citationMode = mode,
678 citationNoteNum = note_num,
679 citationHash = hash,
680 }
681end
684------------------------------------------------------------------------
685-- Constants
686-- @section constants
688--- Author name is mentioned in the text.
689-- @see Citation
690-- @see Cite
691M.AuthorInText = "AuthorInText"
693--- Author name is suppressed.
694-- @see Citation
695-- @see Cite
696M.SuppressAuthor = "SuppressAuthor"
698--- Default citation style is used.
699-- @see Citation
700-- @see Cite
701M.NormalCitation = "NormalCitation"
703--- Table cells aligned left.
704-- @see Table
705M.AlignLeft = "AlignLeft"
707--- Table cells right-aligned.
708-- @see Table
709M.AlignRight = "AlignRight"
711--- Table cell content is centered.
712-- @see Table
713M.AlignCenter = "AlignCenter"
715--- Table cells are alignment is unaltered.
716-- @see Table
717M.AlignDefault = "AlignDefault"
719--- Default list number delimiters are used.
720-- @see OrderedList
721M.DefaultDelim = "DefaultDelim"
723--- List numbers are delimited by a period.
724-- @see OrderedList
725M.Period = "Period"
727--- List numbers are delimited by a single parenthesis.
728-- @see OrderedList
729M.OneParen = "OneParen"
731--- List numbers are delimited by a double parentheses.
732-- @see OrderedList
733M.TwoParens = "TwoParens"
735--- List are numbered in the default style
736-- @see OrderedList
737M.DefaultStyle = "DefaultStyle"
739--- List items are numbered as examples.
740-- @see OrderedList
741M.Example = "Example"
743--- List are numbered using decimal integers.
744-- @see OrderedList
745M.Decimal = "Decimal"
747--- List are numbered using lower-case roman numerals.
748-- @see OrderedList
749M.LowerRoman = "LowerRoman"
751--- List are numbered using upper-case roman numerals
752-- @see OrderedList
753M.UpperRoman = "UpperRoman"
755--- List are numbered using lower-case alphabetic characters.
756-- @see OrderedList
757M.LowerAlpha = "LowerAlpha"
759--- List are numbered using upper-case alphabetic characters.
760-- @see OrderedList
761M.UpperAlpha = "UpperAlpha"
764------------------------------------------------------------------------
765-- Helper Functions
766-- @section helpers
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
775-- @usage
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
786 error(pd)
787 else
788 return pd
789 end
790end
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
796-- @usage
797-- -- within a file defining a pandoc filter:
798-- function Str(text)
799-- return pandoc.Str(utf8.upper(text))
800-- end
801--
802-- return {pandoc.global_filter()}
803-- -- the above is equivallent to
804-- -- return {{Str = Str}}
805function M.global_filter()
806 local res = {}
807 for k, v in pairs(_G) do
808 if M.Inline.constructor[k] or M.Block.constructor[k] or k == "Doc" then
809 res[k] = v
810 end
811 end
812 return res
813end
815return M