Module:Graph
From mcr
Documentation for this module may be created at Module:Graph/doc
local p = {}
function p.wrap( s, length )
s = mw.text.trim( s )
local oldindex = 1
local index = string.find( s, ' ' )
while ( index ) do
if ( index - oldindex >= length ) then
local temp = string.sub( s, 1, index - 1 ) .. '\n' .. string.sub( s, index + 1 )
s = temp
oldindex = index + 1
end
index = string.find( s, ' ', index + 1)
end
return s
end
function p.cleanseDisplayTitle( displaytitle )
local s = displaytitle
s = string.gsub( s, '"', '' )
s = string.gsub( s, '%[', '' )
s = string.gsub( s, '%]', '' )
s = string.gsub( s, '&', 'and' )
return s
end
function p.getNodes( category, property )
local nodes = {};
local result = mw.smw.getQueryResult( '[[Category:' .. category .. ']]|?' .. property )
if result ~= nil then
for k1,v1 in pairs( result.results ) do
nodes[v1.fulltext] = {}
nodes[v1.fulltext].displaytitle = p.cleanseDisplayTitle( v1.displaytitle )
nodes[v1.fulltext].dependencies = {}
for k2,v2 in pairs ( v1.printouts[property] ) do
nodes[v1.fulltext].dependencies[v2.fulltext] = true
end
end
end
return nodes
end
function p.getPageName( title )
return string.sub( title, string.find( title, ':' ) + 1 )
end
function p.draw(frame)
local nodes = p.getNodes( frame.args[1], frame.args[2] )
local s = 'digraph dependency_graph {\n'
s = s .. 'rankdir=LR;\n'
s = s .. 'node [shape=box, fontsize=10, fontname="Arial"];\n'
for k1,v1 in pairs( nodes ) do
s = s .. p.getPageName( k1 ) .. ' [URL="[[' .. k1 .. '|' .. v1.displaytitle .. ']]", label="' .. v1.displaytitle .. '"];\n'
end
for k1,v1 in pairs( nodes ) do
for k2, v2 in pairs ( v1.dependencies ) do
s = s .. p.getPageName( k1 ) .. '->' .. p.getPageName( k2 ) .. '\n'
end
end
s = s .. '}\n'
return frame:callParserFunction{ name = '#tag', args = { 'graphviz', format='png', s } }
end
return p