Khác biệt giữa bản sửa đổi của “Mô đun:String”

Fix
n Đã nhập 1 phiên bản
Dòng 1: Dòng 1:
--[=[ 
Mô đun này nhằm cung cấp các hàm chuỗi cơ bản.
Phần nhiều hàm này cho phép gọi với các tham số có tên, các tham số không có
tên, hoặc pha trộn cả hai kiểu. Nếu sử dụng các tham số có tên, MediaWiki tự
động bỏ qua khoảng cách đằng truớc và đằng sau tham số. Tùy cách sử dụng, bạn có
thể cần giữ hoặc bỏ qua khoảng cách này.
Các tùy chọn toàn cục:
    ignore_errors: Nếu là 'true' hoặc 1, bất cứ trạng thái lỗi nào sẽ cho ra
        chuỗi rỗng thay vì thông báo lỗi.
   
    error_category: Nếu xuất hiện lỗi, trang được tự động xếp vào thể loại này
        và thông báo lỗi sẽ được hiển thị. Thể loại mặc định là
        [[Thể loại:Trang gây lỗi trong mô đun String]].
       
    no_category: Nếu là 'true' hoặc 1, trang không được tự động xếp vào thể loại
        khi xuất hiện lỗi.
Các trường hợp kiểm thử đơn vị cho mô đun này có sẵn tại [[Module:String/tests]].
]=]
--[[
--[[


Dòng 59: Dòng 82:
Parameters
Parameters
     s: The string to return a subset of
     s: The string to return a subset of
     i: The first index of the substring to return, defaults to 1.
     i: The fist 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.


Dòng 407: Dòng 430:
if plain then
if plain then
pattern = str._escapePattern( pattern )
pattern = str._escapePattern( pattern )
replace = string.gsub( replace, "%%", "%%%%" ) --Only need to escape replacement sequences.
replace = mw.ustring.gsub( replace, "%%", "%%%%" ) --Only need to escape replacement sequences.
end
end


Dòng 512: Dòng 535:
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
Dòng 620: Dòng 606:
]]
]]
function str._escapePattern( pattern_str )
function str._escapePattern( pattern_str )
return ( string.gsub( pattern_str, "[%(%)%.%%%+%-%*%?%[%^%$%]]", "%%%0" ) )
return mw.ustring.gsub( pattern_str, "([%(%)%.%%%+%-%*%?%[%^%$%]])", "%%%1" )
end
end


return str
return str