Модуль:YearMetaCat2

Википедия — ирекле энциклопедия мәғлүмәте

Для документации этого модуля может быть создана страница Модуль:YearMetaCat2/doc

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

local p = {}

-- переменные
local year -- год, положительное число
local BC   -- 0 == н.э. 1 == до н.э.
local templ -- строка-шаблон вида 'Мир в %s году%s'
local title = mw.title.getCurrentTitle().text

-- опции
local year_min = -40000 -- 0 == только н.э.
local year_max = 2100 -- XXI
local range = 5

-- экспортируемые функции
local getArgs = require('Module:Arguments').getArgs
local sparseIpairs = require('Module:TableTools').sparseIpairs
--local toroman = require('Module:Math').Roman
local toroman = require('Module:Roman').convert
local gsub = mw.ustring.gsub

local function get_templ(s)
    -- формируем строку-шаблон вида:
    -- 'Б. э. т. 99 йылда донъя' -> '%s йылда донъя%s'
    -- определяем BC 
    local t
    t, BC = gsub(s,  'Б%. э%. т%.[0-9]+ (йыл[да]?) ', '%%s %1%%s')
    local n = BC
    if BC ~= 1 then
        t, n = gsub(s, '[0-9]+ (йыл[да]?)', '%%s %1%%s')
    end
    if n ~= 1 then
        -- не найдено или найдено больше одного
        error('Год не найден')
    end
    -- в/во
    templ = gsub(t, '%%s йылда', ' %%s йылда')
end

local function get_year(t)
    _, _, year = mw.ustring.find(t, '([0-9]+) йыл')
    if not year then error('год не найден') end
    year = tonumber(year)
end

local function format(y, wiki)
    local bcs, t
    if y < 1 then
        y = 1 - y
        bcs = 'Б. э. т.'
        --t = tostring(c)..' Б. э. т.'
        t = '-'..y
    else
        bcs = ''
        t = y
    end
    local s
    if wiki then
        -- в/во
        local tt = templ
        if y == 2 then
            tt = gsub(templ, '%%s йылда', '%%s йылда')
        end
        s = string.format(tt, y, bcs)
        s = string.format('[[:К:%s|%s]]', s, t)
    else
        s = t
    end
    return s
end

local function navbox()
    local y
    y = year
    if BC == 1 then
        -- пропускаем 0
        -- 1 Б. э. т. y == 0, 2 Б. э. т. y == -1 и т.д.
        y = 1 - year
    end
    local wt = mw.html.create('table'):addClass('standard'):attr('align', 'center')
    local row = wt:tag('tr')
    local ystart
    if year_min < 1 then
        ystart = math.max(year_min+1, y - range)
    else
        ystart = math.max(year_min, y - range)
    end
    local yend = math.min(year_max, y + range) -- FIXME: Б. э. т.
    for i = ystart, yend do
        if i == 1 and i~= ystart then -- разд. Б. э. т./б. э.
            row:tag('th'):wikitext('')
        end
        if i == y then
            row:tag('th'):wikitext(format(i, false))
        else
            row:tag('td'):wikitext(format(i, true))
        end
    end
    return tostring(wt)
end

local function do_expand(s)
-- <год> - год без слова "год"
-- <ключ> - ключ сортировки, н.э. - номер года,
-- до н.э. - отрицательное число начиная с -99 (-99 == 1 год до н.э. -98 == 2 год до н.э. и т.д.)
-- <десятилетие> - десятилетие числом (без окончания -е/-х)
-- <век> - век римскими цифрами
    local d = math.floor(year/10)*10 -- десятилетие
    local c = toroman(math.floor((year-1)/100)+1) -- быуат
    --  
    if c == 'II' then
        s = gsub(s, '<век>та', '<век>та')
    end
    if BC == 1 then
        s = gsub(s, '<год> (йыл[да]?)', year..' Б. э. т. %1 ')
        s = gsub(s, '<ключ>', year-10000) -- ?
        s = gsub(s, '<десятилетие>(-[ех] йыл[дар][ҙа]?)', d..'Б. э. т. %1 ') -- йылдар/йылдарҙа/годах
        s = gsub(s, '<век> (быуат[та]?)', c..' Б. э. т.  %1 ')
    else
        s = gsub(s, '<год>', year)
        s = gsub(s, '<ключ>', year)
        s = gsub(s, '<десятилетие>', d)
        s = gsub(s, '<век>', c)
    end
    return s
end

local function cats(args)
    local y, t
    local ret = ''
    for _, y in sparseIpairs(args) do
        t = mw.text.split(y, '!', true)
        -- в/во
        local tt = t[1]
        if year == 2 then
            tt = gsub(tt, '<год> йылда', '<год> йылда')
        end
        if t[2] and t[2] ~= '' then
            ret = ret .. string.format('[[К:%s|%s]]', tt, t[2])
        else
            ret = ret .. string.format('[[К:%s]]', tt)
        end
    end
    return do_expand(ret)
end

function p.main(frame)
    local args = getArgs(frame)
    title = args['title'] or title
    -- разбор аргументов
    range = tonumber(args['range'] or 5)
    year_min = tonumber(args['min'] or -40000)
    year_max = tonumber(args['max'] or 2100)
    -- нахождение текушего года
    get_year(title)
    -- создание шаблона-строки
    get_templ(title)
    -- создание навбокса и категорий
    return navbox(title) .. cats(args)
end

function p.expand(frame)
    local args = getArgs(frame)
    title = args['title'] or title
    get_year(title)
    BC = mw.ustring.find(title, '[0-9]+ год[ау]? до н%. э%.')
    if BC then
        BC = 1
    else
        BC = 0
    end
    -- в/во
    local tt = args[1]
    if year == 2 then
        tt = mw.ustring.gsub(args[1], '<год> йылда', '<год> йылда')
    end
    return do_expand(tt)
end

return p