Produceert broodkruimelnavigaties. Roep de module niet direct aan, maar gebruik het sjabloon {{Broodkruimel}}
.
Functie(s) van stukken code zijn beschreven in de module zelf.
Toepassingen[]
[bekijken | bewerken | geschiedenis | cache legen]Bovenstaande documentatie is getranscludeerd uit Module:Broodkruimel/doc.
-- Credits: Dutch Wikipedia (https://nl.wikipedia.org/wiki/Module:Broodkruimel)
-- Wikipedia is licensed under CC-BY-SA 3.0
-- All Dutch/English text may be translated if that's desired. Don't forget to translate everything, or else things will break!
local p = {}
-- Opening tag for all tables inside the breadcrumb
p.table = '<table cellspacing="1" cellpadding="0">'
-- Curly brackets behind branches
p.curly = '<big>}</big>'
-- Cloud thingy for categories that aren't included
p.wolkje = '(...)'
-- Maximum amount of categories before stopping script
p.MAX = 50
-- Category for pages where the breadcrumb trail has been eaten by birds (a.k.a. categories with breadcrumb problems)
p.probleemcat = 'Broodkruimelproblemen'
-- We keep track of issues. The issue type is assigned with a
-- letter which, in turn, is used as sort key for the category.
-- When there are multiple issues, the last letter will be used.
-- "C": Something's wrong with the text. Possibly a missing
-- closing tag etc.
-- "P": Issue with parameters, invalid parameter value given
-- "M": p.MAX has been reached, category tree isn't complete
-- "O": The page (or category) is uncategorised
p.problemen = ""
--[[
broodkruimel( frame )
Creates breadcrumb navigation for the categories of the page.
When frame.args[pagina] is given, that page will be used.
If not, and frame.args[1] is given, that page will be used.
If none of the above, the current page will be used.
]]
function p.broodkruimel( frame )
-- Which page?
local title = frame.args.pagina or frame.args[1]
if ( not title or title == "" ) then
title = mw.title.getCurrentTitle().prefixedText
end
-- Depth?
local level = 8
p.force = false
if ( frame.args.lengte and frame.args.lengte ~= "" ) then
n = tonumber( frame.args.lengte )
if ( n ~= nil and n > 0 ) then
level = n
if ( frame.args.forceer and frame.args.forceer ~= "" ) then
p.force = true
end
else
p.probleem( "P" )
end
end
-- Maximum amount of parallell 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
-- Resize text?
local txtsize = 20
p.verklein = false
if ( frame.args.startgrootte and frame.args.startgrootte ~= "" ) then
p.verklein = true
local n = tonumber( frame.args.startgrootte )
if ( n ~= nil and n > 0 ) then
txtsize = 2 * n
else
p.probleem( "P" )
end
end
-- List of ignoryness?
p.negeerlijst = {}
if ( frame.args.negeer and frame.args.negeer ~= "" ) then
p.negeerlijst = p.parseNegeerLijst( frame.args.negeer )
end
-- Create tree
local trees = p.createCategoryTree( title, level )
-- Create crumb
local ntitle, ntree = next( trees )
local kruimel = ""
if ( type( ntree ) == "table" and next( ntree ) ) then
kruimel = p.createBreadCrumb( ntitle, ntree, txtsize )
else
-- If it's impossible to create breadcrumbs because the page
-- is present in more categories than max_branch, we simply
-- print the categories above the page. We have to show something,
-- right?
kruimel = p.printcats( p.hoofdcats )
if ( p.hoofdcats == nil or next( p.hoofdcats ) == nil ) then
-- uncategorised page
p.probleem( "O" )
end
end
return kruimel .. p.printprobleem()
end
--[[
parseCategories( wikitext )
Searches (and finds) valid categories in text.
]]
function p.parseCategories( txt )
if ( txt == nil ) then return {} end
local cats = {}
local pattern = "%[%[%s*[Cc][Aa][Tt][Ee][Gg][Oo][Rr][YyIi][Ee]?%s*:%s*([^|%]]+)[|%]]"
for category in mw.ustring.gmatch( txt, pattern ) do
category = p.cleanupCatTitle( category )
-- Skip categories with stupid things, like templates.
if ( mw.ustring.find( category, "[{}%[%]]" ) == nill) then
-- If the name exceeds 100 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
-- The categories directly above the page are skipped and
-- will be used only when making a tree is impossible
p.hoofdcats = cats
end
return cats
end
--[[
createCategoryTree( title, maxlevel )
Create tree of categories above title, until the distance of
maxlevel. Stops if the tree has more branches than p.max_branch.
]]
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
-- Main category of the wiki, translate accordingly
if ( cat == "Categorie:Hoofd" ) 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 = p.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 )
Create breadcrumb navigation for trees.
title: title of the page where the breadcrumb is for
trees: category trees created by createCategoryTrees(title)
txtsize: speaks for itself, hopefully. If not: text 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> → </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 )
Standardises a category name: Remove excessive spaces at the beginning
and end, replace underscores with spaces, capitalise the first letter,
add 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 = "Categorie:" .. title
return title
end
--[[
removeCatNS( title )
Removes "Categorie:" or "Category:"
]]
function p.removeCatNS( title )
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][Ee]%s*:%s*", "" )
return title
end
--[[
parseNegeerLijst( str )
Parses a list of category names, seperated by commas
]]
function p.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
--[[
printCats( cats )
Prints categories in categories.
If makeing a breadcrumb list isn't possible, the function is used
to show the categories that are directly above the page.
]]
function p.printcats( cats )
local str = ""
str = str .. "<div style='text-align: left;'> Categorieën: "
if ( ( type( cats ) ~= "table" ) or next( cats ) == nil ) then
str= str .. "''Geen''"
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 "[[Categorie:" .. p.probleemcat .. "|" .. p.problemen .. "]]"
else
return ""
end
end
function p.probleem( letter )
p.problemen = letter
end
--[[
pptable( table )
Creates a string of a table generated 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
return p