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.
This commit is contained in:
parent
3362cb89d9
commit
2e916b3abf
2 changed files with 27 additions and 60 deletions
|
@ -233,7 +233,7 @@ M.BulletList = M.Block:create_constructor(
|
||||||
--- Creates a code block element
|
--- Creates a code block element
|
||||||
-- @function CodeBlock
|
-- @function CodeBlock
|
||||||
-- @tparam string text code string
|
-- @tparam string text code string
|
||||||
-- @tparam[opt] Attributes attributes element attributes
|
-- @tparam[opt] Attr attributes element attributes
|
||||||
-- @treturn Block code block element
|
-- @treturn Block code block element
|
||||||
M.CodeBlock = M.Block:create_constructor(
|
M.CodeBlock = M.Block:create_constructor(
|
||||||
"CodeBlock",
|
"CodeBlock",
|
||||||
|
@ -254,7 +254,7 @@ M.DefinitionList = M.Block:create_constructor(
|
||||||
--- Creates a div element
|
--- Creates a div element
|
||||||
-- @function Div
|
-- @function Div
|
||||||
-- @tparam {Block,...} content block content
|
-- @tparam {Block,...} content block content
|
||||||
-- @tparam[opt] Attributes attributes element attributes
|
-- @tparam[opt] Attr attributes element attributes
|
||||||
-- @treturn Block code block element
|
-- @treturn Block code block element
|
||||||
M.Div = M.Block:create_constructor(
|
M.Div = M.Block:create_constructor(
|
||||||
"Div",
|
"Div",
|
||||||
|
@ -266,7 +266,7 @@ M.Div = M.Block:create_constructor(
|
||||||
-- @function Header
|
-- @function Header
|
||||||
-- @tparam int level header level
|
-- @tparam int level header level
|
||||||
-- @tparam {Inline,...} content inline content
|
-- @tparam {Inline,...} content inline content
|
||||||
-- @tparam Attributes attributes element attributes
|
-- @tparam Attr attributes element attributes
|
||||||
-- @treturn Block header element
|
-- @treturn Block header element
|
||||||
M.Header = M.Block:create_constructor(
|
M.Header = M.Block:create_constructor(
|
||||||
"Header",
|
"Header",
|
||||||
|
@ -386,7 +386,7 @@ M.Cite = M.Inline:create_constructor(
|
||||||
--- Creates a Code inline element
|
--- Creates a Code inline element
|
||||||
-- @function Code
|
-- @function Code
|
||||||
-- @tparam string text brief image description
|
-- @tparam string text brief image description
|
||||||
-- @tparam[opt] Attributes attributes additional attributes
|
-- @tparam[opt] Attr attributes additional attributes
|
||||||
-- @treturn Inline code element
|
-- @treturn Inline code element
|
||||||
M.Code = M.Inline:create_constructor(
|
M.Code = M.Inline:create_constructor(
|
||||||
"Code",
|
"Code",
|
||||||
|
@ -409,7 +409,7 @@ M.Emph = M.Inline:create_constructor(
|
||||||
-- @tparam {Inline,..} caption text used to describe the image
|
-- @tparam {Inline,..} caption text used to describe the image
|
||||||
-- @tparam string src path to the image file
|
-- @tparam string src path to the image file
|
||||||
-- @tparam[opt] string title brief image description
|
-- @tparam[opt] string title brief image description
|
||||||
-- @tparam[opt] Attributes attributes additional attributes
|
-- @tparam[opt] Attr attributes additional attributes
|
||||||
-- @treturn Inline image element
|
-- @treturn Inline image element
|
||||||
M.Image = M.Inline:create_constructor(
|
M.Image = M.Inline:create_constructor(
|
||||||
"Image",
|
"Image",
|
||||||
|
@ -434,7 +434,7 @@ M.LineBreak = M.Inline:create_constructor(
|
||||||
-- @tparam {Inline,..} content text for this link
|
-- @tparam {Inline,..} content text for this link
|
||||||
-- @tparam string target the link target
|
-- @tparam string target the link target
|
||||||
-- @tparam[opt] string title brief link description
|
-- @tparam[opt] string title brief link description
|
||||||
-- @tparam[opt] Attributes attributes additional attributes
|
-- @tparam[opt] Attr attributes additional attributes
|
||||||
-- @treturn Inline image element
|
-- @treturn Inline image element
|
||||||
M.Link = M.Inline:create_constructor(
|
M.Link = M.Inline:create_constructor(
|
||||||
"Link",
|
"Link",
|
||||||
|
@ -560,7 +560,7 @@ M.Space = M.Inline:create_constructor(
|
||||||
--- Creates a Span inline element
|
--- Creates a Span inline element
|
||||||
-- @function Span
|
-- @function Span
|
||||||
-- @tparam {Inline,..} content inline content
|
-- @tparam {Inline,..} content inline content
|
||||||
-- @tparam[opt] Attributes attributes additional attributes
|
-- @tparam[opt] Attr attributes additional attributes
|
||||||
-- @treturn Inline span element
|
-- @treturn Inline span element
|
||||||
M.Span = M.Inline:create_constructor(
|
M.Span = M.Inline:create_constructor(
|
||||||
"Span",
|
"Span",
|
||||||
|
@ -623,69 +623,36 @@ M.Superscript = M.Inline:create_constructor(
|
||||||
-- Helpers
|
-- Helpers
|
||||||
-- @section helpers
|
-- @section helpers
|
||||||
|
|
||||||
-- Attributes
|
-- Attr
|
||||||
M.Attributes = {}
|
M.Attr = {}
|
||||||
|
M.Attr._field_names = {identifier = 1, classes = 2, attributes = 3}
|
||||||
--- Create a new set of attributes (Attr).
|
--- Create a new set of attributes (Attr).
|
||||||
-- @function Attributes
|
-- @function Attr
|
||||||
-- @tparam table key_values table containing string keys and values
|
-- @tparam[opt] string identifier element identifier
|
||||||
-- @tparam[opt] string id element identifier
|
|
||||||
-- @tparam[opt] {string,...} classes element classes
|
-- @tparam[opt] {string,...} classes element classes
|
||||||
|
-- @tparam[opt] table attributes table containing string keys and values
|
||||||
-- @return element attributes
|
-- @return element attributes
|
||||||
M.Attributes.__call = function(t, key_values, id, classes)
|
M.Attr.__call = function(t, identifier, classes, attributes)
|
||||||
id = id or ''
|
identifier = identifier or ''
|
||||||
classes = classes or {}
|
classes = classes or {}
|
||||||
local attr = {id, classes, key_values}
|
attributes = attributes or {}
|
||||||
|
local attr = {identifier, classes, attributes}
|
||||||
setmetatable(attr, t)
|
setmetatable(attr, t)
|
||||||
return attr
|
return attr
|
||||||
end
|
end
|
||||||
M.Attributes.__index = function(t, k)
|
M.Attr.__index = function(t, k)
|
||||||
if rawget(t, k) then
|
return rawget(t, k) or
|
||||||
return rawget(t, k)
|
rawget(t, M.Attr._field_names[k]) or
|
||||||
elseif k == "id" then
|
rawget(getmetatable(t), k)
|
||||||
if rawget(t, 1) == '' then
|
|
||||||
return nil
|
|
||||||
else
|
|
||||||
return rawget(t, 1)
|
|
||||||
end
|
|
||||||
elseif k == "class" then
|
|
||||||
if #(rawget(t, 2)) == 0 then
|
|
||||||
return nil
|
|
||||||
else
|
|
||||||
return table.concat(t[2], ' ')
|
|
||||||
end
|
|
||||||
else
|
|
||||||
for _, p in ipairs(t[3]) do
|
|
||||||
if k == p[1] then
|
|
||||||
return p[2]
|
|
||||||
end
|
|
||||||
end
|
|
||||||
return nil
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
M.Attributes.__newindex = function(t, k, v)
|
M.Attr.__newindex = function(t, k, v)
|
||||||
if rawget(t, k) then
|
if M.Attr._field_names[k] then
|
||||||
|
rawset(t, M.Attr._field_names[k], v)
|
||||||
|
else
|
||||||
rawset(t, k, v)
|
rawset(t, k, v)
|
||||||
elseif k == "id" then
|
|
||||||
rawset(t, 1, v)
|
|
||||||
elseif k == "class" then
|
|
||||||
if type(v) == "string" then
|
|
||||||
rawset(t, 2, {v})
|
|
||||||
else
|
|
||||||
rawset(t, 2, v)
|
|
||||||
end
|
|
||||||
else
|
|
||||||
for _, p in ipairs(rawget(t, 3)) do
|
|
||||||
if k == p[1] then
|
|
||||||
p[2] = v
|
|
||||||
return
|
|
||||||
end
|
|
||||||
end
|
|
||||||
kv = rawget(t, 3)
|
|
||||||
kv[#kv + 1] = {k, v}
|
|
||||||
rawset(t, 3, kv)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
setmetatable(M.Attributes, M.Attributes)
|
setmetatable(M.Attr, M.Attr)
|
||||||
|
|
||||||
|
|
||||||
--- Creates a single citation.
|
--- Creates a single citation.
|
||||||
|
|
|
@ -279,6 +279,6 @@ newtype LuaAttr = LuaAttr { fromLuaAttr :: Attr }
|
||||||
|
|
||||||
instance StackValue LuaAttr where
|
instance StackValue LuaAttr where
|
||||||
push lua (LuaAttr (id', classes, kv)) =
|
push lua (LuaAttr (id', classes, kv)) =
|
||||||
pushViaConstructor lua "Attributes" kv id' classes
|
pushViaConstructor lua "Attr" id' classes kv
|
||||||
peek lua idx = fmap LuaAttr <$> peek lua idx
|
peek lua idx = fmap LuaAttr <$> peek lua idx
|
||||||
valuetype _ = TTABLE
|
valuetype _ = TTABLE
|
||||||
|
|
Loading…
Add table
Reference in a new issue