Module:RawImage/sandbox
Appearance
This is the module sandbox page for Module:RawImage (diff). |
--[=[
rawimage
Implements [[Template:Raw image]]
Usage:
'pagename' should be the name of a page in Page: namespace, with or without the "Page:" in front.
Note for editors: rawimage displays images centred in the user's standard thumb size.
It is not recommended to offer further display options,
as this would disincentivise the replacement of raw images.
]=]
require('strict')
local p = {}
local getArgs = require('Module:Arguments').getArgs
local function error_message(message)
return require('Module:Error').error({message = table.concat({'[[Module:RawImage]] error:', message}, ' ') .. ' Invoke with {{raw image|' .. mw.title.getCurrentTitle().text .. '}}'})
end
local function _rawimage(args)
local pagename = args[1] or args.pagename
-- Bail out if no page was specified
if not pagename then
return error_message()
end
-- Check if a (valid) specific page has been given.
local page = mw.title.new(pagename, 'Page')
if not page then
return error_message('Invalid page name ' .. pagename .. 'specified.')
end
local cats = {}
-- Add link if an IA identifier was provided
local iacat
local iatxt = ''
if args.ia then
iacat = '[[Category:' .. 'Raw images with Internet Archive link' .. ']]'
iatxt = 'The original scan of this work is available at the ' .. '[' .. 'https://archive.org/details/' .. args.ia .. ' Internet Archive]'
local iazip = args.iazip or args.ia
local iaziplink = 'https://archive.org/download/' .. args.ia .. '/' .. iazip .. '_jp2.zip/'
iatxt = iatxt .. '([' .. iaziplink .. ' all files]).'
if args.iaimg then
local iaimglink = iaziplink .. iazip .. '_jp2%2F' .. args.iaimg
iatxt = iatxt .. ' A [' .. iaimglink .. ' high-resolution scan of this page] is available.'
iacat = '[[Category:' .. 'Raw images with Internet Archive page link' .. ']]'
end
else
iacat = '[[Category:' .. 'Raw images without Internet Archive link' .. ']]'
end
table.insert(cats, iacat)
-- Set the category to be used in output depending on namespace
if mw.title.getCurrentTitle():inNamespace('Page') then
table.insert(cats, '[[Category:' .. 'Pages with raw images' .. ']]')
else
table.insert(cats, '[[Category:' .. 'Texts with raw images' .. ']]')
end
if page.isSubpage then
-- this page is a subpage, so compose the name of the hi-res file.
local pagebase = page.baseText
local pagenum = page.subpageText
local hiRes = mw.title.new(pagebase .. '-' .. pagenum .. '.png', 'File')
-- Set defaults for the output
local image = '[[File:' .. pagebase .. '|page=' .. pagenum .. '|frameless|center|360px]]'
local text = '(Upload an image to replace this placeholder.)'
-- Check if the hi-res version exists and use that if available
if hiRes.fileExists then
image = '[[File:' .. hiRes.text .. '|frameless|center|360px]]'
text = '([[:File:' .. hiRes.text .. '|Improve this image]])'
end
if iatxt ~= '' then
text = iatxt
end
-- Format the output
local frame = mw.getCurrentFrame()
local outtxt = frame:expandTemplate {
title = 'Block center',
args = {image .. frame:expandTemplate {
title = 'Right',
args = {frame:expandTemplate {
title = 'X-smaller block',
args = {text}
}}
}}
} .. table.concat(cats)
return outtxt
else
-- Not a subpage, so this page corresponds to a single-page image
return '[[File:' .. page.text .. '|frameless|center|360px]]' .. table.concat(cats)
end
end
function p.rawimage(frame)
-- Template frame will never contain args, so skip it for performance
local args = getArgs(frame, {
wrappers = 'Template:raw image'
})
return _rawimage(args)
end
return p