Documentation for this module may be created at Module:Graphical link/doc
local p = {}
local HF = require('Module:HF')
local getlist = function( frame, name, ... )
return frame:expandTemplate{ title = ':'..name, args = ... }
end
function p.main( frame )
frame = frame:getParent()
-- decide text
local link = frame.args[1]
local text = frame.args[2]
if text == '' or text == nil then
text = link
elseif text == 's' then
text = link .. 's'
end
-- decide image
local image = nil
local allimages = {}
local arg = HF.trim(frame.args[3])
-- set seed because it seems to never change unless manually set
math.randomseed(tonumber(os.date('%s')))
if string.sub( arg, -5 ) == '/list' and mw.title.new(arg) ~= nil then
-- get images from a list page
local imagetag = frame.args[4]
if imagetag == nil then
imagetag = 'image'
end
local size = tonumber(getlist( frame, arg, {'count'}))
local choice = math.random(size)
for i = 1, size do
local value = getlist( frame, arg, { i } )
value = getlist( frame, value..'/data', { '', imagetag } )
if i == choice then
image = value
end
allimages[i] = value
end
else
-- get images from arguments
local size = 0
for i, value in ipairs(frame.args) do
if i > 2 then -- avoid the text arguments
size = size + 1
allimages[size] = value
end
end
local choice = math.random(size)
image = allimages[choice]
end
-- decide on properties
local size = frame.args['size']
if HF.isempty(size) then
size = '64px'
elseif tonumber(size) ~= nil then
size = size..'px'
end
local imagesize = frame.args['imagesize']
if HF.isempty(imagesize) then
imagesize = '48px'
elseif tonumber(imagesize) ~= nil then
imagesize = imagesize..'px'
end
local fontsize = frame.args['fontsize']
if HF.isempty(fontsize) then
fontsize = 'inherit'
elseif tonumber(fontsize) ~= nil then
fontsize = fontsize..'px'
end
-- create the actual content
local wrapper = mw.html.create('span')
wrapper: css{
float = 'left',
position = 'relative',
width = size,
height = size,
['background-color'] = '#515151',
['border-radius'] = '7px',
margin = '8px',
padding = '4px',
border = '3px outset #383838',
['text-align'] = 'center',
['word-wrap'] = 'normal'
}
: wikitext(table.concat{ '[','[File:', image, '|center|', imagesize, '|alt=', text,
'|link=', link, ']', ']' })
if string.sub( link, 0, 3 ) == 'http' then
wrapper: node(
mw.html.create('span')
: css{
position = 'absolute',
padding = '0px',
width = size,
bottom = '2px',
left = '4px',
['font-size'] = fontsize
}
: wikitext(table.concat{'[', link, ' ', text, ']'})
)
else
wrapper: node(
mw.html.create('span')
: css{
position = 'absolute',
padding = '0px',
width = size,
bottom = '2px',
left = '4px',
['font-size'] = fontsize
}
: wikitext(table.concat{'[', '[', link, '|', text, ']', ']'})
)
end
-- add non used images so they don't appear on the wiki feed
for i, value in ipairs(allimages) do
allimages[i] = '['..'[File:'..value..'|1px]'..']'
end
wrapper:node(
mw.html.create('span')
:css('display', 'none')
:wikitext(table.concat(allimages))
)
return wrapper
end
return p