Module:ImageLink

local ImageLink = {}

-- Dependencies local HF = require("Dev:HF") local FN = require("Module:Filename")

function ImageLink.basic(frame) local args = arguments(frame.args) local link, img, label = ImageLink._createLink(args) return link end

function ImageLink._createLink(args) -- define args.display (default: inline) args['display'] = (({['inlineblock'] = 'inline-block'})[args['display']]) or args['display'] or 'inline' -- define args.label (before or after; default: after) args['label'] = (({['after'] = 'after', ['before'] = 'before'})[args['label']]) or 'after' -- define args.image (default: '') args['image'] = args['image'] or '' -- define args.border (true or false; default: true) args['border'] = ternary(args['border'], true, false) -- define args.width, args.height (%%x%%px or %%x%% or %%px or %%) local width, height if(args['size']) then width, height = getSize(args['size']) end args['width'] = tonumber(mw.ustring.match(args['width'] or width or '', '%d*')) args['height'] = tonumber(mw.ustring.match(args['height'] or height or '', '%d*')) -- define args.selflink (default: articlename) args['selflink'] = args['selflink'] or mw.title.getCurrentTitle.prefixedText -- define args.link (default: '') args['link'] = args['link'] or '' if args['link'] == '*none*' then args['link'] = '' end -- define args.text (default: '') args['text'] = args['text'] or args['link'] or '' -- define args.text (default: ' ') args['separator'] = args['separator'] or ' ' if args['text'] == '*none*' then args['text'] = '' end if args['text'] == '' then args['label'] = 'none' end local tag = ternary(args['display'] == 'inline', 'span', 'div') local link = mw.html.create(tag):css('display', args['display']):addClass(args['display'] .. '-image') if args['class'] then link:addClass(args['class']) end if args['style'] then link:cssText(args['style']) end local label = mw.html.create(tag) --if args['label'] == 'before' or args['label'] == 'after' then link:addClass('label-'..args['label']) if args['labelclass'] then label:addClass(args['labelclass']) end if args['labelstyle'] then label:cssText(args['labelstyle']) end if args['link'] == args['selflink'] then label:wikitext(' ' .. args['text'] .. ' '):done elseif args['link'] == '' then label:wikitext(args['text']):done else label:wikitext(formatnil('%s', args['link'], args['text'])):done end --else --   label = nil --end local img = mw.html.create(tag) if args['image'] ~= '' then local size = ternary(args['height'], tostring(args['width'] or )..'x'..tostring(args['height'] or )..'px', tostring(args['width'] or '')..'px') if args['iconclass'] then img:addClass(args['iconclass']) end if args['width'] then img:attr('data-width', tostring(args['width'])) end if args['height'] then img:attr('data-height', tostring(args['height'])) end if args['border'] == true then img:addClass('border') end if args['iconstyle'] then img:cssText(args['iconstyle']) end img:wikitext(mw.ustring.format(, args['image'], size, ternary(args['border'], 'border|', ), ternary(args['link'] == args['selflink'], '', args['link']), args['text'])):done else img = nil end if args['label'] == 'before' then if label then link:node(label) end if args['separator'] and img then link:wikitext(args['separator']) end if img then link:node(img) end elseif args['label'] == 'after' then if img then link:node(img) end if args['separator'] and img then link:wikitext(args['separator']) end if label then link:node(label) end else if img then link:node(img) end end return link, img, label end

function formatnil(text, ...) if select("#", ...) == 0 then return text end for i=1, select("#", ...) do       if select(i, ...) == nil then return nil end end return mw.ustring.format(text, ...) end

function append(orig, new) if new then if mw.ustring.sub(new, 1, 1) == '-' then if mw.ustring.sub(new, 2, 2) == '-' then return mw.ustring.sub(new, 2) else return orig .. mw.ustring.sub(new, 2) end else return new end else return nil end end

function getSize(size) local fields = HF.explode('x', mw.ustring.gsub(tostring(size or ), 'px', , 1)) local width = tonumber(fields[1]) or nil local height = tonumber(fields[2]) or nil return width, height end

function deHash(text, sel) if text == nil then return nil, nil end local res, hash, offset = nil, nil, nil text = tostring(text) offset = mw.ustring.find(text, '#', 1, true) if offset then res = mw.ustring.sub(text, 1, offset-1) hash = mw.ustring.sub(text, offset+1) else res = text hash = nil end if sel == nil then return res, hash else return select(sel, res, hash) end end

function ternary(cond, T, F)   if cond and cond ~= 'false'and cond ~= '0' or cond == nil or cond == '' then return T else return F end end

function arguments(origArgs) local args = {} for k, v in pairs(origArgs) do       if type(v) == 'string' then v = HF.trim(v) end if v ~= '' then args[k] = v end end return args end

return ImageLink