Module:String2: Difference between revisions
From the Croc Wiki, the Croc encyclopedia
Jump to navigationJump to search
Content added Content deleted
(Prune code duplication in title function) |
(function posnq) |
||
Line 1: | Line 1: | ||
local p = {} |
local p = {} |
||
p.upper = function(frame) |
p.upper = function(frame) |
||
Line 10: | Line 11: | ||
return string.lower(s) |
return string.lower(s) |
||
end |
end |
||
p.sentence = function (frame ) |
p.sentence = function (frame ) |
||
Line 15: | Line 17: | ||
return p.ucfirst(frame) |
return p.ucfirst(frame) |
||
end |
end |
||
p.ucfirst = function (frame ) |
p.ucfirst = function (frame ) |
||
Line 42: | Line 45: | ||
end |
end |
||
end |
end |
||
p.title = function (frame ) |
p.title = function (frame ) |
||
Line 66: | Line 70: | ||
return table.concat(words, " ") |
return table.concat(words, " ") |
||
end |
end |
||
⚫ | |||
⚫ | |||
-- Wikidata English labels generally begin with a lowercase letter. [[:d:Help:Label#Capitalization]] |
-- Wikidata English labels generally begin with a lowercase letter. [[:d:Help:Label#Capitalization]] |
||
p.label = p.ucfirst |
p.label = p.ucfirst |
||
-- 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 77: | Line 85: | ||
return s |
return s |
||
end |
end |
||
-- nowiki ensures that a string of text is treated by the MediaWiki software as just a string |
-- nowiki ensures that a string of text is treated by the MediaWiki software as just a string |
||
Line 84: | Line 93: | ||
return mw.text.nowiki(str) |
return mw.text.nowiki(str) |
||
end |
end |
||
-- posnq (position, no quotes) returns the numerical start position of the first occurrence |
|||
-- of one piece of text ("match") inside another ("str"). |
|||
-- It return nil if no match is found. |
|||
-- It takes the text to be searched as the first unnamed parameter, which is trimmed. |
|||
-- It takes the text to search for as the second unnamed parameter, which is trimmed and |
|||
-- any doubel quotes " are stripped out. |
|||
p.posnq = function(frame) |
|||
local str = mw.text.trim(frame.args[1] or "") |
|||
--if str == "" then return nil end |
|||
local match = mw.text.trim(frame.args[2] or ""):gsub('"', '') |
|||
-- just take the start position |
|||
local pos = str:find(match, 1, true) |
|||
return pos |
|||
end |
|||
return p |
return p |