Module:Infobox: Difference between revisions

From Official Factorio Wiki
Jump to navigation Jump to search
(Created item parsing method)
 
(And the other typo)
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
local p = {}
local p = {}
function p.technology_parsing(frame)
  local args = frame.args
  if args[1] == "?" then
    return args[1] -- same as old template, likely not needed
  end
  local ret = ""
  local techs = p._split(args[1], "+")
  for _, tech in ipairs(techs) do
    local tech_parts = p._split(tech, ",")
    local tech_name = tech_parts[1] .. " (research)"
    if not p._page_exists(tech_name) then -- fall back to plain name if page with " (research)" doesn't exist
      tech_name = tech_parts[1]
    end
    local tech_level = tech_parts[2] or ""
    if tech_level == "" then -- fall back to level from tech name if none given manually
      local second_to_last_char = string.sub(tech_parts[1], -2, -2)
      if second_to_last_char == " " then
        tech_level = string.sub(tech_parts[1], -1) -- last character should be the level of the tech
      end
    end
    ret = ret .. frame:expandTemplate{title = 'icon/special', args = {tech_name, tech_level, color=args.color or "999"}}
  end
  return ret
end


function p.item_parsing(frame)
function p.item_parsing(frame)
Line 28: Line 55:


   return result
   return result
end
-- @param page_title string The title of the page
-- @return boolean
function p._page_exists(page_title)
  if page_title and page_title ~= "" then
    return mw.title.new(page_title).exists
  else
    return false
  end
end
end


return p
return p

Latest revision as of 18:38, 1 October 2024

Lua methods used by Template:Infobox.

Functions

technology_parsing

Takes one argument in the form of a string and one named argument, "color". The string is expected to have the following format:

Military 4 + Advanced oil processing + Advanced electronics 2, 2

This string is split so that each entry between a + is forwarded to Template:Icon/special so that it displays the technology and has the level as the caption.

Before forwarding the arguments to the icon template, " (research)" is appended to the technology if a page with that name exists. Furthermore, the technology level is deduced from the technology name if it is not manually specified.

The color defaults to "999".

item_parsing

Takes one argument in the form of a string. The string is expected to have the following format:

Iron plate, 2 + Iron ore, 45 + Electronic circuit, 5

This string is split so that each entry between a + is forwarded to Template:Icon/special so that it displays the item and has the number as the caption.



local p = {}

function p.technology_parsing(frame)
  local args = frame.args
  if args[1] == "?" then
    return args[1] -- same as old template, likely not needed
  end

  local ret = ""
  local techs = p._split(args[1], "+")
  for _, tech in ipairs(techs) do
    local tech_parts = p._split(tech, ",")
    local tech_name = tech_parts[1] .. " (research)"
    if not p._page_exists(tech_name) then -- fall back to plain name if page with " (research)" doesn't exist
      tech_name = tech_parts[1]
    end

    local tech_level = tech_parts[2] or ""
    if tech_level == "" then -- fall back to level from tech name if none given manually
      local second_to_last_char = string.sub(tech_parts[1], -2, -2)
      if second_to_last_char == " " then
        tech_level = string.sub(tech_parts[1], -1) -- last character should be the level of the tech
      end
    end
    ret = ret .. frame:expandTemplate{title = 'icon/special', args = {tech_name, tech_level, color=args.color or "999"}}
  end
  return ret
end

function p.item_parsing(frame)
  local args = frame.args
  if args[1] == "?" then
    return args[1] -- same as old template, likely not needed
  end

  local ret = ""
  local items = p._split(args[1], "+")
  for _, item in ipairs(items) do
    local icon_args = p._split(item, ",")
    icon_args[2] = icon_args[2] or ""
    ret = ret .. frame:expandTemplate{title = 'icon/special', args = icon_args}
  end
  return ret
end

-- @param inputstr string A string to be split
-- @param separator string The separator
-- @return string[] @The separated strings, without the separator
function p._split(inputstr, separator)
  local result = {}

  for str in string.gmatch(inputstr, "([^"..separator.."]+)") do
    table.insert(result, mw.text.trim(str))
  end

  return result
end

-- @param page_title string The title of the page
-- @return boolean
function p._page_exists(page_title)
  if page_title and page_title ~= "" then
    return mw.title.new(page_title).exists
  else
    return false
  end
end

return p