Модул:AutomaticArchiveNavigator — разлика између измена

Iz Vojne Enciklopedije
Пређи на навигацију Пређи на претрагу
(+)
 
м (1 измена увезена)
 
(Нису приказане 2 међуизмене 2 корисника)
Ред 1: Ред 1:
-------------------------------------------------------------------------------
--                        AutomaticArchiveNavigator
--
--
-- This module produces a talk archive banner, together with an automatically-
-- This module implements {{Automatic archive navigator}}
-- generated list of navigation links to other archives of the talk page in
--
-- question. It implements {{Automatic archive navigator}} and
-- {{Talk archive navigation}}.
local p = {}
-------------------------------------------------------------------------------
 
local args
local yesno = require('Module:Yesno')
local frame
 
local thisPage
-------------------------------------------------------------------------------
-- Helper functions
-------------------------------------------------------------------------------
 
local function makeWikilink(page, display)
if display then
return string.format('[[%s|%s]]', page, display)
else
return string.format('[[%s]]', page)
end
end
 
local function escapePattern(s)
-- Escape punctuation in a string so it can be used in a Lua pattern.
s = mw.ustring.gsub(s, '%p', '%%%0')
return s
end
 
-------------------------------------------------------------------------------
-- Navigator class
-------------------------------------------------------------------------------
 
local Navigator = {}
Navigator.__index = Navigator
 
local zero        = false
local spacein    = false
local spaceinzero = false
 
function Navigator.new(args, cfg, currentTitle)
local obj = setmetatable({}, Navigator)
-- Set inputs
obj.args = args
obj.cfg = cfg
obj.currentTitle = currentTitle
 
-- Archive prefix
-- Decode HTML entities so users can enter things like "Archive " from
-- wikitext.
obj.archivePrefix = obj.args.prefix or obj:message('archive-prefix')
obj.archivePrefix = mw.text.decode(obj.archivePrefix)
 
-- Current archive number
do
local pattern = string.format(
'^%s?0?([1-9][0-9]*)$',
escapePattern(obj.archivePrefix)
)
local pattern1 = string.format(
'^0([1-9][0-9]*)$',
escapePattern(obj.archivePrefix)
)
local pattern2 = string.format(
'^%s([1-9][0-9]*)$',
escapePattern(obj.archivePrefix)
)
local pattern3 = string.format(
'^%s0([1-9][0-9]*)$',
escapePattern(obj.archivePrefix)
)
obj.currentArchiveNum = obj.currentTitle.subpageText:match(pattern)
obj.currentArchiveNum = tonumber(obj.currentArchiveNum)
if obj.currentTitle.subpageText:match(pattern1) then zero = true end
if obj.currentTitle.subpageText:match(pattern2) then spacein = true end
if obj.currentTitle.subpageText:match(pattern3) then spaceinzero = true end
end
-- Highest archive number
obj.highestArchiveNum = require('Module:Highest archive number')._main(
obj.currentTitle.nsText ..
':' ..
obj.currentTitle.baseText ..
'/' ..
obj.archivePrefix
)
 
return obj
end
 
function Navigator:message(key, ...)
local msg = self.cfg[key]
if select('#', ...) > 0 then
return mw.message.newRawMessage(msg, ...):plain()
else
return msg
end
end
 
function Navigator:makeBlurb()
local args = self.args
if args[1] == '1' then
-- The old template used "|1" to suppress the blurb.
return ''
else
local ret
if args.text then
ret = args.text
else
local talkPage = self.currentTitle.nsText ..
':' ..
self.currentTitle.baseText
if args.period then
ret = self:message('blurb-period', talkPage, args.period)
elseif args['период'] then
ret = self:message('blurb-period', talkPage, args['период'])
else
ret = self:message('blurb-noperiod', talkPage)
end
end
return ret
end
end


function Navigator:makeMessageBox()
-- Get a formatted link to the subpage a certain distance away, or nil
local args = self.args
-- if that subpage does not exist. e.g. If the current subpage is
-- /Archive 27, then getSubpageLink(3) returns a link to Archive 30.
local image
local function getSubpageLink(offset)
if args.image then
    local subpageName
image = args.image
    local startIdx, endIdx, archiveNum = mw.ustring.find(thisPage.subpageText, '^Archive ([0-9]+)')
else
    if archiveNum then
local icon = args.icon or self:message('default-icon')
        subpageName = 'Archive ' .. (archiveNum + offset)
image = string.format(
    elseif tonumber(thisPage.subpageText) then
'[[File:%s|%s|alt=|link=]]',
        subpageName = thisPage.subpageText + offset
icon,
    else
self:message('image-size')
        return nil  -- Couldn't parse out a subpage number
)
    end
end


local mbox = require('Module:Message box').main('tmbox', {
    local page = mw.title.new(thisPage.baseText .. '/' .. subpageName, thisPage.namespace)
image = image,
    if page.exists then
imageright = args.imageright,
        return '[[../' .. subpageName .. '|' .. subpageName .. ']]'
style = args.style or 'width:80%;margin-left:auto;margin-right:auto',
    else
textstyle = args.textstyle or 'text-align:center',
        return nil
text = self:makeBlurb()
    end
})
 
return mbox
end
end


function Navigator:getArchiveNums()
local function getLinksText()
-- Returns an array of the archive numbers to format.
    local arrowSpacer = '  '
local noLinks = tonumber(self.args.links) or self:message('default-link-count')
    local linkSpacer = '    '
noLinks = math.floor(noLinks)
    if mw.ustring.len(thisPage.subpageText) <= 4 then
-- If |noredlinks is "yes", true or absent, don't allow red links. If it is
        -- If page names are short, we want more space. But why the mix of regular, non-breaking, and em spaces?
-- 'no' or false, allow red links.
        local emSpace = frame:expandTemplate({title = 'Unicode', args = {mw.ustring.char(8195)}})
local allowRedLinks = yesno(self.args.noredlinks) == false
        arrowSpacer = '&nbsp; ' .. emSpace .. ' &nbsp;&nbsp;'
        linkSpacer = '&nbsp; ' .. emSpace .. ' &nbsp; ' .. emSpace .. '&nbsp;'
local current = self.currentArchiveNum
    end
local highest = self.highestArchiveNum
   
 
    local s = ''
if not current or not highest or noLinks < 1 then
   
return {}
    for offset = -5, -3 do
elseif noLinks == 1 then
        local link = getSubpageLink(offset)
return {current}
        if link then
end
            if offset == -3 then
 
                s = s .. link .. linkSpacer
local function getNum(i, current)
            else
-- Gets an archive number given i, the position in the array away from
                s = s .. link .. ' ←' .. arrowSpacer
-- the current archive, and the current archive number. The first two
            end
-- offsets are consecutive; the third offset is rounded up to the
            break
-- nearest 5; and the fourth and subsequent offsets are rounded up to
        end
-- the nearest 10. The offsets are calculated in such a way that archive
    end
-- numbers will not be duplicated.
   
if -2 <= i and i <= 2 then
    for offset = -2, -1 do
return current + i
        local link = getSubpageLink(offset)
elseif -3 <= i and i <= 3 then
        if link then
return current + 2 - (current + 2) % 5 + (i / 3) * 5
            s = s .. link .. linkSpacer
elseif 4 <= i then
        end
return current + 7 - (current + 7) % 10 + (i - 3) * 10
    end
else
return current + 2 - (current + 2) % 10 + (i + 3) * 10
end
end
 
local nums = {}
 
-- Archive nums lower than the current page.
for i = -1, -math.floor((noLinks - 1) / 2), -1 do
local num = getNum(i, current)
if num <= 1 then
table.insert(nums, 1, 1)
break
else
table.insert(nums, 1, num)
end
end


-- Current page.
    s = s .. '<span style="font-size:115%;">[[' .. thisPage.fullText .. '|' .. thisPage.subpageText .. ']]</span>'
if nums[#nums] < current then
   
table.insert(nums, current)
    for offset = 1, 2 do
end
        local link = getSubpageLink(offset)
        if link then
            s = s .. linkSpacer .. link
        end
    end
   
    for offset = 5, 3, -1 do
        local link = getSubpageLink(offset)
        if link then
            if offset == 3 then
                s = s .. linkSpacer .. link
            else
                s = s .. arrowSpacer .. '→ ' .. link
            end
            break
        end
    end


-- Higher archive nums.
    return s
for i = 1, math.ceil((noLinks - 1) / 2) do
local num = getNum(i, current)
if num <= highest then
table.insert(nums, num)
elseif allowRedLinks and (i <= 2 or i <= 3 and num == nums[#nums] + 1) then
-- Only insert one red link, and only if it is consecutive.
table.insert(nums, highest + 1)
break
elseif nums[#nums] < highest then
-- Insert the highest archive number if it isn't already there.
table.insert(nums, highest)
break
else
break
end
end
 
return nums
end
end


function Navigator:makeArchiveLinksWikitable()
local function getMessage()
local lang = mw.language.getContentLanguage()
    if args[1] == '1' then
local nums = self:getArchiveNums()
        return ''
local noLinks = #nums
    else
if noLinks < 1 then
        local msg = '----\n'
return ''
        if args.text then
end
            msg = msg .. args.text
 
        else
-- Make the table of links.
            msg = msg .. "Ова страна је '''[[Help:Archiving a talk page|архива]]''' ранијих расправа"
local links = {}
            if args.period then
local isCompact = noLinks > 7
                msg = msg .. "&#32;for the period '''" .. args.period .. "'''"
local currentIndex
            end
for i, num in ipairs(nums) do
            msg = msg .. ". '''Не мењајте садржај ове странице.''' Ако желите да покренете нову расправу или оживите стару урадите то на "
local subpage
            msg = msg .. "[[" .. thisPage.rootPageTitle.fullText .. "|актуелној страници за разговор]]."
if spaceinzero == true or zero == true then
        end
subpage = self.archivePrefix .. '0' .. tostring(num)
        return msg
else
    end
subpage = self.archivePrefix .. tostring(num)
end
local display
if isCompact then
display = tostring(num)
else
display = self:message('archive-link-display', num)
end
local link = makeWikilink('../' .. subpage, display)
if num == self.currentArchiveNum then
link = string.format('<span style="font-size:115%%;">%s</span>', link)
currentIndex = i
end
table.insert(links, link)
end
 
-- Add the arrows.
-- We must do the forwards arrow first as we are adding elements to the
-- links table. If we did the backwards arrow first the index for the
-- current archive would be wrong.
currentIndex = currentIndex or math.ceil(#links / 2)
for i = currentIndex + 1, #links do
if nums[i] - nums[i - 1] > 1 then
table.insert(links, i, lang:getArrow('forwards'))
break
end
end
for i = currentIndex - 1, 1, -1 do
if nums[i + 1] - nums[i] > 1 then
table.insert(links, i + 1, lang:getArrow('backwards'))
break
end
end
 
-- Output the wikitable.
local ret = {}
local width
if noLinks <= 3 then
width = string.format('%dem', noLinks * 10)
elseif noLinks <= 7 then
width = string.format('%dem', (noLinks + 3) * 5)
else
width = '50em'
end
ret[#ret + 1] = string.format(
'{| style="width:%s;background:transparent;' ..
'margin:0 auto 0.5em;text-align:center"',
width
)
for i, s in ipairs(links) do
if i % 20 == 1 then
ret[#ret + 1] = '\n|-'
end
ret[#ret + 1] = '\n| '
ret[#ret + 1] = s
end
ret[#ret + 1] = '\n|}'
return table.concat(ret)
end
end


function Navigator:__tostring()
local function _aan()
return self:makeMessageBox() ..
    frame = mw.getCurrentFrame()
'\n' ..  
self:makeArchiveLinksWikitable() ..  
    -- For testing purposes, allow passing in the page name as a param.
' __NONEWSECTIONLINK__ __NOEDITSECTION__'
    if args.title then
        thisPage = mw.title.new(args.title)
    else
        thisPage = mw.title.getCurrentTitle()
    end
   
    local image = args.image
    if not image then
        image = '[[File:' .. (args.icon or 'Replacement filing cabinet.svg') .. '|40x40px|alt=|link=]]'
    end
   
    local mbox = frame:expandTemplate({title = 'tmbox', args = {
        image = image,
        imageright = args.imageright,
        style = args.style or 'width:80%;margin-left:auto;margin-right:auto;',
        textstyle = args.textstyle or 'text-align:center;',
        text = getLinksText() .. '\n' .. getMessage()
    }})
   
    return mbox .. '__NONEWSECTIONLINK__ __NOEDITSECTION__'
end
end


-------------------------------------------------------------------------------
function p.aan(frame)
-- Exports
    local origArgs
-------------------------------------------------------------------------------
    -- If called via #invoke, use the args passed into the invoking template.
 
    -- Otherwise, for testing purposes, assume args are being passed directly in.
local p = {}
    if frame == mw.getCurrentFrame() then
 
        origArgs = frame:getParent().args
function p._aan(args, cfg, currentTitle)
    else
cfg = cfg or mw.loadData('Module:AutomaticArchiveNavigator/config')
        origArgs = frame
currentTitle = currentTitle or mw.title.getCurrentTitle()
    end
local aan = Navigator.new(args, cfg, currentTitle)
return tostring(aan)
    -- ParserFunctions considers the empty string to be false, so to preserve the previous
    -- template behavior, change any empty arguments to nil, so Lua will consider
    -- them false too.
    args = {}
    for k, v in pairs(origArgs) do
        if v ~= '' then
            args[k] = v
        end
    end
   
    return _aan()
end
end
 
function p._tan(args)
args.links = args.links or 3
args.noredlinks = args.noredlinks or false
return p._aan(args)
end
 
function p._exportClasses()
return {
Navigator = Navigator
}
end
 
setmetatable(p, {__index = function(t, k)
return function(frame)
local args = require('Module:Arguments').getArgs(frame, {
wrappers = {
'Template:Automatic archive navigator',
'Template:Talk archive navigation'
}
})
return p['_' .. k](args)
end
end})
 
return p
return p

Тренутна верзија на датум 5. фебруар 2021. у 13:49

Документација модула

--
-- This module implements {{Automatic archive navigator}}
--
 
local p = {}
 
local args
local frame
local thisPage

-- Get a formatted link to the subpage a certain distance away, or nil
-- if that subpage does not exist. e.g. If the current subpage is
-- /Archive 27, then getSubpageLink(3) returns a link to Archive 30.
local function getSubpageLink(offset)
    local subpageName
    local startIdx, endIdx, archiveNum = mw.ustring.find(thisPage.subpageText, '^Archive ([0-9]+)')
    if archiveNum then
        subpageName = 'Archive ' .. (archiveNum + offset)
    elseif tonumber(thisPage.subpageText) then
        subpageName = thisPage.subpageText + offset
    else
        return nil  -- Couldn't parse out a subpage number
    end

    local page = mw.title.new(thisPage.baseText .. '/' .. subpageName, thisPage.namespace)
    if page.exists then
        return '[[../' .. subpageName .. '|' .. subpageName .. ']]'
    else
        return nil
    end
end

local function getLinksText()
    local arrowSpacer = '&nbsp;&nbsp;'
    local linkSpacer = '&nbsp;&nbsp;&nbsp;&nbsp;'
    if mw.ustring.len(thisPage.subpageText) <= 4 then
        -- If page names are short, we want more space. But why the mix of regular, non-breaking, and em spaces?
        local emSpace = frame:expandTemplate({title = 'Unicode', args = {mw.ustring.char(8195)}})
        arrowSpacer = '&nbsp; ' .. emSpace .. ' &nbsp;&nbsp;'
        linkSpacer = '&nbsp; ' .. emSpace .. ' &nbsp; ' .. emSpace .. '&nbsp;'
    end
    
    local s = ''
    
    for offset = -5, -3 do
        local link = getSubpageLink(offset)
        if link then
            if offset == -3 then
                s = s .. link .. linkSpacer
            else
                s = s .. link .. ' ←' .. arrowSpacer
            end
            break
        end
    end
    
    for offset = -2, -1 do
        local link = getSubpageLink(offset)
        if link then
            s = s .. link .. linkSpacer
        end
    end

    s = s .. '<span style="font-size:115%;">[[' .. thisPage.fullText .. '|' .. thisPage.subpageText .. ']]</span>'
    
    for offset = 1, 2 do
        local link = getSubpageLink(offset)
        if link then
            s = s .. linkSpacer .. link
        end
    end
    
    for offset = 5, 3, -1 do
        local link = getSubpageLink(offset)
        if link then
            if offset == 3 then
                s = s .. linkSpacer .. link
            else
                s = s .. arrowSpacer .. '→ ' .. link
            end
            break
        end
    end

    return s
end

local function getMessage()
    if args[1] == '1' then
        return ''
    else
        local msg = '----\n'
        if args.text then
            msg = msg .. args.text
        else
            msg = msg .. "Ова страна је '''[[Help:Archiving a talk page|архива]]''' ранијих расправа"
            if args.period then
                msg = msg .. "&#32;for the period '''" .. args.period .. "'''"
            end
            msg = msg .. ". '''Не мењајте садржај ове странице.''' Ако желите да покренете нову расправу или оживите стару урадите то на "
            msg = msg .. "[[" .. thisPage.rootPageTitle.fullText .. "|актуелној страници за разговор]]."
        end
        return msg
    end
end

local function _aan()
    frame = mw.getCurrentFrame()
 
    -- For testing purposes, allow passing in the page name as a param.
    if args.title then
        thisPage = mw.title.new(args.title)
    else
        thisPage = mw.title.getCurrentTitle()
    end
    
    local image = args.image
    if not image then
        image = '[[File:' .. (args.icon or 'Replacement filing cabinet.svg') .. '|40x40px|alt=|link=]]'
    end
    
    local mbox = frame:expandTemplate({title = 'tmbox', args = {
        image = image,
        imageright = args.imageright,
        style = args.style or 'width:80%;margin-left:auto;margin-right:auto;',
        textstyle = args.textstyle or 'text-align:center;',
        text = getLinksText() .. '\n' .. getMessage()
    }})
    
    return mbox .. '__NONEWSECTIONLINK__ __NOEDITSECTION__'
end

function p.aan(frame)
    local origArgs
    -- If called via #invoke, use the args passed into the invoking template.
    -- Otherwise, for testing purposes, assume args are being passed directly in.
    if frame == mw.getCurrentFrame() then
        origArgs = frame:getParent().args
    else
        origArgs = frame
    end
 
    -- ParserFunctions considers the empty string to be false, so to preserve the previous 
    -- template behavior, change any empty arguments to nil, so Lua will consider
    -- them false too.
    args = {}
    for k, v in pairs(origArgs) do
        if v ~= '' then
            args[k] = v
        end
    end
    
    return _aan()
end
 
return p