Module:NavboxGenerator

Revision as of 18:11, 25 October 2024 by Sur (talk | contribs) (parametize data)

Not finished.

Overview

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

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

For understanding the input syntax, it is recommended to view the Simple Example first.

Simple Example

The input can be seen at Data:ItemNavboxSample.json

{{#invoke:NavboxGenerator|createNavbox|Data:ItemNavboxSample.json}}

Outputs

=Items=
=Vitality=
=500=
background-color:#c97a03;
{{#invoke:ItemData/nav|get_item_nav_cards|Armor|500|1250}}
background-image: linear-gradient(#c1a671, #d0bb95);
=1250=
background-color:#c97a03;
{{#invoke:ItemData/nav|get_item_nav_cards|Armor|1250|3000}}
background-image: linear-gradient(#c1a671, #d0bb95);
=Weapon=
=500=
background-color:#c97a03;
{{#invoke:ItemData/nav|get_item_nav_cards|Weapon|500|1250}}
background-image: linear-gradient(#c1a671, #d0bb95);
=1250=
background-color:#c97a03;
{{#invoke:ItemData/nav|get_item_nav_cards|Weapon|1250|3000}}

Styled Example

The input can be seen at Data:ItemNavbox.json

{{#invoke:NavboxGenerator|createNavbox|Data:ItemNavbox.json}}

Outputs

={{Souls|1250|Shadow=text-shadow: 1px 1px rgba(0, 0, 0, 0.3);}}=
background-color:#c97a03;
{{#invoke:ItemData/nav|get_item_nav_cards|Armor|1250|3000}}
background-image: linear-gradient(#c1a671, #d0bb95);
={{Souls|500|Shadow=text-shadow: 1px 1px rgba(0, 0, 0, 0.3);}}=
background-color:#c97a03;
{{#invoke:ItemData/nav|get_item_nav_cards|Armor|500|1250}}
background-image: linear-gradient(#c1a671, #d0bb95);
={{Souls|1250|Shadow=text-shadow: 1px 1px rgba(0, 0, 0, 0.3);}}=
background-color:#c97a03;
{{#invoke:ItemData/nav|get_item_nav_cards|Weapon|1250|3000}}
background-image: linear-gradient(#c1a671, #d0bb95);
={{Souls|500|Shadow=text-shadow: 1px 1px rgba(0, 0, 0, 0.3);}}=
background-color:#c97a03;
{{#invoke:ItemData/nav|get_item_nav_cards|Weapon|500|1250}}


Needed changes

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 boxSize = 5 - level  -- Adjust this to control the box size reduction per level
    local fontSize = 150 - (level * 20)  -- Adjust to decrease font size per level
    
    for key, value in pairs(data) do
        if type(value) == "table" then
            table.insert(result, string.format(
                '<div style="font-size:%d%%; border:1px solid #aaa; padding:5px; margin:2px;">=%s=</div>',
                fontSize, key
            ))
            table.insert(result, generateNavbox(value, level + 1))  -- Recursively process next level
        else
            table.insert(result, string.format(
                '<div style="font-size:%d%%; padding-left:%dpx; margin:2px;">%s</div>',
                fontSize, level * 10, value
            ))
        end
    end
    
    return 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