git.iko.soy/typeable/pandoc
on ef8430e help sign in
typeable/pandoc/data/pandoc.lua
953 lines 27 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"
29local List = require 'pandoc.List'
31------------------------------------------------------------------------
32-- The base class for pandoc's AST elements.
33-- @type Element
34-- @local
35local Element = {}
37--- Create a new element subtype
38-- @local
39function Element:make_subtype(o)
40 o = o or {}
41 setmetatable(o, self)
42 self.__index = self
43 return o
44end
46--- Create a new element given its tag and arguments
47-- @local
48function Element:new(tag, ...)
49 local element = { t = tag }
50 local content = {...}
51 -- special case for unary constructors
52 if #content == 1 then
53 element.c = content[1]
54 -- Don't set 'c' field if no further arguments were given. This is important
55 -- for nullary constructors like `Space` and `HorizontalRule`.
56 elseif #content > 0 then
57 element.c = content
58 end
59 setmetatable(element, self)
60 self.__index = self
61 return element
62end
64--- Create a new constructor
65-- @local
66-- @param tag Tag used to identify the constructor
67-- @param fn Function to be called when constructing a new element
68-- @param accessors names to use as accessors for numerical fields
69-- @return function that constructs a new element
70function Element:create_constructor(tag, fn, accessors)
71 local constr = self:make_subtype({tag = tag, getters = {}, setters = {}})
73 -- Add accessors to the metatable
74 if type(accessors) == "string" then
75 constr.getters[accessors] = function(elem)
76 return elem.c
77 end
78 constr.setters[accessors] = function(elem, v)
79 elem.c = v
80 end
81 else
82 for i = 1, #(accessors or {}) do
83 if type(accessors[i]) == "string" then
84 constr.getters[accessors[i]] = function(elem)
85 return elem.c[i]
86 end
87 constr.setters[accessors[i]] = function(elem, v)
88 elem.c[i] = v
89 end
90 else -- only two levels of nesting are supported
91 for k, v in ipairs(accessors[i]) do
92 constr.getters[v] = function(elem)
93 return elem.c[i][k]
94 end
95 constr.setters[v] = function(elem, v)
96 elem.c[i][k] = v
97 end
98 end
99 end
100 end
101 end
103 function constr:new(...)
104 local obj = fn(...)
105 setmetatable(obj, self)
106 self.__index = function(t, k)
107 if getmetatable(t).getters[k] then
108 return getmetatable(t).getters[k](t)
109 elseif k == "t" then
110 return getmetatable(t)["tag"]
111 else
112 return getmetatable(t)[k]
113 end
114 end
115 self.__newindex = function(t, k, v)
116 if getmetatable(t).setters[k] then
117 getmetatable(t).setters[k](t, v)
118 else
119 rawset(t, k, v)
120 end
121 end
122 return obj
123 end
124 self.constructor = self.constructor or {}
125 self.constructor[tag] = constr
126 return constr
127end
129--- Calls the constructor, creating a new element.
130-- @local
131function Element.__call(t, ...)
132 return t:new(...)
133end
135------------------------------------------------------------------------
136--- Pandoc Document
137-- @section document
139--- A complete pandoc document
140-- @function Pandoc
141-- @tparam {Block,...} blocks document content
142-- @tparam[opt] Meta meta document meta data
143function M.Pandoc(blocks, meta)
144 meta = meta or {}
145 return {
146 ["blocks"] = List:new(blocks),
147 ["meta"] = meta,
148 ["pandoc-api-version"] = {1,17,0,5},
149 }
150end
152-- DEPRECATED synonym:
153M.Doc = M.Pandoc
155------------------------------------------------------------------------
156-- Meta
157-- @section Meta
159--- Create a new Meta object. It sets the metatable of the given table to
160--- `Meta`.
161-- @function Meta
162-- @tparam meta table table containing document meta information
163M.Meta = {}
164M.Meta.__call = function(t, meta)
165 return setmetatable(meta, t)
166end
167setmetatable(M.Meta, M.Meta)
170------------------------------------------------------------------------
171-- MetaValue
172-- @section MetaValue
173M.MetaValue = Element:make_subtype{}
174M.MetaValue.__call = function(t, ...)
175 return t:new(...)
176end
177--- Meta blocks
178-- @function MetaBlocks
179-- @tparam {Block,...} blocks blocks
181--- Meta inlines
182-- @function MetaInlines
183-- @tparam {Inline,...} inlines inlines
185--- Meta list
186-- @function MetaList
187-- @tparam {MetaValue,...} meta_values list of meta values
188M.meta_value_list_types = {
189 "MetaBlocks",
190 "MetaInlines",
191 "MetaList",
193for i = 1, #M.meta_value_list_types do
194 M[M.meta_value_list_types[i]] = M.MetaValue:create_constructor(
195 M.meta_value_list_types[i],
196 function(content)
197 return List:new(content)
198 end
199 )
200end
202--- Meta map
203-- @function MetaMap
204-- @tparam table key_value_map a string-indexed map of meta values
205M.MetaMap = M.MetaValue:create_constructor(
206 "MetaMap",
207 function (mm) return mm end
210--- Creates string to be used in meta data.
211-- Does nothing, lua strings are meta strings.
212-- @function MetaString
213-- @tparam string str string value
214function M.MetaString(str)
215 return str
216end
218--- Creates boolean to be used in meta data.
219-- Does nothing, lua booleans are meta booleans.
220-- @function MetaBool
221-- @tparam boolean bool boolean value
222function M.MetaBool(bool)
223 return bool
224end
226------------------------------------------------------------------------
227-- Blocks
228-- @section Block
230--- Block elements
231M.Block = Element:make_subtype{}
232M.Block.__call = function (t, ...)
233 return t:new(...)
234end
236--- Creates a block quote element
237-- @function BlockQuote
238-- @tparam {Block,...} content block content
239-- @treturn Block block quote element
240M.BlockQuote = M.Block:create_constructor(
241 "BlockQuote",
242 function(content) return {c = content} end,
243 "content"
246--- Creates a bullet (i.e. unordered) list.
247-- @function BulletList
248-- @tparam {{Block,...},...} content list of items
249-- @treturn Block block quote element
250M.BulletList = M.Block:create_constructor(
251 "BulletList",
252 function(content) return {c = content} end,
253 "content"
256--- Creates a code block element
257-- @function CodeBlock
258-- @tparam string text code string
259-- @tparam[opt] Attr attr element attributes
260-- @treturn Block code block element
261M.CodeBlock = M.Block:create_constructor(
262 "CodeBlock",
263 function(text, attr) return {c = {attr or M.Attr(), text}} end,
264 {{"identifier", "classes", "attributes"}, "text"}
267--- Creates a definition list, containing terms and their explanation.
268-- @function DefinitionList
269-- @tparam {{{Inline,...},{Block,...}},...} content list of items
270-- @treturn Block block quote element
271M.DefinitionList = M.Block:create_constructor(
272 "DefinitionList",
273 function(content) return {c = List:new(content)} end,
274 "content"
277--- Creates a div element
278-- @function Div
279-- @tparam {Block,...} content block content
280-- @tparam[opt] Attr attr element attributes
281-- @treturn Block code block element
282M.Div = M.Block:create_constructor(
283 "Div",
284 function(content, attr)
285 return {c = {attr or M.Attr(), List:new(content)}}
286 end,
287 {{"identifier", "classes", "attributes"}, "content"}
290--- Creates a block quote element.
291-- @function Header
292-- @tparam int level header level
293-- @tparam {Inline,...} content inline content
294-- @tparam[opt] Attr attr element attributes
295-- @treturn Block header element
296M.Header = M.Block:create_constructor(
297 "Header",
298 function(level, content, attr)
299 return {c = {level, attr or M.Attr(), content}}
300 end,
301 {"level", {"identifier", "classes", "attributes"}, "content"}
304--- Creates a horizontal rule.
305-- @function HorizontalRule
306-- @treturn Block horizontal rule
307M.HorizontalRule = M.Block:create_constructor(
308 "HorizontalRule",
309 function() return {} end
312--- Creates a line block element.
313-- @function LineBlock
314-- @tparam {{Inline,...},...} content inline content
315-- @treturn Block block quote element
316M.LineBlock = M.Block:create_constructor(
317 "LineBlock",
318 function(content) return {c = List:new(content)} end,
319 "content"
322--- Creates a null element.
323-- @function Null
324-- @treturn Block null element
325M.Null = M.Block:create_constructor(
326 "Null",
327 function() return {} end
330--- Creates an ordered list.
331-- @function OrderedList
332-- @tparam {{Block,...},...} items list items
333-- @param[opt] listAttributes list parameters
334-- @treturn Block
335M.OrderedList = M.Block:create_constructor(
336 "OrderedList",
337 function(items, listAttributes)
338 listAttributes = listAttributes or {1, M.DefaultStyle, M.DefaultDelim}
339 return {c = {listAttributes, List:new(items)}}
340 end,
341 {{"start", "style", "delimiter"}, "content"}
344--- Creates a para element.
345-- @function Para
346-- @tparam {Inline,...} content inline content
347-- @treturn Block block quote element
348M.Para = M.Block:create_constructor(
349 "Para",
350 function(content) return {c = List:new(content)} end,
351 "content"
354--- Creates a plain element.
355-- @function Plain
356-- @tparam {Inline,...} content inline content
357-- @treturn Block block quote element
358M.Plain = M.Block:create_constructor(
359 "Plain",
360 function(content) return {c = List:new(content)} end,
361 "content"
364--- Creates a raw content block of the specified format.
365-- @function RawBlock
366-- @tparam string format format of content
367-- @tparam string text string content
368-- @treturn Block block quote element
369M.RawBlock = M.Block:create_constructor(
370 "RawBlock",
371 function(format, text) return {c = {format, text}} end,
372 {"format", "text"}
375--- Creates a table element.
376-- @function Table
377-- @tparam {Inline,...} caption table caption
378-- @tparam {AlignDefault|AlignLeft|AlignRight|AlignCenter,...} aligns alignments
379-- @tparam {int,...} widths column widths
380-- @tparam {Block,...} headers header row
381-- @tparam {{Block,...}} rows table rows
382-- @treturn Block block quote element
383M.Table = M.Block:create_constructor(
384 "Table",
385 function(caption, aligns, widths, headers, rows)
386 return {
387 c = {
388 List:new(caption),
389 List:new(aligns),
390 List:new(widths),
391 List:new(headers),
392 List:new(rows)
393 }
394 }
395 end,
396 {"caption", "aligns", "widths", "headers", "rows"}
400------------------------------------------------------------------------
401-- Inline
402-- @section Inline
404--- Inline element class
405M.Inline = Element:make_subtype{}
406M.Inline.__call = function (t, ...)
407 return t:new(...)
408end
410--- Creates a Cite inline element
411-- @function Cite
412-- @tparam {Inline,...} content List of inlines
413-- @tparam {Citation,...} citations List of citations
414-- @treturn Inline citations element
415M.Cite = M.Inline:create_constructor(
416 "Cite",
417 function(content, citations)
418 return {c = {List:new(citations), List:new(content)}}
419 end,
420 {"citations", "content"}
423--- Creates a Code inline element
424-- @function Code
425-- @tparam string text brief image description
426-- @tparam[opt] Attr attr additional attributes
427-- @treturn Inline code element
428M.Code = M.Inline:create_constructor(
429 "Code",
430 function(text, attr) return {c = {attr or M.Attr(), text}} end,
431 {{"identifier", "classes", "attributes"}, "text"}
434--- Creates an inline element representing emphasised text.
435-- @function Emph
436-- @tparam {Inline,..} content inline content
437-- @treturn Inline emphasis element
438M.Emph = M.Inline:create_constructor(
439 "Emph",
440 function(content) return {c = List:new(content)} end,
441 "content"
444--- Creates a Image inline element
445-- @function Image
446-- @tparam {Inline,..} caption text used to describe the image
447-- @tparam string src path to the image file
448-- @tparam[opt] string title brief image description
449-- @tparam[opt] Attr attr additional attributes
450-- @treturn Inline image element
451M.Image = M.Inline:create_constructor(
452 "Image",
453 function(caption, src, title, attr)
454 title = title or ""
455 attr = attr or M.Attr()
456 return {c = {attr, List:new(caption), {src, title}}}
457 end,
458 {{"identifier", "classes", "attributes"}, "caption", {"src", "title"}}
461--- Create a LineBreak inline element
462-- @function LineBreak
463-- @treturn Inline linebreak element
464M.LineBreak = M.Inline:create_constructor(
465 "LineBreak",
466 function() return {} end
469--- Creates a link inline element, usually a hyperlink.
470-- @function Link
471-- @tparam {Inline,..} content text for this link
472-- @tparam string target the link target
473-- @tparam[opt] string title brief link description
474-- @tparam[opt] Attr attr additional attributes
475-- @treturn Inline image element
476M.Link = M.Inline:create_constructor(
477 "Link",
478 function(content, target, title, attr)
479 title = title or ""
480 attr = attr or M.Attr()
481 return {c = {attr, List:new(content), {target, title}}}
482 end,
483 {{"identifier", "classes", "attributes"}, "content", {"target", "title"}}
486--- Creates a Math element, either inline or displayed.
487-- @function Math
488-- @tparam "InlineMath"|"DisplayMath" mathtype rendering specifier
489-- @tparam string text Math content
490-- @treturn Inline Math element
491M.Math = M.Inline:create_constructor(
492 "Math",
493 function(mathtype, text)
494 return {c = {mathtype, text}}
495 end,
496 {"mathtype", "text"}
498--- Creates a DisplayMath element (DEPRECATED).
499-- @function DisplayMath
500-- @tparam string text Math content
501-- @treturn Inline Math element
502M.DisplayMath = M.Inline:create_constructor(
503 "DisplayMath",
504 function(text) return M.Math("DisplayMath", text) end,
505 {"mathtype", "text"}
507--- Creates an InlineMath inline element (DEPRECATED).
508-- @function InlineMath
509-- @tparam string text Math content
510-- @treturn Inline Math element
511M.InlineMath = M.Inline:create_constructor(
512 "InlineMath",
513 function(text) return M.Math("InlineMath", text) end,
514 {"mathtype", "text"}
517--- Creates a Note inline element
518-- @function Note
519-- @tparam {Block,...} content footnote block content
520M.Note = M.Inline:create_constructor(
521 "Note",
522 function(content) return {c = List:new(content)} end,
523 "content"
526--- Creates a Quoted inline element given the quote type and quoted content.
527-- @function Quoted
528-- @tparam "DoubleQuote"|"SingleQuote" quotetype type of quotes to be used
529-- @tparam {Inline,..} content inline content
530-- @treturn Inline quoted element
531M.Quoted = M.Inline:create_constructor(
532 "Quoted",
533 function(quotetype, content) return {c = {quotetype, List:new(content)}} end,
534 {"quotetype", "content"}
536--- Creates a single-quoted inline element (DEPRECATED).
537-- @function SingleQuoted
538-- @tparam {Inline,..} content inline content
539-- @treturn Inline quoted element
540-- @see Quoted
541M.SingleQuoted = M.Inline:create_constructor(
542 "SingleQuoted",
543 function(content) return M.Quoted(M.SingleQuote, content) end,
544 {"quotetype", "content"}
546--- Creates a single-quoted inline element (DEPRECATED).
547-- @function DoubleQuoted
548-- @tparam {Inline,..} content inline content
549-- @treturn Inline quoted element
550-- @see Quoted
551M.DoubleQuoted = M.Inline:create_constructor(
552 "DoubleQuoted",
553 function(content) return M.Quoted("DoubleQuote", content) end,
554 {"quotetype", "content"}
557--- Creates a RawInline inline element
558-- @function RawInline
559-- @tparam string format format of the contents
560-- @tparam string text string content
561-- @treturn Inline raw inline element
562M.RawInline = M.Inline:create_constructor(
563 "RawInline",
564 function(format, text) return {c = {format, text}} end,
565 {"format", "text"}
568--- Creates text rendered in small caps
569-- @function SmallCaps
570-- @tparam {Inline,..} content inline content
571-- @treturn Inline smallcaps element
572M.SmallCaps = M.Inline:create_constructor(
573 "SmallCaps",
574 function(content) return {c = List:new(content)} end,
575 "content"
578--- Creates a SoftBreak inline element.
579-- @function SoftBreak
580-- @treturn Inline softbreak element
581M.SoftBreak = M.Inline:create_constructor(
582 "SoftBreak",
583 function() return {} end
586--- Create a Space inline element
587-- @function Space
588-- @treturn Inline space element
589M.Space = M.Inline:create_constructor(
590 "Space",
591 function() return {} end
594--- Creates a Span inline element
595-- @function Span
596-- @tparam {Inline,..} content inline content
597-- @tparam[opt] Attr attr additional attributes
598-- @treturn Inline span element
599M.Span = M.Inline:create_constructor(
600 "Span",
601 function(content, attr)
602 return {c = {attr or M.Attr(), List:new(content)}}
603 end,
604 {{"identifier", "classes", "attributes"}, "content"}
607--- Creates a Str inline element
608-- @function Str
609-- @tparam string text content
610-- @treturn Inline string element
611M.Str = M.Inline:create_constructor(
612 "Str",
613 function(text) return {c = text} end,
614 "text"
617--- Creates text which is striked out.
618-- @function Strikeout
619-- @tparam {Inline,..} content inline content
620-- @treturn Inline strikeout element
621M.Strikeout = M.Inline:create_constructor(
622 "Strikeout",
623 function(content) return {c = List:new(content)} end,
624 "content"
627--- Creates a Strong element, whose text is usually displayed in a bold font.
628-- @function Strong
629-- @tparam {Inline,..} content inline content
630-- @treturn Inline strong element
631M.Strong = M.Inline:create_constructor(
632 "Strong",
633 function(content) return {c = List:new(content)} end,
634 "content"
637--- Creates a Subscript inline element
638-- @function Subscript
639-- @tparam {Inline,..} content inline content
640-- @treturn Inline subscript element
641M.Subscript = M.Inline:create_constructor(
642 "Subscript",
643 function(content) return {c = List:new(content)} end,
644 "content"
647--- Creates a Superscript inline element
648-- @function Superscript
649-- @tparam {Inline,..} content inline content
650-- @treturn Inline strong element
651M.Superscript = M.Inline:create_constructor(
652 "Superscript",
653 function(content) return {c = List:new(content)} end,
654 "content"
658------------------------------------------------------------------------
659-- Helpers
660-- @section helpers
662local function assoc_key_equals (x)
663 return function (y) return y[1] == x end
664end
666-- Lookup a value in an associative list
667-- @function lookup
668-- @tparam {{key, value},...} alist associative list
669-- @param key key for which the associated value is to be looked up
670local function lookup(alist, key)
671 return (List.find_if(alist, assoc_key_equals(key)) or {})[2]
672end
674--- Return an iterator which returns key-value pairs of an associative list.
675-- @function apairs
676-- @tparam {{key, value},...} alist associative list
677local apairs = function (alist)
678 local i = 1
679 local cur
680 function nxt ()
681 cur = rawget(alist, i)
682 if cur then
683 i = i + 1
684 return cur[1], cur[2]
685 end
686 return nil
687 end
688 return nxt, nil, nil
689end
691-- AttributeList, a metatable to allow table-like access to attribute lists
692-- represented by associative lists.
693local AttributeList = {
694 __index = function (t, k)
695 if type(k) == "number" then
696 return rawget(t, k)
697 else
698 return lookup(t, k)
699 end
700 end,
702 __newindex = function (t, k, v)
703 local cur, idx = List.find_if(t, assoc_key_equals(k))
704 if v == nil then
705 table.remove(t, idx)
706 elseif cur then
707 cur[2] = v
708 elseif type(k) == "number" then
709 rawset(t, k, v)
710 else
711 rawset(t, #t + 1, {k, v})
712 end
713 end,
715 __pairs = apairs
718-- convert a table to an associative list. The order of key-value pairs in the
719-- alist is undefined. The table should either contain no numeric keys or
720-- already be an associative list.
721-- @tparam table associative list or table without numeric keys.
722-- @treturn table associative list
723local to_alist = function (tbl)
724 if #tbl ~= 0 or next(tbl) == nil then
725 -- probably already an alist
726 return tbl
727 end
728 local alist = {}
729 local i = 1
730 for k, v in pairs(tbl) do
731 alist[i] = {k, v}
732 i = i + 1
733 end
734 return alist
735end
737-- Attr
738M.Attr = {}
739M.Attr._field_names = {identifier = 1, classes = 2, attributes = 3}
740--- Create a new set of attributes (Attr).
741-- @function Attr
742-- @tparam[opt] string identifier element identifier
743-- @tparam[opt] {string,...} classes element classes
744-- @tparam[opt] table attributes table containing string keys and values
745-- @return element attributes
746M.Attr.__call = function(t, identifier, classes, attributes)
747 identifier = identifier or ''
748 classes = List:new(classes or {})
749 attributes = setmetatable(to_alist(attributes or {}), AttributeList)
750 local attr = {identifier, classes, attributes}
751 setmetatable(attr, t)
752 return attr
753end
754M.Attr.__index = function(t, k)
755 return rawget(t, k) or
756 rawget(t, M.Attr._field_names[k]) or
757 rawget(getmetatable(t), k)
758end
759M.Attr.__newindex = function(t, k, v)
760 if M.Attr._field_names[k] then
761 rawset(t, M.Attr._field_names[k], v)
762 else
763 rawset(t, k, v)
764 end
765end
766setmetatable(M.Attr, M.Attr)
769--- Creates a single citation.
770-- @function Citation
771-- @tparam string id citation identifier (like a bibtex key)
772-- @tparam AuthorInText|SuppressAuthor|NormalCitation mode citation mode
773-- @tparam[opt] {Inline,...} prefix citation prefix
774-- @tparam[opt] {Inline,...} suffix citation suffix
775-- @tparam[opt] int note_num note number
776-- @tparam[opt] int hash hash number
777M.Citation = function(id, mode, prefix, suffix, note_num, hash)
778 prefix = prefix or {}
779 suffix = suffix or {}
780 note_num = note_num or 0
781 hash = hash or 0
782 return {
783 citationId = id,
784 citationPrefix = prefix,
785 citationSuffix = suffix,
786 citationMode = mode,
787 citationNoteNum = note_num,
788 citationHash = hash,
789 }
790end
793------------------------------------------------------------------------
794-- Constants
795-- @section constants
797--- Author name is mentioned in the text.
798-- @see Citation
799-- @see Cite
800M.AuthorInText = "AuthorInText"
802--- Author name is suppressed.
803-- @see Citation
804-- @see Cite
805M.SuppressAuthor = "SuppressAuthor"
807--- Default citation style is used.
808-- @see Citation
809-- @see Cite
810M.NormalCitation = "NormalCitation"
812--- Table cells aligned left.
813-- @see Table
814M.AlignLeft = "AlignLeft"
816--- Table cells right-aligned.
817-- @see Table
818M.AlignRight = "AlignRight"
820--- Table cell content is centered.
821-- @see Table
822M.AlignCenter = "AlignCenter"
824--- Table cells are alignment is unaltered.
825-- @see Table
826M.AlignDefault = "AlignDefault"
828--- Default list number delimiters are used.
829-- @see OrderedList
830M.DefaultDelim = "DefaultDelim"
832--- List numbers are delimited by a period.
833-- @see OrderedList
834M.Period = "Period"
836--- List numbers are delimited by a single parenthesis.
837-- @see OrderedList
838M.OneParen = "OneParen"
840--- List numbers are delimited by a double parentheses.
841-- @see OrderedList
842M.TwoParens = "TwoParens"
844--- List are numbered in the default style
845-- @see OrderedList
846M.DefaultStyle = "DefaultStyle"
848--- List items are numbered as examples.
849-- @see OrderedList
850M.Example = "Example"
852--- List are numbered using decimal integers.
853-- @see OrderedList
854M.Decimal = "Decimal"
856--- List are numbered using lower-case roman numerals.
857-- @see OrderedList
858M.LowerRoman = "LowerRoman"
860--- List are numbered using upper-case roman numerals
861-- @see OrderedList
862M.UpperRoman = "UpperRoman"
864--- List are numbered using lower-case alphabetic characters.
865-- @see OrderedList
866M.LowerAlpha = "LowerAlpha"
868--- List are numbered using upper-case alphabetic characters.
869-- @see OrderedList
870M.UpperAlpha = "UpperAlpha"
873------------------------------------------------------------------------
874-- Helper Functions
875-- @section helpers
877--- Parse the given string into a Pandoc document.
878-- The method used to interpret input is specified by *format*. Acceptable
879-- values for this parameter are equal to those that can be given to the
880-- `--from` command line option.
881-- @tparam string markup the markup to be parsed
882-- @tparam[opt] string format format specification, defaults to "markdown".
883-- @treturn Pandoc pandoc document
884-- @usage
885-- local org_markup = "/emphasis/" -- Input to be read
886-- local document = pandoc.read(org_markup, "org")
887-- -- Get the first block of the document
888-- local block = document.blocks[1]
889-- -- The inline element in that block is an `Emph`
890-- assert(block.content[1].t == "Emph")
891function M.read(markup, format)
892 format = format or "markdown"
893 local pd = pandoc._read(format, markup)
894 if type(pd) == "string" then
895 error(pd)
896 else
897 return pd
898 end
899end
901--- Runs command with arguments, passing it some input, and returns the output.
902-- @treturn string Output of command.
903-- @raise A table containing the keys `command`, `error_code`, and `output` is
904-- thrown if the command exits with a non-zero error code.
905-- @usage
906-- local ec, output = pandoc.pipe("sed", {"-e","s/a/b/"}, "abc")
907function M.pipe (command, args, input)
908 local ec, output = pandoc._pipe(command, args, input)
909 if ec ~= 0 then
910 err = setmetatable(
911 { command = command, error_code = ec, output = output},
912 { __tostring = function(e)
913 return "Error running " .. e.command
914 .. " (error code " .. e.error_code .. "): "
915 .. e.output
916 end
917 }
918 )
919 error(err)
920 end
921 return output
922end
924--- Use functions defined in the global namespace to create a pandoc filter.
925-- All globally defined functions which have names of pandoc elements are
926-- collected into a new table.
927-- @return A list of filter functions
928-- @usage
929-- -- within a file defining a pandoc filter:
930-- function Str(text)
931-- return pandoc.Str(utf8.upper(text))
932-- end
933--
934-- return {pandoc.global_filter()}
935-- -- the above is equivallent to
936-- -- return {{Str = Str}}
937function M.global_filter()
938 local res = {}
939 function is_filter_function(k)
940 return M.Inline.constructor[k] or
941 M.Block.constructor[k] or
942 k == "Meta" or k == "Doc" or k == "Pandoc" or
943 k == "Block" or k == "Inline"
944 end
945 for k, v in pairs(_G) do
946 if is_filter_function(k) then
947 res[k] = v
948 end
949 end
950 return res
951end
953return M