Module:CDP Hierarchy: Difference between revisions

From atwg
No edit summary
No edit summary
Line 18: Line 18:
function p.label( s )
function p.label( s )
return '<<TABLE BORDER="0"><TR><TD>' .. p.wrap( s, 30 ) .. '</TD></TR></TABLE>>'
return '<<TABLE BORDER="0"><TR><TD>' .. p.wrap( s, 30 ) .. '</TD></TR></TABLE>>'
end
function p.getNodes( outcome )
local nodes = {};
local outcomes = mw.smw.getQueryResult( '[[' .. outcome .. ']]|?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
end


Line 55: Line 38:
local outputs = mw.smw.getQueryResult(
local outputs = mw.smw.getQueryResult(
'[[Category:Outputs]][[Parent Outcome::' .. outcome .. ']]|?Description' )
'[[Category:Outputs]][[Parent Outcome::' .. outcome .. ']]|?Description' )
for k,v in pairs( outputs ) do
for k,v in pairs( outputs.results ) do
local output_name = string.gsub( v.fulltext, ':', '' )
local output_name = string.gsub( v.fulltext, ':', '' )
s = s .. output_name .. ' [URL="[[' .. v.fulltext .. ']]", label='
s = s .. output_name .. ' [URL="[[' .. v.fulltext .. ']]", label='
s = s .. p.label( v.printouts.Description )
s = s .. p.label( table.concat( v.printouts.Description ) )
s = s .. ', style=filled, color=black, fillcolor=grey50, fontcolor=white'
s = s .. ', style=filled, color=black, fillcolor=grey50, fontcolor=white'
s = s .. '];\n'
s = s .. '];\n'

Revision as of 06:59, 10 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.buildGraph( outcome )
	local outcomes = mw.smw.getQueryResult( '[[' .. outcome .. ']]|?Description' )
    if outcomes == nil then
    	return ''
    end
	local node = table.remove( outcomes.results )
	local outcome_name = string.gsub( outcome, ':', '' )
	local s = 'digraph ' .. outcome_name
	s = s .. ' {\n'
	s = s .. 'graph [splines=ortho, nodesep=1];\n'
	s = s .. 'node [shape=box, fontsize=10, fontname="Arial bold"];\n'
	s = s .. 'edge [color=gray75];\n'
	s = s .. outcome_name .. ' [URL="[[' .. outcome .. ']]", label='
	s = s .. p.label( table.concat( node.printouts.Description ) )
	s = s .. ', style=filled, color=black, fillcolor=aquamarine4, fontcolor=white'
	s = s .. '];\n'
	local outputs = mw.smw.getQueryResult(
		'[[Category:Outputs]][[Parent Outcome::' .. outcome .. ']]|?Description' )
	for k,v in pairs( outputs.results ) do
		local output_name = string.gsub( v.fulltext, ':', '' )
		s = s .. output_name .. ' [URL="[[' .. v.fulltext .. ']]", label='
		s = s .. p.label( table.concat( v.printouts.Description ) )
		s = s .. ', style=filled, color=black, fillcolor=grey50, fontcolor=white'
		s = s .. '];\n'
		s = s .. outcome_name .. '->' .. output_name .. '\n'
	end
	s = s .. '}\n'
	return s
end

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

return p