Module:Sandbox/Lakelimbo/Filetype
This module is used to detect an image's file type.
What it does
When used on a page in the File namespace, the module will parse the page title, and detect the file extension based on the characters after the last period. It will then check that extension against a list, and return the file type as a text string.
If the module is invoked in a different namespace, or the file has no file extension, the module will return Invalid file extension.. If the module is invoked on a file page with an extension not on the current list, it will return Unsupported file type. instead.
Currently, the module can detect the following file types:
| File extension | Returned value |
|---|---|
.png
|
PNG |
.jpg .jpeg
|
JPEG |
.gif
|
GIF |
.svg
|
SVG |
.webp
|
WEBP |
This module also accepts one argument, which allows for a string of text to be examined, rather than the page title.
How it works
The first part of the module creates a table called "extensions" that contains the list of supported file types, and how their file extensions will look.
local f = {}
local extensions = {
png = { "png" },
jpeg = { "jpeg", "jpg" },
gif = { "gif" },
svg = { "svg" },
webp = { "webp" },
}
The next part creates the function called "main", the only function of this template.
function f.main(frame)
The next part creates a local variable called "filename" and uses either the first argument passed to the module, or uses the page title of the page that invoked the module. The contents of "filename" are then converted to lowercase.
local filename = frame.args[1] or mw.title.getCurrentTitle().text filename = mw.ustring.lower(filename)
Next, the local variable "ext" checks "filename" to to see if it ends with characters after a period. If not, it will return the string Invalid file extension., and end the module.
local ext = filename:match("^.+%.([^.]+)$")
if not ext then
return tostring("Invalid file extension.")
end
If the "ext" check was successful, the "result" variable will check for a match between the file extensions in the "extensions" table and the contents of the "ext" variable. If there is a match, "result" will be converted to uppercase.
local result
for main, aliases in pairs(extensions) do
for _, alias in ipairs(aliases) do
if ext == alias then
result = mw.ustring.upper(main)
end
end
end
If there was no match between "ext" and the "extensions" table, the module will return Unsupported file type. and end.
if not result then
return tostring("Unsupported file type.")
end
Otherwise, the module will return the contents of "result" and end.
return tostring(result) end return f
--[[
Simple module to output and alias a valid file
extension.
--]]
local f = {}
---@type table<string, table<string>>
local extensions = {
png = { "png" },
jpeg = { "jpeg", "jpg" },
gif = { "gif" },
svg = { "svg" },
webp = { "webp" },
}
---@return string
function f.main(frame)
---@type string
local filename = frame.args[1] or mw.title.getCurrentTitle().text
filename = mw.ustring.lower(filename)
-- Take the filename and check whether it
-- contains an extension
---@type string | nil
local ext = filename:match("^.+%.([^.]+)$")
if not ext then
return tostring("Invalid file extension.")
end
-- Loop and yield the result to `result`.
-- If the extension is not on the list,
-- it will stay as nil.
---@type string | nil
local result
for main, aliases in pairs(extensions) do
for _, alias in ipairs(aliases) do
if ext == alias then
result = mw.ustring.upper(main)
end
end
end
if not result then
return tostring("Unsupported file type.")
end
return tostring(result)
end
return f
- No categories associated with this article