Изменения

обновление
Строка 3: Строка 3:  
local getArgs = require('Module:Arguments').getArgs
 
local getArgs = require('Module:Arguments').getArgs
 
local yesno = require('Module:Yesno')
 
local yesno = require('Module:Yesno')
local lang = mw.getContentLanguage()
+
local mwlang = mw.getContentLanguage()
 
local err = "―" -- NthDay nil result
 
local err = "―" -- NthDay nil result
 +
local tCon = table.concat
    
-- 00) Блок многократно используемых списков
 
-- 00) Блок многократно используемых списков
Строка 14: Строка 15:  
local params = { {"г", "g"}, {"ю", "j"}}
 
local params = { {"г", "g"}, {"ю", "j"}}
 
local comment = { '<span style="border-bottom: 1px dotted; cursor: help" title="по юлианскому календарю">','</span>'}
 
local comment = { '<span style="border-bottom: 1px dotted; cursor: help" title="по юлианскому календарю">','</span>'}
local category = {["params"]="<!--[[Категория:Модуль:calendar:Страницы с некорректными параметрами]]-->"}
      
local known_tzs = {
 
local known_tzs = {
Строка 46: Строка 46:  
}
 
}
   −
local category_msg = ""
   
local category = {
 
local category = {
 +
["no_parameters"]=
 +
"<!--[[Категория:Модуль:Calendar:Страницы без параметров]]-->",
 
["incomplete_parameters"]=
 
["incomplete_parameters"]=
 
"<!--[[Категория:Модуль:Calendar:Страницы с неполными или некорректными параметрами]]-->",
 
"<!--[[Категория:Модуль:Calendar:Страницы с неполными или некорректными параметрами]]-->",
Строка 61: Строка 62:  
["ending"]=".</span>",
 
["ending"]=".</span>",
 
["no_pattern_match"]="строка «%s» не совпадает с заданными паттернами",
 
["no_pattern_match"]="строка «%s» не совпадает с заданными паттернами",
["no_valid_date"]="дата «%s·%s·%s» не является корректной",
+
["no_valid_date"]="дата «%s» не является корректной",
 
["wrong_jd"]="юлианская дата %s вне диапазона",
 
["wrong_jd"]="юлианская дата %s вне диапазона",
 +
["no data"]="нет входящих данных",
 
["too_many_arguments"]="ожидается менее %i аргументов",
 
["too_many_arguments"]="ожидается менее %i аргументов",
 
["too_little_arguments"]="ожидается более %i аргументов",
 
["too_little_arguments"]="ожидается более %i аргументов",
 
["wrong_calculation"]="даты %s и %s не прошли проверку, %s дней разница",
 
["wrong_calculation"]="даты %s и %s не прошли проверку, %s дней разница",
["unknown_calendar"]="параметр календаря %s неизвестен",
+
["unknown_param"]="параметр %s неизвестен",
 
["unknown_error"]="неизвестная ошибка",
 
["unknown_error"]="неизвестная ошибка",
 
["tech_error"]="ошибка в функции %s",
 
["tech_error"]="ошибка в функции %s",
 +
 
-- [""]="",
 
-- [""]="",
 
}
 
}
Строка 209: Строка 212:  
elseif type(tbl) ~= "table" then return tbl
 
elseif type(tbl) ~= "table" then return tbl
 
elseif (tbl.day or tbl.month or tbl.year) then
 
elseif (tbl.day or tbl.month or tbl.year) then
return "Y"..(tbl.year or "?").."•M"..(tbl.month or "?").."•D"..(tbl.day or "?")
+
return (tbl.year or "?").."-"..(tbl.month or "?").."-"..(tbl.day or "?")
 
else return (tbl[3] or "?").."-"..(tbl[2] or "?").."-"..(tbl[1] or "?")
 
else return (tbl[3] or "?").."-"..(tbl[2] or "?").."-"..(tbl[1] or "?")
 
end
 
end
Строка 351: Строка 354:  
----[[ УСТАРЕЛО ]]----
 
----[[ УСТАРЕЛО ]]----
 
local numstr2date = function(numstr)
 
local numstr2date = function(numstr)
local lang = mw.getContentLanguage()
   
local format = "Y-m-d"
 
local format = "Y-m-d"
local iso_date = lang:formatDate(format,numstr)
+
local iso_date = mwlang:formatDate(format,numstr)
 
     local y,m,d = string.match(iso_date, "(%d+)-(%d+)-(%d+)")
 
     local y,m,d = string.match(iso_date, "(%d+)-(%d+)-(%d+)")
 
local dateout = {["year"]=purif(y), ["month"]=purif(m), ["day"]=purif(d)}
 
local dateout = {["year"]=purif(y), ["month"]=purif(m), ["day"]=purif(d)}
Строка 373: Строка 375:  
--        dateout = {["year"]=nums[3], ["month"]=nums[2], ["day"]=nums[1]}
 
--        dateout = {["year"]=nums[3], ["month"]=nums[2], ["day"]=nums[1]}
 
--    else
 
--    else
-- local lang = mw.getContentLanguage()
+
-- local mwlang = mw.getContentLanguage()
-- implement lang:formatDate(format,datein,true) here
+
-- implement mwlang:formatDate(format,datein,true) here
 
--        return error("Не распознано " .. numstr .. " как дата")
 
--        return error("Не распознано " .. numstr .. " как дата")
 
--    end
 
--    end
Строка 424: Строка 426:  
local day = purif((d or "-"):match("(%d+)"))
 
local day = purif((d or "-"):match("(%d+)"))
 
if not month then  
 
if not month then  
msg = category["params"]
+
msg = category.incomplete_parameters
 
month = purif(month_to_num[string.lower(mw.ustring.match((d or ""),"(%a+)") or "-")])  
 
month = purif(month_to_num[string.lower(mw.ustring.match((d or ""),"(%a+)") or "-")])  
 
end
 
end
 
if (not day) and ((purif(string.match(m or "","(%d+)") or "") or 32) <= (monthd[month] or 31)) then  
 
if (not day) and ((purif(string.match(m or "","(%d+)") or "") or 32) <= (monthd[month] or 31)) then  
msg = category["params"]
+
msg = category.incomplete_parameters
 
day = purif(m:match("(%d+)") or "")  
 
day = purif(m:match("(%d+)") or "")  
 
end
 
end
 
if not year then  
 
if not year then  
msg = category["params"]
+
msg = category.incomplete_parameters
 
year = purif(string.match(m or "","(%d+)") or "")  
 
year = purif(string.match(m or "","(%d+)") or "")  
 
end
 
end
Строка 441: Строка 443:  
local function glue(d1,m1,y1,d2,m2,y2)
 
local function glue(d1,m1,y1,d2,m2,y2)
 
if (not d1) and (not m1) and (not y1) and (not d2) and (not m2) and (not y2) then
 
if (not d1) and (not m1) and (not y1) and (not d2) and (not m2) and (not y2) then
return category["params"] end
+
return category.incomplete_parameters end
 
local gd,gm,gy,jd,jm,jy =  
 
local gd,gm,gy,jd,jm,jy =  
 
(d1 or ""),
 
(d1 or ""),
Строка 454: Строка 456:  
return table.concat({comment[1],trim(trim(jd .. " " .. jm) .. " " .. jy ),
 
return table.concat({comment[1],trim(trim(jd .. " " .. jm) .. " " .. jy ),
 
comment[2]," ([[",trim(gd .. " " .. gm),"]]",gm_sep[1],(gy:match("(%d+)") or ""),
 
comment[2]," ([[",trim(gd .. " " .. gm),"]]",gm_sep[1],(gy:match("(%d+)") or ""),
gm_sep[2],gy,gm_sep[3],")",category["params"]})
+
gm_sep[2],gy,gm_sep[3],")",category.incomplete_parameters})
 
end
 
end
   Строка 720: Строка 722:  
     local args = getArgs(frame, { frameOnly = true })
 
     local args = getArgs(frame, { frameOnly = true })
 
     local datein = args[1]
 
     local datein = args[1]
     return p.bxDate( datein )
+
     return (p.bxDate( datein ))
 
end
 
end
   −
function p.bxDate( datein )
+
function p.bxDate( txtDateIn , strFormat, params ) -- к отладке
filling_months(lang, month_lang)
+
local txtDateOut, date, status = "", {}, {brk = false, errorCat = "", errorText = ""}
    -- парсинг входящей даты по шаблону
+
strFormat = strFormat or "j xg Y"
    local date = parse_date(datein)
+
-- заглушка - таблица параметров на будущее
    if not (type(date.year) == 'number') then return ("<span class=error>Не удалось распознать год. Данные: " .. unwarp(date) .. "; " .. datein .. "</span>") end
+
params = params or {}
    if not (1 <= date.month and date.month <= 12) then return ("<span class=error>Не удалось распознать месяц. Данные: " .. unwarp(date).. "; " .. datein .. "</span>") end
+
if not txtDateIn then  
    if not date.day then
+
status.errorText = tCon(errors.start,errors.no_data,errors.ending)
    return month_lang.ru_N[date.month] .. " " .. date.year
+
status.errorCat = category.no_parameters
    elseif not (1 <= date.day and date.day <= month_end_day(date.month,date.year)) then
+
status.brk = true
        return ("<span class=error>Не удалось распознать день. Данные: " .. unwarp(date) .. "; " .. datein .. "</span>")
+
else
    end
+
-- заполнение служебных таблиц
    return table.concat({date.day,month_lang.ru_G[date.month],date.year}," ")
+
filling_months(lang, month_lang)
end
+
end
 
+
if not status.brk then
function p.bxDateTemp( txtDateIn ) -- к отладке
+
-- парсинг входящей даты по шаблону
-- заполнение служебных таблиц
+
date = parse_date(txtDateIn)
filling_months(lang, month_lang)
+
    -- заменить сообщения об ошибках на списочные
+
    if not (type(date.year) == 'number') then  
local status, txtDateOut = {errorCat = "", errorText = ""}, ""
+
    status.errorText = tCon{
    -- парсинг входящей даты по шаблону
+
    "<span class=error>Не удалось распознать год. Данные: ", unwarp(date),  
    local date = parse_date(txtDateIn)
+
    "; ", txtDateIn ,"</span>"}
   
+
    status.errorCat = category.incomplete_parameters
    if not (type(date.year) == 'number') then status.errorText = table.concat{
+
    status.brk = true
    "<span class=error>Не удалось распознать год. Данные: ", unwarp(date),  
+
    end
    "; ", txtDateIn ,"</span>"} end
+
    if not (1 <= date.month and date.month <= 12) then  
    if not (1 <= date.month and date.month <= 12) then status.errorText = table.concat{
+
    status.errorText = tCon{
    "<span class=error>Не удалось распознать месяц. Данные: ", unwarp(date),  
+
    "<span class=error>Не удалось распознать месяц. Данные: ", unwarp(date),  
    "; ", txtDateIn, "</span>"} end
+
    "; ", txtDateIn, "</span>"}  
    if not date.day then
+
    status.errorCat = category.incomplete_parameters
    txtDateOut = month_lang.ru_N[date.month] .. " " .. date.year
+
    status.brk = true
    elseif not (1 <= date.day and date.day <= month_end_day(date.month,date.year)) then  
+
    end
        status.errorText = table.concat{"<span class=error>Не удалось распознать день. Данные: ",
+
    if not date.day then
        unwarp(date), "; ", txtDateIn, "</span>"} end
+
    strFormat = trim(string.gsub(string.gsub(strFormat,"xg","F"),"[dDjlNwzW]",""))
-- тут нужна проверка, не было ли ошибок
+
    elseif not (1 <= date.day and date.day <= month_end_day(date.month,date.year)) then  
txtDateOut = table.concat({date.day,month_lang.ru_G[date.month],date.year}," ")
+
        status.errorText = tCon{"<span class=error>Не удалось распознать день. Данные: ",
 +
        unwarp(date), "; ", txtDateIn, "</span>"}
 +
        status.errorCat = category.incomplete_parameters
 +
    status.brk = true
 +
    end
 +
end
 +
if not status.brk then
 +
txtDateOut = mwlang:formatDate(strFormat,tCon({date.year,date.month,date.day},"-"),true)
 +
end
 
     return txtDateOut, date, status
 
     return txtDateOut, date, status
 
end
 
end
Строка 763: Строка 773:  
function p.ToDate( frame ) -- возможно неиспользуемая
 
function p.ToDate( frame ) -- возможно неиспользуемая
 
     local args = getArgs(frame, { frameOnly = true })
 
     local args = getArgs(frame, { frameOnly = true })
     local lang = mw.getContentLanguage()
+
     local mwlang = mw.getContentLanguage()
 
     local datein = args[1]
 
     local datein = args[1]
 
     local format = "j xg Y"
 
     local format = "j xg Y"
Строка 770: Строка 780:  
     else format = args[2]
 
     else format = args[2]
 
     end
 
     end
     return lang:formatDate(format,datein,true)
+
     return mwlang:formatDate(format,datein,true)
 
end
 
end
   Строка 986: Строка 996:  
mw.log("g1date " .. (undate(g1date ) or ""))
 
mw.log("g1date " .. (undate(g1date ) or ""))
 
mw.log("g2date " .. (undate(g2date ) or ""))
 
mw.log("g2date " .. (undate(g2date ) or ""))
return err .. category["params"]
+
return err .. category.incomplete_parameters
 
end
 
end
 
end
 
end
    
return p
 
return p
Анонимный участник