Відмінності між версіями «Модуль:Portal»

Матеріал з Вікіпедії — вільної енциклопедії.
Перейти до навігації Перейти до пошуку
[неперевірена версія][перевірена версія]
Рядок 59: Рядок 59:
 
:tag('td')
 
:tag('td')
 
:css('text-align', 'center')
 
:css('text-align', 'center')
:wikitext(string.format('[[File:%s|32x28px|alt=Portal icon|class=noviewer]]', image))
+
:wikitext('[[Файл:' .. imagename .. '|32x28px|alt=Значок порталу]]')
 
:done()
 
:done()
 
:tag('td')
 
:tag('td')
Рядок 66: Рядок 66:
 
:css('font-style', 'italic')
 
:css('font-style', 'italic')
 
:css('font-weight', 'bold')
 
:css('font-weight', 'bold')
:wikitext(string.format('[[Portal:%s|%s%sportal]]', portal, portal, args['break'] and '<br />' or ' '))
+
:wikitext('[[Портал:' .. v .. '|Портал' .. ((args['break'] == 'yes' and '<br />') or ' ') .. '«' .. v .. '»]]')
 
end
 
end
   

Версія за 09:09, 29 червня 2015

{{i}} Документація модуля[перегляд] [редагувати] [історія] [очистити кеш]

Модуль застарів, документація з англійської Вікіпедії, яка не відображає поточний стан в українській:


This module has two functions, portal and image. The portal produces a box with links to a portal or to multiple portals, and is used by the {{portal}} template. It is most often used in the "See also" section of an article. The image function produces the name of the image used by the specified portal.

Portal function

The portal function produces a box of portal links. Шаблон:Портал/Документація

Image function

The image function produces the name of the image used by the specified portal.

Використання

{{#invoke:Portal|image|portal}}

Example

  • {{#invoke:Portal|image|Мистецтво}} → Mr. Smiley Face.svg

Image dupes function

The image dupes function returns a list of all images that are being used by more than one portal (aliases are not included). This can be helpful in identifying image entries that should be changed to use aliases.

Використання

{{#invoke:Portal|imageDupes}}

Display all function

The display all function returns a box containing all portals that have images. This is used for maintenance, and should not be displayed in articles, because a) there are around 1500 portals with images, and displaying 1500 images on one page takes up a lot of server resources, and b) the module has no way to know the correct capitalisation of a portal name, so some links to portals will be broken. This function can be seen at en:Template:Portal/doc/all (Шаблон:Портал/документація/all).

Використання

{{#invoke:Portal|displayAll}}

-- Модуль для реалізації {{Портал}}

local p = {}
 
local HtmlError = require('Модуль:Error')

-- This function generates the html code.
local function _portal(args)

	local root = mw.html.create('div')
		:addClass('noprint portal')
		:addClass(args.left and 'tleft' or 'tright')
		: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()

	-- Start the table. This corresponds to the start of the wikitext table in the old [[Template:Portal]].
	local tableroot = root:tag('table')
		:css('background', '#f9f9f9')
		:css('font-size', '85%')
		:css('line-height', '110%')
		:css('max-width', '175px')
		:css('width', type(args.boxsize) == 'string' and (args.boxsize .. 'px') or nil)
    
    -- If no portals have been specified, display an error and add the page to a tracking category.
	if not args[1] then
		tableroot:wikitext('<strong class="error">No portals specified: please specify at least one portal</strong>[[Category:Portal templates without a parameter]]')
	end

    -- Display the portals specified in the positional arguments.
    for i,v in ipairs(args) do
        v = mw.ustring.match(v, '^%s*(.*%S)') or ''  -- Trim whitespace.
        
        -- Portal image names are stored in subtemplates of [[Шаблон:Портал/Зображення]].
        -- The name of the subtemplate is the portal name in all lower case, but with
        -- the first character in upper case.
        
        -- Work out the image subtemplate location.
        local lang = mw.getContentLanguage()
        local imagetemplatename = 'Портал/Зображення/' .. 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, 'Шаблон')
        if not (goodtitlecall and imagetemplateobject and imagetemplateobject.exists) then
            imagetemplatename = 'Портал/Зображення/За замовчуванням'
        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')
				:css('vertical-align', 'middle')
				:tag('td')
					:css('text-align', 'center')
					:wikitext('[[Файл:' .. imagename .. '|32x28px|alt=Значок порталу]]')
					:done()
				:tag('td')
					:css('padding', '0 0.2em')
					:css('vertical-align', 'middle')
					:css('font-style', 'italic')
					:css('font-weight', 'bold')
					:wikitext('[[Портал:' .. v .. '|Портал' .. ((args['break'] == 'yes' and '<br />') or ' ') .. '«' .. v .. '»]]')
    end

    return tostring(root)
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