aboutsummaryrefslogtreecommitdiff
path: root/data
diff options
context:
space:
mode:
authorAlbert Krewinkel <albert@zeitkraut.de>2021-11-28 02:08:01 +0100
committerGitHub <noreply@github.com>2021-11-27 17:08:01 -0800
commit3692a1d1e83703fbf235214f2838cd92683c625c (patch)
tree2eb377285e1ca485c03ea60eef1d92ff58827666 /data
parent0d25232bbf2998cccf6ca4b1dc6e8d6f36eb9c60 (diff)
downloadpandoc-3692a1d1e83703fbf235214f2838cd92683c625c.tar.gz
Lua: use package pandoc-lua-marshal (#7719)
The marshaling functions for pandoc's AST are extracted into a separate package. The package comes with a number of changes: - Pandoc's List module was rewritten in C, thereby improving error messages. - Lists of `Block` and `Inline` elements are marshaled using the new list types `Blocks` and `Inlines`, respectively. These types currently behave identical to the generic List type, but give better error messages. This also opens up the possibility of adding element-specific methods to these lists in the future. - Elements of type `MetaValue` are no longer pushed as values which have `.t` and `.tag` properties. This was already true for `MetaString` and `MetaBool` values, which are still marshaled as Lua strings and booleans, respectively. Affected values: + `MetaBlocks` values are marshaled as a `Blocks` list; + `MetaInlines` values are marshaled as a `Inlines` list; + `MetaList` values are marshaled as a generic pandoc `List`s. + `MetaMap` values are marshaled as plain tables and no longer given any metatable. - The test suite for marshaled objects and their constructors has been extended and improved. - A bug in Citation objects, where setting a citation's suffix modified it's prefix, has been fixed.
Diffstat (limited to 'data')
-rw-r--r--data/pandoc.List.lua142
-rw-r--r--data/pandoc.lua247
2 files changed, 0 insertions, 389 deletions
diff --git a/data/pandoc.List.lua b/data/pandoc.List.lua
deleted file mode 100644
index b33c30876..000000000
--- a/data/pandoc.List.lua
+++ /dev/null
@@ -1,142 +0,0 @@
---[[
-List.lua
-
-Copyright © 2017–2020 Albert Krewinkel
-
-Permission to use, copy, modify, and/or distribute this software for any
-purpose with or without fee is hereby granted, provided that the above
-copyright notice and this permission notice appear in all copies.
-
-THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
-WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
-MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
-ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
-WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
-ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
-OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ]]
-
---- Pandoc's List type and helper methods
--- @classmod pandoc.List
--- @author Albert Krewinkel
--- @copyright © 2017–2020 Albert Krewinkel
--- @license MIT
-local List = {
- _VERSION = "1.0.0"
-}
-
---- Create a new list.
--- @param[opt] o table that should be altered into a list (default: `{}`)
--- @return the altered input table
-function List:new (o)
- o = o or {}
- setmetatable(o, self)
- self.__index = self
- return o
-end
-
---- Concatenates two lists.
--- @param list second list concatenated to the first
--- @return a new list containing all elements from list1 and list2
-function List:__concat (list)
- local res = List.clone(self)
- List.extend(res, list)
- return res
-end
-
---- Returns a (shallow) copy of the list.
-function List:clone ()
- local lst = setmetatable({}, getmetatable(self))
- List.extend(lst, self)
- return lst
-end
-
---- Adds the given list to the end of this list.
--- @param list list to appended
-function List:extend (list)
- for i = 1, #list do
- self[#self + 1] = list[i]
- end
-end
-
---- Returns a new list containing all items satisfying a given condition.
--- @param pred condition items must satisfy.
--- @return a new list containing all items for which `test` was true.
-function List:filter (pred)
- local res = setmetatable({}, getmetatable(self))
- for i = 1, #self do
- if pred(self[i], i) then
- res[#res + 1] = self[i]
- end
- end
- return res
-end
-
---- Returns the value and index of the first occurrence of the given item.
--- @param needle item to search for
--- @param[opt] init index at which the search is started (default: 1)
--- @return first item equal to the needle, or nil if no such item exists.
--- @return index of that element
-function List:find (needle, init)
- return List.find_if(self, function(x) return x == needle end, init)
-end
-
---- Returns the value and index of the first element for which the predicate
---- holds true.
--- @param pred the predicate function
--- @param[opt] init index at which the search is started (default: 1)
--- @return first item for which `test` succeeds, or nil if no such item exists.
--- @return index of that element
-function List:find_if (pred, init)
- init = (init == nil and 1) or (init < 0 and #self - init) or init
- for i = init, #self do
- if pred(self[i], i) then
- return self[i], i
- end
- end
- return nil
-end
-
---- Checks if the list has an item equal to the given needle.
--- @param needle item to search for
--- @param[opt] init index at which the search is started; defaults to 1.
--- @return true if a list item is equal to the needle, false otherwise
-function List:includes (needle, init)
- return not (List.find(self, needle, init) == nil)
-end
-
---- Insert an element into the list. Alias for `table.insert`.
--- @param list list
--- @param[opt] pos position at which the new element is to be inserted
--- @param value value to insert
-List.insert = table.insert
-
---- Returns a copy of the current list by applying the given function to
--- all elements.
--- @param fn function which is applied to all list items.
-function List:map (fn)
- local res = setmetatable({}, getmetatable(self))
- for i = 1, #self do
- res[i] = fn(self[i], i)
- end
- return res
-end
-
---- Remove element from list (alias for `table.remove`)
--- @param list list
--- @param[opt] pos position of the element to be removed (default: #list)
--- @return the removed element
-List.remove = table.remove
-
---- Sort list in-place (alias for `table.sort`)
--- @param list list
--- @param[opt] comp comparison function; default to `<` operator.
-List.sort = table.sort
-
--- Set metatable with __call metamethod. This allows the use of `List`
--- as a constructor function.
-local ListMT = {
- __call = List.new
-}
-setmetatable(List, ListMT)
-
-return List
diff --git a/data/pandoc.lua b/data/pandoc.lua
deleted file mode 100644
index 7e5ff799b..000000000
--- a/data/pandoc.lua
+++ /dev/null
@@ -1,247 +0,0 @@
---[[
-pandoc.lua
-
-Copyright © 2017–2019 Albert Krewinkel
-
-Permission to use, copy, modify, and/or distribute this software for any purpose
-with or without fee is hereby granted, provided that the above copyright notice
-and this permission notice appear in all copies.
-
-THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
-REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
-FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
-INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
-OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
-TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
-THIS SOFTWARE.
-]]
-
----
--- Lua functions for pandoc scripts.
---
--- @author Albert Krewinkel
--- @copyright © 2017–2019 Albert Krewinkel
--- @license MIT
-local M = {}
-
--- Re-export bundled modules
-M.List = require 'pandoc.List'
-M.mediabag = require 'pandoc.mediabag'
-M.path = require 'pandoc.path'
-M.system = require 'pandoc.system'
-M.types = require 'pandoc.types'
-M.utils = require 'pandoc.utils'
-M.text = require 'text'
-
--- Local names for modules which this module depends on.
-local List = M.List
-local utils = M.utils
-
-
-------------------------------------------------------------------------
--- Accessor objects
---
--- Create metatables which allow to access numerical indices via accessor
--- methods.
--- @section
--- @local
-
---- Create a new table which allows to access numerical indices via accessor
--- functions.
--- @local
-local function create_accessor_behavior (tag)
- local behavior = {tag = tag}
- behavior.__eq = utils.equals
- behavior.__index = function(t, k)
- if k == "t" then
- return getmetatable(t)["tag"]
- end
- return getmetatable(t)[k]
- end
- behavior.__pairs = function (t)
- return next, t
- end
- return behavior
-end
-
-
-------------------------------------------------------------------------
--- The base class for types
--- @type Type
--- @local
-local Type = {}
-Type.name = 'Type'
-Type.__index = Type
-Type.behavior = {
- __type = Type,
- new = function (obj)
- obj = obj or {}
- setmetatable(obj, self)
- return obj
- end
-}
-Type.behavior.__index = Type.behavior
-
---- Set a new behavior for the type, inheriting that of the parent type if none
---- is specified explicitly
--- @param behavior the behavior object for this type.
--- @local
-function Type:set_behavior (behavior)
- behavior = behavior or {}
- behavior.__index = rawget(behavior, '__index') or behavior
- behavior.__type = self
- if not getmetatable(behavior) and getmetatable(self) then
- setmetatable(behavior, getmetatable(self).behavior)
- end
- self.behavior = behavior
-end
-
---- Create a new subtype, using the given table as base.
--- @param name name of the new type
--- @param[opt] behavior behavioral object for the new type.
--- @return a new type
--- @local
-function Type:make_subtype(name, behavior)
- local newtype = setmetatable({}, self)
- newtype.name = name
- newtype.__index = newtype
- newtype:set_behavior(behavior)
- return newtype
-end
-
-
-------------------------------------------------------------------------
--- The base class for pandoc's AST elements.
--- @type AstElement
--- @local
-local AstElement = Type:make_subtype 'AstElement'
-AstElement.__call = function(t, ...)
- local success, ret = pcall(t.new, t, ...)
- if success then
- return setmetatable(ret, t.behavior)
- else
- error(string.format('Constructor for %s failed: %s\n', t.name, ret))
- end
-end
-
---- Make a new subtype which constructs a new value when called.
--- @local
-function AstElement:make_subtype(...)
- local newtype = Type.make_subtype(self, ...)
- newtype.__call = self.__call
- return newtype
-end
-
---- Create a new constructor
--- @local
--- @param tag Tag used to identify the constructor
--- @param fn Function to be called when constructing a new element
--- @param accessors names to use as accessors for numerical fields
--- @return function that constructs a new element
-function AstElement:create_constructor(tag, fn)
- local constr = self:make_subtype(tag, create_accessor_behavior(tag))
- function constr:new(...)
- return setmetatable(fn(...), self.behavior)
- end
- self.constructor = self.constructor or {}
- self.constructor[tag] = constr
- return constr
-end
-
---- Convert AstElement input into a list if necessary.
--- @local
-local function ensureList (x)
- if x.tag then
- -- Lists are not tagged, but all elements are
- return List:new{x}
- else
- return List:new(x)
- end
-end
-
---- Ensure a given object is an Inline element, or convert it into one.
--- @local
-local function ensureInlineList (x)
- if type(x) == 'string' then
- return List:new{M.Str(x)}
- else
- return ensureList(x)
- end
-end
-
-------------------------------------------------------------------------
--- Meta
--- @section Meta
-
---- Create a new Meta object. It sets the metatable of the given table to
---- `Meta`.
--- @function Meta
--- @tparam meta table table containing document meta information
-M.Meta = AstElement:make_subtype'Meta'
-M.Meta.behavior.clone = M.types.clone.Meta
-function M.Meta:new (meta) return meta end
-
-
-------------------------------------------------------------------------
--- MetaValue
--- @section MetaValue
-M.MetaValue = AstElement:make_subtype('MetaValue')
-M.MetaValue.behavior.clone = M.types.clone.MetaValue
-
---- Meta blocks
--- @function MetaBlocks
--- @tparam {Block,...} blocks blocks
-M.MetaBlocks = M.MetaValue:create_constructor(
- 'MetaBlocks',
- function (content) return ensureList(content) end
-)
-
---- Meta inlines
--- @function MetaInlines
--- @tparam {Inline,...} inlines inlines
-M.MetaInlines = M.MetaValue:create_constructor(
- 'MetaInlines',
- function (content) return ensureInlineList(content) end
-)
-
---- Meta list
--- @function MetaList
--- @tparam {MetaValue,...} meta_values list of meta values
-M.MetaList = M.MetaValue:create_constructor(
- 'MetaList',
- function (content)
- if content.tag == 'MetaList' then
- return content
- end
- return ensureList(content)
- end
-)
-for k, v in pairs(List) do
- M.MetaList.behavior[k] = v
-end
-
---- Meta map
--- @function MetaMap
--- @tparam table key_value_map a string-indexed map of meta values
-M.MetaMap = M.MetaValue:create_constructor(
- "MetaMap",
- function (mm) return mm end
-)
-
---- Creates string to be used in meta data.
--- Does nothing, lua strings are meta strings.
--- @function MetaString
--- @tparam string str string value
-function M.MetaString(str)
- return str
-end
-
---- Creates boolean to be used in meta data.
--- Does nothing, lua booleans are meta booleans.
--- @function MetaBool
--- @tparam boolean bool boolean value
-function M.MetaBool(bool)
- return bool
-end
-
-return M