Module:CDP Hierarchy: Difference between revisions

From atwg
No edit summary
No edit summary
Line 52: Line 52:
local output_name = string.gsub( k2, ':', '' )
local output_name = string.gsub( k2, ':', '' )
s = s .. output_name .. ' [URL="[[' .. k2 .. ']]", label=' .. p.label( v2 )
s = s .. output_name .. ' [URL="[[' .. k2 .. ']]", label=' .. p.label( v2 )
s = s .. ', style=filled, color=black, bgcolor=grey75, fontcolor=white'
s = s .. ', style=filled, color=black, fillcolor=grey75, fontcolor=white'
s = s .. '];\n'
s = s .. '];\n'
end
end

Revision as of 17:27, 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 bold"];\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 .. output_name .. ' [URL="[[' .. k2 .. ']]", label=' .. p.label( v2 )
			s = s .. ', style=filled, color=black, fillcolor=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