Module:Ballad tree

From International Robin Hood Bibliography

Documentation for this module may be created at Module:Ballad tree/doc

local testing = false
local p = {}
local u = require 'Module:Utilities'
local PARENT, ORIGINAL, VERSION, VARIANT, VARIANTTITLE, 
BALLADITEMTYPE, LANGUAGE, PAGEREFERENCE, COMMENT = 1, 2, 3, 4, 5, 6, 7, 8, 9
local output = ''
local taxonomy, items, nItems
local peekaboos = 0

function p.growTree( frame )
	local ballad = u.getFrameParam( 'Ballad', frame )
	if u.isValidParam( ballad ) then
		local smwResult = frame:preprocess(
			'{{#ask:[[ObjectClass::Ballad item]][[Title::' .. ballad .. ']]|?Parent|?Original|?Version|?Variant|?VariantTitle|' ..
			'?BalladItemType|?Language|?PageReference|?Comment|format=array|sep=⊞|propsep=⊗|mainlabel=-|' ..
			'sort=BalladItemType,Version,Variant,Language,Parent,Original|order=asc,asc,asc,asc,asc,asc}}'
		)
		items = mw.text.split( smwResult, '⊞',  true)
		nItems = #( items )
		for idx = 1, nItems do
			items[idx] = mw.text.split( items[idx], '⊗',  true )
		end

--		output = smwResult
		BuildTaxonomy()
	end
	if not testing then output = frame:preprocess(output) end
	return output
end

function BuildTaxonomy()
	taxonomy = {'heads', 'nHeads'}
	taxonomy.taxons = {}
	taxonomy.taxons[1] = 'Essential editions'
	taxonomy.taxons[2] = 'Primary sources'
	taxonomy.taxons[3] = 'Popular editions'
	taxonomy.taxons[4] = 'Facsimiles'
	taxonomy.taxons[5] = 'Scholarly editions'
	taxonomy.taxons[6] = 'Variant lists'
	taxonomy.taxons[7] = 'Translations'
	taxonomy.taxons[8] = 'Discussion'
	taxonomy.taxons[9] = 'Brief mention'
	taxonomy.nTaxons = #( taxonomy.taxons )

	for idx = 1, taxonomy.nTaxons do taxonomy[ taxonomy.taxons[idx] ] = '' end

	local balladItemType, version, variant, language, parent, original = '', '', '', '', '', ''
	local hasImbeddedUl = false
	for idx = 1, nItems do 
		local curBalladItemType, curVersion, curVariant, curLanguage, curParent, curOriginal =
		items[idx][BALLADITEMTYPE], items[idx][VERSION], items[idx][VARIANT], items[idx][LANGUAGE],
		items[idx][PARENT], items[idx][ORIGINAL]
		local itemText = ''

		-- Closing and opening Uls and LIs
		if u.isValidParam( balladItemType ) then
			if curBalladItemType == balladItemType and curVersion == version
			and curVariant == variant and curLanguage == language then
				if not hasImbeddedUl then
					if u.isValidParam( curOriginal ) and (curOriginal == original or curOriginal == parent) then
						itemText = itemText .. VisibilityToggle()
						hasImbeddedUl = true
					end
				else 
					if curOriginal ~= original and curOriginal ~= parent then
						itemText = itemText .. '</li></ul></li>'
						hasImbeddedUl = false
					end
					itemText = itemText .. '</li>'
					-- itemText = itemText .. '</ul>'
				end
			else
				if hasImbeddedUl then 
					itemText = itemText .. '</li></ul>'
					hasImbeddedUl = false
				end
				itemText = itemText .. '</li></ul>'
			end
		end

		-- Setting new head
		local head = false
		local heading = ''
		if curBalladItemType ~= balladItemType then
			heading = '<h2>' .. curBalladItemType .. '</h2>'
			head = true
		end

		-- Setting subheads for Primary sources
		if curBalladItemType == 'Primary sources' then
			if curVersion ~= version and u.isValidParam( curVersion ) then
				heading = heading .. '<h3>Version: ' .. curVersion .. '</h3>'
				head = true
			end
			if curVariant ~= variant and u.isValidParam( curVariant ) then
				local h
				if curVersion == '' then n = '3' else n = '4' end
				heading = heading .. '<h' .. n .. '>Variant: ' .. curVariant .. '</h' .. n .. '>'
				head = true
			end
		end

		-- Closing open list, if required, before adding head and new list
		if head then
			if hasImbeddedUl then 
				itemText = itemText .. '</li></ul>'
				hasImbeddedUl = false
			end
			if idx > 1 then itemText = itemText .. '</li></ul>' end
			itemText = itemText .. heading .. '<ul>'
		end

		-- Adding current line
		itemText = itemText .. BuildListItem(idx)

		-- Closing LI (and Ul) for last line, else saving item data for comparison with next item
		if idx == nItems then 
			if hasImbeddedUl then itemText = itemText .. '</ul></li>' end
			itemText = itemText .. '</li></ul>'
		else 
			balladItemType = curBalladItemType
			version = curVersion
			variant = curVariant
			language = curLanguage
			parent = curParent
			original = curOriginal
		end

		-- Adding current item to the taxon
		taxonomy[curBalladItemType] = taxonomy[curBalladItemType] .. itemText

	end

	-- Building output from taxonomy structure
	for idx = 1, taxonomy.nTaxons do
		output = output .. taxonomy[ taxonomy.taxons[idx] ]
	end

end

function BuildListItem(idx)
	local line = '<li>{{:' .. items[idx][PARENT] .. '}}'
	if u.isValidParam( items[idx][PAGEREFERENCE] ) then
		line = line .. ', ' .. items[idx][PAGEREFERENCE]
	end
	if u.isValidParam( items[idx][VARIANTTITLE] ) then
		line = line .. ". Title: '" .. items[idx][VARIANTTITLE] .. "'"
	end
	if u.isValidParam( items[idx][COMMENT] ) then
		line = line .. '. ' .. items[idx][COMMENT]
	end
	return line
end

function VisibilityToggle()
	peekaboos = peekaboos + 1
	local peekabooStr = tostring(peekaboos)
	return '<span class="peekaboo-toggle peekaboo' .. peekabooStr .. ' hiding" id="peekaboo-toggle'
	.. peekabooStr .. '">Later editions ...</span><ul class="peekaboo hidden" id="peekaboo' .. peekabooStr .. '"ul>'
end

return p