Module:String2: Difference between revisions
From the Croc Wiki, the Croc encyclopedia
Jump to navigationJump to search
Content added Content deleted
(function nowiki) |
(function ucfirst) |
||
Line 33: | Line 33: | ||
local rest = string.sub(s, letterpos + 1) |
local rest = string.sub(s, letterpos + 1) |
||
return s1 .. first .. string.upper(letter) .. string.lower(rest) |
return s1 .. first .. string.upper(letter) .. string.lower(rest) |
||
else |
|||
return s1 .. s |
|||
end |
|||
end |
|||
p.ucfirst = function (frame ) |
|||
local s = mw.text.trim( frame.args[1] or "" ) |
|||
local s1 = "" |
|||
-- if it's a list chop off and (store as s1) everything up to the first <li> |
|||
local lipos = string.find(s, "<li>" ) |
|||
if lipos then |
|||
s1 = string.sub(s, 1, lipos + 3) |
|||
s = string.sub(s, lipos + 4) |
|||
end |
|||
-- s1 is either "" or the first part of the list markup, so we can continue |
|||
-- and prepend s1 to the returned string |
|||
if string.find(s, "^%[%[[^|]+|[^%]]+%]%]") then |
|||
-- this is a piped wikilink, so we capitalise the text, not the pipe |
|||
local b, c = string.find(s, "|%A*%a") -- find the first letter after the pipe |
|||
return s1 .. string.sub(s, 1, c-1) .. string.upper(string.sub(s, c, c)) .. string.sub(s, c+1) |
|||
end |
|||
local letterpos = string.find(s, '%a') |
|||
if letterpos then |
|||
local first = string.sub(s, 1, letterpos - 1) |
|||
local letter = string.sub(s, letterpos, letterpos) |
|||
local rest = string.sub(s, letterpos + 1) |
|||
return s1 .. first .. string.upper(letter) .. rest |
|||
else |
else |
||
return s1 .. s |
return s1 .. s |