Module:CDP Hierarchy: Difference between revisions

From atwg
No edit summary
No edit summary
Line 47: Line 47:
local outcome_name = string.gsub( k1, ':', '' )
local outcome_name = string.gsub( k1, ':', '' )
s = s .. outcome_name .. ' [URL="[[' .. k1 .. ']]", label=' .. p.label( v1.description )
s = s .. outcome_name .. ' [URL="[[' .. k1 .. ']]", label=' .. p.label( v1.description )
s = s .. ', style=filled, color=green, fillcolor=green, fontcolor=white'
s = s .. ', style=filled, color=black, fillcolor=aquamarine4, fontcolor=white'
s = s .. '];\n'
s = s .. '];\n'
for k2,v2 in pairs( v1.outputs ) do
for k2,v2 in pairs( v1.outputs ) do

Revision as of 17:20, 9 February 2018

Documentation for this module may be created at Module:CDP Hierarchy/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 ) .. '<BR/>' .. string.sub( s, index + 1 )
			s = temp
			oldindex = index + 4
		end
		index = string.find( s, ' ', index + 1)
	end
	return s
end

function p.label( s )
	return '<<TABLE BORDER="0"><TR><TD>' .. p.wrap( s, 30 ) .. '</TD></TR></TABLE>>'
end

function p.getNodes()
	local nodes = {};
	local outcomes = mw.smw.getQueryResult( '[[Category:Outcomes]]|?Description' )
    if outcomes ~= nil then
		for k1,v1 in pairs( outcomes.results ) do
			nodes[v1.fulltext] = {}
			nodes[v1.fulltext].description = table.concat( v1.printouts.Description )
			nodes[v1.fulltext].outputs = {}
			local outputs = mw.smw.getQueryResult( '[[Category:Outputs]][[Parent Outcome::' .. v1.fulltext .. ']]|?Description' )
			for k2,v2 in pairs ( outputs.results ) do
				nodes[v1.fulltext].outputs[v2.fulltext] = table.concat( v2.printouts.Description )
			end
		end
	end
   return nodes
end

function p.buildGraph()
	local nodes = p.getNodes()
	local s = 'digraph hierarchy'
	s = s .. ' {\n'
	s = s .. 'node [shape=box, fontsize=10, fontname="Arial"];\n'
	s = s .. 'edge [color=gray75];\n'

	for k1,v1 in pairs( nodes ) do
		local outcome_name = string.gsub( k1, ':', '' )
		s = s .. outcome_name .. ' [URL="[[' .. k1 .. ']]", label=' .. p.label( v1.description )
		s = s .. ', style=filled, color=black, fillcolor=aquamarine4, fontcolor=white'
		s = s .. '];\n'
		for k2,v2 in pairs( v1.outputs ) do
			local output_name = string.gsub( k2, ':', '' )
			s = s .. k2 .. ' [URL="[[' .. k2 .. ']]", label=' .. p.label( v2 )
			s = s .. ', color=grey75, bgcolor=grey75, fontcolor=white'
			s = s .. '];\n'
		end
		for k2,v2 in pairs( v1.outputs ) do
			local output_name = string.gsub( k2, ':', '' )
			s = s .. outcome_name .. '->' .. output_name .. '\n'
		end
	end
	s = s .. '}\n'
	return s
end

function p.show(frame)
	local s = p.buildGraph()
	return frame:callParserFunction{ name = '#tag', args = { 'graphviz', format='png', s } }
end

return p