Модуль:MetaCatDoc

Материал из wikixw
Перейти к навигации Перейти к поиску

Модуль для создания страницы документации шаблонов, использующих модули

Модуль генерирует список категорий и список шаблонов. Используется в шаблонах {{year-doc}}, {{decade-doc}} и {{century-doc}}.

Пример: {{#invoke:MetaCatDoc|main}} в шаблоне «{{Архитектура по векам}}» покажет

Ошибка Lua на строке 16: bad argument #1 to 'gsub' (string expected, got nil).


---*- mode: lua; coding: utf-8; -*-

local p = {}

local title = mw.title.getCurrentTitle().text
local getArgs = require('Module:Arguments').getArgs
local toroman = require('Module:Roman').convert

local ft = string.format

function p.main(frame)
    local args = getArgs(frame)
    title = args['title'] or title
    local templ = string.gsub(title, '/doc$', '') -- rm /doc
    local text = mw.title.new(templ,10):getContent()
    text = string.gsub(text, '<noinclude>.-</noinclude>', '')
    text = string.gsub(text, '<!--.--->', '')
    if string.find(text, 'YearMetaCat2') then
        mn = 'YearMetaCat2'
    elseif string.find(text, 'DecadeMetaCat') then
        mn = 'DecadeMetaCat'
    elseif string.find(text, 'CenturyMetaCat') then
        mn = 'CenturyMetaCat'
    else
        error('не найден модуль (YearMetaCat2/DecadeMetaCat/CenturyMetaCat)')
    end

    local ret = ';Добавляет категории:\n'
    local cats = string.gsub(text, '.-{{#invoke:'..mn..'|main(.-)}}.*', '%1')
    for _, c in ipairs(mw.text.split(cats, '|', true)) do
        if c ~= '' and not string.find(c, '=') then
            local cc = mw.text.split(c, '!', true)
            local c1 = mw.text.trim(cc[1])
            if mn == 'CenturyMetaCat' then
                local c3 = mw.text.trim(cc[3] or '')
                local c4 = mw.text.trim(cc[4] or '')
                local ss
                if c3 ~= '' and c4 ~= '' then
                    c3 = tonumber(c3)
                    c4 = tonumber(c4)
                    if c3 < 0 and c4 < 0 then
                        ss = ft('с %s по %s века до н. э. «%s»', toroman(-c3), toroman(-c4), c1)
                    elseif c3 < 0 and c4 > 0 then
                        ss = ft('с %s века до н. э. по %s век «%s»', toroman(-c3), toroman(c4), c1)
                    else
                        ss = ft('с %s по %s века «%s»', toroman(c3), toroman(c4), c1)
                    end
                elseif c3 ~= '' then
                    c3 = tonumber(c3)
                    if c3 < 0 then
                        ss = ft('с %s века до н. э. «%s»', toroman(-c3), c1)
                    else
                        ss = ft('с %s века «%s»', toroman(c3), c1)
                    end
                elseif c4 ~= '' then
                    c4 = tonumber(c4)
                    if c4 < 0 then
                        ss = ft('до %s века до н. э. «%s»', toroman(-c4), c1)
                    else
                        ss = ft('до %s века «%s»', toroman(c4), c1)
                    end
                else
                    ss = c1
                end
                ret = ret..'* '..ss..'\n'
            else -- not century
                ret = ret..'* '..c1..'\n'
            end
        end
    end

    local templs = ''
    local n = 0
    for t in string.gmatch(text, '{{[^#].-}}') do
        t = mw.text.trim(string.sub(t, 3, -3))
        t = string.gsub(t, '{{#invoke:'..mn..'|expand|', '')
        n = n + 1
        local i = string.find(t, '|')
        if i then
            local tn = string.sub(t, 0, i-1)
            local a = string.sub(t, i)
            templs = templs..ft('* {{[[Ш:%s|%s]]%s}}\n', tn, tn, a)
        else
            templs = templs..ft('* {{[[Ш:%s|%s]]}}\n', t, t)
        end
    end
    if n == 1 then
        ret = ret..';Добавляет шаблон:\n'..templs
    elseif n > 1 then
        ret = ret..';Добавляет шаблоны:\n'..templs
    end

    return ret
end

return p