Jump to content

Module:IconLink

From Creopedia

This documentation is transcluded from Module:IconLink/doc.

Renders a link with icon

Implements Template:IconLink.


require( 'strict' )

local p = {}

--- @param args table
--- @return table
local function getProps( args )
	return {
		icon = args.icon,
		link = args.link or args[1],
		text = args.text or args.link or args[1],
		size = args.size or '20px',
		class = args.class or ''
	}
end

--- Build the file link for the icon
---
--- An empty size is left out rather than passed through as an empty parameter.
--- '[[File:x||link=y]]' carries a literal '||', and because templates are
--- expanded before tables are parsed, a hand-written wikitext row containing one
--- is split into extra cells. Omitting the parameter draws the file at its own
--- dimensions, which is what an empty size was asking for anyway.
---
--- @param icon string
--- @param link string
--- @param size string|nil
--- @return string
local function getIconWikitext( icon, link, size )
	if size == nil or size == '' then
		return string.format( '[[File:%s|link=%s|class=metadata]]', icon, link )
	end

	return string.format( '[[File:%s|%s|link=%s|class=metadata]]', icon, size, link )
end

--- Render the icon link
---
--- @param args table
--- @return mw.html
local function getIconLinkHtml( args )
	local root = mw.html.create( 'span' )

	root
		:addClass( 't-icon-link' )
		:addClass( args.class )

	root:tag( 'span' )
		:addClass( 't-icon-link__icon' )
		:wikitext( getIconWikitext( args.icon, args.link, args.size ) )

	root:tag( 'span' )
		:addClass( 't-icon-link__text' )
		:wikitext( string.format( '[[%s|%s]]', args.link, args.text ) )

	return root
end

--- Wikitext entry point for the module
---
--- @param frame mw.frame
--- @return string
function p.main( frame )
	local getArgs = require( 'Module:Arguments' ).getArgs
	return p._main( getArgs( frame ) )
end

--- Render the icon link
---
--- @param args table
--- @return string
function p._main( args )
	local props = getProps( args )

	if not props.icon then
		error( 'No icon provided' )
	end

	if not props.link or not props.text then
		error( 'No link or text provided' )
	end

	return mw.getCurrentFrame():extensionTag {
		name = 'templatestyles', args = { src = 'Module:IconLink/styles.css' }
	} .. tostring( getIconLinkHtml( props ) )
end

return p