Module:Footnotes: Difference between revisions
Richardpruen (talk | contribs) m 1 revision imported: Citation templates |
No edit summary |
||
Line 231: | Line 231: | ||
result = table.concat ({args.bracket_left, result, args.bracket_right, args.postscript}):gsub ('%s+', ' '); -- strip redundant spaces | result = table.concat ({args.bracket_left, result, args.bracket_right, args.postscript}):gsub ('%s+', ' '); -- strip redundant spaces | ||
return result .. err_msg; | return result .. err_msg; | ||
end | |||
--[[--------------------------< H Y P H E N _ T O _ D A S H >-------------------------------------------------- | |||
Converts a hyphen to a dash under certain conditions. The hyphen must separate | |||
like items; unlike items are returned unmodified. These forms are modified: | |||
letter - letter (A - B) | |||
digit - digit (4-5) | |||
digit separator digit - digit separator digit (4.1-4.5 or 4-1-4-5) | |||
letterdigit - letterdigit (A1-A5) (an optional separator between letter and | |||
digit is supported – a.1-a.5 or a-1-a-5) | |||
digitletter - digitletter (5a - 5d) (an optional separator between letter and | |||
digit is supported – 5.a-5.d or 5-a-5-d) | |||
any other forms are returned unmodified. | |||
str may be a comma- or semicolon-separated list | |||
This code copied from Module:Citation/CS1. The only modification is to require Module:Citation/CS1/Utilities | |||
so that it has access to the functions is_set() and has_accept_as_written() | |||
]] | |||
local function hyphen_to_dash( str ) | |||
local utilities = require ('Module:Citation/CS1/Utilities'); -- only modification so that this function has access to is_set() and has_accept_as_written() | |||
if not utilities.is_set (str) then | |||
return str; | |||
end | |||
local accept; -- Boolean | |||
str = str:gsub ('&[nm]dash;', {['–'] = '–', ['—'] = '—'}); -- replace — and – entities with their characters; semicolon mucks up the text.split | |||
str = str:gsub ('-', '-'); -- replace HTML numeric entity with hyphen character | |||
str = str:gsub (' ', ' '); -- replace entity with generic keyboard space character | |||
local out = {}; | |||
local list = mw.text.split (str, '%s*[,;]%s*'); -- split str at comma or semicolon separators if there are any | |||
for _, item in ipairs (list) do -- for each item in the list | |||
item, accept = utilities.has_accept_as_written (item); -- remove accept-this-as-written markup when it wraps all of item | |||
if not accept and mw.ustring.match (item, '^%w*[%.%-]?%w+%s*[%-–—]%s*%w*[%.%-]?%w+$') then -- if a hyphenated range or has endash or emdash separators | |||
if item:match ('^%a+[%.%-]?%d+%s*%-%s*%a+[%.%-]?%d+$') or -- letterdigit hyphen letterdigit (optional separator between letter and digit) | |||
item:match ('^%d+[%.%-]?%a+%s*%-%s*%d+[%.%-]?%a+$') or -- digitletter hyphen digitletter (optional separator between digit and letter) | |||
item:match ('^%d+[%.%-]%d+%s*%-%s*%d+[%.%-]%d+$') or -- digit separator digit hyphen digit separator digit | |||
item:match ('^%d+%s*%-%s*%d+$') or -- digit hyphen digit | |||
item:match ('^%a+%s*%-%s*%a+$') then -- letter hyphen letter | |||
item = item:gsub ('(%w*[%.%-]?%w+)%s*%-%s*(%w*[%.%-]?%w+)', '%1–%2'); -- replace hyphen, remove extraneous space characters | |||
else | |||
item = mw.ustring.gsub (item, '%s*[–—]%s*', '–'); -- for endash or emdash separated ranges, replace em with en, remove extraneous whitespace | |||
end | |||
end | |||
table.insert (out, item); -- add the (possibly modified) item to the output table | |||
end | |||
local temp_str = ''; -- concatenate the output table into a comma separated string | |||
temp_str, accept = utilities.has_accept_as_written (table.concat (out, ', ')); -- remove accept-this-as-written markup when it wraps all of concatenated out | |||
if accept then | |||
temp_str = utilities.has_accept_as_written (str); -- when global markup removed, return original str; do it this way to suppress boolean second return value | |||
return temp_str; | |||
else | |||
return temp_str; -- else, return assembled temp_str | |||
end | |||
end | end | ||
Line 255: | Line 320: | ||
args.page = pframe.args.p or pframe.args.page or ''; | args.page = pframe.args.p or pframe.args.page or ''; | ||
args.pages = pframe.args.pp or pframe.args.pages or ''; | args.pages = pframe.args.pp or pframe.args.pages or ''; | ||
args.pages = ('' ~= args.pages) and hyphen_to_dash (args.pages) or ''; | |||
args.location = pframe.args.loc or ''; | args.location = pframe.args.loc or ''; | ||
args.ref = pframe.args.ref or pframe.args.Ref or ''; | args.ref = pframe.args.ref or pframe.args.Ref or ''; | ||
Line 410: | Line 476: | ||
args.page = pframe.args[table.concat ({n, 'p'})] or ''; -- insource locations for this source | args.page = pframe.args[table.concat ({n, 'p'})] or ''; -- insource locations for this source | ||
args.pages = pframe.args[table.concat ({n, 'pp'})] or ''; | args.pages = pframe.args[table.concat ({n, 'pp'})] or ''; | ||
args.pages = ('' ~= args.pages) and hyphen_to_dash (args.pages) or ''; | |||
args.location = pframe.args[table.concat ({n, 'loc'})] or ''; | args.location = pframe.args[table.concat ({n, 'loc'})] or ''; | ||
args.ignore = ('yes' == pframe.args[table.concat ({n, 'ignore-false-positive'})]) or ('yes' == pframe.args[table.concat ({n, 'ignore-err'})]); | args.ignore = ('yes' == pframe.args[table.concat ({n, 'ignore-false-positive'})]) or ('yes' == pframe.args[table.concat ({n, 'ignore-err'})]); | ||
table.insert (out, core (args)); -- save the rendering of this source | table.insert (out, core (args)); -- save the rendering of this source |