Mòdul:Breadcrumb

De Viccionari
Icona de documentació de mòdul Documentació del mòdul[mostra] [modifica] [refresca]

A continuació es mostra la documentació transclosa de la subpàgina /ús. [salta a la caixa de codi]


Genera una ruta de navegació. Vegeu la plantilla principal {{ruta de navegació}}.

-- Original from nl.wiki by User:MrBlueSky

local p = {}

p.table = '<table cellspacing="1" cellpadding="0">'	-- opening tag for all tables in the breadcrumb
p.curly = '<big>}</big>'							-- curly between branches
p.wolkje = '(...)'									-- cloud for omitted categories
p.MAX = 50											-- maximum number of categories before we give it up

-- Tracking problems with a letter used as sorting key in the tracking category.
-- If there are more than one problem, it takes just the latter.
--   "C": Closing: there is something wrong with the wikitext, as a missing closing tag or alike
--   "P": Parameter: an invalid value was specified for a parameter
--   "M": Max: we have reached p.MAX, so the tree is not finished
--   "O": the page (or category) is uncategorized
p.problemen = ""
p.probleemcat = 'Ruta de navegació amb problemes'	-- category for pages with breadcrumb problems

-- TODO: i18n category namespace prefix, see {{ns:Category}} and
--	[[Special:ApiSandbox#action=query&format=json&meta=siteinfo&siprop=namespaces%7cnamespacealiases]]

--[[ 
	parseNegeerLijst( str )

	Parse a comma-separated list of category names
]]
local function parseNegeerLijst( str )
	local lijst = {}
	
	cats = mw.text.split( str, ',', true )
	
	for _, cat in pairs( cats ) do
		cat = p.cleanupCatTitle( cat )
		lijst[cat] = ""
	end
	
	return lijst
end

--[[
    breadcrumb(frame)

    Creates a breadcrumb navigation for the categories of a page.
    If frame.args[page] is specified, that page is used.
    Else, use the page from frame.args[1]
    If neither is specified, the current page is used.
    
]]
function p.breadcrumb( frame )

    -- For which page?
    local title = frame.args.page or frame.args[1]
	if ( not title or title == "" ) then
		title = mw.title.getCurrentTitle().prefixedText
	end
	
	-- Height?
	local level = 8
	p.force = false
	if ( frame.args.length and frame.args.length ~= "" ) then
		n = tonumber( frame.args.length )
		if ( n ~= nil and n > 0 ) then
			level = n
			if ( frame.args.force and frame.args.force ~= "" ) then
				p.force = true
			end
		else
			p.probleem( "P" )
		end
	end
	
	-- Maximum number of parallel branches?
	p.max_branch = 4
	if ( frame.args.maxbranch and frame.args.maxbranch ~= "" ) then
		local n = tonumber( frame.args.maxbranch )
		if ( n ~= nil and n > 0 ) then
			p.max_branch = n
		else
			p.probleem( "P" )
		end
	end
	
	-- Shrink text?
	local txtsize = 20
	p.verklein = false
	if ( frame.args.startsize and frame.args.startsize ~= "" ) then
		p.verklein = true
		local n = tonumber( frame.args.startsize ) 
		if ( n ~= nil and n > 0 ) then
			txtsize = 2 * n
		else
			p.probleem( "P" )
		end
	end
	
	-- Ignore list?
	p.negeerlijst = {}
	if ( frame.args.ignore and frame.args.ignore ~= "" ) then
		p.negeerlijst = parseNegeerLijst( frame.args.ignore )
	end
		
	-- Make tree
	local trees = p.createCategoryTree( title, level )
	
	-- Make crumb
	local ntitle, ntree = next( trees )
	local kruimel = ""
	if ( type( ntree ) == "table" and next( ntree ) ) then
		kruimel = p.createBreadCrumb( ntitle, ntree, txtsize )
		if frame.args.fontsize then
			kruimel = '<div style="font-size: ' .. frame.args.fontsize .. ';">' .. kruimel .. '</div>'
		end
	else
		-- If we cannot make breadcrumbs because the page is in more than max_branch cats,
		-- we simply print the parent cats: at least we have to show something.
		kruimel = p.printcats( p.hoofdcats )
		if ( p.hoofdcats == nil or next( p.hoofdcats ) == nil ) then
			-- uncategorized page
			p.probleem( "O" )
		end
	end
	
	return kruimel .. p.printprobleem() .. (p.show_TOC(frame) or "")
	
end

--[[

	parseCategories( wikitext )

	Finds valid categories in wikitext.

]]
local function parseCategories( txt )
	
	if ( txt == nil ) then return {} end
	
	local cats = {}
	
	-- TODO: i18n
	local pattern = "%[%[%s*[Cc][Aa][Tt][Ee][Gg][Oo][Rr][YyIi][Aa]?%s*:%s*([^|%]]+)[|%]]"
        
	for category in mw.ustring.gmatch( txt, pattern ) do
		category = p.cleanupCatTitle( category )
		-- Skip categories with crazy things: templates, etc.
		if ( mw.ustring.find( category, "[{}%[%]]" ) == nill) then
			-- If the name is longer than 50 characters,
			-- something is probably wrong 
			if ( mw.ustring.len( category ) < 100 ) then
				if ( p.negeerlijst[category] == nil ) then
					cats[category] = ""
					p.branchcount = p.branchcount + 1
				end
			else
				if ( p.hoofdcats == nil ) then
					p.probleem( "C" )
				end
			end
		end
	end
	
	if ( p.hoofdcats == nil ) then
		-- Store the cats directly above the page,
		-- we use them when we cannot make a tree
		p.hoofdcats = cats
	end
	
	return cats
	
end

--[[

	createCategoryTree( title, maxlevel )

	Creates a tree from the categories above title, up to maxlevel.
	Stops if the tree contains more than p.max_branch branches.

]]
function p.createCategoryTree( title, maxlevel ) 

	local level = 0
	local tree = { [title] = "" } 
	
	p.seen = {}
	p.count = 0
	
	while level < maxlevel do
		level = level + 1
		p.branchcount = 0
		local new_tree = p.addLevel( mw.clone (tree) )
		if ( not p.force and p.branchcount > p.max_branch ) then
			return tree
		elseif ( new_tree == nil ) then
			return tree
		end
		tree = new_tree
	end
	
	return tree
	
end

function p.addLevel ( tree )

	local result = {}
	
	for cat,rest in pairs( tree ) do
		
		if ( rest == "" ) then
			if ( p.seen[cat] ) then
				-- TODO: i18n, getSiteLink from Q1281
				if ( cat == "Categoria:Principal" ) then
					result[cat] = {}
				else
					result[cat] = { ["..."] = {} }
					p.branchcount = p.branchcount + 1
				end
			else
				p.count = p.count + 1
				if ( p.count > p.MAX ) then
					p.probleem( "M" )
					return nil
				end
				page = mw.title.new( cat )
				local cats = parseCategories( page:getContent() )
				result[cat] = cats
				p.seen[cat] = true
			end
		else
			local temp = p.addLevel( rest )
			if ( temp == nil ) then
				return nil
			else
				result[cat] = temp
			end
		end
	end
	
	return result
	
end


--[[

	createBreadCrumb( title, trees, txtsize )

	Creates a breadcrumb navigation for trees.
	title: page from which we generate the breadcrumb
	trees: category trees as created by createCategoryTrees (title)
	txtsize: font size

]]
function p.createBreadCrumb( title, tree, txtsize )
	
	tree = tree or {}
	local str = ""
	
	if ( p.verklein ) then
		str = str .. '<div style="font-size: ' .. math.floor( txtsize/2 ) .. 'pt">'
	end
	
	str = str .. p.table .. '<tr><td align="right">'
	
	local count = 0
	
	for ntitle, ntree in pairs( tree ) do
		count = count + 1
		if ( ntitle == "..." ) then
			str = str .. p.wolkje
		else
			if ( type( ntree ) == "table" ) then
				str = str .. p.createBreadCrumb( ntitle, ntree, txtsize-1 ) 
			else
				str = str .. p.createBreadCrumb( ntitle, {}, txtsize-1 ) 
			end
		end
	end
	
	str = str .. "</td><td>"
	
	if ( count > 1 ) then
		str = str .. p.curly .. '</td><td align="right">'
	end
	if ( count > 0 ) then
		str = str .. '</td><td>&nbsp;&rarr;&nbsp;</td><td>'
	end
	
	str = str .. "[[:" .. title .. "|" .. p.removeCatNS( title ) .. "]]"
	str = str .. "</td></tr></table>"
	
	if ( p.verklein ) then str = str .. "</div>" end

	return str
	
end

--[[
	cleanupCatTitle( title )

	Standardizes a category name:
		superfluous spaces at the beginning or end,
		replace underscores with spaces,
		uppercase first letter, canonical namespace.
]]
function p.cleanupCatTitle( title )
	
	title = mw.text.trim( title )
	title = p.removeCatNS( title )
	title = mw.ustring.gsub( title, "_", " " )
	title = mw.language.getContentLanguage():ucfirst( title )
	title = "Category:" .. title
	
	return title
	
end

--[[
	removeCatNS( title )

	Removes "Categoria:" or "Category:"
]]
function p.removeCatNS( title )
	-- TODO: i18n
	title = mw.ustring.gsub( title, "^[Cc][Aa][Tt][Ee][Gg][Oo][Rr][Yy]%s*:%s*", "" )
	title = mw.ustring.gsub( title, "^[Cc][Aa][Tt][Ee][Gg][Oo][Rr][Ii][Aa]%s*:%s*", "" )
	return title
end

--[[
	printCats( cats )

	Print the categories in cats.
	If we can't make breadcrumbs, simply show the cats directly above.
]]
function p.printcats( cats )
	
	local str = ""
	
	str = str .. "<div style='text-align: left;'>&nbsp;Categories: " -- TODO: i18n
	
	if ( ( type( cats ) ~= "table" ) or next( cats ) == nil ) then
		str= str .. "''No''"
	else
		for cat in pairs( cats ) do
			str = str .. '[[:' .. cat .. '|' .. p.removeCatNS( cat ) .. ']]'
			if ( next( cats, cat ) ~= nil ) then
				str = str .. " • "
			end
		end
	end
	
	str = str .. '</div>'
	
	return str
	
end

function p.printprobleem( )
	
	if ( p.problemen ~= nil and p.problemen ~= "" ) then
		return "[[Category:" .. p.probleemcat .. "|" .. p.problemen .. "]]"
	else
		return ""
	end
end

function p.probleem( letter )
	p.problemen = letter
end

--[[
	pptable( table )

	Creates a string from a table created by createCategoryTrees ().
	Only for testing and debugging.
]]
function p.pptable( table ) 
	
	local str = ""
        table = table or {}

	for k,v in pairs( table ) do
		str = str .. k .. ": "
		if ( type( v ) == "string" ) then
		else
			str = str .. "{" .. p.pptable(v) .. "}, "
		end
		
	end
	
	return str
end

-- en:Module:category_tree
-- Show a table of contents with links to each letter in the language's script.
function p.show_TOC(frame)
	local args = frame:getParent().args
	local toc_code = args.toc
	if toc_code == nil or toc_code == "" then return end
	
	local titleText = mw.title.getCurrentTitle().text
	
	local inCategoryPages = mw.site.stats.pagesInCategory(titleText, "pages")
	local inCategorySubcats = mw.site.stats.pagesInCategory(titleText, "subcats")
	
	local TOC_type
	
	-- Compute type of table of contents required.
	if inCategoryPages > 2500 or inCategorySubcats > 2500 then
		TOC_type = "ampliat"
	elseif inCategoryPages > 200 or inCategorySubcats > 200 then
		TOC_type = "normal"
	else
		-- No (usual) need for a TOC if all pages or subcategories can fit on one page;
		-- but allow this to be overridden by a custom TOC handler.
		TOC_type = "none"
	end
	
	local ret
	if TOC_type ~= "none" then
		local templatename = "Template:" .. toc_code .. "-categoria"
		
		local TOC_template
		if TOC_type == "ampliat" then
			-- This category is very large, see if there is a "full" version of the TOC.
			local TOC_template_full = mw.title.new(templatename .. "/ampliat")
			
			if TOC_template_full.exists then
				TOC_template = TOC_template_full
			end
		end
		
		if not TOC_template then
			local TOC_template_normal = mw.title.new(templatename)
			if TOC_template_normal.exists then
				TOC_template = TOC_template_normal
			end
		end
		
		if TOC_template then
			ret = frame:expandTemplate{title = TOC_template, args = {}}
		end
	end
	
	-- If we have a lang code then trigger catfix
	local lang = args.lang or toc_code
	if require("Module:llengua").existeix(lang) then
		ret = (ret or "") .. require("Module:utilitats").catfix(lang, args.sc)
	end
	
	return ret
end

return p