Module:Infobox/parsing

From Official Factorio Wiki
< Module:Infobox
Revision as of 11:58, 25 October 2024 by Bilka (talk | contribs) (Moved parsing methods to separate module)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Lua methods used by Template:Infobox via Module:Infobox.

Edit this documentation on Module:Infobox/parsing/doc.

Methods

_technology

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

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

Item, count + Iron plate, 45 + Iron ore + 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. The number (, N) is optional and defaults to nothing.

_crafting

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

Item ingredient, count + Iron ore, 45 + Electronic circuit, 5 = Item product, count + Advanced circuit, 4

The text in front of the equals sign are treated as the recipe ingredients, the text behind the equals sign is treated as the recipe products. The products are optional and default to the name of the current page. For both ingredients and products, the count (, N) is optional and defaults to 1.

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 count as the caption.

_crafting_raw

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

Item ingredient, count + Iron ore, 45 + Electronic circuit, 5

The text in front of the equals sign are treated as the recipe ingredients, the text behind the equals sign is treated as the recipe products. If this is given the format from "_crafting", then the products are ignored. For the ingredients the count (, N) is optional and defaults to 1.

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 count as the caption.



local util = require("Module:Util")

local parsing = {}

function parsing._technology(frame, args)
  if args[1] == "?" or util._empty_arg(args[1]) then
    return args[1]
  end

  local ret = {}
  local techs = util._split(args[1], "+")
  for _, tech in ipairs(techs) do
    local tech_parts = util._split(tech, ",")
    local tech_name = tech_parts[1] .. " (research)"
    if not util._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+1] = frame:expandTemplate{title = 'icon/special', args = {tech_name, tech_level, color=args.color or "999"}}
  end
  return table.concat(ret)
end

function parsing._item(frame, item_string)
  if item_string == "?" or util._empty_arg(item_string) then
    return item_string
  end

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

function parsing._crafting(frame, recipe)
  if recipe == "?" or util._empty_arg(recipe) then
    return recipe
  end
  
  local recipe_parts = util._split(recipe, "=")  
  local ingredients = parsing._crafting_part(frame, recipe_parts[1])
  
  local products = recipe_parts[2]
  if products then
    products = parsing._crafting_part(frame, products)
  else
    local item_name = frame:expandTemplate{title = 'No language suffix/No namespace'}
    products = frame:expandTemplate{title = 'icon/special', args = {item_name, "1"}}
  end
  
  return ingredients .. " &rarr; " .. products
end

function parsing._crafting_raw(frame, recipe)
  if recipe == "?" or util._empty_arg(recipe) then
    return recipe
  end

  local recipe_parts = util._split(recipe, "=")
  return parsing._crafting_part(frame, recipe_parts[1])
end

-- @param frame
-- @param str string A list of "item, count" separated by "+"
-- @return string @The resulting icons with a "+" between each icon
function parsing._crafting_part(frame, str)
  local ret = {}
  local items = util._split(str, "+")
  for _, item in ipairs(items) do
    local item_parts = util._split(item, ",")
    local item_count = item_parts[2] or "1" -- fall back to 1, not empty!
    item_count = frame:expandTemplate{title = 'Crop', args = {item_count}}
    ret[#ret+1] = frame:expandTemplate{title = 'icon/special', args = {item_parts[1], item_count}}
  end
  return table.concat(ret, "+")
end

return parsing