Module:String2/sandbox: Difference between revisions

Content deleted Content added
No edit summary
No edit summary
 
(8 intermediate revisions by 3 users not shown)
Line 12:
end
 
p.ucfirst = function (frame )
local s = frame.args[1];
if not s or '' == s or s:match ('^%s+$') then -- when <s> is nil, empty, or only whitespace
Line 22:
 
local prefix_patterns_t = { -- sequence of prefix patterns
'^\127[^\127]*UNIQ%-%-%a+%-%x+%-QINU[^\127]*\127', -- stripmarker
'^([%*;:#]+)', -- various list markup
'^(\'\'\'*)', -- bold / italic markup
Line 35:
local prefixes_t = {}; -- list, bold/italic, and html-like markup, & whitespace saved here
 
-- local function prefix_strip (s, prefixes_t) -- local function to strip prefixes from <s>
local function prefix_strip (s) -- local function to strip prefixes from <s>
for _, pattern in ipairs (prefix_patterns_t) do -- spin through <prefix_patterns_t>
if s:match (pattern) then -- when there is a match
Line 48 ⟶ 47:
end
 
-- local prefixes_t = {}; -- list, bold/italic, and html-like markup, & whitespace saved here
local prefix_removed; -- flag; boolean true as long as prefix_strip() finds and removes a prefix
repeat -- one by one remove list, bold/italic, html-like markup, whitespace, etc from start of <s>
-- s, prefix_removed = prefix_strip (s, prefixes_t);
s, prefix_removed = prefix_strip (s);
until (not prefix_removed); -- until <prefix_removed> is nil
 
s1 = table.concat (prefixes_t); -- recreate the prefix string for later reattachment
 
-- if it's a list chop off and (store as s1) everything up to the first <li>
-- why just list markup? Could be any sort of html markup:
-- <span title="Spanish-language text"><i lang="es">casa</i></span> (this from {{lang|es|casa}})
-- what about wikitext italic or bold markup? (''italic text'', '''bold text''') – both may be legitimate
-- local lipos = mw.ustring.find(s, "<li>" ) -- why not constrained to start of string?
-- if lipos then
-- s1 = mw.ustring.sub(s, 1, lipos + 3) -- get <li> tag (assumes that the tag is the first text in the string <s> – it may not be)
-- s = mw.ustring.sub(s, lipos + 4) -- get everything after the <li> tag
-- end
-- -- s1 is either "" or the first part of the list markup, so we can continue
-- -- and prepend s1 to the returned string
-- local letterpos
 
local first_text = mw.ustring.match (s, '^%[%[[^%]]+%]%]'); -- extract wikilink at start of string if present; TODO: this can be string.match()?
Line 100 ⟶ 84:
 
return s1 .. s; -- reattach prefix string (if present) and done
 
-- if mw.ustring.find(s, "^%[%[[^|]+|[^%]]+%]%]") then
-- -- this is a piped wikilink, so we capitalise the text, not the pipe
-- local _
-- _, letterpos = mw.ustring.find(s, "|%W*%w") -- find the first letter after the pipe
-- else
-- letterpos = mw.ustring.find(s, '%w')
-- end
-- if letterpos then
-- local first = mw.ustring.sub(s, 1, letterpos - 1)
-- local letter = mw.ustring.sub(s, letterpos, letterpos)
-- local rest = mw.ustring.sub(s, letterpos + 1)
-- return s1 .. first .. mw.ustring.upper(letter) .. rest
-- else
-- return s1 .. s
-- end
end
 
 
p.title = function (frame )
-- http://grammar.yourdictionary.com/capitalization/rules-for-capitalization-in-titles.html
-- recommended by The U.S. Government Printing Office Style Manual:
Line 410 ⟶ 379:
function p.startswith(frame)
return (frame.args[1]:sub(1, frame.args[2]:len()) == frame.args[2]) and 'yes' or ''
end
 
-- Implements [[Template:Isnumeric]]
function p.isnumeric(frame)
local s = frame.args[1] or frame:getParent().args[1]
local boolean = (frame.args.boolean or frame:getParent().args.boolean) == 'true'
if type(s) == 'string' and mw.getContentLanguage():parseFormattedNumber( s ) then
return boolean and 1 or s
end
return boolean and 0 or ''
end
 
-- Checks if a value in a group of numbers is not an interger.
-- Allows usage of an |empty= parameter to allow empty values to be skipped.
function p.isInteger(frame)
local values = frame.args or frame:getParent().args
local allow_empty = frame.args.empty or frame:getParent().args.empty
 
for _, value in ipairs(values) do
-- Trim spaces
value = value and value:gsub("^%s*(.-)%s*$", "%1")
if value == "" or value == nil then
if not allow_empty then
return false -- Empty values are not allowed
-- end
-- else
value = tonumber(value)
if not (type(value) == "number" and value == math.floor(value)) then
-- return s1 .. sfalse
-- end
-- end
end
 
return true
end
 
-- Returns an error found in a string.
function p.getError(frame)
local text = frame.args[1] or frame:getParent().args[1]
local error_message = text:match('(<strong class="error">.-</strong>)')
return error_message or nil
end