Module:HotlinkVignette
Jump to navigation
Jump to search
Documentation for this module may be created at Module:HotlinkVignette/doc
-- <nowiki> local p = {} local getArgs = require("Module:Arguments")["getArgs"] local hash = require("Module:Hash") -- "switch statement" for path based on crop position local positions = { top = function (args) if not args["width"] or not args["height"] then return nil end local result = "/top-crop" if not args["upscale"] then result = result .. "-down" end result = result .. "/width/" .. args["width"] .. "/height/" .. args["height"] return result end, center = function (args) if not args["width"] or not args["height"] then return nil end local result = "/zoom-crop" if not args["upscale"] then result = result .. "-down" end result = result .. "/width/" .. args["width"] .. "/height/" .. args["height"] return result end, custom = function (args) if not args["width"] or not args["window width"] or not args["window height"] then return nil end local result = "/window-crop" if args["fixed"] and args["height"] then result = result .. "-fixed/width/" .. args["width"] .. "/height/" .. args["height"] else result = result .. "/width/" .. args["width"] end result = result .. "/x-offset/" .. (args["x offset"] or 0) .. "/y-offset/" .. (args["y offset"] or 0) .. "/window-width/" .. args["window width"] .. "/window-height/" .. args["window height"] return result end } -- "switch statement" for path based on mode local modes = { set = function (args) if not args["width"] or not args["height"] then return nil end local result = "/fixed-aspect-ratio" if not args["upscale"] then result = result .. "-down" end result = result .. "/width/" .. args["width"] .. "/height/" .. args["height"] return result end, width = function (args) if not args["width"] then return nil end local result = "/scale-to-width" if not args["upscale"] then result = result .. "-down" end result = result .. "/" .. args["width"] return result end, height = function (args) if not args["height"] then return nil end return "/scale-to-height-down/" .. args["height"] end, max = function (args) if not args["width"] or not args["height"] then return nil end local result = "/thumbnail" if not args["upscale"] then result = result .. "-down" end result = result .. "/width/" .. args["width"] .. "/height/" .. args["height"] return result end, crop = function (args) return args["position"] and type(positions[args["position"]]) == "function" and positions[args["position"]](args) or nil end } function p.main(frame) -- attempt to determine if provided with valid frame object local no_frame = not (type(frame.preprocess) == "function") --[[ - retrieve arguments - if no valid frame object, assume table of arguments --]] local raw_args, args if no_frame then raw_args = frame args = mw.clone(frame) for k, v in pairs(args) do if type(v) == "string" then v = v:match("^%s*(.-)%s*$") if v == "" then v = nil end args[k] = v end end else raw_args = getArgs(frame, {trim = false, removeBlanks = false}) args = getArgs(frame) end local image = args["image"] -- output error message if no image was specified if not image then return raw_args["error"] or ("<strong class=\"error\">Template error:" .. " An image has not been specified.</strong>") end -- determine langauge and domain for image URL local language = args["language"] or mw.language.getContentLanguage():getCode() if language == "en" then language = nil end local domain = args["domain"] or mw.site.server:match("^.*/(.*)%.fandom%.com.*$") or mw.site.server:match("^.*/(.*)%.wikia%.org.*$") if not args["keep subdomains"] then domain = domain:match("^.-%.?([^%.]*)$") end if args["language subdomains"] and language then domain = language .. "." .. domain language = nil end --[[ - if possible, assume local wiki and use parser function "filepath" to get base image URL - else, contruct base image URL from arguments - reassign "image" variable because we can --]] if not (args["domain"] or args["language"]) and not no_frame then image = frame:preprocess("{{filepath: " .. image .. "}}") :match("^(.*/).-$") else -- determine code for image URL local code = args["code"] if not code or #code < 2 then code = hash.MD5(image).hexdigest() end -- contruct base image URL from arguments image = "https://static.wikia.nocookie.net/" .. domain .. "/images/" .. mw.ustring.sub(code, 1, 1) .. "/" .. mw.ustring.sub(code, 1, 2) .. "/" .. mw.uri.encode(image, "WIKI") .. "/revision/" end image = image .. (args["replaced"] or "latest") -- create URI object and, if applicable, add language local url = mw.uri.new(image) if language then url:extend({["path-prefix"] = language}) end local link = args["link"] local result = "" --[[ - if applicable, construct link open and set as return value - if link specified as non-whitespace, use specified target --- if internal/interwiki format, retrieve full URL - if link not specified, use base image URL --]] if link then if link:match("://") then result = "[" .. link else result = "<span class=\"plainlinks\">[" .. tostring(mw.uri.fullUrl(link)) end elseif not raw_args["link"] then result = "<span class=\"plainlinks\">[" .. tostring(url) end -- add to path based on mode and, if applicable, crop position url["path"] = url["path"] .. (args["mode"] and type(modes[args["mode"]]) == "function" and modes[args["mode"]](args) or "") -- construct query string if args["query"] then url:extend(mw.uri.parseQueryString(args["query"])) end if args["fill"] then if url["query"] and url.query.fill then if type(url.query.fill) == "table" then table.insert(url.query.fill, args["fill"]) else url.query.fill = {url.query.fill, args["fill"]} end else url:extend({fill = args["fill"]}) end end -- if language is applicable and was overwritten, re-add it to query if language and language ~= url.query["path-prefix"] then if type(url.query["path-prefix"]) == "table" then table.insert(url.query["path-prefix"], language) else url.query["path-prefix"] = {url.query["path-prefix"], language} end end -- concatenate image URL to return value result = result .. " " .. tostring(url) -- if applicable, construct link close and concatenate to return value if link then if link:match("://") then result = result .. "]" else result = result .. "]</span>" end elseif not raw_args["link"] then result = result .. "]</span>" end return result end return p -- </nowiki>