Modul:Citation/CS1/Date validation: Forskelle mellem versioner

Content deleted Content added
glemte at rette det hele til u.å. i test
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 -- yearfx 2019-initial numerical year month day format12-15
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 -- åretfx sidst, numerisk dag-måned15-år12-format2019
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?%/, +[1-9]%d?[ -]%d%d%d%da?$") then -- fx numeriskJanuary dag/måned år15, fx 5/6 20162019
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 date_string:match("^%a+ +[1-9]%d?, +[1-9]%d%d%d%a?$") then -- month-initial: month day, year
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 mw.ustring.date_string:match(date_string, "^%a+ +[1-9]%d?[ +%-–][1-9]%d?,a+ +[1-9]%d%d%d%a?$") then -- month-initial day range: monthfx day–day,15 year;January days2019 areeller separated15 byJan endash2019
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 date_string:mw.ustring.match(date_string, "^[1-9]%d? ?[%-–] ?[1-9]%d? +%a+ +[1-9]%d%d%d%a?$") then -- dayfx 15-initial: day17 monthJanuary year2019
day, day2, month, anchor_year, year=stringmw.ustring.match(date_string, "(%d%d*?) ?[%s*-–] ?(%d%d?) +(%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("^[1-9]%d?%. +%a+%.? +[1-9]%d%d%d%a?$") then -- dag først: dag. måned år, dansk punktum tillades
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 mw.ustring.match(date_string, "^[1-9]%d?[%-–][1-9]%d? +%a+ +[1-9]%d%d%d%a?$") then -- day-range-initial: day–day month year; days are separated by endash
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 (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 -- intervalfx samme måned: dag15.–dag-17. månedjanuar 2019 år
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 -- dayfx initial15 month-day-range: day monthJanuary - day month year; uses17 spacedFebruary endash2019
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 -- Danskfx day initial month-day-range: day15. monthjanuar - day17. month year; uses spacedfebruar endash2019
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 -- monthfx initialJanuary 15 month-day-range: monthFebruary day – month day17, year; uses spaced endash2019
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 -- dayfx initial15 month-day-year-range:January day month year2018 - day month year; uses17 spacedFebruary endash2019
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 -- dagfx 15. månedjanuar år2018 - dag17. månedfebruar år2019
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 -- monthfx initialJanuary month-day-year-range: month day15, year2018 - monthFebruary day17, year; uses spaced endash2019
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 -- specialfx casevinteren Winter/Summer year2019-year (YYYY-YY); year separated with unspaced endash20
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 -- specialfx casevinteren Winter/Summer year2019-year; year separated with unspaced endash2020
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 +*[%-–] +*[%aå]+%.? +[1-9]%d%d%d%a?$") then -- month/seasonfx yearjanuar 2019 - month/season year; separated byfebruar spaced2020 endash
month, year, month2, anchor_year, year2=mw.ustring.match(date_string, "([%aå]+%.?) +(%d%d%d%d) +*[%-–] +*([%aå]+%.?) +((%d%d%d%d)%a?)");
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 -- månedsfx januar-/årstids-interval år; samme årfebruar 2019
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 date_string:mw.ustring.match(date_string, "^[%aå1-9]+%.? +d%d%d?%s?[%-–]%s?[1-9]%d%d%d?%a?$") then -- måned/årstidfx år eller egennavn år2018-2019
monthyear, anchor_year, yearyear2=date_string:mw.ustring.match(date_string, "([%aå]+d%.d%d%d?)%s*?[%-–]%s?((%d%d%d%d?)%a?)"); -- å er nødvendig for forår/efterår
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 mw.ustring.match(date_string, "^[1-9]%d%d%d?[%-–][1-9]%d%d%d?%a?$") then -- Year range: YYY-YYY or YYY-YYYY or YYYY–YYYY; separated by unspaced endash; 100-9999
year, anchor_year, year2=mw.ustring.match(date_string, "(%d%d%d%d?)[%-–]((%d%d%d%d?)%a?)");
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 -- Yearfx range: YYYY–YY; separated by unspaced endash2018-19
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
 
elseif date_string:match("^[1-9]%d%d%d?%a?$") then -- year; here accept either YYY or YYYY
anchor_year, year=date_string:match("((%d%d%d%d?)%a?)");
if false == is_valid_year(year) then
return false;
end
 
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 hyphenndash to an ndashhyphen. Not called if the cs1|2
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%d *%- *[1-9]%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%-%d%d%d%d') and
not param_val:match ('%d%d?%/%d%d?[%s-]%d%d%d%d') then -- hvis ikke ymd eller dmy eller d/m y eller d/m-y dato
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