Line 1: |
Line 1: |
| local p = {} | | local p = {} |
| local private = {} | | local private = {} |
| + | |
| + | -- whether to handle Stardew Valley beta fields (don't forget to comment or uncomment the beta fields in /doc) |
| + | local enableBeta = false |
| | | |
| --########## | | --########## |
Line 6: |
Line 9: |
| --########## | | --########## |
| -- Start a SMAPI compatibility table. | | -- Start a SMAPI compatibility table. |
− | -- @test mw.log(p.header()) | + | -- @test mw.log(p.header({})) |
− | function p.header() | + | function p.header(frame) |
| return | | return |
− | '<table class="wikitable plainlinks">' | + | private.style(frame) |
− | .. "<tr><th>mod name</th><th>author</th><th>compatibility</th><th>broke in</th><th>source</th><th> </th></tr>"; | + | .. '<table class="wikitable sortable plainlinks" id="mod-list">' |
| + | .. "<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>"; |
| end | | end |
| | | |
Line 17: |
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", // test\n"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], '%s*//[^"\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 |
| | | |
| --- Render a mod row in the SMAPI compatibility table. | | --- Render a mod row in the SMAPI compatibility table. |
| -- @param frame The arguments passed to the script. | | -- @param frame The arguments passed to the script. |
− | -- @test mw.log(p.entry({ args = { name="Lookup Anything", name2="LookupAnything", author="Pathoschild", author2="Pathos", id="Pathoschild.LookupAnything", ["old ids"]="LookupAnything", ["nexus id"]="541", ["cf id"]="4250", ["github"]="Pathoschild/StardewMods", warnings="warning A, warning B", links="https://google.ca" }})) | + | -- @test mw.log(p.entry({ args = { name="Content Patcher, ContentPatcher", author="Pathoschild, Pathos", id="Pathoschild.ContentPatcher, ContentPatcher", nexus="1915", chucklefish="4250", curse="309243,content-patcher", github="Pathoschild/StardewMods", warnings="warning A, warning B" }})) |
| function p.entry(frame) | | function p.entry(frame) |
| -- read input args | | -- read input args |
− | local names = private.parseCommaDelimited((frame.args["name"] or '') .. ',' .. (frame.args["name2"] or '')) | + | local names = private.parseCommaDelimited(frame.args["name"] or '') |
− | local authors = private.parseCommaDelimited((frame.args["author"] or '') .. ',' .. (frame.args["author2"] or '')) | + | local authors = private.parseCommaDelimited(frame.args["author"] or '') |
− | local ids = private.parseCommaDelimited((frame.args["id"] or '') .. ',' .. (frame.args["old ids"] or '')) | + | local ids = private.parseCommaDelimited(frame.args["id"] or '') |
− | local nexusID = private.emptyToNil(frame.args["nexus id"]) | + | local nexusId = private.emptyToNil(frame.args["nexus"]) |
| local github = private.emptyToNil(frame.args["github"]) | | local github = private.emptyToNil(frame.args["github"]) |
| local summary = private.emptyToNil(frame.args["summary"]) | | local summary = private.emptyToNil(frame.args["summary"]) |
Line 35: |
Line 70: |
| local unofficialVersion = private.emptyToNil(frame.args["unofficial version"]) | | local unofficialVersion = private.emptyToNil(frame.args["unofficial version"]) |
| local unofficialUrl = private.emptyToNil(frame.args["unofficial url"]) | | local unofficialUrl = private.emptyToNil(frame.args["unofficial url"]) |
− | local oldIDs = private.emptyToNil(frame.args["old ids"]) | + | local chucklefishId = private.emptyToNil(frame.args["chucklefish"]) |
− | local chucklefishID = private.emptyToNil(frame.args["cf id"]) | + | local curseforgeId = private.emptyToNil(frame.args["curse"]) |
− | local customUrl = private.emptyToNil(frame.args["url"]) | + | local moddropId = private.emptyToNil(frame.args["moddrop"]) |
− | local customSource = private.emptyToNil(frame.args["source"]) | + | local customUrl = private.emptyToNil(frame.args["url"]) |
− | local name2 = private.emptyToNil(frame.args["name2"]) | + | local customSource = private.emptyToNil(frame.args["source"]) |
− | local author2 = private.emptyToNil(frame.args["author2"]) | |
− | local links = private.parseCommaDelimited(frame.args["links"])
| |
− | local warnings = private.parseCommaDelimited(frame.args["warnings"])
| |
| | | |
− | local betaSummary = private.emptyToNil(frame.args["beta summary"]) | + | local warnings = private.parseCommaDelimited(frame.args["warnings"]) |
− | local betaBrokeIn = private.emptyToNil(frame.args["beta broke in"])
| + | local devNote = private.emptyToNil(frame.args["dev note"]) |
− | local betaStatus = private.emptyToNil(frame.args["beta status"])
| + | local contentPackFor = private.emptyToNil(frame.args["content pack for"]) |
− | local betaUnofficialVersion = private.emptyToNil(frame.args["beta unofficial version"]) | |
− | local betaUnofficialUrl = private.emptyToNil(frame.args["beta unofficial url"]) | |
| | | |
− | -- parse compatibility | + | local betaSummary = nil |
− | local compat = private.getCompatInfo(status, summary, brokeIn, unofficialVersion, unofficialUrl) | + | local betaBrokeIn = nil |
− | local betaCompat = nil | + | local betaStatus = nil |
− | if betaStatus or betaBrokeIn or betaUnofficialUrl or betaUnofficialVersion then | + | local betaUnofficialVersion = nil |
− | betaCompat = private.getCompatInfo(betaStatus, betaSummary, betaBrokeIn, betaUnofficialVersion, betaUnofficialUrl)
| + | local betaUnofficialUrl = nil |
− | end
| + | if enableBeta then |
− | | + | betaSummary = private.emptyToNil(frame.args["beta summary"]) |
− | -- get main URL
| + | betaBrokeIn = private.emptyToNil(frame.args["beta broke in"]) |
− | local url = nil | + | betaStatus = private.emptyToNil(frame.args["beta status"]) |
− | if nexusID then | + | betaUnofficialVersion = private.emptyToNil(frame.args["beta unofficial version"]) |
− | url = "https://www.nexusmods.com/stardewvalley/mods/" .. mw.uri.encode(nexusID, "PATH") | + | betaUnofficialUrl = private.emptyToNil(frame.args["beta unofficial url"]) |
− | elseif chucklefishID then
| |
− | url = "https://community.playstarbound.com/resources/" .. mw.uri.encode(chucklefishID, "PATH") | |
− | else
| |
− | url = customUrl | |
| end | | end |
| | | |
Line 72: |
Line 98: |
| sourceUrl = "https://github.com/" .. string.gsub(mw.uri.encode(github, "PATH"), "%%2F", "/") | | sourceUrl = "https://github.com/" .. string.gsub(mw.uri.encode(github, "PATH"), "%%2F", "/") |
| end | | end |
| + | local hasSource = sourceUrl ~= nil |
| | | |
− | -- get background color | + | -- parse compatibility |
− | local background = '#999' | + | local compat = private.getCompatInfo(status, summary, brokeIn, unofficialVersion, unofficialUrl, hasSource) |
− | if compat.status == "ok" or compat.status == "optional" then | + | local betaCompat = nil |
− | background = '#9F9' | + | if enableBeta and (betaStatus or betaBrokeIn or betaUnofficialUrl or betaUnofficialVersion) then |
− | elseif compat.status == "workaround" or compat.status == "unofficial" then | + | betaCompat = private.getCompatInfo(betaStatus, betaSummary, betaBrokeIn, betaUnofficialVersion, betaUnofficialUrl, hasSource) |
− | background = '#CF9' | + | end |
− | elseif compat.status == "broken" then | + | |
− | background = '#F99' | + | -- get main URL |
− | elseif compat.status == "obsolete" or compat.status == "abandoned" then | + | local url = nil |
− | background = '#999' | + | if nexusId then |
| + | url = "https://www.nexusmods.com/stardewvalley/mods/" .. mw.uri.encode(nexusId, "PATH") |
| + | elseif moddropId then |
| + | url = "https://www.moddrop.com/stardew-valley/mods/" .. mw.uri.encode(moddropId, "PATH") |
| + | elseif curseforgeId then |
| + | url = "https://www.curseforge.com/projects/" .. mw.uri.encode(curseforgeId, "PATH") |
| + | elseif chucklefishId then |
| + | url = "https://community.playstarbound.com/resources/" .. mw.uri.encode(chucklefishId, "PATH") |
| + | elseif customUrl then |
| + | url = customUrl |
| + | elseif hasSource then |
| + | url = sourceUrl |
| end | | end |
| | | |
Line 89: |
Line 127: |
| row:addClass("mod") | | row:addClass("mod") |
| row:attr("id", names[1] and mw.uri.anchorEncode(names[1])); | | row:attr("id", names[1] and mw.uri.anchorEncode(names[1])); |
| + | row:attr("data-id", table.concat(ids, ",")) |
| row:attr("data-name", table.concat(names, ",")) | | row:attr("data-name", table.concat(names, ",")) |
− | row:attr("data-id", table.concat(ids, ","))
| |
| row:attr("data-author", table.concat(authors, ",")) | | row:attr("data-author", table.concat(authors, ",")) |
− | row:attr("data-url", url) | + | row:attr("data-cf-id", chucklefishId) |
− | row:attr("data-nexus-id", nexusID) | + | row:attr("data-curseforge-id", curseforgeId) |
− | row:attr("data-cf-id", chucklefishID) | + | row:attr("data-moddrop-id", moddropId) |
| + | row:attr("data-nexus-id", nexusId) |
| row:attr("data-github", github) | | row:attr("data-github", github) |
| row:attr("data-custom-source", customSource) | | row:attr("data-custom-source", customSource) |
| + | row:attr("data-url", url) |
| row:attr("data-status", compat.status) | | row:attr("data-status", compat.status) |
| row:attr("data-summary", compat.summary) | | row:attr("data-summary", compat.summary) |
Line 102: |
Line 142: |
| row:attr("data-unofficial-version", compat.unofficialVersion) | | row:attr("data-unofficial-version", compat.unofficialVersion) |
| row:attr("data-unofficial-url", compat.unofficialUrl) | | row:attr("data-unofficial-url", compat.unofficialUrl) |
− | row:attr("data-beta-status", betaCompat and betaCompat.status) | + | if enableBeta then |
− | row:attr("data-beta-summary", betaCompat and betaCompat.summary)
| + | row:attr("data-beta-status", betaCompat and betaCompat.status) |
− | row:attr("data-beta-broke-in", betaCompat and betaCompat.brokeIn)
| + | row:attr("data-beta-summary", betaCompat and betaCompat.summary) |
− | row:attr("data-beta-unofficial-version", betaCompat and betaCompat.unofficialVersion)
| + | row:attr("data-beta-broke-in", betaCompat and betaCompat.brokeIn) |
− | row:attr("data-beta-unofficial-url", betaCompat and betaCompat.unofficialUrl)
| + | row:attr("data-beta-unofficial-version", betaCompat and betaCompat.unofficialVersion) |
| + | row:attr("data-beta-unofficial-url", betaCompat and betaCompat.unofficialUrl) |
| + | end |
| row:attr("data-warnings", private.emptyToNil(table.concat(warnings, ","))) | | row:attr("data-warnings", private.emptyToNil(table.concat(warnings, ","))) |
− | row:attr("style", "line-height: 1em; background: " .. background .. ";") | + | row:attr("data-content-pack-for", contentPackFor) |
| + | row:attr("data-dev-note", devNote) |
| row:newline() | | row:newline() |
| | | |
Line 164: |
Line 207: |
| | | |
| -- stable status | | -- stable status |
− | field:wikitext(compat.summaryIcon .. " " .. compat.summary) | + | field:wikitext("<span class=\"mod-summary\">" .. compat.summaryIcon .. " " .. compat.summary .. "</span>") |
| if compat.status == "optional" then | | if compat.status == "optional" then |
| field:wikitext("<ref name=\"optional-update\" />") | | field:wikitext("<ref name=\"optional-update\" />") |
Line 170: |
Line 213: |
| | | |
| -- beta status | | -- beta status |
− | if betaCompat ~= nill then | + | if betaCompat ~= nil then |
| field:wikitext("<br />") | | field:wikitext("<br />") |
− | field:wikitext("'''SDV beta only:''' " .. betaCompat.summaryIcon .. " " .. betaCompat.summary) | + | field:wikitext("'''SDV 1.6 beta only:''' <span class=\"mod-beta-summary\">" .. betaCompat.summaryIcon .. " " .. betaCompat.summary .. "</span>") |
| if betaCompat.status == "optional" then | | if betaCompat.status == "optional" then |
| field:wikitext("<ref name=\"optional-update\" />") | | field:wikitext("<ref name=\"optional-update\" />") |
Line 209: |
Line 252: |
| do | | do |
| if sourceUrl then | | if sourceUrl then |
− | row:wikitext("<td>[" .. sourceUrl .. " source]</td>") | + | row:wikitext("<td class=\"mod-source\">[" .. sourceUrl .. " source]</td>") |
| else | | else |
− | row:wikitext("<td><span style=\"color: red; font-size: 0.85em; opacity: 0.5;\">closed source</span></td>") | + | row:wikitext("<td class=\"mod-source\"><span>closed source</span></td>") |
| end | | end |
| row:newline() | | row:newline() |
Line 219: |
Line 262: |
| do | | do |
| local field = mw.html.create("td") | | local field = mw.html.create("td") |
− | field:attr("style", "font-size: 0.8em") | + | field:attr("class", "mod-metadata") |
| | | |
| -- anchor | | -- anchor |
| field:wikitext("[[#" .. (names[1] or '') .. "|#]] ") | | field:wikitext("[[#" .. (names[1] or '') .. "|#]] ") |
| | | |
− | -- reference links | + | -- dev note |
− | do | + | if devNote then |
− | local linkCount = #links | + | local devNoteField = mw.html.create("abbr") |
− | for i = 1, linkCount do | + | devNoteField:attr("title", devNote) |
− | field:wikitext("[" .. links[i] .. " " .. i .. "] ")
| + | devNoteField:wikitext("[dev note]") |
− | end | + | |
| + | field:node(devNoteField) |
| end | | end |
| | | |
− | -- 'no id' warning | + | -- validation |
| if #ids == 0 then | | if #ids == 0 then |
− | field:wikitext("⚠ no id") | + | field:wikitext("[⚠ no id] ") |
| end | | end |
− |
| |
− | -- backwards-compatible metadata (temporary)
| |
− | field:wikitext('<div div class="mod-metadata" style="display: none;">')
| |
− | field:wikitext('<div class="mod-anchor">' .. (names[1] and mw.uri.anchorEncode(names[1]) or '') .. "</div>")
| |
− | field:wikitext('<div class="mod-id">' .. mw.text.encode(table.concat(ids, ",")) .. "</div>")
| |
− | field:wikitext('<div class="mod-url">' .. mw.text.encode(url or '') .. "</div>")
| |
− | if nexusID ~= nil then
| |
− | field:wikitext('<div class="mod-nexus-id">' .. mw.text.encode(nexusID or '') .. "</div>")
| |
− | end
| |
− | if chucklefishID ~= nil then
| |
− | field:wikitext('<div class="mod-cf-id">' .. mw.text.encode(chucklefishID or '') .. "</div>")
| |
− | end
| |
− | if github ~= nil then
| |
− | field:wikitext('<div class="mod-github">' .. mw.text.encode(github or '') .. '</div>')
| |
− | end
| |
− | if customSource ~= nil then
| |
− | field:wikitext('<div class="mod-custom-source">' .. mw.text.encode(customSource or '') .. '</div>')
| |
− | end
| |
− | field:wikitext('<div class="mod-status">' .. mw.text.encode(compat.status) .. '</div>')
| |
− | if compat.brokeIn ~= nil then
| |
− | field:wikitext('<div class="mod-broke-in">' .. mw.text.encode(compat.brokeIn) .. '</div>')
| |
− | end
| |
− | if compat.unofficialVersion ~= nil and compat.unofficialUrl ~= nil then
| |
− | field:wikitext('<div class="mod-unofficial-version">' .. mw.text.encode(compat.unofficialVersion) .. '</div>')
| |
− | field:wikitext('<div class="mod-unofficial-url">' .. mw.text.encode(compat.unofficialUrl) .. '</div>')
| |
− | end
| |
− | if betaCompat ~= nil then
| |
− | field:wikitext('<div class="mod-beta-status">' .. mw.text.encode(betaCompat.status) .. '</div>')
| |
− | if betaCompat.brokeIn ~= nil then
| |
− | field:wikitext('<div class="mod-beta-broke-in">' .. mw.text.encode(betaCompat.brokeIn) .. '</div>')
| |
− | end
| |
− | if betaCompat.unofficialVersion ~= nil and betaCompat.unofficialUrl ~= nil then
| |
− | field:wikitext('<div class="mod-beta-unofficial-version">' .. mw.text.encode(betaCompat.unofficialVersion) .. '</div>')
| |
− | field:wikitext('<div class="mod-beta-unofficial-url">' .. mw.text.encode(betaCompat.unofficialUrl) .. '</div>')
| |
− | end
| |
− | end
| |
− | if #warnings > 0 then
| |
− | field:wikitext('<div class="mod-warnings">' .. mw.text.encode(table.concat(warnings, ",")) .. '</div>')
| |
− | end
| |
− | field.wikitext("</div>")
| |
| | | |
| row:node(field) | | row:node(field) |
Line 283: |
Line 287: |
| end | | end |
| | | |
− | -- Print an HTML attribute (like key="value") if a non-empty value is specified. This is a temporary method during the migration to Lua. | + | |
| + | --########## |
| + | --## Private functions |
| + | --########## |
| + | -- Get the <templatestyles> tag for the module's stylesheet. |
| -- @param frame The arguments passed to the script. | | -- @param frame The arguments passed to the script. |
− | -- @test mw.log(p.printAttribute({ args = { "someKey", "some \"'<> value" }}))
| + | function private.style(frame) |
− | function p.printAttribute(frame) | + | if frame.extensionTag ~= nil then |
− | local key = frame.args[1] | + | return frame:extensionTag('templatestyles', '', {src = 'Module:SMAPI compatibility/styles.css'}) |
− | local value = frame.args[2]
| |
− | | |
− | if value == nil or value == "" then
| |
− | return "" | |
| else | | else |
− | return key .. "=\"" .. mw.text.encode(value) .. "\"" | + | return "" -- called from the debug console |
| end | | end |
| end | | end |
| | | |
− |
| |
− | --##########
| |
− | --## Private functions
| |
− | --##########
| |
| -- Get the normalised compatibility info for a mod. | | -- Get the normalised compatibility info for a mod. |
| -- @param status The specified status code. If nil or blank, it'll be derived from the other fields. | | -- @param status The specified status code. If nil or blank, it'll be derived from the other fields. |
Line 331: |
Line 331: |
| | | |
| -- derive summary | | -- derive summary |
− | if summary == nil then | + | if not summary then |
| if status == "ok" then | | if status == "ok" then |
| summary = "use latest version." | | summary = "use latest version." |
Line 351: |
Line 351: |
| end | | end |
| elseif status == "obsolete" then | | elseif status == "obsolete" then |
− | summary = "obsolete." | + | summary = "remove this mod (obsolete)." |
| elseif status == "abandoned" then | | elseif status == "abandoned" then |
− | summary = "no longer maintained." | + | summary = "remove this mod (no longer maintained)." |
| else | | else |
| summary = "'''error:''' unknown status '" .. status .. "'." | | summary = "'''error:''' unknown status '" .. status .. "'." |
Line 367: |
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 |