Modul:Citation/CS1/Date validation: Forskelle mellem versioner
Content deleted Content added
Weblars (diskussion | bidrag) glemte at rette det hele til u.å. i test |
Weblars (diskussion | bidrag) synk med sandkasse, ændring af brug af tankestreger og bindestreger i datointervaller samt ændring af rækkefølge for test af datoformat, se Moduldiskussion:Citation/CS1#Tankestreg_-->_bindestreg |
||
Linje 403:
anchor_year can be used in CITEREF anchors
COinS_date is ISO 8601 format date; see make_COInS_date()
Der bliver nedenfor brugt regulære udtryk som er forklaret i
https://www.mediawiki.org/wiki/Extension:Scribunto/Lua_reference_manual#Patterns
F.eks. matcher %d et tal mellem 0 og 9, %a et bogstav fra a til z, %s matcher et mellerumstegn
Desuden betyder +-tegnet 1 eller flere gentagelser af foregående tegn,
?-tegnet betyder nul eller 1 gentagelse, mens * betyder 0 eller flere gentagelser af foregående tegn
. (punktum) matcher et hvilket som helst tegn
%- matcher en bindestreg og %. matcher et punktum
[1-9] er en såkaldt tegnklasse der matcher et af tallene fra 1 til 9,
mens f.eks. tegnklassen [a-zå] matcher et bogstav fra a til z samt et å
^ betyder begyndelsen af strengen og $ betyder i slutningen.
]]
Line 416 ⟶ 427:
local coins_date;
if date_string:match("^%d%d%d%d%-%d%d%-%d%d$") then --
year, month, day=string.match(date_string, "(%d%d%d%d)%-(%d%d)%-(%d%d)");
if 12 < tonumber(month) or 1 > tonumber(month) or 1582 > tonumber(year) or 0 == tonumber(day) then return false; end -- month or day number not valid or not Gregorian calendar
anchor_year = year;
elseif date_string:match("^[1-9]%d?%. +%a+%.? +[1-9]%d%d%d%a?$") then -- fx 15. december 2019b, 15. dec. 2019b (b angiver at det er anden udgivelse for pågældende forfatter dette år)
day, month, anchor_year, year=string.match(date_string, "(%d%d*)%.?%s*(%a+%.?)%s*((%d%d%d%d)%a?)");
month = get_month_number_da (month);
if 0 == month then return false; end -- return false if month text isn't one of the twelve months
elseif date_string:match("^%d%d%-%d%d%-%d%d%d%d$") then --
day, month, year=string.match(date_string, "(%d%d)%-(%d%d)%-(%d%d%d%d)");
if 12 < tonumber(month) or 1 > tonumber(month) or 1582 > tonumber(year) or 0 == tonumber(day) then return false; end -- month number not valid or not Gregorian calendar
anchor_year = year;
elseif date_string:match("^[1-9]%d%d%d?%a?$") then -- fx 2019 (eller et trecifret årstal)
anchor_year, year=date_string:match("((%d%d%d%d?)%a?)");
if false == is_valid_year(year) then
return false;
end
elseif date_string:match("^%a+ +[1-9]%d?
month, day, anchor_year, year=string.match(date_string, "(%a+)%s*(%d%d?),%s*((%d%d%d%d)%a?)");
month = get_month_number (month);
if 0 == month then return false; end -- return false if month text isn't one of the twelve months
elseif
day, month, anchor_year, year=string.match(date_string, "(%d%d*)%s*(%a+)%s*((%d%d%d%d)%a?)");
month = get_month_number (month);
if 0 == month then return false; end -- return false if month text isn't one of the twelve months
elseif date_string:match("^[%aå]+%.? +%d%d%d%d%a?$") then -- fx januar 2019 eller January 2019
month, anchor_year, year=date_string:match("([%aå]+%.?)%s*((%d%d%d%d)%a?)"); -- å er nødvendig for foråret/efteråret
if not is_valid_year(year) then return false; end
if not is_valid_month_or_season (month) and 0 == is_proper_name (month) then return false; end
if 0 ~= get_month_number(month) then -- determined to be a valid range so just check this one to know if month or season
month = get_month_number(month);
elseif 0 ~= get_month_number_da(month) then -- dansk måned?
month = get_month_number_da(month);
elseif 0 ~= get_season_number(month) then
month = get_season_number(month);
else
month = is_proper_name (month); -- must be proper name; not supported in COinS
end
elseif date_string:match("^[1-9]%d?%/[1-9]%d?[ -]%d%d%d%d$") then -- fx 15/12 2019
day, month, year=string.match(date_string, "(%d%d?)%/(%d%d?)[%s-](%d%d%d%d)");
if 12 < tonumber(month) or 1 > tonumber(month) or 1582 > tonumber(year) or 0 == tonumber(day) then return false; end -- month number not valid or not Gregorian calendar
anchor_year = year;
elseif mw.ustring.match(date_string, "^%a+ +[1-9]%d?[%-–][1-9]%d?, +[1-9]%d%d%d%a?$") then -- fx January 15-17, 2019 eller January 15–17, 2019
month, day, day2, anchor_year, year=mw.ustring.match(date_string, "(%a+) +(%d%d?)[%-–](%d%d?), +((%d%d%d%d)%a?)");
if tonumber(day) >= tonumber(day2) then return false; end -- date range order is left to right: earlier to later; dates may not be the same;
Line 444 ⟶ 485:
year2=year;
elseif
day, day2, month, anchor_year, year=
if tonumber(day) >= tonumber(day2) then return false; end -- date range order is left to right: earlier to later; dates may not be the same;
month = get_month_number (month);
Line 462 ⟶ 493:
year2=year;
elseif mw.ustring.match(date_string, "^[1-9]%d?%. ?[%-–] ?[1-9]%d?%. +%a+%.? +[1-9]%d%d%d%a?$") then --
day, day2, month, anchor_year, year=mw.ustring.match(date_string, "(%d%d?)%. ?[%-–] ?(%d%d?)%. +(%a+%.?) +((%d%d%d%d)%a?)");
if tonumber(day) >= tonumber(day2) then return false; end -- date range order is left to right: earlier to later; dates may not be the same;
month = get_month_number_da (month);
Line 470 ⟶ 501:
year2=year;
elseif mw.ustring.match(date_string, "^[1-9]%d? +%a+ [%-–] [1-9]%d? +%a+ +[1-9]%d%d%d%a?$") then --
day, month, day2, month2, anchor_year, year=mw.ustring.match(date_string, "(%d%d?) +(%a+) [%-–] (%d%d?) +(%a+) +((%d%d%d%d)%a?)");
if (not is_valid_month_season_range(month, month2)) or not is_valid_year(year) then return false; end -- date range order is left to right: earlier to later;
Line 477 ⟶ 508:
year2=year;
elseif mw.ustring.match(date_string, "^[1-9]%d?%. +%a+%.? ?[%-–] ?[1-9]%d?%. +%a+%.? +[1-9]%d%d%d%a?$") then
day, month, day2, month2, anchor_year, year=mw.ustring.match(date_string, "(%d%d?)%. +(%a+%.?) ?[%-–] ?(%d%d?)%. +(%a+%.?) +((%d%d%d%d)%a?)");
if (not is_valid_month_season_range_da(month, month2)) or not is_valid_year(year) then return false; end -- date range order is left to right: earlier to later;
month = get_month_number (month); -- for metadata
Line 484 ⟶ 515:
year2=year;
elseif mw.ustring.match(date_string, "^%a+ +[1-9]%d? [%-–] %a+ +[1-9]%d?, +[1-9]%d%d%d?%a?$") then --
month, day, month2, day2, anchor_year, year=mw.ustring.match(date_string, "(%a+) +(%d%d?) [%-–] (%a+) +(%d%d?), +((%d%d%d%d)%a?)");
if (not is_valid_month_season_range(month, month2)) or not is_valid_year(year) then return false; end
Line 491 ⟶ 522:
year2=year;
elseif mw.ustring.match(date_string, "^[1-9]%d? +%a+ +[1-9]%d%d%d [%-–] [1-9]%d? +%a+ +[1-9]%d%d%d%a?$") then --
day, month, year, day2, month2, anchor_year, year2=mw.ustring.match(date_string, "(%d%d?) +(%a+) +(%d%d%d%d?) [%-–] (%d%d?) +(%a+) +((%d%d%d%d?)%a?)");
if tonumber(year2) <= tonumber(year) then return false; end -- must be sequential years, left to right, earlier to later
Line 498 ⟶ 529:
month2 = get_month_number (month2);
elseif mw.ustring.match(date_string, "^[1-9]%d?%. +%a+%.? +[1-9]%d%d%d ?[%-–] ?[1-9]%d?%. +%a+%.? +[1-9]%d%d%d%a?$") then --
day, month, year, day2, month2, anchor_year, year2=mw.ustring.match(date_string, "(%d%d?)%. +(%a+%.?) +(%d%d%d%d?) ?[%-–] ?(%d%d?)%. +(%a+%.?) +((%d%d%d%d?)%a?)");
if tonumber(year2) <= tonumber(year) then return false; end -- must be sequential years, left to right, earlier to later
if not is_valid_year(year2) or not is_valid_month_range_style(month, month2) then return false; end -- year2 no more than one year in the future; months same style
Line 505 ⟶ 536:
month2 = get_month_number_da (month2);
elseif mw.ustring.match(date_string, "^%a+ +[1-9]%d?, +[1-9]%d%d%d [%-–] %a+ +[1-9]%d?, +[1-9]%d%d%d%a?$") then --
month, day, year, month2, day2, anchor_year, year2=mw.ustring.match(date_string, "(%a+) +(%d%d?), +(%d%d%d%d) [%-–] (%a+) +(%d%d?), +((%d%d%d%d)%a?)");
if tonumber(year2) <= tonumber(year) then return false; end -- must be sequential years, left to right, earlier to later
Line 512 ⟶ 543:
month2 = get_month_number (month2);
elseif mw.ustring.match(date_string, "^%a+ +[1-9]%d%d%d[%-–]%d%d%a?$") then --
local century;
month, year, century, anchor_year, year2=mw.ustring.match(date_string, "(%a+) +((%d%d)%d%d)[%-–]((%d%d)%a?)");
Line 522 ⟶ 553:
month = get_season_number (month);
elseif mw.ustring.match(date_string, "^%a+ +[1-9]%d%d%d[%-–][1-9]%d%d%d%a?$") then --
month, year, anchor_year, year2=mw.ustring.match(date_string, "(%a+) +(%d%d%d%d)[%-–]((%d%d%d%d)%a?)");
if 'vinteren' ~= month and 'Winter' ~= month and 'Summer' ~= month then return false end; -- 'month' can only be Winter or Summer
Line 530 ⟶ 561:
month = get_season_number (month); -- for metadata
elseif mw.ustring.match(date_string, "^[%aå]+%.? +[1-9]%d%d%d
month, year, month2, anchor_year, year2=mw.ustring.match(date_string, "([%aå]+%.?) +(%d%d%d%d)
anchor_year=year..'–'..anchor_year; -- assemble anchor_year from both years
if tonumber(year) >= tonumber(year2) then return false; end -- left to right, earlier to later, not the same
Line 546 ⟶ 577:
end
elseif mw.ustring.match(date_string, "^[%aå]+%.? ?[%-–] ?[%aå]+%.? +[1-9]%d%d%d%a?$") then --
month, month2, anchor_year, year=mw.ustring.match(date_string, "([%aå]+%.?) ?[%-–] ?([%aå]+%.?)%s*((%d%d%d%d)%a?)");
if (not is_valid_month_season_range(month, month2) and not is_valid_month_season_range_da(month, month2)) or (not is_valid_year(year)) then return false; end
if 0 ~= get_month_number(month) then -- determined to be a valid range so just check this one to know if month or season
Line 561 ⟶ 592:
year2=year;
elseif
anchor_year=year..'–'..anchor_year; -- assemble anchor year from both years
if tonumber(year) >= tonumber(year2) then return false; end -- left to right, earlier to later, not the same
if not is_valid_year(year2) then return false; end -- no year farther in the future than next year
elseif mw.ustring.match(date_string, "^[1-9]%d%d%d[%-–]%d%d%a?$") then
local century;
year, century, anchor_year, year2=mw.ustring.match(date_string, "((%d%d)%d%d)[%-–]((%d%d)%a?)");
Line 589 ⟶ 606:
if tonumber(year) >= tonumber(year2) then return false; end -- left to right, earlier to later, not the same
if not is_valid_year(year2) then return false; end -- no year farther in the future than next year
else
Line 871 ⟶ 882:
--[[--------------------------< D A T E _ H Y P H E N _ T O _ D A S H >----------------------------------------
Loops through the list of date-holding parameters and converts any
template has any date errors.
Modifies the date_parameters_list and returns true if hyphens are replaced, else returns false.
Funktionen retter ikke kun bindestreger til tankestreger, men er tilpasset testen i
Moduldiskussion:Citation/CS1/Date validation/testcases
således at årstalsintervaller (1963-1968) og datointervaller indenfor samme måned (3.-5. juni 2019) og
månedsintervaller indenfor samme år (maj-juni 2019) alle bruger bindestreg, mens tankestreg foretrækkes
ved andre datointervaller (f.eks. 3. maj – 5. juni 2019, 3. maj 2018 – 5. juni 2019, maj 2018 – juni 2019)
Desuden skal der være mellemrum omkring tankestreg, men ikke omkring bindestreg
]]
Line 882 ⟶ 899:
local n;
for param_name, param_val in pairs(date_parameters_list) do -- for each date-holding parameter in the list
if not param_val:match ('^[1-9]%d%d%d
not param_val:match ('%d%d%d%d%-%d%d%-%d%d') and
not param_val:match ('%d%d%-%d%d%-%d%d%d%d') and
not param_val:match ('%d%d?%/%d%d?[%s-]%d%d%d%d') then
param_val, n = param_val:gsub ('%-', ' – '); -- replace any hyphen with ndash
if 0 ~= n then
date_parameters_list[param_name] = param_val; -- update the list
result = true;
end
end
if not param_val:match ('[1-9]%d?%. +%a+%.? *– *[1-9]%d?%. +%a+%.? +[1-9]%d%d%d') and
not param_val:match ('[1-9]%d?%. +%a+%.? +[1-9]%d%d%d *– *[1-9]%d?%. +%a+%.? +[1-9]%d%d%d') and
not param_val:match ('^%a+%.? +[1-9]%d%d%d *– *%a+%.? +[1-9]%d%d%d') then
param_val, n = param_val:gsub (' *– *', '-'); -- erstat tankestreg med bindestreg
if 0 ~= n then
date_parameters_list[param_name] = param_val; -- update the list
result = true;
end
end
if param_val:match ('[^ ]–[^ ]') then
param_val, n = param_val:gsub ('–', ' – ');
if 0 ~= n then
date_parameters_list[param_name] = param_val; -- update the list
result = true;
end
end
if param_val:match (' %- ') then
param_val, n = param_val:gsub (' +%- +', '-');
if 0 ~= n then
date_parameters_list[param_name] = param_val; -- update the list
|