Module:Color: Difference between revisions
Jump to navigation
Jump to search
no edit summary
PhantomCaleb (talk | contribs) No edit summary |
PhantomCaleb (talk | contribs) No edit summary |
||
(15 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
local | local p = {} | ||
local h = {} | |||
local data = mw.loadData("Module:Color/Data") | |||
local utilsString = require("Module:UtilsString") | local utilsString = require("Module:UtilsString") | ||
local | local CAT_INVALID_ARGS = "[[Category:"..require("Module:Constants/category/invalidArgs").."]]" | ||
local | local CAT_INVALID_COLOR = "[[Category:Articles using invalid color names]]" | ||
local | function h.warn(msg) | ||
local utilsError = require("Module:UtilsError") | |||
utilsError.warn(msg) | |||
end | |||
local | function h.invalidColor(colorId) | ||
local TransclusionArguments = require("Module:Transclusion Arguments") | |||
h.warn(string.format("<code>%s</code> is not a valid color name. See [[Template:Color]] for a list of supported colors.", colorId)) | |||
-- We only store invalid colors for performance reasons | |||
-- Storing all template usages seems to break pages that use the template a lot | |||
TransclusionArguments.store({ | |||
module = "Module:Color", | |||
args = { | |||
[1] = colorId, | |||
[2] = text, | |||
}, | |||
isValid = false, | |||
}) | |||
end | |||
function p.Main(frame) | function p.Main(frame) | ||
Line 24: | Line 43: | ||
function p.color(colorId, text) | function p.color(colorId, text) | ||
if text == nil or text == "" then | if text == nil or text == "" then | ||
h.warn("<code>text</code> parameter is required.") | |||
return text or "", | return text or "", CAT_INVALID_ARGS | ||
end | end | ||
if colorId == nil or colorId == "" then | if colorId == nil or colorId == "" then | ||
h.warn("<code>color</code> parameter is required.") | |||
return text, | return text, CAT_INVALID_ARGS | ||
end | end | ||
if string.find(colorId, "\n", 1, true) or string.find(colorId, "\r", 1, true) or utilsString.startsWith(colorId, " ") or utilsString.endsWith(colorId, " ") then | if string.find(colorId, "\n", 1, true) or string.find(colorId, "\r", 1, true) or utilsString.startsWith(colorId, " ") or utilsString.endsWith(colorId, " ") then | ||
h.warn("<code>color</code> argument contains invalid whitespace such as newlines or leading/trailing spaces.") | |||
return text, | return text, CAT_INVALID_ARGS | ||
end | end | ||
local colorData = data.colors[colorId] | local colorData = data.colors[colorId] | ||
if colorData == nil then | if colorData == nil then | ||
h.invalidColor(colorId) | |||
return text, | return text, CAT_INVALID_COLOR | ||
end | end | ||
local colorValue = colorData.color | local colorValue = colorData.color | ||
local html = mw.html.create("span") | local html = mw.html.create("span") | ||
:addClass(" | :addClass("zw-color") | ||
:wikitext(text) | :wikitext(text) | ||
if utilsString.startsWith(colorValue, "linear-gradient") then | if utilsString.startsWith(colorValue, "linear-gradient") then | ||
Line 65: | Line 84: | ||
action = "See [[Template:Color]] for a list of supported colors." | action = "See [[Template:Color]] for a list of supported colors." | ||
end | end | ||
h.warn(string.format("Color <code>%s</code> is being discontinued. %s", colorId, action)) | |||
return result, | return result, CAT_INVALID_COLOR | ||
end | end | ||
return result | return result | ||
Line 92: | Line 111: | ||
} | } | ||
} | } | ||
local games = game and {game} or Franchise.enum({ | local games = game and {game} or Franchise.enum({includeNonfiction = true}) | ||
local listAllGames = game == nil | local listAllGames = game == nil | ||
local colorKeys = utilsTable.keys(data.colors) | local colorKeys = utilsTable.keys(data.colors) | ||
Line 225: | Line 244: | ||
} | } | ||
p.Schemas | function p.Schemas() | ||
return { | |||
-- For auto-generated doc at Module:Color/Documentation | |||
color = { | |||
colorName = { | |||
type = "string", | |||
required = true, | |||
desc = "The name of a color from [[Module:Color/Data]]", | |||
}, | |||
text = { | |||
type = "string", | |||
required = true, | |||
desc = "The text to color", | |||
}, | |||
}, | }, | ||
-- For auto-generated doc at Module:Color/Data/Documentation | |||
type = " | Data = { | ||
type = "record", | |||
required = true, | required = true, | ||
properties = { | |||
{ | |||
name = "colors", | |||
required = true, | |||
type = "map", | |||
keys = { | |||
type = "string", | |||
}, | |||
values = { | |||
type = "record", | |||
properties = { | |||
{ | |||
name = "color", | |||
required = true, | |||
type = "string", | |||
desc = "A [https://developer.mozilla.org/en-US/docs/Web/CSS/hex-color hex color] (with preceding #) or a [https://developer.mozilla.org/en-US/docs/Web/CSS/gradient/linear-gradient linear-gradient].", | |||
}, | |||
{ | |||
name = "isDefault", | |||
type = "boolean", | |||
desc = "Set this to <code>true</code> when the color is the game's default dialogue color. Used by [[Module:Cite]] to set the default text color for citations." | |||
}, | |||
{ | |||
name = "defaultFor", | |||
type = "array", | |||
name = "notes", | items = { type = "string" }, | ||
desc = "A list of wiki page names referring to the characters and/or interfaces whose text uses the color by default. Used by [[Module:Cite]] to set the default text color for citations." | |||
}, | |||
{ | |||
name = "notes", | |||
type = "string", | |||
desc = "Anything that editors should know about the color – the context in which it appears, other colors it could be confused with, etc.", | |||
}, | |||
{ | |||
name = "deprecated", | |||
oneOf = { | |||
{ | |||
type = "string" | |||
}, | |||
{ | |||
type = "boolean" | |||
}, | |||
}, | }, | ||
desc = "The name of the color that should be used instead of this color going forward. If multiple colors apply, just put <code>true</code>.", | |||
}, | }, | ||
} | } | ||
} | } | ||
Line 282: | Line 314: | ||
} | } | ||
} | } | ||
end | |||
p.Documentation | function p.Documentation() | ||
return { | |||
color = { | |||
params = {"colorName", "text"}, | params = {"colorName", "text"}, | ||
returns = { | returns = { | ||
Line 294: | Line 327: | ||
{ | { | ||
args = {"TMC Blue", "guy in green tights"}, | args = {"TMC Blue", "guy in green tights"}, | ||
expect = {'<span class=" | expect = {'<span class="zw-color" style="color:#37acbe">guy in green tights</span>', nil}, | ||
}, | }, | ||
{ | { | ||
args = {"notAColor", "foo"}, | args = {"notAColor", "foo"}, | ||
expect = {"foo", "[[Category:Articles | expect = {"foo", "[[Category:Articles using invalid color names]]"}, | ||
}, | }, | ||
}, | }, | ||
}, | }, | ||
} | } | ||
end | |||
return p | return p |