Module:Slug
From Landrace.Wiki - The Landrace Cannabis Wiki
More actions
Documentation for this module may be created at Module:Slug/doc
local p = {}
local function trim(s)
return (s:gsub("^%s+", ""):gsub("%s+$", ""))
end
local function slugify(s)
if not s or s == "" then
return ""
end
s = trim(s)
-- Use ustring so we don't explode on non-ASCII
local U = mw.ustring
s = U.lower(s)
-- Common normalizations
s = U.gsub(s, "&", " and ")
s = U.gsub(s, "’", "'") -- curly apostrophe -> straight
s = U.gsub(s, "[\"“”]", "") -- quotes
s = U.gsub(s, "[‘']", "") -- drop apostrophes entirely
-- Replace anything that's not a letter/number with hyphens
-- This keeps unicode letters/digits too (good for place names).
s = U.gsub(s, "[^%pL%pN]+", "-")
-- Collapse and trim hyphens
s = U.gsub(s, "%-+", "-")
s = U.gsub(s, "^%-+", "")
s = U.gsub(s, "%-+$", "")
return s
end
function p.make(frame)
-- Allow either positional or named arg
local s = frame.args[1] or frame.args.text or ""
return slugify(s)
end
return p