Modul:Portal3
Utseende
Moduldokumentasjon
Denne modulen implementerer {{Portal3}}. Se malens side for bruk.
- Modul:Portal3 • Modul:Portal3/sandkasse • forskjellig (diff)
-- Denne modulen implementerer {{Portal3}}
local p = {}
local HtmlBuilder = require('Module:HtmlBuilder')
local HtmlError = require('Module:Error')
-- Denne funksjonen genererer html-koden.
local function _portal(args)
local root = HtmlBuilder.create('div')
root
.addClass('noprint')
.addClass((args.left == 'yes' and 'tleft') or 'tright')
.addClass('portal')
.css('border', 'solid #aaa 1px')
.css('margin', args.margin or (args.left == 'yes' and '0.5em 1em 0.5em 0') or '0.5em 0 0.5em 1em')
.newline()
-- Starter tabellen. Dette tilsvarer starten av wikitekst-tabellen
-- i den gamle [[Mal:Portal3]].
local tableroot = root.tag('table')
.css('background', '#f9f9f9')
.css('font-size', '85%')
.css('line-height', '110%')
.css('max-width', '175px')
.css('width', args.boxsize and (args.boxsize .. 'px'))
-- Hvis ingen protaler er angitt, vises en feilmelding og siden legges i en sporingskategori.
if not args[1] then
tableroot.wikitext(
tostring(HtmlError.error{'Ingen portaler er spesifisert: vennligst angi minst én portal'})
.. '[[Kategori:Portalmaler uten parameter]]'
)
end
-- Viser portalene angitt i posisjonsargumentene.
for i,v in ipairs(args) do
v = mw.ustring.match(v, '^%s*(.*%S)') or '' -- Fjerner tomrom.
-- Portalbildene lagres på undersiden 'Module:Portal3/Data'.
-- Finn lokaliseringen av bildene.
local lang = mw.getContentLanguage()
local imagetemplatename = 'Portal3/Bilder/' .. lang:ucfirst(lang:lc(v))
-- Check the image template name. We need three checks: 1) check with pcall to see if
-- we are over the expensive function call limit; 2) check if the proposed image template
-- name uses invalid characters (mw.title.new returns nil if this is the case); and 3)
-- check if the image subtemplate exists.
local goodtitlecall, imagetemplateobject = pcall(mw.title.new, imagetemplatename, 'Mal')
if not (goodtitlecall and imagetemplateobject and imagetemplateobject.exists) then
imagetemplatename = 'Portal3/Bilder/Standard'
end
-- Expand the image subtemplate to get the image name
local imagename = mw.getCurrentFrame():expandTemplate{ title = imagetemplatename }
-- Generate the html for the image and the portal name.
tableroot
.newline()
.tag('tr')
.attr('valign', 'middle')
.tag('td')
.css('text-align', 'center')
.wikitext('[[Fil:' .. imagename .. '|32x28px|alt=Portalikon]]')
.done()
.tag('td')
.css('padding', '0 0.2em')
.css('vertical-align', 'middle')
.css('font-style', 'italic')
.css('font-weight', 'bold')
.wikitext('[[Portal:' .. v .. '|Portal:' .. v .. ((args['break'] == 'yes' and '<br />') or ' ') .. ']]')
end
return tostring(root)
end
-- Kontrollerer at parameternavnet er gyldig
function validate( name )
name = tostring( name );
-- Normale argumenter
if portalbilde.basic_arguments[ name ] then
return true;
end
-- Argumenter med tall i seg
name = name:gsub( "%d+", "#" );
if portalbilde.numbered_arguments[ name ] then
return true;
end
-- Finnes ikke, argumentet støttes ikke.
return false
end
-- This function gets the arguments passed to the module and passes them
-- to the _portal() function above.
function p.portal(frame)
local orig_args
if frame == mw.getCurrentFrame() then
-- We're being called via #invoke. If the invoking template passed any arguments,
-- use them. Otherwise, use the arguments that were passed into the template.
orig_args = frame:getParent().args
for k, v in pairs(frame.args) do
orig_args = frame.args
break
end
else
-- We're being called from another module or from the debug console, so assume
-- the arguments are passed in directly.
orig_args = frame
end
-- We want to list all the portals in the order they were passed to the template.
-- We also want to be able to deal with positional arguments passed explicitly,
-- for example {{portal|2=Politics}}. However, pairs() doesn't guarantee the correct
-- order, and ipairs() will stop after the first nil value. To get around this, we
-- create a new table of arguments where nil values have been removed, so that we
-- can traverse the numerical arguments using ipairs(). We also remove values which
-- only consist of whitespace. ParserFunctions considers these to be false, and by
-- removing them Lua will consider them false too.
local args = {} -- Arguments table.
local name_args = {} -- Temporary table for named arguments.
for k, v in pairs(orig_args) do
if mw.ustring.match(v, '%S') then -- Remove values that are only whitespace.
if type(k) == 'number' then
table.insert(args, k) -- Put positional argument keys into the arguments table so we can sort them.
else
-- Put named argument values in their own table while we sort the positional arguments,
-- so that we don't have to cycle through all the original arguments again.
name_args[k] = v
end
end
end
table.sort(args) -- Sort the positional argument keys into numerical order.
for i,v in ipairs(args) do
args[i] = orig_args[v] -- Replace positional argument keys with their corresponding values.
end
for k,v in pairs(name_args) do
args[k] = v -- Add named arguments to the args table
end
return _portal(args)
end
return p