Module:CDP Hierarchy Mermaid: Difference between revisions

From atwg
(Created page with "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 >= lengt...")
 
No edit summary
 
(38 intermediate revisions by the same user not shown)
Line 28: Line 28:


function p.label( a )
function p.label( a )
return '"' .. p.wrapArray( a, 50 ) .. '"'
return '"' .. p.wrapArray( a, 30 ) .. '"'
end
end


Line 37: Line 37:
     end
     end
local s = 'graph TB\n'
local s = 'graph TB\n'
s = s .. 'goal[' .. p.label( {goal } )
s = s .. 'classDef mermaid-goal fill:#104e8b,stroke:black;\n'
-- [URL="[[Main Page]]", label=' .. p.label( { goal } )
s = s .. 'classDef mermaid-outcome fill:#458b74,stroke:black;\n'
s = s .. ']\n'
s = s .. 'goal[' .. p.label( { goal } ) .. ']\n'
s = s .. 'class goal mermaid-goal\n'
for k,v in pairs( outcomes.results ) do
for k,v in pairs( outcomes.results ) do
local outcome_name = string.gsub( v.fulltext, ':', '' )
local outcome_name = string.gsub( v.fulltext, ':', '' )
-- s = s .. outcome_name .. ' [URL="[[' .. v.fulltext .. ']]", label='
s = s .. outcome_name .. '[' ..
s = s .. outcome_name .. '['
p.label( { table.concat( v.printouts.Description ) } ) .. ']\n'
s = s .. p.label( { table.concat( v.printouts.Description ) } )
s = s .. 'class ' .. outcome_name .. ' mermaid-outcome\n'
s = s .. ']\n'
s = s .. 'click ' .. outcome_name .. ' "' .. v.fullurl .. '"\n'
s = s .. 'goal -->' .. outcome_name .. '\n'
s = s .. 'goal -->' .. outcome_name .. '\n'
end
end
Line 58: Line 59:
     end
     end
local outcome_name = string.gsub( outcome, ':', '' )
local outcome_name = string.gsub( outcome, ':', '' )
local s = 'digraph ' .. outcome_name
local s = 'graph TB\n'
s = s .. ' {\n'
s = s .. 'classDef mermaid-outcome fill:#458b74,stroke:black;\n'
s = s .. 'rankdir=LR;\n'
s = s .. 'classDef mermaid-output fill:#888888,stroke:black;\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='
local node = table.remove( outcomes.results )
local node = table.remove( outcomes.results )
s = s .. p.label( { table.concat( node.printouts.Description ) } )
s = s .. outcome_name .. '[' ..
s = s .. ', style=filled, color=black, fillcolor=aquamarine4, fontcolor=white'
p.label( { table.concat( node.printouts.Description ) } ) .. ']\n'
s = s .. '];\n'
s = s .. 'class ' .. outcome_name .. ' mermaid-outcome\n'
local outputs = mw.smw.getQueryResult(
local outputs = mw.smw.getQueryResult(
'[[Category:Outputs]][[Parent Outcome::' .. outcome .. ']]|?Description|?Primary Team|?Collaborating Team' )
'[[Category:Outputs]][[Parent Outcome::' .. outcome .. ']]|?Description|?Primary Team|?Collaborating Team' )
Line 80: Line 78:
collaborating = 'Collaborating Teams: ' .. collaborating
collaborating = 'Collaborating Teams: ' .. collaborating
end
end
s = s .. output_name .. ' [URL="[[' .. v.fulltext .. ']]", label='
s = s .. output_name .. '[' ..
s = s .. p.label( { table.concat( v.printouts.Description ),
p.label( { table.concat( v.printouts.Description ), primary, collaborating } )
primary, collaborating } )
s = s .. ']\n'
s = s .. ', style=filled, color=black, fillcolor=grey50, fontcolor=white'
s = s .. 'class ' .. output_name .. ' mermaid-output\n'
s = s .. '];\n'
s = s .. 'click ' .. output_name.. ' "' .. v.fullurl .. '"\n'
s = s .. outcome_name .. '->' .. output_name .. '\n'
s = s .. outcome_name .. '-->' .. output_name .. '\n'
end
end
s = s .. '}\n'
return s
return s
end
end
Line 93: Line 90:
function p.showGoal(frame)
function p.showGoal(frame)
local s = p.buildGoalGraph( frame.args[1] )
local s = p.buildGoalGraph( frame.args[1] )
return frame:callParserFunction{ name = '#tag', args = { 'graphviz', format='png', s } }
return '<div style="font-size:14pt;">' .. frame:callParserFunction{ name = '#mermaid', args = { s } } .. '</div>'
end
end


function p.showOutcome(frame)
function p.showOutcome(frame)
local s = p.buildOutcomeGraph( frame.args[1] )
local s = p.buildOutcomeGraph( frame.args[1] )
return frame:callParserFunction{ name = '#tag', args = { 'graphviz', format='png', s } }
return '<div style="font-size:14pt;">' .. frame:callParserFunction{ name = '#mermaid', args = { s } } .. '</div>'
end
end


return p
return p

Latest revision as of 15:02, 21 February 2018

Documentation for this module may be created at Module:CDP Hierarchy Mermaid/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.wrapArray( a, length )
	s = ''
	for k,v in pairs( a ) do
		if #s > 0 then
			s = s .. '<BR/>'
		end
		s = s .. p.wrap( v, length )
	end
	return s
end

function p.label( a )
	return '"' .. p.wrapArray( a, 30 ) .. '"'
end

function p.buildGoalGraph( goal )
	local outcomes = mw.smw.getQueryResult( '[[Category:Outcomes]][[Archived::!true]]|?Description' )
    if outcomes == nil then
    	return ''
    end
	local s = 'graph TB\n'
	s = s .. 'classDef mermaid-goal fill:#104e8b,stroke:black;\n'
	s = s .. 'classDef mermaid-outcome fill:#458b74,stroke:black;\n'
	s = s .. 'goal[' .. p.label( { goal } ) .. ']\n'
	s = s .. 'class goal mermaid-goal\n'
	for k,v in pairs( outcomes.results ) do
		local outcome_name = string.gsub( v.fulltext, ':', '' )
		s = s .. outcome_name .. '[' ..
			 p.label( { table.concat( v.printouts.Description ) } ) .. ']\n'
		s = s .. 'class ' .. outcome_name .. ' mermaid-outcome\n'
		s = s .. 'click ' .. outcome_name .. ' "' .. v.fullurl .. '"\n'
		s = s .. 'goal -->' .. outcome_name .. '\n'
	end
	return s
end


function p.buildOutcomeGraph( outcome )
	local outcomes = mw.smw.getQueryResult( '[[' .. outcome .. ']][[Archived::!true]]|?Description' )
    if outcomes == nil then
    	return ''
    end
	local outcome_name = string.gsub( outcome, ':', '' )
	local s = 'graph TB\n'
	s = s .. 'classDef mermaid-outcome fill:#458b74,stroke:black;\n'
	s = s .. 'classDef mermaid-output fill:#888888,stroke:black;\n'
	local node = table.remove( outcomes.results )
	s = s .. outcome_name .. '[' ..
		p.label( { table.concat( node.printouts.Description ) } ) .. ']\n'
	s = s .. 'class ' .. outcome_name .. ' mermaid-outcome\n'
	local outputs = mw.smw.getQueryResult(
		'[[Category:Outputs]][[Parent Outcome::' .. outcome .. ']]|?Description|?Primary Team|?Collaborating Team' )
	for k,v in pairs( outputs.results ) do
		local output_name = string.gsub( v.fulltext, ':', '' )
		local primary = table.concat( v.printouts['Primary Team'] )
		if #primary > 0 then
			primary = '<BR/>Primary Team: ' .. primary
		end
		local collaborating = table.concat( v.printouts['Collaborating Team'], ', ' )
		if #collaborating > 0 then
			collaborating = 'Collaborating Teams: ' .. collaborating
		end
		s = s .. output_name .. '[' ..
			p.label( { table.concat( v.printouts.Description ), primary, collaborating } )
		s = s .. ']\n'
		s = s .. 'class ' .. output_name .. ' mermaid-output\n'
		s = s .. 'click ' .. output_name.. ' "' .. v.fullurl .. '"\n'
		s = s .. outcome_name .. '-->' .. output_name .. '\n'
	end
	return s
end

function p.showGoal(frame)
	local s = p.buildGoalGraph( frame.args[1] )
	return '<div style="font-size:14pt;">' .. frame:callParserFunction{ name = '#mermaid', args = { s } } .. '</div>'
end

function p.showOutcome(frame)
	local s = p.buildOutcomeGraph( frame.args[1] )
	return '<div style="font-size:14pt;">' .. frame:callParserFunction{ name = '#mermaid', args = { s } } .. '</div>'
end

return p