Module:String: Difference between revisions
Appearance
pc>Pppery m 32 revisions imported from wikipedia:Module:String: Re-import from Wikipedia only with correct interwiki prefix |
pc>Pppery Fix |
||
Line 59: | Line 59: | ||
Parameters | Parameters | ||
s: The string to return a subset of | s: The string to return a subset of | ||
i: The | i: The first index of the substring to return, defaults to 1. | ||
j: The last index of the string to return, defaults to the last character. | j: The last index of the string to return, defaults to the last character. | ||
Line 407: | Line 407: | ||
if plain then | if plain then | ||
pattern = str._escapePattern( pattern ) | pattern = str._escapePattern( pattern ) | ||
replace = | replace = string.gsub( replace, "%%", "%%%%" ) --Only need to escape replacement sequences. | ||
end | end | ||
Line 512: | Line 512: | ||
end | end | ||
-- findpagetext returns the position of a piece of text in a page | |||
-- First positional parameter or |text is the search text | |||
-- Optional parameter |title is the page title, defaults to current page | |||
-- Optional parameter |plain is either true for plain search (default) or false for Lua pattern search | |||
-- Optional parameter |nomatch is the return value when no match is found; default is nil | |||
function str._findpagetext(args) | |||
-- process parameters | |||
local nomatch = args.nomatch or "" | |||
if nomatch == "" then nomatch = nil end | |||
-- | |||
local text = mw.text.trim(args[1] or args.text or "") | |||
if text == "" then return nil end | |||
-- | |||
local title = args.title or "" | |||
local titleobj | |||
if title == "" then | |||
titleobj = mw.title.getCurrentTitle() | |||
else | |||
titleobj = mw.title.new(title) | |||
end | |||
-- | |||
local plain = args.plain or "" | |||
if plain:sub(1, 1) == "f" then plain = false else plain = true end | |||
-- get the page content and look for 'text' - return position or nomatch | |||
local content = titleobj and titleobj:getContent() | |||
return content and mw.ustring.find(content, text, 1, plain) or nomatch | |||
end | |||
function str.findpagetext(frame) | |||
local args = frame.args | |||
local pargs = frame:getParent().args | |||
for k, v in pairs(pargs) do | |||
args[k] = v | |||
end | |||
if not (args[1] or args.text) then return nil end | |||
-- just the first value | |||
return (str._findpagetext(args)) | |||
end | |||
--[[ | --[[ | ||
Helper function that populates the argument list given that user may need to use a mix of | Helper function that populates the argument list given that user may need to use a mix of | ||
Line 583: | Line 620: | ||
]] | ]] | ||
function str._escapePattern( pattern_str ) | function str._escapePattern( pattern_str ) | ||
return | return ( string.gsub( pattern_str, "[%(%)%.%%%+%-%*%?%[%^%$%]]", "%%%0" ) ) | ||
end | end | ||
return str | return str |