Module:String2: Difference between revisions

From the Croc Wiki, the Croc encyclopedia
Jump to navigationJump to search
Content added Content deleted
(added function "label" which capitalize only first letter for fetched wikidata labels d:Help:Label#Capitalization. Almost same as "sentence" function except that "label" doesn't lowering the rest of text.)
(update from sandbox -- fix for when the text passed is an html list)
Line 13:
p.sentence = 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')
Line 23 ⟶ 32:
local letter = string.sub(s, letterpos, letterpos)
local rest = string.sub(s, letterpos + 1)
return s1 .. first .. string.upper(letter) .. string.lower(rest)
else
return s1 .. s
end
end