Toggle menu
84
103
34
3.2K
Landrace.Wiki - The Landrace Cannabis Wiki
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Module:Slug

From Landrace.Wiki - The Landrace Cannabis Wiki

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