Module:NavboxGenerator: Difference between revisions
initial |
m preprocess output for wikitext |
||
(4 intermediate revisions by the same user not shown) | |||
Line 3: | Line 3: | ||
local function generateNavbox(data, level) | local function generateNavbox(data, level) | ||
local result = {} | local result = {} | ||
local | local boxSizePercent = 100 - (level-1)*15 -- 15% less per level, 100%, 85%, 70%, 55%, etc. | ||
local fontSize = | local fontSize = 170 - (level * 15) -- Adjust font size reduction per level | ||
for key, value in pairs(data) do | for key, value in pairs(data) do | ||
if | if key == "Style" then | ||
-- Skip "Style" as it's just an attribute holder | |||
elseif key == "Display" then | |||
-- Leaf node, render Display content | |||
table.insert(result, string.format( | table.insert(result, string.format( | ||
'<div style="font-size:%d%% | '<div style="text-align: center; font-size:%d%%;">%s</div>', | ||
fontSize, | fontSize, value | ||
)) | )) | ||
elseif type(value) == "table" then | |||
-- Nested node, render title with optional Style and recurse | |||
table.insert(result, string.format( | table.insert(result, string.format( | ||
'<div style=" | '<div style="width: %s%%; margin-left: auto; margin-right: auto; text-align: center; font-size:%d%%; border:1px solid #aaa; %s">%s</div>', | ||
fontSize, | boxSizePercent, fontSize, value.Style or "", key | ||
)) | )) | ||
table.insert(result, generateNavbox(value, level + 1)) -- Recursively process next level | |||
end | end | ||
end | end | ||
return table.concat(result, "\n") | return mw.getCurrentFrame():preprocess(table.concat(result, "\n")) | ||
end | end | ||
function p.createNavbox(frame) | function p.createNavbox(frame) | ||
local jsonData = mw.loadJsonData( | local dataPageName = frame.args[1] | ||
if dataPageName == nil then return "dataPageName parameter missing" end | |||
local jsonData = mw.loadJsonData(dataPageName) | |||
return generateNavbox(jsonData, 1) | return generateNavbox(jsonData, 1) | ||
end | end | ||
return p | return p |
Latest revision as of 20:38, 25 October 2024
Not finished.
Overview[edit source]
Create's a navbox from a json file for input where keys are a title div container, and value "Display"
is the display text. Each title div container gets smaller and smaller the further it is in the json.
Parameters[edit source]
Within each level of the json:
- <key> - Title to display in the div/container/box. Can wrap it in
<span style="mystyle">key</span>
for styling it. - Style - Style for the div
- Display - Text to display below the div
- <Any other value> - Recursively calls for the contained layer, with <Any other value> as the new <key>
Examples[edit source]
For understanding the input syntax, it is recommended to view the Simple Example first.
Simple Example[edit source]
The input can be seen at Data:ItemNavboxSample.json
{{#invoke:NavboxGenerator|createNavbox|Data:ItemNavboxSample.json}}
Outputs
500 |
---|
Enduring Spirit |
500 |
---|
Extra Health |
500 |
---|
Extra Regen |
500 |
---|
Extra Stamina |
500 |
---|
ACTIVE |
Healing Rite |
500 |
---|
Melee Lifesteal |
500 |
---|
Sprint Boots |
1,250 |
---|
Bullet Armor |
1,250 |
---|
Bullet Lifesteal |
1,250 |
---|
Combat Barrier |
1,250 |
---|
Debuff Reducer |
1,250 |
---|
ACTIVE |
Divine Barrier |
1,250 |
---|
Enchanter's Barrier |
1,750 |
---|
Enduring Speed |
1,250 |
---|
Healbane |
1,250 |
---|
Healing Booster |
1,750 |
---|
ACTIVE |
Healing Nova |
1,250 |
---|
Reactive Barrier |
1,250 |
---|
ACTIVE |
Restorative Locket |
1,250 |
---|
ACTIVE |
Return Fire |
1,250 |
---|
Spirit Armor |
1,250 |
---|
Spirit Lifesteal |
500 |
---|
Basic Magazine |
500 |
---|
Close Quarters |
500 |
---|
Headshot Booster |
500 |
---|
Monster Rounds |
500 |
---|
Rapid Rounds |
500 |
---|
Restorative Shot |
1,250 |
---|
Active Reload |
1,250 |
---|
Berserker |
1,250 |
---|
ACTIVE |
Fleetfoot |
1,250 |
---|
Kinetic Dash |
1,250 |
---|
Long Range |
1,250 |
---|
Melee Charge |
1,250 |
---|
Mystic Shot |
1,250 |
---|
Slowing Bullets |
1,250 |
---|
Soul Shredder Bullets |
1,250 |
---|
Swift Striker |
Styled Example[edit source]
The input can be seen at Data:ItemNavbox.json
{{#invoke:NavboxGenerator|createNavbox|Data:ItemNavbox.json}}
Outputs
1,250 |
---|
Bullet Armor |
1,250 |
---|
Bullet Lifesteal |
1,250 |
---|
Combat Barrier |
1,250 |
---|
Debuff Reducer |
1,250 |
---|
ACTIVE |
Divine Barrier |
1,250 |
---|
Enchanter's Barrier |
1,750 |
---|
Enduring Speed |
1,250 |
---|
Healbane |
1,250 |
---|
Healing Booster |
1,750 |
---|
ACTIVE |
Healing Nova |
1,250 |
---|
Reactive Barrier |
1,250 |
---|
ACTIVE |
Restorative Locket |
1,250 |
---|
ACTIVE |
Return Fire |
1,250 |
---|
Spirit Armor |
1,250 |
---|
Spirit Lifesteal |
500 |
---|
Enduring Spirit |
500 |
---|
Extra Health |
500 |
---|
Extra Regen |
500 |
---|
Extra Stamina |
500 |
---|
ACTIVE |
Healing Rite |
500 |
---|
Melee Lifesteal |
500 |
---|
Sprint Boots |
1,250 |
---|
Active Reload |
1,250 |
---|
Berserker |
1,250 |
---|
ACTIVE |
Fleetfoot |
1,250 |
---|
Kinetic Dash |
1,250 |
---|
Long Range |
1,250 |
---|
Melee Charge |
1,250 |
---|
Mystic Shot |
1,250 |
---|
Slowing Bullets |
1,250 |
---|
Soul Shredder Bullets |
1,250 |
---|
Swift Striker |
500 |
---|
Basic Magazine |
500 |
---|
Close Quarters |
500 |
---|
Headshot Booster |
500 |
---|
Monster Rounds |
500 |
---|
Rapid Rounds |
500 |
---|
Restorative Shot |
Needed changes[edit source]
1. Style/Formatting made prettier 2. A way to have the json iterated in order; for some reason mw.loadJsonData doesn't load the keys in order they appear.
This is not used on any pages currently.
local p = {}
local function generateNavbox(data, level)
local result = {}
local boxSizePercent = 100 - (level-1)*15 -- 15% less per level, 100%, 85%, 70%, 55%, etc.
local fontSize = 170 - (level * 15) -- Adjust font size reduction per level
for key, value in pairs(data) do
if key == "Style" then
-- Skip "Style" as it's just an attribute holder
elseif key == "Display" then
-- Leaf node, render Display content
table.insert(result, string.format(
'<div style="text-align: center; font-size:%d%%;">%s</div>',
fontSize, value
))
elseif type(value) == "table" then
-- Nested node, render title with optional Style and recurse
table.insert(result, string.format(
'<div style="width: %s%%; margin-left: auto; margin-right: auto; text-align: center; font-size:%d%%; border:1px solid #aaa; %s">%s</div>',
boxSizePercent, fontSize, value.Style or "", key
))
table.insert(result, generateNavbox(value, level + 1)) -- Recursively process next level
end
end
return mw.getCurrentFrame():preprocess(table.concat(result, "\n"))
end
function p.createNavbox(frame)
local dataPageName = frame.args[1]
if dataPageName == nil then return "dataPageName parameter missing" end
local jsonData = mw.loadJsonData(dataPageName)
return generateNavbox(jsonData, 1)
end
return p