Module:File: Difference between revisions
From Neodyland Wiki
More actions
m extend |
m too lazy now for completing this; volunteers welcome |
||
| Line 6: | Line 6: | ||
local parts = mw.text.split( title, '.', true ) | local parts = mw.text.split( title, '.', true ) | ||
return parts[#parts] | return parts[#parts] | ||
end | |||
-- mapping file extensions to MIME-types | |||
funcs.extensionMap = { | |||
PNG = "image/png", | |||
SVG = "image/svg+xml", | |||
TIFF = "image/tiff", | |||
TIF = "image/tiff", | |||
JPEG = "image/jpeg", | |||
JPE = "image/jpeg" | |||
} | |||
-- =p.File("Foo.bar.svg").mime() | |||
funcs.mime = function() | |||
local extension = funcs.extension():upper() | |||
return funcs.extensionMap[extension] or "unknown" | |||
end | end | ||
return funcs | return funcs | ||
| Line 17: | Line 31: | ||
p.extensionUpper = function(frame) | p.extensionUpper = function(frame) | ||
return File(frame.args[1] or frame.args["file"] or frame.args["title"]).extension():upper() | return File(frame.args[1] or frame.args["file"] or frame.args["title"]).extension():upper() | ||
end | |||
p.mime = function(frame) | |||
return File(frame.args[1] or frame.args["file"] or frame.args["title"]).mime() | |||
end | end | ||
return p | return p | ||
Revision as of 14:15, 1 October 2013
This documentation is transcluded from Module:File/doc.
Scope
File title and file information. If the code here grows beyond a border line, this should be converted to an interface and the processing code should be outsourced.[Please clarify – October 2024]
Usage
Note that an error is thrown if the title can't be constructed (e.g. a title containing invalid characters is supplied) or metadata isn't available (e.g. due to missing file or extraction error). It is your responsibility to catch them; in Lua through pcall(); from Wikitext as {{#iferror: {{#invoke:File|function|file=File:P%bar]baz.qqq}} | meaningful message}}.
From Wikitext
Examples:
{{#invoke:File|woExtension|file=File:Test.C.JpeG}}→Script error: The function "woExtension" does not exist.{{#invoke:File|extension|file=File:Test.C.JpeG}}→jpeg{{#invoke:File|csExtension|file=File:Test.C.JpeG}}→Script error: The function "csExtension" does not exist.{{#invoke:File|mime|file=File:DOESNOTEXIST-0a8de8c83.Png}}→image/pngdeprecated (but works even if the file does not exist and has no metadata){{#invoke:File|mimeType|file=File:DOESNOTEXIST-0a8de8c83.Png}}→Script error: The function "mimeType" does not exist.preferred (but will not work if the file does not exist to parse its metadata){{#invoke:File|fileExists|file=File:Commons-logo.png}}→Script error: The function "fileExists" does not exist.{{#invoke:File|fileExists|file=File:DOESNOTEXIST-0a8de8c83.png}}→Script error: The function "fileExists" does not exist.{{#invoke:File|fileExistsRelaxed|file=File:Ba[!#llot-sprite.png}}→Script error: The function "fileExistsRelaxed" does not exist.
{{#invoke:File|width|file=File:Commons-logo.png}}→Script error: The function "width" does not exist.{{#invoke:File|height|file=File:Commons-logo.png}}→Script error: The function "height" does not exist.{{#invoke:File|dimensions|file=File:Commons-logo.png}}→Script error: The function "dimensions" does not exist.{{#invoke:File|size|file=File:Commons-logo.png}}→Script error: The function "size" does not exist.
-- Module for handling Media files (Origin: Wikimedia Commons)
local File = function(title)
local funcs = {}
-- =p.File("Foo.bar.svg").extension()
funcs.extension = function()
local parts = mw.text.split( title, '.', true )
return parts[#parts]
end
-- mapping file extensions to MIME-types
funcs.extensionMap = {
PNG = "image/png",
SVG = "image/svg+xml",
TIFF = "image/tiff",
TIF = "image/tiff",
JPEG = "image/jpeg",
JPE = "image/jpeg"
}
-- =p.File("Foo.bar.svg").mime()
funcs.mime = function()
local extension = funcs.extension():upper()
return funcs.extensionMap[extension] or "unknown"
end
return funcs
end
local p = {}
p.File = File
p.extension = function(frame)
return File(frame.args[1] or frame.args["file"] or frame.args["title"]).extension():lower()
end
p.extensionUpper = function(frame)
return File(frame.args[1] or frame.args["file"] or frame.args["title"]).extension():upper()
end
p.mime = function(frame)
return File(frame.args[1] or frame.args["file"] or frame.args["title"]).mime()
end
return p