「モジュール:String」の版間の差分
細 1版 をインポートしました |
細 1版 をインポートしました |
||
| (3人の利用者による、間の5版が非表示) | |||
| 15行目: | 15行目: | ||
error_category: If an error occurs, specifies the name of a category to | error_category: If an error occurs, specifies the name of a category to | ||
include with the error message. The default category is | include with the error message. The default category is | ||
[Category: | [Category:Errors reported by Module String]. | ||
no_category: If set to 'true' or 1, no category will be added if an error | no_category: If set to 'true' or 1, no category will be added if an error | ||
| 59行目: | 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. | ||
| 407行目: | 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 | ||
| 512行目: | 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 | ||
| 540行目: | 577行目: | ||
function str._error( error_str ) | function str._error( error_str ) | ||
local frame = mw.getCurrentFrame() | local frame = mw.getCurrentFrame() | ||
local error_category = frame.args.error_category or ' | local error_category = frame.args.error_category or 'Errors reported by Module String' | ||
local ignore_errors = frame.args.ignore_errors or false | local ignore_errors = frame.args.ignore_errors or false | ||
local no_category = frame.args.no_category or false | local no_category = frame.args.no_category or false | ||
| 548行目: | 585行目: | ||
end | end | ||
local error_str = '<strong class="error"> | local error_str = '<strong class="error">String Module Error: ' .. error_str .. '</strong>' | ||
if error_category ~= '' and not str._getBoolean( no_category ) then | if error_category ~= '' and not str._getBoolean( no_category ) then | ||
error_str = '[[Category:' .. error_category .. ']]' .. error_str | error_str = '[[Category:' .. error_category .. ']]' .. error_str | ||
| 583行目: | 620行目: | ||
]] | ]] | ||
function str._escapePattern( pattern_str ) | function str._escapePattern( pattern_str ) | ||
return | return ( string.gsub( pattern_str, "[%(%)%.%%%+%-%*%?%[%^%$%]]", "%%%0" ) ) | ||
end | end | ||
return str | return str | ||