Module:String2: Difference between revisions

From the Croc Wiki, the Croc encyclopedia
Jump to navigationJump to search
Content added Content deleted
(function ucfirst)
(Prune code duplication)
Line 90: Line 90:
return table.concat(words, " ")
return table.concat(words, " ")
end
end
-- Capitalizing only first letter for fetched Wikidata labels.

-- Wikidata English labels generally begin with a lowercase letter. [[:d:Help:Label#Capitalization]]
p.label = function (frame )
p.label = p.ucfirst
-- Capitalizing only first letter for fetched Wikidata labels.
-- Wikidata English labels generally begin with a lowercase letter. [[:d:Help:Label#Capitalization]]
local s = mw.text.trim( frame.args[1] or "" )
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 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 first .. string.upper(letter) .. rest
else
return s
end
end

-- stripZeros finds the first number and strips leading zeros (apart from units)
-- stripZeros finds the first number and strips leading zeros (apart from units)
-- e.g "0940" -> "940"; "Year: 0023" -> "Year: 23"; "00.12" -> "0.12"
-- e.g "0940" -> "940"; "Year: 0023" -> "Year: 23"; "00.12" -> "0.12"
Line 126: Line 108:
return mw.text.nowiki(str)
return mw.text.nowiki(str)
end
end



return p
return p