Module:Mermaid: Difference between revisions

From mcr
(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
Line 23: Line 23:
s = string.gsub( s, '&', 'and' )
s = string.gsub( s, '&', 'and' )
s = p.wrap ( s, 15 )
s = p.wrap ( s, 15 )
return '<<TABLE BORDER="0"><TR><TD>' .. s .. extra .. '</TD></TR></TABLE>>'
return s .. extra
end
end


Line 78: Line 78:
end
end
return false
return false
end
function p.buildGoalGraph( plan )
s = s .. outcome_name .. '[' ..
p.label( { table.concat( v.printouts['Long Name'] ) } ) .. ']\n'
s = s .. 'class ' .. outcome_name .. ' mermaid-outcome\n'
s = s .. plan_name .. ' -->' .. outcome_name .. '\n'
return s
end
end


Line 84: Line 92:
local nodes = p.getNodes( category, depends_on_property, status_property,
local nodes = p.getNodes( category, depends_on_property, status_property,
owner_property, project_property, column_property, tag_property )
owner_property, project_property, column_property, tag_property )
local s = 'digraph '
local s = 'graph LR\n'
s = s .. 'pert_chart'
s = s .. 'classDef mermaid-epic fill:gray50,stroke:gray50;\n'
s = s .. ' {\n'
s = s .. 'classDef mermaid-in-progress fill:darkgreen,stroke:darkgreen;\n'
s = s .. 'rankdir=LR;\n'
s = s .. 'classDef mermaid-blocked fill:firebrick,stroke:firebrick;\n'
s = s .. 'node [shape=box, fontsize=10, fontname="Arial"];\n'
s = s .. 'classDef mermaid-blocked-on-review fill:darkorange2,stroke:darkorange2;\n'
s = s .. 'edge [color=gray75];\n'
s = s .. 'classDef mermaid-done fill:blue3,stroke:blue3;\n'


for k1,v1 in pairs( nodes ) do
for k1,v1 in pairs( nodes ) do
s = s .. p.getPageName( k1 ) .. ' [URL="[[' .. k1 .. ']]", label=' .. v1.displaytitle
local name = p.getPageName( k1 )
s = s .. name .. '[' .. v1.displaytitle .. ']\n';
--[URL="[[' .. k1 .. ']]"
--s = s .. 'click ' .. outcome_name .. ' "' .. v.fullurl .. '"\n'
if v1.projects['Epic'] then
if v1.projects['Epic'] then
s = s .. ', color=gray50, fontcolor=gray50'
s = s .. 'class ' .. name .. ' mermaid-epic\n'
s = s .. ', shape=box3d'
--s = s .. ', shape=box3d'
else
else
if p.contains( v1.tags, 'Deployment' ) then
if p.contains( v1.tags, 'Deployment' ) then
s = s .. ', shape=cds'
--s = s .. ', shape=cds'
end
end
if v1.column == 'Planning' then
if v1.column == 'In Progress' then
s = s .. ', color=darkgoldenrod, fontcolor=darkgoldenrod'
s = s .. 'class ' .. name .. ' mermaid-in-progress\n'
elseif v1.column == 'In Progress' then
s = s .. ', color=darkgreen, fontcolor=darkgreen'
elseif v1.column == 'Blocked' then
elseif v1.column == 'Blocked' then
s = s .. ', color=firebrick, fontcolor=firebrick'
s = s .. 'class ' .. name .. ' mermaid-blocked\n'
elseif v1.column == 'Blocked on Review' then
elseif v1.column == 'Blocked on Review' then
s = s .. ', color=darkorange2, fontcolor=darkorange2'
s = s .. 'class ' .. name .. ' mermaid-blocked-on-review\n'
elseif v1.column == 'Done' then
elseif v1.column == 'Done' then
s = s .. ', color=blue3, fontcolor=blue3'
s = s .. 'class ' .. name .. ' mermaid-done\n'
end
end
end
end
s = s .. '];\n'
end
end
for k1,v1 in pairs( nodes ) do
for k1,v1 in pairs( nodes ) do
for k2, v2 in pairs ( v1.dependencies ) do
for k2, v2 in pairs ( v1.dependencies ) do
s = s .. p.getPageName( k2 ) .. '->' .. p.getPageName( k1 ) .. '\n'
s = s .. p.getPageName( k2 ) .. '-->' .. p.getPageName( k1 ) .. '\n'
end
end
end
end
s = s .. '}\n'
return s
return s
end
end
Line 125: Line 132:
function p.pert_chart(frame)
function p.pert_chart(frame)
local s = p.drawGraph( 'Phabricator Tasks', 'Subtask', 'Status', 'Owner', 'Project', 'MCR Column', 'Tag' )
local s = p.drawGraph( 'Phabricator Tasks', 'Subtask', 'Status', 'Owner', 'Project', 'MCR Column', 'Tag' )
return frame:callParserFunction{ name = '#tag', args = { 'graphviz', format='png', s } }
return '<div style="width:100%;text-align:center;font-size:14pt;">' ..
frame:callParserFunction{ name = '#mermaid', args = { s } } .. '</div>'
end
end


return p
return p

Revision as of 11:30, 28 May 2018

Documentation for this module may be created at Module: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.cleanseDisplayTitle( displaytitle, extra )
	local s = displaytitle
	s = string.gsub( s, '"', '' )
	s = string.gsub( s, '%[', '' )
	s = string.gsub( s, '%]', '' )
	s = string.gsub( s, '&', 'and' )
	s = p.wrap ( s, 15 )
	return s .. extra
end

function p.getNodes( category, depends_on_property, status_property,
	owner_property, project_property, column_property, tag_property )
	local nodes = {};
	local result = mw.smw.getQueryResult( '[[Category:' .. category .. ']]|?' ..
		depends_on_property .. '|?' .. status_property .. '|?' ..
		owner_property .. '|?' .. project_property .. '|?' .. column_property ..
		'|?' .. tag_property )
    if result ~= nil then
		for k1,v1 in pairs( result.results ) do
			nodes[v1.fulltext] = {}
			local extra = ''
			local owner = table.concat( v1.printouts[owner_property], ', ' )
			if ( #owner > 0 ) then
				extra = '<BR/>' .. owner
			end
			for k2,v2 in pairs ( v1.printouts[project_property] ) do
				if string.match( v2, '^MCR%-SDC' ) or
					string.match( v2, '^Multi%-Content%-Revisions' ) then
					extra = extra .. '<BR/>' .. v2 .. ' (' ..
						v1.printouts[column_property][1] .. ')'
					break;
				end
			end
			nodes[v1.fulltext].displaytitle =
				p.cleanseDisplayTitle( v1.displaytitle, extra )
			nodes[v1.fulltext].status = v1.printouts[status_property][1]
			nodes[v1.fulltext].dependencies = {} 
			for k2,v2 in pairs ( v1.printouts[depends_on_property] ) do
				nodes[v1.fulltext].dependencies[v2.fulltext] = true
			end
			nodes[v1.fulltext].projects = {}
			for k2,v2 in pairs ( v1.printouts[project_property] ) do
				nodes[v1.fulltext].projects[v2] = true
			end
			nodes[v1.fulltext].column = v1.printouts[column_property][1]
			nodes[v1.fulltext].tags = v1.printouts[tag_property]
		end
	end
   return nodes
end

function p.getPageName( title )
	return string.sub( title, string.find( title, ':' ) + 1 )
end

function p.contains( t, value )
	for k,v in pairs ( t ) do
		if v == value then
			return true
		end
	end
	return false
end

function p.buildGoalGraph( plan )
		s = s .. outcome_name .. '[' ..
			 p.label( { table.concat( v.printouts['Long Name'] ) } ) .. ']\n'
		s = s .. 'class ' .. outcome_name .. ' mermaid-outcome\n'
		s = s .. plan_name .. ' -->' .. outcome_name .. '\n'
	return s
end

function p.drawGraph( category, depends_on_property, status_property,
	owner_property, project_property, column_property, tag_property )
	local nodes = p.getNodes( category, depends_on_property, status_property,
		owner_property, project_property, column_property, tag_property )
	local s = 'graph LR\n'
	s = s .. 'classDef mermaid-epic fill:gray50,stroke:gray50;\n'
	s = s .. 'classDef mermaid-in-progress fill:darkgreen,stroke:darkgreen;\n'
	s = s .. 'classDef mermaid-blocked fill:firebrick,stroke:firebrick;\n'
	s = s .. 'classDef mermaid-blocked-on-review fill:darkorange2,stroke:darkorange2;\n'
	s = s .. 'classDef mermaid-done fill:blue3,stroke:blue3;\n'

	for k1,v1 in pairs( nodes ) do
		local name = p.getPageName( k1 )
		s = s .. name .. '[' .. v1.displaytitle .. ']\n';
		--[URL="[[' .. k1 .. ']]"
		--s = s .. 'click ' .. outcome_name .. ' "' .. v.fullurl .. '"\n'
		if v1.projects['Epic'] then
			s = s .. 'class ' .. name .. ' mermaid-epic\n'
			--s = s .. ', shape=box3d'
		else
			if p.contains( v1.tags, 'Deployment' ) then
				--s = s .. ', shape=cds'
			end
			if v1.column == 'In Progress' then
				s = s .. 'class ' .. name .. ' mermaid-in-progress\n'
			elseif v1.column == 'Blocked' then
				s = s .. 'class ' .. name .. ' mermaid-blocked\n'
			elseif v1.column == 'Blocked on Review' then
				s = s .. 'class ' .. name .. ' mermaid-blocked-on-review\n'
			elseif v1.column == 'Done' then
				s = s .. 'class ' .. name .. ' mermaid-done\n'
			end
		end
	end
	for k1,v1 in pairs( nodes ) do
		for k2, v2 in pairs ( v1.dependencies ) do
			s = s .. p.getPageName( k2 ) .. '-->' .. p.getPageName( k1 ) .. '\n'
		end
	end
	return s
end

function p.pert_chart(frame)
	local s = p.drawGraph( 'Phabricator Tasks', 'Subtask', 'Status', 'Owner', 'Project', 'MCR Column', 'Tag' )
	return '<div style="width:100%;text-align:center;font-size:14pt;">' ..
		frame:callParserFunction{ name = '#mermaid', args = { s } } .. '</div>'
end

return p