git.iko.soy/typeable/pandoc
on master help sign in
Lua module: simplify Attributes, rename to Attr

Attributes was written to behave much like a normal table, in order to simplify working with it. However, all Attr containing elements were changed to provide panflute-like accessors to Attr components, rendering the previous approach unnecessary.

commit2e916b3abfefc0a57964e8f33aec2d37877f9ced authorAlbert Krewinkel <albert@zeitkraut.de> dateSun 30 Apr 2017, 11:50 (9 years ago) parent3362cb8
2 files changed+27−60
data/pandoc.lua+26−59 src/Text/Pandoc/Lua/StackInstances.hs+1−1
data/pandoc.lua+26−59
@@ -233,7 +233,7 @@ M.BulletList = M.Block:create_constructor(
233233--- Creates a code block element
234234-- @function CodeBlock
235235-- @tparam string text code string
236−-- @tparam[opt] Attributes attributes element attributes
236+-- @tparam[opt] Attr attributes element attributes
237237-- @treturn Block code block element
238238M.CodeBlock = M.Block:create_constructor(
239239 "CodeBlock",
@@ -254,7 +254,7 @@ M.DefinitionList = M.Block:create_constructor(
254254--- Creates a div element
255255-- @function Div
256256-- @tparam {Block,...} content block content
257−-- @tparam[opt] Attributes attributes element attributes
257+-- @tparam[opt] Attr attributes element attributes
258258-- @treturn Block code block element
259259M.Div = M.Block:create_constructor(
260260 "Div",
@@ -266,7 +266,7 @@ M.Div = M.Block:create_constructor(
266266-- @function Header
267267-- @tparam int level header level
268268-- @tparam {Inline,...} content inline content
269−-- @tparam Attributes attributes element attributes
269+-- @tparam Attr attributes element attributes
270270-- @treturn Block header element
271271M.Header = M.Block:create_constructor(
272272 "Header",
@@ -386,7 +386,7 @@ M.Cite = M.Inline:create_constructor(
386386--- Creates a Code inline element
387387-- @function Code
388388-- @tparam string text brief image description
389−-- @tparam[opt] Attributes attributes additional attributes
389+-- @tparam[opt] Attr attributes additional attributes
390390-- @treturn Inline code element
391391M.Code = M.Inline:create_constructor(
392392 "Code",
@@ -409,7 +409,7 @@ M.Emph = M.Inline:create_constructor(
409409-- @tparam {Inline,..} caption text used to describe the image
410410-- @tparam string src path to the image file
411411-- @tparam[opt] string title brief image description
412−-- @tparam[opt] Attributes attributes additional attributes
412+-- @tparam[opt] Attr attributes additional attributes
413413-- @treturn Inline image element
414414M.Image = M.Inline:create_constructor(
415415 "Image",
@@ -434,7 +434,7 @@ M.LineBreak = M.Inline:create_constructor(
434434-- @tparam {Inline,..} content text for this link
435435-- @tparam string target the link target
436436-- @tparam[opt] string title brief link description
437−-- @tparam[opt] Attributes attributes additional attributes
437+-- @tparam[opt] Attr attributes additional attributes
438438-- @treturn Inline image element
439439M.Link = M.Inline:create_constructor(
440440 "Link",
@@ -560,7 +560,7 @@ M.Space = M.Inline:create_constructor(
560560--- Creates a Span inline element
561561-- @function Span
562562-- @tparam {Inline,..} content inline content
563−-- @tparam[opt] Attributes attributes additional attributes
563+-- @tparam[opt] Attr attributes additional attributes
564564-- @treturn Inline span element
565565M.Span = M.Inline:create_constructor(
566566 "Span",
@@ -623,69 +623,36 @@ M.Superscript = M.Inline:create_constructor(
623623-- Helpers
624624-- @section helpers
625625
626−-- Attributes
627−M.Attributes = {}
626+-- Attr
627+M.Attr = {}
628+M.Attr._field_names = {identifier = 1, classes = 2, attributes = 3}
628629--- Create a new set of attributes (Attr).
629−-- @function Attributes
630−-- @tparam table key_values table containing string keys and values
631−-- @tparam[opt] string id element identifier
630+-- @function Attr
631+-- @tparam[opt] string identifier element identifier
632632-- @tparam[opt] {string,...} classes element classes
633+-- @tparam[opt] table attributes table containing string keys and values
633634-- @return element attributes
634−M.Attributes.__call = function(t, key_values, id, classes)
635− id = id or ''
635+M.Attr.__call = function(t, identifier, classes, attributes)
636+ identifier = identifier or ''
636637 classes = classes or {}
637− local attr = {id, classes, key_values}
638+ attributes = attributes or {}
639+ local attr = {identifier, classes, attributes}
638640 setmetatable(attr, t)
639641 return attr
640642end
641−M.Attributes.__index = function(t, k)
642− if rawget(t, k) then
643− return rawget(t, k)
644− elseif k == "id" then
645− if rawget(t, 1) == '' then
646− return nil
647− else
648− return rawget(t, 1)
649− end
650− elseif k == "class" then
651− if #(rawget(t, 2)) == 0 then
652− return nil
653− else
654− return table.concat(t[2], ' ')
655− end
656− else
657− for _, p in ipairs(t[3]) do
658− if k == p[1] then
659− return p[2]
660− end
661− end
662− return nil
663− end
643+M.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)
664647end
665−M.Attributes.__newindex = function(t, k, v)
666− if rawget(t, k) then
667− rawset(t, k, v)
668− elseif k == "id" then
669− rawset(t, 1, v)
670− elseif k == "class" then
671− if type(v) == "string" then
672− rawset(t, 2, {v})
673− else
674− rawset(t, 2, v)
675− end
648+M.Attr.__newindex = function(t, k, v)
649+ if M.Attr._field_names[k] then
650+ rawset(t, M.Attr._field_names[k], v)
676651 else
677− for _, p in ipairs(rawget(t, 3)) do
678− if k == p[1] then
679− p[2] = v
680− return
681− end
682− end
683− kv = rawget(t, 3)
684− kv[#kv + 1] = {k, v}
685− rawset(t, 3, kv)
652+ rawset(t, k, v)
686653 end
687654end
688−setmetatable(M.Attributes, M.Attributes)
655+setmetatable(M.Attr, M.Attr)
689656
690657
691658--- Creates a single citation.
src/Text/Pandoc/Lua/StackInstances.hs+1−1
@@ -279,6 +279,6 @@ newtype LuaAttr = LuaAttr { fromLuaAttr :: Attr }
279279
280280instance StackValue LuaAttr where
281281 push lua (LuaAttr (id', classes, kv)) =
282− pushViaConstructor lua "Attributes" kv id' classes
282+ pushViaConstructor lua "Attr" id' classes kv
283283 peek lua idx = fmap LuaAttr <$> peek lua idx
284284 valuetype _ = TTABLE