Jump to content
Search

The new Ubuntu Wiki is live. Legacy content may be unavailable and page links may have changed. Read the announcement on Discourse.

Module:ButtonLink

From Ubuntu Wiki

Documentation for this module may be created at Module:ButtonLink/doc

local p = {}
local arguments = require('Module:Arguments')

-- Renders a single button link with optional icon.
-- Supports internal wiki links [[Page|text]] and external [https://url text].
--
-- Usage from wikitext:
--   {{#invoke:ButtonLink|link|text=Label|target=Page}}                   -- internal link
--   {{#invoke:ButtonLink|link|text=Label|url=https://example.com}}       -- external link
--   {{#invoke:ButtonLink|link|text=Label|target=Page|icon=desktop}}      -- with icon

function p.render(frame, data)
	local text = data.text or ''
	local target = data.target or ''
	local url = data.url or ''
	local icon = data.icon or ''
	local iconMarkup = ''
	if icon ~= '' then
		iconMarkup = '<span class="ubuntu-pragma-icon-' .. icon .. '" aria-hidden="true"></span>'
	end

	-- A leading colon on [[:target]] is required for namespaced targets
	-- The colon is fine for normal article links.
	local linkWikitext = ''
	if url ~= '' then
		linkWikitext = '[' .. url .. ' ' .. iconMarkup .. text .. ']'
	elseif target ~= '' then
		linkWikitext = '[[:' .. target .. '|' .. iconMarkup .. text .. ']]'
	else
		linkWikitext = iconMarkup .. text
	end

	local linkHtml = frame:preprocess(linkWikitext)

	local button = mw.html.create('span')
		:addClass('ubuntu-card-button-link')

	button:wikitext(linkHtml)

	return tostring(button)
end

function p.link(frame)
	local args = arguments.getArgs(frame, { parentFirst = true })
	return p.render(frame, {
		text = args.text or args[1],
		target = args.target,
		url = args.url,
		icon = args.icon,
	})
end

return p