Module:String2/sandbox: Difference between revisions

Content deleted Content added
restore code for discussion
sync
Line 1:
local getArgs = require('Module:Arguments').getArgs
local p = {}
 
Line 292 ⟶ 291:
end
 
--[[--------------------------< H Y P H E N _ T O _ D A S H >--------------------------------------------------
--[[
 
Truncate a string
Converts a hyphen to a dash under certain conditions. The hyphen must separate
Implements Template:Trunc, which is similar to string.sub, but when second
like items; unlike items are returned unmodified. These forms are modified:
argument is missing or is not a number, return entire string
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
 
Usage:
{{#invoke:String2|trunc|abc123|3}}
Parameters:
args[1]: string to truncate
args[2]: number of characters to keep
]]
function p.trunchyphen_to_dash(frame str )
 
local args = getArgs(frame,{parentFirst=true})
str = str:gsub ('&[nm]dash;', {['&ndash;'] = '–', ['&mdash;'] = '—'}); -- replace &mdash; and &ndash; entities with their characters; semicolon mucks up the text.split
if not args[1] then
str = str:gsub ('&#45;', '-'); -- replace HTML numeric entity with hyphen character
return ""
 
str = str:gsub ('&nbsp;', ' '); -- replace &nbsp; 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 = mw.text.trim(item) -- trim whitespace
if 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
 
if not args[2] then
return table.concat (out, ', '); -- concatenate the output table into a comma separated string
return args[1]
end
 
length = tonumber(args[2])
function p.hyphen2dash( frame )
if not length then
return local str = frame.args[1] or ''
return p.hyphen_to_dash(str)
end
return mw.ustring.sub(args[1],1,length)
end