Module:CDP Hierarchy

From atwg
Revision as of 17:08, 9 February 2018 by Ccicalese (talk | contribs)

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>' .. s .. '</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
		s = s .. k1 .. ' [URL="[[' .. k1 .. ']]", label=' .. p.label( v1.description )
		s = s .. ', color=green, bgcolor=green, fontcolor=white'
		s = s .. '];\n'
		for k2,v2 in pairs( v1.outputs ) do
			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
			s = s .. k1 .. '->' .. k2 .. '\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