git.iko.soy/typeable/pandoc
on 932e395 help sign in
typeable/pandoc/data/pandoc.lua
706 lines 20 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.2.0"
29--- Attributes
30-- @type Attributes
31M.Attributes = {}
32setmetatable(M.Attributes, M.Attributes)
34M.Attributes.__index = function(t, k)
35 if k == "id" then
36 return t[1]
37 elseif k == "class" then
38 return table.concat(t[2], ' ')
39 else
40 return t.kv[k]
41 end
42end
44--- Create a new set of attributes (Attr).
45-- @function Attributes
46M.Attributes.__call = function(t, key_values, id, classes)
47 local kv = {}
48 for i = 1, #key_values do
49 kv[key_values[i][1]] = key_values[i][2]
50 end
51 id = id or ''
52 classes = classes or {}
53 local attr = {id, classes, key_values, kv = kv}
54 setmetatable(attr, t)
55 return attr
56end
57M.Attributes.empty = M.Attributes('', {}, {})
60--- Creates a single citation.
61-- @function Citation
62-- @tparam string id citation identifier (like a bibtex key)
63-- @tparam AuthorInText|SuppressAuthor|NormalCitation mode citation mode
64-- @tparam[opt] {Inline,...} prefix citation prefix
65-- @tparam[opt] {Inline,...} suffix citation suffix
66-- @tparam[opt] int note_num note number
67-- @tparam[opt] int note_num hash number
68M.Citation = function(id, mode, prefix, suffix, note_num, hash)
69 prefix = prefix or {}
70 suffix = suffix or {}
71 note_num = note_num or 0
72 hash = hash or 0
73 return {
74 citationId = id,
75 citationPrefix = prefix,
76 citationSuffix = suffix,
77 citationMode = mode,
78 citationNoteNum = note_num,
79 citationHash = hash,
80 }
81end
83------------------------------------------------------------------------
84-- The base class for pandoc's AST elements.
85-- @type Element
86-- @local
87local Element = {}
89--- Create a new element subtype
90-- @local
91function Element:make_subtype(o)
92 o = o or {}
93 setmetatable(o, self)
94 self.__index = self
95 return o
96end
98--- Create a new element given its tag and arguments
99-- @local
100function Element:new(tag, ...)
101 local element = { t = tag }
102 local content = {...}
103 -- special case for unary constructors
104 if #content == 1 then
105 element.c = content[1]
106 -- Don't set 'c' field if no further arguments were given. This is important
107 -- for nullary constructors like `Space` and `HorizontalRule`.
108 elseif #content > 0 then
109 element.c = content
110 end
111 setmetatable(element, self)
112 self.__index = self
113 return element
114end
116--- Create a new constructor
117-- @local
118-- @param tag Tag used to identify the constructor
119-- @param fn Function to be called when constructing a new element
120-- @return function that constructs a new element
121function Element:create_constructor(tag, fn)
122 local constr = self:make_subtype({tag = tag})
123 function constr:new(...)
124 local obj = fn(...)
125 setmetatable(obj, self)
126 self.__index = function(t, k)
127 if k == "c" then
128 return t["content"]
129 elseif k == "t" then
130 return getmetatable(t)["tag"]
131 else
132 return getmetatable(t)[k]
133 end
134 end
135 return obj
136 end
137 self.constructor = self.constructor or {}
138 self.constructor[tag] = constr
139 return constr
140end
142--- Calls the constructor, creating a new element.
143-- @local
144function Element.__call(t, ...)
145 return t:new(...)
146end
148------------------------------------------------------------------------
149--- Pandoc Document
150-- @section document
152--- A complete pandoc document
153-- @function Doc
154-- @tparam {Block,...} blocks document content
155-- @tparam[opt] Meta meta document meta data
156function M.Doc(blocks, meta)
157 meta = meta or {}
158 return {
159 ["blocks"] = blocks,
160 ["meta"] = meta,
161 ["pandoc-api-version"] = {1,17,0,5},
162 }
163end
166------------------------------------------------------------------------
167-- MetaValue
168-- @section MetaValue
169M.MetaValue = Element:make_subtype{}
170M.MetaValue.__call = function(t, ...)
171 return t:new(...)
172end
173--- Meta blocks
174-- @function MetaBlocks
175-- @tparam {Block,...} blocks blocks
177--- Meta inlines
178-- @function MetaInlines
179-- @tparam {Inline,...} inlines inlines
181--- Meta list
182-- @function MetaList
183-- @tparam {MetaValue,...} meta_values list of meta values
185--- Meta boolean
186-- @function MetaBool
187-- @tparam boolean bool boolean value
189--- Meta map
190-- @function MetaMap
191-- @tparam table a string-index map of meta values
193--- Meta string
194-- @function MetaString
195-- @tparam string str string value
196M.meta_value_types = {
197 "MetaBlocks",
198 "MetaBool",
199 "MetaInlines",
200 "MetaList",
201 "MetaMap",
202 "MetaString"
204for i = 1, #M.meta_value_types do
205 M[M.meta_value_types[i]] = M.MetaValue:create_constructor(
206 M.meta_value_types[i],
207 function(content)
208 return {c = content}
209 end
210 )
211end
213------------------------------------------------------------------------
214-- Block
215-- @section Block
217M.Block = Element:make_subtype{}
218M.Block.__call = function (t, ...)
219 return t:new(...)
220end
222--- Creates a block quote element
223-- @function BlockQuote
224-- @tparam {Block,...} content block content
225-- @treturn Block block quote element
226M.BlockQuote = M.Block:create_constructor(
227 "BlockQuote",
228 function(content) return {c = content} end
231--- Creates a bullet (i.e. unordered) list.
232-- @function BulletList
233-- @tparam {{Block,...},...} content list of items
234-- @treturn Block block quote element
235M.BulletList = M.Block:create_constructor(
236 "BulletList",
237 function(content) return {c = content} end
240--- Creates a code block element
241-- @function CodeBlock
242-- @tparam string code code string
243-- @tparam[opt] Attributes attributes element attributes
244-- @treturn Block code block element
245M.CodeBlock = M.Block:create_constructor(
246 "CodeBlock",
247 function(code, attributes) return {c = {attributes, code}} end
250--- Creates a definition list, containing terms and their explanation.
251-- @function DefinitionList
252-- @tparam {{{Inline,...},{Block,...}},...} content list of items
253-- @treturn Block block quote element
254M.DefinitionList = M.Block:create_constructor(
255 "DefinitionList",
256 function(content) return {c = content} end
259--- Creates a div element
260-- @function Div
261-- @tparam {Block,...} content block content
262-- @tparam[opt] Attributes attributes element attributes
263-- @treturn Block code block element
264M.Div = M.Block:create_constructor(
265 "Div",
266 function(content, attributes) return {c = {attributes, content}} end
269--- Creates a block quote element.
270-- @function Header
271-- @tparam int level header level
272-- @tparam Attributes attributes element attributes
273-- @tparam {Inline,...} content inline content
274-- @treturn Block header element
275M.Header = M.Block:create_constructor(
276 "Header",
277 function(level, attributes, content)
278 return {c = {level, attributes, content}}
279 end
282--- Creates a horizontal rule.
283-- @function HorizontalRule
284-- @treturn Block horizontal rule
285M.HorizontalRule = M.Block:create_constructor(
286 "HorizontalRule",
287 function() return {} end
290--- Creates a line block element.
291-- @function LineBlock
292-- @tparam {{Inline,...},...} content inline content
293-- @treturn Block block quote element
294M.LineBlock = M.Block:create_constructor(
295 "LineBlock",
296 function(content) return {c = content} end
299--- Creates a null element.
300-- @function Null
301-- @treturn Block null element
302M.Null = M.Block:create_constructor(
303 "Null",
304 function() return {} end
307--- Creates an ordered list.
308-- @function OrderedList
309-- @tparam {{Block,...},...} items list items
310-- @param[opt] listAttributes list parameters
311-- @treturn Block
312M.OrderedList = M.Block:create_constructor(
313 "OrderedList",
314 function(items, listAttributes)
315 return {c = {listAttributes,items}}
316 end
319--- Creates a para element.
320-- @function Para
321-- @tparam {Inline,...} content inline content
322-- @treturn Block block quote element
323M.Para = M.Block:create_constructor(
324 "Para",
325 function(content) return {c = content} end
328--- Creates a plain element.
329-- @function Plain
330-- @tparam {Inline,...} content inline content
331-- @treturn Block block quote element
332M.Plain = M.Block:create_constructor(
333 "Plain",
334 function(content) return {c = content} end
337--- Creates a raw content block of the specified format.
338-- @function RawBlock
339-- @tparam string format format of content
340-- @tparam string content string content
341-- @treturn Block block quote element
342M.RawBlock = M.Block:create_constructor(
343 "RawBlock",
344 function(format, content) return {c = {format, content}} end
347--- Creates a table element.
348-- @function Table
349-- @tparam {Inline,...} caption table caption
350-- @tparam {AlignDefault|AlignLeft|AlignRight|AlignCenter,...} aligns alignments
351-- @tparam {int,...} widths column widths
352-- @tparam {Block,...} headers header row
353-- @tparam {{Block,...}} rows table rows
354-- @treturn Block block quote element
355M.Table = M.Block:create_constructor(
356 "Table",
357 function(caption, aligns, widths, headers, rows)
358 return {c = {caption, aligns, widths, headers, rows}}
359 end
363------------------------------------------------------------------------
364-- Inline
365-- @section Inline
367--- Inline element class
368M.Inline = Element:make_subtype{}
369M.Inline.__call = function (t, ...)
370 return t:new(...)
371end
373--- Creates a Cite inline element
374-- @function Cite
375-- @tparam {Inline,...} content List of inlines
376-- @tparam {Citation,...} citations List of citations
377-- @treturn Inline citations element
378M.Cite = M.Inline:create_constructor(
379 "Cite",
380 function(content, citations) return {c = {citations, content}} end
383--- Creates a Code inline element
384-- @function Code
385-- @tparam string code brief image description
386-- @tparam[opt] Attributes attributes additional attributes
387-- @treturn Inline code element
388M.Code = M.Inline:create_constructor(
389 "Code",
390 function(code, attributes) return {c = {attributes, code}} end
393--- Creates an inline element representing emphasised text.
394-- @function Emph
395-- @tparam {Inline,..} content inline content
396-- @treturn Inline emphasis element
397M.Emph = M.Inline:create_constructor(
398 "Emph",
399 function(content) return {c = content} end
402--- Creates a Image inline element
403-- @function Image
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(
410 "Image",
411 function(caption, src, title, attributes)
412 title = title or ""
413 attributes = attributes or Attribute.empty
414 return {c = {attributes, caption, {src, title}}}
415 end
418--- Create a LineBreak inline element
419-- @function LineBreak
420-- @treturn Inline linebreak element
421M.LineBreak = M.Inline:create_constructor(
422 "LineBreak",
423 function() return {} end
426--- Creates a link inline element, usually a hyperlink.
427-- @function Link
428-- @tparam {Inline,..} content text for this link
429-- @tparam string target the link target
430-- @tparam[opt] string title brief link description
431-- @tparam[opt] Attributes attributes additional attributes
432-- @treturn Inline image element
433M.Link = M.Inline:create_constructor(
434 "Link",
435 function(content, target, title, attributes)
436 title = title or ""
437 attributes = attributes or Attribute.empty
438 return {c = {attributes, content, {target, title}}}
439 end
442--- Creates a Math inline element
443-- @function Math
444-- @tparam InlineMath|DisplayMath mathtype Display specifier
445-- @tparam string text Math content
446-- @treturn Inline Math element
447M.Math = M.Inline:create_constructor(
448 "Math",
449 function(mathtype, text)
450 return {c = {mathtype, text}}
451 end
454--- Creates a Note inline element
455-- @function Note
456-- @tparam {Block,...} content footnote block content
457M.Note = M.Inline:create_constructor(
458 "Note",
459 function(contents) return {c = contents} end
462--- Creates a Quoted inline element
463-- @function Quoted
464-- @tparam DoubleQuote|SingleQuote quotetype type of quotes to be used
465-- @tparam {Inline,..} content inline content
466-- @treturn Inline quoted element
467M.Quoted = M.Inline:create_constructor(
468 "Quoted",
469 function(quotetype, content) return {c = {quotetype, content}} end
471--- Creates a RawInline inline element
472-- @function RawInline
473-- @tparam string format format of the contents
474-- @tparam string text string content
475-- @treturn Inline raw inline element
476M.RawInline = M.Inline:create_constructor(
477 "RawInline",
478 function(format, text) return {c = {format, text}} end
481--- Creates text rendered in small caps
482-- @function SmallCaps
483-- @tparam {Inline,..} content inline content
484-- @treturn Inline smallcaps element
485M.SmallCaps = M.Inline:create_constructor(
486 "SmallCaps",
487 function(content) return {c = content} end
490--- Creates a SoftBreak inline element.
491-- @function SoftBreak
492-- @treturn Inline softbreak element
493M.SoftBreak = M.Inline:create_constructor(
494 "SoftBreak",
495 function() return {} end
498--- Create a Space inline element
499-- @function Space
500-- @treturn Inline space element
501M.Space = M.Inline:create_constructor(
502 "Space",
503 function() return {} end
506--- Creates a Span inline element
507-- @function Span
508-- @tparam {Inline,..} content inline content
509-- @tparam[opt] Attributes attributes additional attributes
510-- @treturn Inline span element
511M.Span = M.Inline:create_constructor(
512 "Span",
513 function(content, attributes) return {c = {attributes, content}} end
516--- Creates a Str inline element
517-- @function Str
518-- @tparam string text content
519-- @treturn Inline string element
520M.Str = M.Inline:create_constructor(
521 "Str",
522 function(text) return {c = text} end
525--- Creates text which is striked out.
526-- @function Strikeout
527-- @tparam {Inline,..} content inline content
528-- @treturn Inline strikeout element
529M.Strikeout = M.Inline:create_constructor(
530 "Strikeout",
531 function(content) return {c = content} end
534--- Creates a Strong element, whose text is usually displayed in a bold font.
535-- @function Strong
536-- @tparam {Inline,..} content inline content
537-- @treturn Inline strong element
538M.Strong = M.Inline:create_constructor(
539 "Strong",
540 function(content) return {c = content} end
543--- Creates a Subscript inline element
544-- @function Subscript
545-- @tparam {Inline,..} content inline content
546-- @treturn Inline subscript element
547M.Subscript = M.Inline:create_constructor(
548 "Subscript",
549 function(content) return {c = content} end
552--- Creates a Superscript inline element
553-- @function Superscript
554-- @tparam {Inline,..} content inline content
555-- @treturn Inline strong element
556M.Superscript = M.Inline:create_constructor(
557 "Superscript",
558 function(content) return {c = content} end
562------------------------------------------------------------------------
563-- Constants
564-- @section constants
566--- Math content is to be displayed on a separate line.
567-- @see Math
568M.DisplayMath = {}
569M.DisplayMath.t = "DisplayMath"
570--- Math content is to be displayed inline within the paragraph
571-- @see Math
572M.InlineMath = {}
573M.InlineMath.t = "InlineMath"
575--- Double quoted content.
576-- @see Quoted
577M.DoubleQuote = {}
578M.DoubleQuote.t = "DoubleQuote"
580--- Single quoted content.
581-- @see Quoted
582M.SingleQuote = {}
583M.SingleQuote.t = "SingleQuote"
585--- Author name is mentioned in the text.
586-- @see Citation
587-- @see Cite
588M.AuthorInText = {}
589M.AuthorInText.t = "AuthorInText"
591--- Author name is suppressed.
592-- @see Citation
593-- @see Cite
594M.SuppressAuthor = {}
595M.SuppressAuthor.t = "SuppressAuthor"
597--- Default citation style is used.
598-- @see Citation
599-- @see Cite
600M.NormalCitation = {}
601M.NormalCitation.t = "NormalCitation"
603--- Table cells aligned left.
604-- @see Table
605M.AlignLeft = {}
606M.AlignLeft.t = "AlignLeft"
608--- Table cells right-aligned.
609-- @see Table
610M.AlignRight = {}
611M.AlignRight.t = "AlignRight"
613--- Table cell content is centered.
614-- @see Table
615M.AlignCenter = {}
616M.AlignCenter.t = "AlignCenter"
618--- Table cells are alignment is unaltered.
619-- @see Table
620M.AlignDefault = {}
621M.AlignDefault.t = "AlignDefault"
623--- Default list number delimiters are used.
624-- @see OrderedList
625M.DefaultDelim = {}
626M.DefaultDelim.t = "DefaultDelim"
628--- List numbers are delimited by a period.
629-- @see OrderedList
630M.Period = {}
631M.Period.t = "Period"
633--- List numbers are delimited by a single parenthesis.
634-- @see OrderedList
635M.OneParen = {}
636M.OneParen.t = "OneParen"
638--- List numbers are delimited by a double parentheses.
639-- @see OrderedList
640M.TwoParens = {}
641M.TwoParens.t = "TwoParens"
643--- List are numbered in the default style
644-- @see OrderedList
645M.DefaultStyle = {}
646M.DefaultStyle.t = "DefaultStyle"
648--- List items are numbered as examples.
649-- @see OrderedList
650M.Example = {}
651M.Example.t = "Example"
653--- List are numbered using decimal integers.
654-- @see OrderedList
655M.Decimal = {}
656M.Decimal.t = "Decimal"
658--- List are numbered using lower-case roman numerals.
659-- @see OrderedList
660M.LowerRoman = {}
661M.LowerRoman.t = "LowerRoman"
663--- List are numbered using upper-case roman numerals
664-- @see OrderedList
665M.UpperRoman = {}
666M.UpperRoman.t = "UpperRoman"
668--- List are numbered using lower-case alphabetic characters.
669-- @see OrderedList
670M.LowerAlpha = {}
671M.LowerAlpha.t = "LowerAlpha"
673--- List are numbered using upper-case alphabetic characters.
674-- @see OrderedList
675M.UpperAlpha = {}
676M.UpperAlpha.t = "UpperAlpha"
679------------------------------------------------------------------------
680-- Helper Functions
681-- @section helpers
683--- Use functions defined in the global namespace to create a pandoc filter.
684-- All globally defined functions which have names of pandoc elements are
685-- collected into a new table.
686-- @return A list of filter functions
687-- @usage
688-- -- within a file defining a pandoc filter:
689-- function Str(text)
690-- return pandoc.Str(utf8.upper(text))
691-- end
692--
693-- return {pandoc.global_filter()}
694-- -- the above is equivallent to
695-- -- return {{Str = Str}}
696function M.global_filter()
697 local res = {}
698 for k, v in pairs(_G) do
699 if M.Inline.constructor[k] or M.Block.constructor[k] or M.Block.constructors[k] or k == "Doc" then
700 res[k] = v
701 end
702 end
703 return res
704end
706return M