Module:CDP Tasks: Difference between revisions

From atwg
No edit summary
No edit summary
Line 23: Line 23:
local tasks = mw.smw.getQueryResult( '[[Category:Tasks]][[Archived::!true]]' ..
local tasks = mw.smw.getQueryResult( '[[Category:Tasks]][[Archived::!true]]' ..
'|?Depends On Task' ..
'|?Depends On Task' ..
'|?Start Year' )
'|?End Year' )
     if tasks == nil then
     if tasks == nil then
     return ''
     return ''
Line 36: Line 36:
s = s .. task_name .. ' [URL="[[' .. v.fulltext .. ']]", label='
s = s .. task_name .. ' [URL="[[' .. v.fulltext .. ']]", label='
s = s .. p.label( v.displaytitle )
s = s .. p.label( v.displaytitle )
if table.concat( v.printouts['Start Year'] ) == '0' then
if table.concat( v.printouts['End Year'] ) == '0' then
s = s .. ', color=darkgreen, fontcolor=darkgreen'
s = s .. ', color=darkgreen, fontcolor=darkgreen'
end
end

Revision as of 09:42, 13 February 2018

Documentation for this module may be created at Module:CDP Tasks/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.buildDependencyGraph()
	local tasks = mw.smw.getQueryResult( '[[Category:Tasks]][[Archived::!true]]' ..
		'|?Depends On Task' ..
		'|?End Year' )
    if tasks == nil then
    	return ''
    end
	local s = 'digraph tasks'
	s = s .. ' {\n'
	s = s .. 'rankdir=LR;\n'
	s = s .. 'node [shape=box, fontsize=10, fontname="Arial bold"];\n'
	s = s .. 'edge [color=gray75];\n'
	for k,v in pairs( tasks.results ) do
		local task_name = string.gsub( v.fulltext, ':', '' )
		s = s .. task_name .. ' [URL="[[' .. v.fulltext .. ']]", label='
		s = s .. p.label( v.displaytitle )
		if table.concat( v.printouts['End Year'] ) == '0' then
			s = s .. ', color=darkgreen, fontcolor=darkgreen'
		end
		s = s .. '];\n'
	end
	for k1,v1 in pairs( tasks.results ) do
		local target_task_name = string.gsub( v1.fulltext, ':', '' )
		for k2,v2 in pairs( v1.printouts['Depends On Task'] ) do
			local source_task_name = string.gsub( v2.fulltext, ':', '' )
			s = s .. source_task_name .. '->' .. target_task_name .. '\n'
		end
	end
	s = s .. '}\n'
	return s
end

function p.showDependencyGraph(frame)
	local s = p.buildDependencyGraph()
	return frame:callParserFunction{ name = '#tag', args = { 'graphviz', format='png', s } }
end

return p