Open main menu
Home
Random
Donate
Recent changes
Special pages
Community portal
Preferences
About Stockhub
Disclaimers
Search
User menu
Talk
Contributions
Create account
Log in
Editing
Module:Sandbox/Luis150902/Test
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
-- This is a metamodule for producing wikitext trees. local p = {}; local middleclass = require('Module:Middleclass').class; local TreeNode = middleclass('TreeNode'); function TreeNode:initialize(object, options) self.children = {}; self.object = object; self.options = options or {}; end function TreeNode:addChild(child, level) if type(child) ~= "table" then error({ message = "Value passed to child is not a table (cannot currently check for TreeNodes)", source = "TreeView", method = "TreeNode:addChild", status = -1073741585 }, 1); end self.children[child] = child:render(level) end local function process(object, pairFunction) -- This function returns the node text to show for object, invoking -- pairFunction for the keys and values of the object. local metatable = getmetatable(object); if type(object) ~= "table" then return tostring(object); end if metatable and type(metatable.dissector) == "table" then return metatable.dissector:processObject(object, pairFunction); end if metatable.name then return 'An instance of' .. metatable.name; end local function counter() local count = 0; for k, v in pairs(object) do pairFunction(k, v); count = count + 1; end return count; end local success, result = pcall(counter); if success then return 'A table with ' .. tostring(result) .. ' pairs'; end return 'A table with an unknown number of pairs'; end function TreeNode:render(level) if self.options.noRender then -- Options requested not to render, simply to return self. return self; end local pairFunction = function(k, v) self:addChild(TreeNode:new(v, options), (level or 0) + 1); end; local title = process(self.object, pairFunction); if options.nodeMaker then title = options.nodeMaker(title, level); else if options.indent and type(options.indent) ~= "number" then error({ message = "Value passed to options.indent is a not a number and not nil. Default is 30.", source = "TreeView", status = -1073741811 }, 1); end local indent = options.indent or 30; title = tostring(mw.html.create('div') :css('padding-left', tostring(level * options.indent) .. 'px') :css('overflow', 'hidden') :css('white-space', 'nowrap')); end for child, renderingResult in pairs(self.children) do title = title .. '\n' .. renderingResult end end function p.treeView(object, options) local metatable = getmetatable(object); if metatable.tree then return style(metatable.tree(object), options); end return TreeNode:new(object, options):render(); end return p
Summary:
Please note that all contributions to Stockhub may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Stockhub:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Templates used on this page:
Template:Module other
(
edit
)
Template:Module rating
(
edit
)
Template:Ombox
(
edit
)
Module:Arguments
(
edit
)
Module:Message box
(
edit
)
Module:Message box/configuration
(
edit
)
Module:Message box/ombox.css
(
edit
)
Module:Sandbox/Luis150902/Test/doc
(
edit
)
Module:Yesno
(
edit
)