Documentation for this module may be created at Module:AuthorParam/doc
-- Implements the author parameter and categorization of Template:Rip
-- Written by overcast07
local p = {}
local match_characters = "[%[|%],#<>]"
-- Format a credit for display
local function format_param(str)
assert (str, 'str must exist')
local result = ""
if (str == "") then
return str
end
str = mw.text.trim(str)
-- Remove strip markers (including references)
local format_str = mw.text.trim(mw.text.unstrip(str))
-- Check if there are no strip markers
if format_str == str then
-- Check if str can be a page title
if not str:match(match_characters) then
return "[[" .. str .. "]]"
end
else
-- Check if format_str can be a page title and if it is at the start of the original string
if (not format_str:match(match_characters)) and ((str:find(format_str, 1, true)) == 1) then
-- from Module:String
local pattern_str = mw.ustring.gsub(format_str, "([%(%)%.%%%+%-%*%?%[%^%$%]])", "%%%1")
local replace_str = mw.ustring.gsub(format_str, "%%", "%%%%" )
return str:gsub("^"..pattern_str, "[["..replace_str.."]]")
end
end
return str
end
-- Format a category link and check if the category exists
local function format_category(str)
assert (str, 'str must exist')
local result = ""
if (str == "") then
return str
end
local titleObj
-- Attempt to create title object
if pcall(function() titleObj = mw.title.new(str, "Category") end) then
-- Check if category exists and is not a redirect
if titleObj.exists and not titleObj.isRedirect then
return "[[Category:" .. str .. "]]"
end
end
-- If the category does not exist or is a redirect, add a tracking category
return "[[Category:" .. str .. "]][[Category:Pages in non-existent categories|contributor]]"
end
-- Format a credit for automatic categorization
local function categorize(str)
assert (str, 'str must exist')
local result = ""
if (str == "") then
return str
end
str = mw.text.trim(str)
-- Remove strip markers (including references)
local format_str = mw.text.trim(mw.text.unstrip(str))
-- Check if there are no strip markers
if format_str == str then
-- Check if str can be a page title
if not str:match(match_characters) then
return format_category(str)
end
else
-- Check if format_str can be a page title and if it is at the start of the original string
if (not format_str:match(match_characters)) and ((str:find(format_str, 1, true)) == 1) then
return format_category(format_str)
end
end
return ""
end
-- puts categories on the bottom of the page
function p.categorize(frame)
local str = frame.args[1]
local result
if pcall(function() result = categorize(str) end) then
return frame:preprocess(result)
else
return frame:preprocess(str .. "[[Category:Pages with script errors]]")
end
end
-- formats credit, if possible
function p.main(frame)
local str = frame.args[1]
local result
if pcall(function() result = format_param(str) end) then
return frame:preprocess(result)
else
return frame:preprocess(str .. "[[Category:Pages with script errors]]")
end
end
return p