Difference between revisions of "Module:String"

From CrawlWiki
Jump to: navigation, search
m (Testing...)
 
(6 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
local p = {}
 
local p = {}
+
 
 
-- Used in Template:Monster by Property:Hit_dice
 
-- Used in Template:Monster by Property:Hit_dice
 
function p.first_word(frame)
 
function p.first_word(frame)
 
   local text = frame.args[1]
 
   local text = frame.args[1]
   for token in string.gmatch(text, "[^%s,]+") do
+
   for token in string.gmatch(text, '[^%s,]+') do
 
     return token
 
     return token
 
   end
 
   end
Line 12: Line 12:
 
function p.fix_magic_resistance(frame)
 
function p.fix_magic_resistance(frame)
 
   local text = frame.args[1]
 
   local text = frame.args[1]
   if text == "Immune" or text == "immune" then
+
   if text == 'Immune' or text == 'immune' then
     return "1000"
+
     return '1000'
 
   end
 
   end
 
   return text
 
   return text
Line 21: Line 21:
 
function p.fix_monster_size(frame)
 
function p.fix_monster_size(frame)
 
   local text = frame.args[1]
 
   local text = frame.args[1]
   found, _, token = string.find(text, "|([^%]]+)%]")
+
   found, _, token = string.find(text, '|([^%]]+)%]')
 
   if found then
 
   if found then
 
     return token
 
     return token
Line 36: Line 36:
 
function p.fix_gdr(frame)
 
function p.fix_gdr(frame)
 
   local text = frame.args[1]
 
   local text = frame.args[1]
   new_text, _ = string.gsub(text, "%%", "")
+
   new_text, _ = string.gsub(text, '%%', '')
 
   return new_text
 
   return new_text
 
end
 
end
  
-- Used in Template:Spellcaster016
+
-- Used in Template:Schoollink
function p.make_spellcaster_table(frame)
+
function p.school_to_skill(frame)
   local result = [=[<div style="margin: 1.0em; margin-top:0; margin-right:0; padding: 0px; float: left; border:none;">
+
   local school = frame.args[1]
{| class="prettytable" style="border:none; margin:0; padding:0; width:16em;"
+
  if school == 'Poison' or school == 'Air' or school == 'Fire' or
! colspan="2" style="font-size:larger;" | Spell set ]=]
+
    school == 'Ice' or school == 'Earth' then
  if frame.args['number'] then
+
    return school .. ' Magic'
     result = result .. frame.args['number']
+
  elseif school:sub(-1) == 'y' or school:sub(-1) == 's' then
 +
    -- "Necromancy" isn'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
   result = result .. "|-\n"
+
end
   local slots = {'slot1', 'slot2', 'slot3',
+
 
     'slot4', 'slot5', 'slot6'}
+
-- Used for getting correct sort key for categories
  for i, slot in ipairs(slots) do
+
function p.sort_key(frame)
     local val = frame.args[slot]
+
   local name = mw.title.getCurrentTitle().text
    if val and not val=="" then
+
   local key = name
      result = result .. "! align=\"left\" | Slot<sup>" .. tostring(i) .. "</sup>\n| " .. val .. "\n|-\n"
+
    :gsub('^[Bb]ook of ', '')
     end
+
     :gsub('^[Aa] ', '')
 +
     :gsub('^[Tt]he ', '')
 +
  if key == '' then
 +
    return name
 +
  else
 +
     return key
 
   end
 
   end
  result = result .. "|}</div>[[Category:Spellcaster]]"
 
  return result
 
 
end
 
end
 +
 
return p
 
return p

Latest revision as of 15:36, 9 November 2016

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_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(-1) == 'y' or school:sub(-1) == 's' then
    -- "Necromancy" isn'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