Line 21: |
Line 21: |
| function p.footer() | | function p.footer() |
| return '</table>' | | return '</table>' |
| + | end |
| + | |
| + | --- Render the SMAPI compatibility table based on JSON input. |
| + | -- @param frame The arguments passed to the script. |
| + | -- @test mw.log(p.table({ args = { [1]='[ { "name": "24h Clock", "author": "Lajna", "id": "Lajna.24hClock", "nexus": 1695, "github": "LajnaLegenden/Stardew_Valley_Mods", "brokeIn": "SMAPI 3.0", "unofficial": [ "1.0.1-unofficial.1-pathoschild", "https://community.playstarbound.com/threads/updating-mods-for-stardew-valley-1-3.142524/page-76#post-3342641" ] } ]' }})) |
| + | function p.table(frame) |
| + | -- parse data |
| + | local json = string.gsub(frame.args[1], ' *//[^"\r\n]+$', '') |
| + | local data = mw.text.jsonDecode(json, mw.text.JSON_TRY_FIXING) |
| + | |
| + | -- start table |
| + | local table = mw.html.create("table") |
| + | table:addClass("wikitable sortable plainlinks") |
| + | table:attr("id", "mod-list") |
| + | table:wikitext("<tr><th>mod name</th><th>author</th><th><abbr title=\"This only shows whether a mod is *compatible*; it may have bugs unrelated to SMAPI compatibility.\">compatibility</abbr></th><th>broke in</th><th>source</th><th> </th></tr>") |
| + | |
| + | -- add mod rows |
| + | for index,mod in pairs(data) do |
| + | -- temporarily passthrough args to avoid duplicating code until we migrate fully to JSON |
| + | -- (We need tostring on numeric fields since the previous code doesn't support numbers) |
| + | mod.chucklefish = private.toSafeString(mod.chucklefish) |
| + | mod.curse = private.toSafeString(mod.curse) |
| + | mod.moddrop = private.toSafeString(mod.moddrop) |
| + | mod.nexus = private.toSafeString(mod.nexus) |
| + | |
| + | local row = p.entry({ args = mod }) |
| + | table:node(row) |
| + | end |
| + | |
| + | -- return output |
| + | return private.style(frame) .. tostring(table) |
| end | | end |
| | | |
Line 336: |
Line 367: |
| unofficialUrl = unofficialUrl | | unofficialUrl = unofficialUrl |
| } | | } |
| + | end |
| + | |
| + | -- Call tostring() on the value if it's not nil, else return the value as-is. |
| + | -- @param value The value to format. |
| + | function private.toSafeString(value) |
| + | if value then |
| + | return tostring(value) |
| + | else |
| + | return nil |
| + | end |
| end | | end |
| | | |
| -- Get a nil value if the specified value is an empty string, else return the value unchanged. | | -- Get a nil value if the specified value is an empty string, else return the value unchanged. |
− | -- @param value The string to check. | + | -- @param value The string to format. |
| function private.emptyToNil(value) | | function private.emptyToNil(value) |
| if value ~= "" then | | if value ~= "" then |