24,527 ARTICLES
ON THIS WIKI

Module:NavParser

Lua Logo.svg This is the Lua module for Template:NavParser. Below is the documentation for that template.

This template is used to list items in a navbox. Navboxes are constructed out of the {{Navbox}} template and typically also several {{Navbox subgroup}} templates. The {{|tNavParser}} template serves to list links to pages in navboxes, typically being pages of in-game items, for which an image is displayed in the navbox with the item.

Parameters[edit]

Unlike most templates, this template parses a list of semicolon separated items, which is given as the first and only unnamed parameter. Furthermore there is an optional sprites parameter used to link a sprite sheet to the parser.

List parameters[edit]

Each semicolon separated item in the list is by itself a comma separated list of key-value pairs, with the exception of the first and only mandatory value, which is the name of the target page. The optional values have the following keys:

  • text: The text to display, which will override the name of the page given as the first value.
  • img: The name of the file, which is used to display as the image instead of the grid image associated with the page given as the first value. Additionally, the value / can be used to display no image.
  • link: The page to link to, which will override the link of the page given as the first value. Additionally, the value / can be used to display text without a link to a page or an image.
  • sprite: The number of the sprite that should be used as an image for the item, in case a sprite sheet is linked to the parser. This will override any image set using the img key. Alternatively the img key can also be used in conjunction with a number to display a sprite, but this functionality is deprecated.

Examples[edit]

Basic[edit]

Code[edit]

{{NavParser|
  Iron Ingot;
  Gold Ingot, img:Grid Diamond.png, link:Iron Sword;
  No link, link:/;
  Wool, text: Alternate text;
}}

Output[edit]

Iron Ingot Iron Ingot • Gold Ingot Gold Ingot • No link • Alternate text Alternate text

Sprites[edit]

Code[edit]

{{NavParser|sprites=NavSprites MineFactory Reloaded.png|
  Iron Ingot, sprite:1;
  Gold Ingot, sprite:2;
}}

Output[edit]

Iron Ingot Iron Ingot • Gold Ingot Gold Ingot


local p = {}
 
local g = require("Module:Common")
local n = require("Module:NavItem")

function p.main(frame)
	local frame, args = g.getFrameAndArgs(frame)
	local data = args[1]
	local spriteSheet = args.sprites
	local hasSprites = g.isGiven(spriteSheet)

	local out = ""
	local first = true
 
	if g.isGiven(data) then
		for navItem in string.gmatch(data, "[^;]+") do
			navItem = g.trim(navItem)
			if navItem == "(" then
				first = true
				out = out .. " ( "
			elseif navItem == ")" then
				first = false
				out = out .. " )"
			elseif navItem ~= "" then
				local name, text, image, link, sprite = "", "", "", "", ""
				for val in string.gmatch(navItem, "[^,]+") do
					val = g.trim(val)
					local k, v = string.match(val, "(%w+):(.*)")
					if k ~= nil then
						k = g.trim(string.lower(k)) -- Normalize key
					end
					if k == "text" then
						text = g.trim(v)
					elseif k == "img" then
						image = g.trim(v)
					elseif k == "link" then
						link = g.trim(v)
					elseif k == "sprite" then
						sprite = g.trim(v)
					elseif name == "" then
						name = val
					end
				end
				if text == "" then
					text = name
				end
				if image == "" then
					image = "Grid " .. name .. ".png"
				end
				if link == "" then
					link = name
				end
				if sprite == "" and tonumber(image) then
					sprite = image
				end
				if first then
					first = false
				else
					out = out .. g.bullet
				end
				if link == "/" then
					out = out .. text
				elseif image == "/" then
					out = out .. '<span class="navitem-link">' .. g.link(name, text, nil) .. '</span>'
				elseif hasSprites and tonumber(sprite) then
					out = out .. n.makeSpriteNavItem(name, spriteSheet, tonumber(sprite) - 1, link, text)
				elseif tonumber(sprite) then
					out = out .. n.makeCssSpriteNavItem(name, spriteSheet, tonumber(sprite) - 1, link, text)
				else
					out = out .. n.makeNavItem(name, image, link, text)
				end
			end
		end
	end
 
	return out
end

return p