Пређи на садржај

Модул:Subject bar

Iz Vojne Enciklopedije

Документацију овог модула можете да направите на страници Модул:Subject bar/док

local getPortalImage = require('Module:Portal').image

local p = {}

local function getArgNums(prefix, args)
    -- Returns a table containing the numbers of the arguments that exist for the specified prefix. For example, if the
    -- prefix was 'data', and 'data1', 'data2', and 'data5' exist, it would return {1, 2, 5}.
    local nums = {}
    for k, v in pairs(args) do
        local num = tostring(k):match('^' .. prefix .. '([1-9]%d*)$')
        if num then table.insert(nums, tonumber(num)) end
    end
    table.sort(nums)
    return nums
end

local function makeHorizontalRule()
    local row = mw.html.create('tr')
    row
        :tag('td')
            :attr('colspan', '2')
            :tag('hr', {selfClosing = true})
    return tostring(row)
end

local function makeItem(image, text)
    local root = mw.html.create('li')
    root
        :css('float', 'left')
        :css('margin-left', '0.3em')
        :css('height', '3.6em')
        :tag('span')
            :css('display', 'inline-block')
            :css('margin-right', '0.3em')
            :css('width', '30px')
            :css('line-height', '3.6em')
            :css('text-align', 'center')
            :wikitext(image)
            :done()
        :tag('span')
            :css('display', 'inline-block')
            :css('width', '11em')
            :css('vertical-align', 'middle')
            :wikitext(text)
    return tostring(root)
end

local function makeRow(items, heading, subheading, options)
    if #items < 1 then return end
    local swapHeadingSize = type(options) == 'table' and options.swapHeadingSize or false
    local row = mw.html.create('tr')
    row
        :tag('td')
            :css('width', '175px')
            :tag('span')
                :css('font-size', swapHeadingSize and '90%' or '125%')
                :wikitext(heading)
                :done()
            :tag('br', {selfClosing = true})
                :done()
            :tag('span')
                :css('font-size', swapHeadingSize and '125%' or '90%')
                :wikitext(subheading)
    local list = row:tag('td'):css('text-align', 'left'):tag('ul')
    for i, item in ipairs(items) do
        local image = item[1]
        local text = item[2]
        list
            :wikitext(makeItem(image, text))
    end
    return tostring(row)
end

local function makeNumberedRow(prefix, args, heading, subheading, getItemValsFunc, options)
    if args[prefix] then
        args[prefix .. '1'] = args[prefix]
    end
    local argNums = getArgNums(prefix, args)
    local items = {}
    for i, argNum in ipairs(argNums) do
        local image, text = getItemValsFunc(args[prefix .. tostring(argNum)])
        table.insert(items, {image, text})
    end
    return makeRow(items, heading, subheading, options)
end

function p._main(args)
    local rows = {}

    -- Get the book row text.
    local bookHeading = '<span style="font-weight:bold;">[[Википедија:Књиге|Књиге]]</span>'
    local bookSubheading = 'Погледајте&nbsp;колекцију&nbsp;чланака'
    local function getBookItemVals(book)
        local image = '[[Датотека:Office-book.svg|30px|alt=|link=]]'
        local text = mw.ustring.format('<span style="font-weight:bold;font-style:italic;">[[Књига:%s|%s]]</span>', book, book)
        return image, text
    end
    local bookRow = makeNumberedRow('book', args, bookHeading, bookSubheading, getBookItemVals)
    table.insert(rows, bookRow)

    -- Get the portal row text
    local portalHeading = '<span style="font-weight:bold;">[[:Категорија:Портали|Портали]]</span>'
    local portalSubheading = 'Приступ&nbsp;везаним&nbsp;темама'
    local function getPortalItemVals(portal)
        local image = mw.ustring.format('[[Датотека:%s|30x30px]]', getPortalImage{portal})
        local text = mw.ustring.format('<span style="font-weight:bold;">[[Портал:%s|Портал<br /><span style="white-space:nowrap;">%s</span>]]</span>', portal, portal)
        return image, text
    end
    local portalRow = makeNumberedRow('portal', args, portalHeading, portalSubheading, getPortalItemVals)
    table.insert(rows, portalRow)

    -- Get the sister projects row text.
    local sisters = {
        {arg = 'commons', image = 'Commons-logo-2.svg', prefix = 'c', display = 'Медији', from = 'Остави'},
        {arg = 'wikt', image = 'Wiktionary-logo-sr.png', imagesize = '32x32px', prefix = 'wikt', postfix = ' (Српски)', display = 'Дефиниције', from = 'Викиречнику'},
        {arg = 'n', image = 'Wikinews animated 2.gif', imagesize = '46x46px', prefix = 'n', display = 'Вести', from = 'Викивестима'},
        {arg = 'q', image = 'Wikiquote-logo-2.svg', imagesize = '28x28px', prefix = 'q', display = 'Цитати', from = 'Викицитату'},
        {arg = 's', image = 'Wikisource-logo.svg', imagesize = '26x26px', prefix = 's', display = 'Текстови', from = 'Викизворнику'},
        {arg = 'b', image = 'Wikibooks-logo.svg', imagesize = '25x25px', prefix = 'b', display = 'Литература', from = 'Викикњигама'},
        {arg = 'cook', image = 'Cookery wikibooks sr.wikipedia.gif', imagesize = '25x25px', prefix = 'b', display = 'Рецепти', from = 'Викикувару'},
        {arg = 'voy', image = 'Wikivoyage-Logo-v3-icon.svg', prefix = ':en:voy', display = 'Водич', from = 'Википутовању'},
        {arg = 'v', image = 'Wikiversity-logo.svg', prefix = ':en:v', display = 'Ресурси', from = 'Викиверзитету'},
        {arg = 'd', image = 'Animated Wikidata logo.gif', imagesize = '34x34px', prefix = 'd', display = 'Подаци', from = 'Википодацима'},
        {arg = 'species', image = 'Wikispecies-logo.svg', prefix = 'species', display = 'Таксономија', from = 'Викиврстама'},
        {arg = 'm', image = 'Wikimedia Community Logo.svg', imagesize = '24x24px', prefix = 'm', display = 'Садржаји', from = 'Мети'},
        {arg = 'mw', image = 'MediaWiki-logo.svg', prefix = 'mw', display = 'Документација', from = 'Медијавикију'},
        {arg = 'spoken', image = 'Sound-icon.svg', prefix = 'spoken wikipedia', display = 'Аудио', from = '<span style="white-space:nowrap">Аудио-Википедији</span>'},
    }
    local sisterItems = {}
    for i, t in ipairs(sisters) do
        if args[t.arg] then
            -- Get the image file name and image size values.
            local image = mw.ustring.format('[[Датотека:%s|30px|alt=|link=]]', t.image)
--            if t.imagesize ~= '' and t.imagesize ~= nil then
--            	 image = mw.ustring.format('[[Датотека:%s|%s|alt=|link=]]', t.image, t.imagesize)
--            end
            -- Get the text value.
            local prefix = t.prefix
            local search = args[t.arg .. '-search'] or mw.title.getCurrentTitle().text
            local postfix = t.postfix
            postfix = ''  -- postfix and ('#' .. search .. postfix)
            local display = t.display
            local from = t.from
            local text = mw.ustring.format(
                '[[%s:Special:Search/%s%s|%s]]<br />на&nbsp;%s',
                prefix,    search,    postfix, display, from
            )
            if t.arg == 'spoken' then
            	 text = mw.ustring.format('[[Датотека:%s|%s]]<br />на&nbsp;%s',
                				args[t.arg], display, from 
                )		
            end
            -- Add the values to the items table.
            table.insert(sisterItems, {image, text})
        end
    end
    local sisterHeading = "Још&nbsp;садржаја&nbsp;Викимедије"
    local sisterSubheading = '<span style="font-weight:bold;">[[Википедија:Братски пројекти|Братски&nbsp;пројекти]]</span>'
    local sisterRow = makeRow(sisterItems, sisterHeading, sisterSubheading, {swapHeadingSize = true})
    table.insert(rows, sisterRow)

    -- Make the table.
    local root = mw.html.create('table')
    root
        :attr('role', 'presentation')
        :addClass('noprint')
        :addClass('navbox')
        :addClass('metadata')
        :addClass('plainlist')
        :css('background-color', '#f9f9f9')
        :css('border', '1px solid #aaa')
        :css('clear', 'both')
        :css('margin-bottom', '0.5em')
        :css('margin-top', '0.5em')
        :wikitext(table.concat(rows, makeHorizontalRule()))

    return tostring(root)
end

function p.main(frame)
    -- If called via #invoke, use the args passed into the invoking template, or the args passed to #invoke if any exist. Otherwise
    -- assume args are being passed directly in from the debug console or from another Lua module.
    local origArgs
    if frame == mw.getCurrentFrame() then
        origArgs = frame:getParent().args
        for k, v in pairs(frame.args) do
            origArgs = frame.args
            break
        end
    else
        origArgs = frame
    end
    -- Remove blank arguments.
    local args = {}
    for k, v in pairs(origArgs) do
        if v ~= '' then
            args[k] = v
        end
    end
    return p._main(args)
end

return p