Module:String2: Difference between revisions

m (1 revision imported)
(findpagetext)
Line 68: Line 68:
end
end
return table.concat(words, " ")
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
end


Line 93: Line 110:
-- any double quotes " are stripped out.
-- any double quotes " are stripped out.
p.posnq = function(frame)
p.posnq = function(frame)
local str = mw.text.trim(frame.args[1] or "")
local args = frame.args
local match = mw.text.trim(frame.args[2] or ""):gsub('"', '')
local pargs = frame:getParent().args
if str == "" or match == "" then return nil end
for k, v in pairs(pargs) do
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
-- just take the start position
local pos = str:find(match, 1, true)
local pos = mw.ustring.find(str, match, 1, plain) or nomatch
return pos
return pos
end
end
Line 145: Line 170:
txt = txt:gsub(" one ", " a "):gsub("^one", "a"):gsub("One ", "A "):gsub("a ([aeiou])", "an %1"):gsub("A ([aeiou])", "An %1")
txt = txt:gsub(" one ", " a "):gsub("^one", "a"):gsub("One ", "A "):gsub("a ([aeiou])", "an %1"):gsub("A ([aeiou])", "An %1")
return txt
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
end




return p
return p