Jump to content
Toggle menu
  • 3.1K articles
  • 4.4K files
  • 18 users
  • 91.5K edits
Ashenzari's Archive
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Module:String: Difference between revisions

From Ashenzari's Archive
imported>Medar
Add sort_key to get sorting key for category name.
Predelnik (talk | contribs)
No edit summary
 
(3 intermediate revisions by 2 users not shown)
Line 7: Line 7:
     return token
     return token
   end
   end
end
-- Used in Template:Monster by Property:Monster_willpower
function p.fix_willpower(frame)
  local text = frame.args[1]
  if text == 'Immune' or text == 'immune' then
    return '1000'
  end
  return text
end
end


Line 46: Line 55:
     school == 'Ice' or school == 'Earth' then
     school == 'Ice' or school == 'Earth' then
     return school .. ' Magic'
     return school .. ' Magic'
   elseif school:sub(-1) == 'y' or school:sub(-1) == 's' then
   elseif school:sub(-5) == 'craft' or school:sub(-1) == 'y' or school:sub(-1) == 's' then
     -- "Necromancy" isn't pluralised as a skill, and "Hexes" and "Charms" are
     -- "Necromancy" and "Forgecraft" aren't pluralised as a skill, and "Hexes" and "Charms" are
     -- already pluralized as a magic school. The others are singular as a
     -- already pluralized as a magic school. The others are singular as a
     -- school, plural as a skill.
     -- school, plural as a skill.
Line 58: Line 67:
-- Used for getting correct sort key for categories
-- Used for getting correct sort key for categories
function p.sort_key(frame)
function p.sort_key(frame)
   local name = frame.args[1]
   local name = mw.title.getCurrentTitle().text
   local key = name
   local key = name
     :gsub('^[Bb]ook of ', '')
     :gsub('^[Bb]ook of ', '')

Latest revision as of 18:47, 29 September 2026

Small functions for string manipulation.


local p = {}

-- Used in Template:Monster by Property:Hit_dice
function p.first_word(frame)
  local text = frame.args[1]
  for token in string.gmatch(text, '[^%s,]+') do
    return token
  end
end

-- Used in Template:Monster by Property:Monster_willpower
function p.fix_willpower(frame)
  local text = frame.args[1]
  if text == 'Immune' or text == 'immune' then
    return '1000'
  end
  return text
end

-- Used in Template:Monster by Property:Monster_magic_resistance
function p.fix_magic_resistance(frame)
  local text = frame.args[1]
  if text == 'Immune' or text == 'immune' then
    return '1000'
  end
  return text
end

-- Used in Template:Monster by Property:Monster_size
function p.fix_monster_size(frame)
  local text = frame.args[1]
  found, _, token = string.find(text, '|([^%]]+)%]')
  if found then
    return token
  end
  return text
end

-- Used in Template:Monster by Property:Monster_intelligence
function p.fix_monster_intelligence(frame)
  return p.fix_monster_size(frame)
end

-- Used in Template:Armour
function p.fix_gdr(frame)
  local text = frame.args[1]
  new_text, _ = string.gsub(text, '%%', '')
  return new_text
end

-- Used in Template:Schoollink
function p.school_to_skill(frame)
  local school = frame.args[1]
  if school == 'Poison' or school == 'Air' or school == 'Fire' or
     school == 'Ice' or school == 'Earth' then
    return school .. ' Magic'
  elseif school:sub(-5) == 'craft' or school:sub(-1) == 'y' or school:sub(-1) == 's' then
    -- "Necromancy" and "Forgecraft" aren't pluralised as a skill, and "Hexes" and "Charms" are
    -- already pluralized as a magic school. The others are singular as a
    -- school, plural as a skill.
    return school
  else
    return school .. 's'
  end
end

-- Used for getting correct sort key for categories
function p.sort_key(frame)
  local name = mw.title.getCurrentTitle().text
  local key = name
    :gsub('^[Bb]ook of ', '')
    :gsub('^[Aa] ', '')
    :gsub('^[Tt]he ', '')
  if key == '' then
    return name
  else
    return key
  end
end

return p