Module:String2: Difference between revisions

Content deleted Content added
m 1 revision imported
findpagetext
Line 68:
end
return table.concat(words, " ")
end
 
-- findlast finds the last item in a list
-- the first unnamed parameter is the list
-- the second, optional unnamed parameter is the list separator (default = comma space)
-- returns the whole list if separator not found
p.findlast = function(frame)
local s = mw.text.trim( frame.args[1] or "" )
local sep = frame.args[2] or ""
if sep == "" then sep = ", " end
local pattern = ".*" .. sep .. "(.*)"
a, b, last = s:find(pattern)
if a then
return last
else
return s
end
end
 
Line 93 ⟶ 110:
-- any double quotes " are stripped out.
p.posnq = function(frame)
local strargs = mw.text.trim(frame.args[1] or "")
local matchpargs = mw.text.trim(frame.args[2] or ""):gsubgetParent('"', '').args
for k, v in pairs(pargs) do
if str == "" or match == "" then return nil end
args[k] = v
end
local str = mw.text.trim(args[1] or args.source or "")
local match = mw.text.trim(args[2] or args.target or ""):gsub('"', '')
if str == "" or match == "" then return nil end
local plain = mw.text.trim(args[3] or args.plain or "")
if plain == "false" then plain = false else plain = true end
local nomatch = mw.text.trim(args[4] or args.nomatch or "")
-- just take the start position
local pos = str:mw.ustring.find(str, match, 1, trueplain) or nomatch
return pos
end
Line 145 ⟶ 170:
txt = txt:gsub(" one ", " a "):gsub("^one", "a"):gsub("One ", "A "):gsub("a ([aeiou])", "an %1"):gsub("A ([aeiou])", "An %1")
return txt
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
p._findpagetext = function(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
content = titleobj:getContent()
return mw.ustring.find(content, text, 1, plain) or nomatch -- returns multiple values
end
p.findpagetext = function(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 (p._findpagetext(args))
end