Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Join the Playtest on Steam Now: SpiritVale

Module:Definitions: Difference between revisions

From SpiritVale Wiki
No edit summary
Tags: Reverted Mobile edit Mobile web edit
No edit summary
Tags: Manual revert Mobile edit Mobile web edit
Line 67: Line 67:
if DB ~= nil then return DB end
if DB ~= nil then return DB end


-- Preferred: Scribunto JSON loader (reliable for Module:*.json pages)
local ok, obj = pcall(mw.loadJsonData, DATA_TITLE)
if ok and type(obj) == "table" then
DB = obj
return DB
end
-- Fallback: raw content + jsonDecode (kept for safety)
local title = mw.title.new(DATA_TITLE)
local title = mw.title.new(DATA_TITLE)
if not title then
if not title then
Line 87: Line 79:
end
end


local ok2, obj2 = pcall(mw.text.jsonDecode, raw)
local ok, obj = pcall(mw.text.jsonDecode, raw)
if not ok2 or type(obj2) ~= "table" then
if not ok or type(obj) ~= "table" then
DB = {}
DB = {}
return DB
return DB
end
end


DB = obj2
DB = obj
return DB
return DB
end
end
Line 154: Line 146:


-- =============================================================================
-- =============================================================================
-- Link rendering
-- Render
-- - If Link is present, render a real <a href="...">
-- - If Link is blank, render plain text
-- =============================================================================
-- =============================================================================


local function link_to_href(link)
local function render(domain, key)
link = trim(link)
if link == "" then return nil end
 
-- External
if link:match("^https?://") or link:match("^//") then
return link
end
 
-- Internal: allow "Page#Fragment"
local page, frag = link:match("^(.-)#(.*)$")
if not page then
page = link
frag = nil
end
 
local t = mw.title.new(page)
if not t then
return nil
end
 
local url = t:localUrl()
if frag and frag ~= "" then
url = url .. "#" .. mw.uri.anchorEncode(frag)
end
 
return url
end
 
local function text_html(name, link)
name = tostring(name or "")
local href = link_to_href(link)
if href then
return '<a class="sv-def-text" href="' .. enc_attr(href) .. '">' .. mw.text.nowiki(name) .. '</a>'
end
return '<span class="sv-def-text">' .. mw.text.nowiki(name) .. '</span>'
end
 
-- =============================================================================
-- Render (internal)
-- =============================================================================
 
local function _render(domain, key)
local db = load_db()
local db = load_db()
domain = norm_domain(db, domain)
domain = norm_domain(db, domain)
Line 257: Line 205:
'<span ' .. table.concat(attrs, " ") .. title_attr .. '>' ..
'<span ' .. table.concat(attrs, " ") .. title_attr .. '>' ..
icon_html(icon) ..
icon_html(icon) ..
text_html(name, link) ..
'<span class="sv-def-text">' .. mw.text.nowiki(name) .. '</span>' ..
'</span>'
'</span>'
end
end
Line 267: Line 215:
function p.def(frame)
function p.def(frame)
local a = frame.args or {}
local a = frame.args or {}
return _render(a[1] or a.Domain or a.domain, a[2] or a.Key or a.key)
return render(a[1] or a.Domain or a.domain, a[2] or a.Key or a.key)
end
end


-- Supports BOTH:
function p.render(domain, key)
-- 1) Wikitext: {{#invoke:Definitions|render|Stat|Vit}}
return render(domain, key)
-- 2) Lua: p.render("Stat","Vit")
function p.render(a, b)
if type(a) == "table" then
local args = a.args or {}
return _render(args[1], args[2])
end
return _render(a, b)
end
end


return p
return p