Module:Sandbox/Mefisto1029/main: Difference between revisions
Jump to navigation
Jump to search
Mefisto1029 (talk | contribs) No edit summary |
Mefisto1029 (talk | contribs) No edit summary |
||
Line 16: | Line 16: | ||
-- The cfg table contains all localisable strings and configuration, to make it | -- The cfg table contains all localisable strings and configuration, to make it | ||
-- easier to port this module to another wiki. | |||
--local cfg = use_sandbox and mw.loadData('Module:Keyword/config/sandbox') or mw.loadData('Module:Keyword/config') | |||
local cfg = mw.loadData('Module:Sandbox/Mefisto1029/config') | |||
-- The data table contains all informations, to make it | |||
-- easier to port this module to another wiki. | -- easier to port this module to another wiki. | ||
--local m_data = use_sandbox and mw.loadData('Module:Keyword/data/sandbox') or mw.loadData('Module:Keyword/data') | --local m_data = use_sandbox and mw.loadData('Module:Keyword/data/sandbox') or mw.loadData('Module:Keyword/data') | ||
local m_data = mw.loadData('Module:Sandbox/Mefisto1029/data') | local m_data = mw.loadData('Module:Sandbox/Mefisto1029/data') | ||
local i18n = cfg.i18n | local i18n = cfg.i18n | ||
Line 40: | Line 43: | ||
end | end | ||
function h. | -- Preset basic args | ||
function h.preset_args(tpl_args) | |||
tpl_args.id = tpl_args.id or tpl_args[1] | tpl_args.id = tpl_args.id or tpl_args[1] | ||
tpl_args.text = tpl_args.text or tpl_args[2] | tpl_args.text = tpl_args.text or tpl_args[2] | ||
Line 50: | Line 54: | ||
end | end | ||
-- | -- Get data from submodule | ||
function h.get_data(tpl_args) | |||
local data = m_data[tpl_args.id] | |||
if not data then | |||
error(i18n.errors.no_data) | |||
end | |||
return data | |||
end | |||
-- This is similar to util.args.template_to_lua but can execute more than one template | |||
-- in a string and automatically replace that template with the functions | |||
-- but args are as one string (if more than one then need extra splits) | |||
function h.process_templates(tpl_args, content) | function h.process_templates(tpl_args, content) | ||
local caller = tpl_args.options.caller | local caller = tpl_args.options and tpl_args.options.caller or 'keyword' | ||
-- match {{Template}} or {{Template|Arg}} | -- match {{Template}} or {{Template|Arg}} | ||
Line 84: | Line 99: | ||
_keyword = function(tpl_args) | _keyword = function(tpl_args) | ||
h.preset_args(tpl_args) | |||
h. | |||
local data = | local data = h.get_data(tpl_args) | ||
tpl_args.options.caller = 'keyword' | tpl_args.options.caller = 'keyword' | ||
Line 99: | Line 110: | ||
return m_util.html.hoverbox(tpl_args.text or tpl_args.id, content) | return m_util.html.hoverbox(tpl_args.text or tpl_args.id, content) | ||
elseif data.links then | elseif data.links then | ||
for | for _, v in ipairs(data.links) do | ||
if type(v) == 'table' and tpl_args.text == v[1] then | if type(v) == 'table' and tpl_args.text == v[1] then | ||
link = v[2] | link = v[2] | ||
Line 122: | Line 133: | ||
local function _keyword_infobox(tpl_args) | local function _keyword_infobox(tpl_args) | ||
h. | h.preset_args(tpl_args) | ||
local data = | local data = h.get_data(tpl_args) | ||
tpl_args.options.caller = 'keyword_infobox' | tpl_args.options.caller = 'keyword_infobox' |
Revision as of 14:47, 3 October 2025
You might want to create a documentation page for this module.
Editors can experiment in this module's sandbox and testcases pages.
Please add categories to the /doc subpage. Subpages of this module.
Editors can experiment in this module's sandbox and testcases pages.
Please add categories to the /doc subpage. Subpages of this module.
-------------------------------------------------------------------------------
--
-- Module:Keyword
--
-- This module implements Template:Keyword and Template:Keyword infobox
-------------------------------------------------------------------------------
require('strict')
local m_util = require('Module:Util')
-- Should we use the sandbox version of our submodules?
--local use_sandbox = m_util.misc.maybe_sandbox('Keyword')
-- Lazy loading
local f_infocard -- require('Module:Infocard')._main
-- The cfg table contains all localisable strings and configuration, to make it
-- easier to port this module to another wiki.
--local cfg = use_sandbox and mw.loadData('Module:Keyword/config/sandbox') or mw.loadData('Module:Keyword/config')
local cfg = mw.loadData('Module:Sandbox/Mefisto1029/config')
-- The data table contains all informations, to make it
-- easier to port this module to another wiki.
--local m_data = use_sandbox and mw.loadData('Module:Keyword/data/sandbox') or mw.loadData('Module:Keyword/data')
local m_data = mw.loadData('Module:Sandbox/Mefisto1029/data')
local i18n = cfg.i18n
local _keyword -- forward declaration
-- ----------------------------------------------------------------------------
-- Helper functions
-- ----------------------------------------------------------------------------
local h = {}
-- Lazy loading for Module:Infocard
function h.infocard(args)
if not f_infocard then
f_infocard = require('Module:Infocard')._main
end
return f_infocard(args)
end
-- Preset basic args
function h.preset_args(tpl_args)
tpl_args.id = tpl_args.id or tpl_args[1]
tpl_args.text = tpl_args.text or tpl_args[2]
tpl_args.options = tpl_args.options or {}
if not tpl_args.id then
error(i18n.errors.missing_id)
end
end
-- Get data from submodule
function h.get_data(tpl_args)
local data = m_data[tpl_args.id]
if not data then
error(i18n.errors.no_data)
end
return data
end
-- This is similar to util.args.template_to_lua but can execute more than one template
-- in a string and automatically replace that template with the functions
-- but args are as one string (if more than one then need extra splits)
function h.process_templates(tpl_args, content)
local caller = tpl_args.options and tpl_args.options.caller or 'keyword'
-- match {{Template}} or {{Template|Arg}}
local processed = content:gsub("{{(.-)}}", function(inner)
-- split by | if present
local key, text = inner:match("^%s*([^|]+)%s*|?(.-)%s*$")
key = key or ""
text = text or ""
if caller == 'keyword' then
-- just return text: second part if present, else key
return text ~= "" and text or key
elseif caller == 'keyword_infobox' then
-- call _keyword function dynamically
return _keyword({
id = key,
text = text ~= "" and text or key,
})
else
-- fallback: return raw text
return text ~= "" and text or key
end
end)
return processed
end
-- ----------------------------------------------------------------------------
-- Main functions
-- ----------------------------------------------------------------------------
_keyword = function(tpl_args)
h.preset_args(tpl_args)
local data = h.get_data(tpl_args)
tpl_args.options.caller = 'keyword'
local content = h.process_templates(tpl_args, data.content)
local link
if data.no_link then
return m_util.html.hoverbox(tpl_args.text or tpl_args.id, content)
elseif data.links then
for _, v in ipairs(data.links) do
if type(v) == 'table' and tpl_args.text == v[1] then
link = v[2]
break
elseif tpl_args.text == v then
link = tpl_args.text
break
end
end
end
if not link then
if data.default_link then
link = data.default_link
else
link = data.title
end
end
return m_util.html.hoverbox('[[' .. link .. '|' .. (tpl_args.text or tpl_args.id) .. ']]', content)
end
local function _keyword_infobox(tpl_args)
h.preset_args(tpl_args)
local data = h.get_data(tpl_args)
tpl_args.options.caller = 'keyword_infobox'
local content = h.process_templates(tpl_args, data.content)
local infocard_args = {}
infocard_args.header = data.title
infocard_args.subheading = i18n.tooltips.tooltip
infocard_args[1] = content
infocard_args['1class'] = 'tc -normal'
return h.infocard(infocard_args)
end
-- ----------------------------------------------------------------------------
-- Exported functions
-- ----------------------------------------------------------------------------
local p = {}
--
-- Template:Keyword
--
p.keyword = m_util.misc.invoker_factory(_keyword, {
wrappers = cfg.wrappers.keyword,
})
--
-- Template:Keyword infobox
--
p.keyword_infobox = m_util.misc.invoker_factory(_keyword_infobox, {
wrappers = cfg.wrappers.keyword_infobox,
})
return p