Module:BuildVersionTree: Difference between revisions
From Seven Sages of Rome
(Created page with "-- Module:VersionTree local p = {} -- Helper function to build a tree structure from flat data local function buildTree(versions, parentField) local tree = {} local nodes = {} -- First pass: Create all nodes for _, version in ipairs(versions) do nodes[version.title] = { title = version.title, children = {}, processed = false } end -- Second pass: Build relationships for _, version...") |
No edit summary Tag: Manual revert |
||
(11 intermediate revisions by the same user not shown) | |||
Line 8: | Line 8: | ||
-- First pass: Create all nodes | -- First pass: Create all nodes | ||
for _, version in | for _, version in pairs(versions) do | ||
nodes[ | local title = version[1] | ||
title = | nodes[title] = { | ||
title = title, | |||
children = {}, | children = {}, | ||
processed = false | processed = false | ||
Line 17: | Line 18: | ||
-- Second pass: Build relationships | -- Second pass: Build relationships | ||
for _, version in | for _, version in pairs(versions) do | ||
local title = version[1] | |||
local parent = version[parentField] | local parent = version[parentField] | ||
if parent and nodes[parent] then | |||
table.insert(nodes[parent].children, nodes[ | if parent and parent[1] and nodes[parent[1]] then | ||
-- If it has a valid parent, add it as a child | |||
table.insert(nodes[parent[1]].children, nodes[title]) | |||
else | else | ||
-- If no parent or parent | -- If no parent or invalid parent, it's a root node | ||
table.insert(tree, nodes[ | table.insert(tree, nodes[title]) | ||
end | end | ||
end | end | ||
Line 45: | Line 49: | ||
table.insert(result, string.format('<div class="tree-node level-%d" id="%s">', level, nodeId)) | table.insert(result, string.format('<div class="tree-node level-%d" id="%s">', level, nodeId)) | ||
-- Add node content | -- Add node content with simple link | ||
table.insert(result, string.format('<div class="node-content"><div class="node-box"> | table.insert(result, string.format('<div class="node-content"><div class="node-box">%s</div></div>', | ||
node.title -- Simple link without any pipe or display text | |||
)) | |||
if #node.children > 0 then | if #node.children > 0 then | ||
Line 68: | Line 74: | ||
return table.concat(result, '\n') | return table.concat(result, '\n') | ||
end | end | ||
Line 178: | Line 84: | ||
if not versions then | if not versions then | ||
return " | return "SMW query returned no results." | ||
end | end | ||
-- Build tree | -- Build tree | ||
local tree = buildTree( | local tree = buildTree(versions, "Has Parent Version") | ||
-- Sort root nodes | -- Sort root nodes | ||
Line 198: | Line 95: | ||
end) | end) | ||
-- | -- Create output | ||
local result = { | local result = {'<div class="version-tree">'} | ||
-- Render each root node | -- Render each root node |
Latest revision as of 19:54, 27 October 2024
Documentation for this module may be created at Module:BuildVersionTree/doc
-- Module:VersionTree
local p = {}
-- Helper function to build a tree structure from flat data
local function buildTree(versions, parentField)
local tree = {}
local nodes = {}
-- First pass: Create all nodes
for _, version in pairs(versions) do
local title = version[1]
nodes[title] = {
title = title,
children = {},
processed = false
}
end
-- Second pass: Build relationships
for _, version in pairs(versions) do
local title = version[1]
local parent = version[parentField]
if parent and parent[1] and nodes[parent[1]] then
-- If it has a valid parent, add it as a child
table.insert(nodes[parent[1]].children, nodes[title])
else
-- If no parent or invalid parent, it's a root node
table.insert(tree, nodes[title])
end
end
return tree
end
-- Helper function to generate unique IDs for nodes
local function generateId(text)
return 'node-' .. mw.uri.encode(text):gsub('%%', '-')
end
-- Helper function to render a tree node and its children
local function renderNode(node, level)
local result = {}
-- Generate unique ID for this node
local nodeId = generateId(node.title)
-- Create node container
table.insert(result, string.format('<div class="tree-node level-%d" id="%s">', level, nodeId))
-- Add node content with simple link
table.insert(result, string.format('<div class="node-content"><div class="node-box">%s</div></div>',
node.title -- Simple link without any pipe or display text
))
if #node.children > 0 then
-- Add container for children
table.insert(result, '<div class="node-children">')
-- Sort children by title
table.sort(node.children, function(a, b)
return a.title < b.title
end)
-- Render children
for _, child in ipairs(node.children) do
table.insert(result, renderNode(child, level + 1))
end
table.insert(result, '</div>')
end
table.insert(result, '</div>')
return table.concat(result, '\n')
end
-- Main function to process the versions and generate tree view
function p.getTree(frame)
local versions = mw.smw.ask({
"[[Category:Version||Secondary Version]]",
"?Has Parent Version"
})
if not versions then
return "SMW query returned no results."
end
-- Build tree
local tree = buildTree(versions, "Has Parent Version")
-- Sort root nodes
table.sort(tree, function(a, b)
return a.title < b.title
end)
-- Create output
local result = {'<div class="version-tree">'}
-- Render each root node
for _, node in ipairs(tree) do
table.insert(result, renderNode(node, 0))
end
table.insert(result, '</div>')
return table.concat(result, '\n')
end
return p