Difference between revisions of "Module:Icon"

From Dragalia Lost Wiki
Jump to navigation Jump to search
>Eave
(fix empty argument handling)
(removing documentation redundant with /docs page)
 
(2 intermediate revisions by one other user not shown)
Line 5: Line 5:
  
 
-- Usage: {{#invoke:Icon|Wyrmprint|<name>}}
 
-- Usage: {{#invoke:Icon|Wyrmprint|<name>}}
-- Optional additional named args: 'link', 'size', 'text'
 
-- Ex: {{#invoke:Icon|Wyrmprint|Resounding Rendition}}
 
-- with custom link: {{#invoke:Icon|Wyrmprint|Resounding Rendition|link=Wyrmprints}}
 
-- with custom size: {{#invoke:Icon|Wyrmprint|Resounding Rendition|size=60px}}
 
-- with text label: {{#invoke:Icon|Wyrmprint|Resounding Rendition|text=1}}
 
 
function p.Wyrmprint(frame)
 
function p.Wyrmprint(frame)
 
   local name = mw.text.decode(frame.args[1], true)
 
   local name = mw.text.decode(frame.args[1], true)

Latest revision as of 22:59, 21 December 2019

50px Documentation

Usage

This module is intended to back the rendering of more complicated Template:Icon cases. Currently it supports the following cases:

  • Wyrmprints

In addition to main unnamed argument, it takes 3 additional optional named arguments:

  • link: to specify a custom navigation link for the icon thumbnail
  • size: to specify a custom size for the icon thumbnail
  • text: (must be '1' for true or '0' for false) to specify if a text label should be appended after the icon. Defaults to false.

Example

Generally, this module should be called via the regular wikitext templates instead of directly. But here is example syntax for calling the functions from wikitext:

{{#invoke:Icon|Wyrmprint|Heralds of Hinomoto}}

Lua error: Error: Table Wyrmprints not found..

{{#invoke:Icon|Wyrmprint|Heralds of Hinomoto|size=60px}}

Lua error: Error: Table Wyrmprints not found..

{{#invoke:Icon|Wyrmprint|Heralds of Hinomoto|text=1}}

Lua error: Error: Table Wyrmprints not found..

{{#invoke:Icon|Wyrmprint|Heralds of Hinomoto|size=40px|link=Wyrmprints|text=1}}

Lua error: Error: Table Wyrmprints not found..


-- <includeonly> (prevents literal interpretation of links or categories within the module code)
-- Lua Templating for supporting Template:Icon.
local p = {}
local cargo = mw.ext.cargo

-- Usage: {{#invoke:Icon|Wyrmprint|<name>}}
function p.Wyrmprint(frame)
  local name = mw.text.decode(frame.args[1], true)
  local wyrmprint = cargoQuery{
    tables = 'Wyrmprints',
    fields = '_pageName,BaseId,Name',
    args = {
      where = '_pageName="'..name..'" OR Name="'..name..'"',
      groupBy = '_pageName',
      limit = 1
    }
  }[1] -- We only want the first result

  if wyrmprint == nil then
    return '[['..name..']]'
  end

  -- Format the content
  local pageName = wyrmprint['_pageName']
  local baseId = wyrmprint['BaseId']

  local link = valueIfNotEmpty(frame.args['link']) or pageName
  local size = valueIfNotEmpty(frame.args['size']) or '80px'
  local text = frame.args['text'] == '1'
  local thumbnail = '[[File:'..baseId..' 01.png|'..size..'|link='..link..']]'

  if text then
    return thumbnail..' [['..pageName..'|'..wyrmprint['Name']..']]'
  else
    return thumbnail
  end
end


-- Helper functions
function cargoQuery(args)
  return cargo.query(args.tables, args.fields, args.args)
end

-- Returns the string if it's not empty, otherwise returns false
function valueIfNotEmpty(s)
  if (s == nil or s == '') then
    return false
  else
    return s
  end
end


return p
--</includeonly>