Final Fantasy Wiki
Register
m (added option for extra styles)
Tag: sourceedit
Cqm (talk | contribs)
mNo edit summary
Tag: sourceedit
Line 10: Line 10:
 
s = string.sub( s, 2 )
 
s = string.sub( s, 2 )
 
end
 
end
  +
 
return s
 
return s
 
end
 
end
Line 21: Line 22:
 
link = removeInitialColon(link)
 
link = removeInitialColon(link)
 
end
 
end
  +
 
local namespace = link:match('^(.-):')
 
local namespace = link:match('^(.-):')
  +
 
if namespace then
 
if namespace then
 
local nsTable = mw.site.namespaces[namespace]
 
local nsTable = mw.site.namespaces[namespace]
Line 28: Line 31:
 
end
 
end
 
end
 
end
  +
 
return 0
 
return 0
 
end
 
end
Line 35: Line 39:
 
local namespace = p.findNamespaceId(link, false)
 
local namespace = p.findNamespaceId(link, false)
 
local colon
 
local colon
  +
 
if namespace == 6 or namespace == 14 then
 
if namespace == 6 or namespace == 14 then
 
colon = ':'
 
colon = ':'
Line 65: Line 70:
 
end
 
end
   
-- Produces standard context-link text. -- Implements the {{context-link}} template.
+
-- Produces standard context-link text.
 
 
function p.contextlink(frame)
 
function p.contextlink(frame)
 
local args = getArgs(frame)
 
local args = getArgs(frame)
Line 78: Line 82:
 
display = args['t' .. i]
 
display = args['t' .. i]
   
if not link or display then
+
if not link then
 
break
 
break
 
end
 
end

Revision as of 12:55, 21 May 2016

Documentation for this module may be created at Module:Context-link/doc

-- This module  implements the {{context-link}} meta-template.
local getArgs = require('Dev:Arguments').getArgs
local p = {}

-- Helper functions

local function removeInitialColon(s)
	-- Removes the initial colon from a string, if present.
	if string.sub( s, 0, 1 ) == ':' then
	    s = string.sub( s, 2 )
	end

	return s
end

function p.findNamespaceId(link, removeColon)
	-- Finds the namespace id (namespace number) of a link or a pagename. This
	-- function will not work if the link is enclosed in double brackets. Colons
	-- are trimmed from the start of the link by default. To skip colon
	-- trimming, set the removeColon parameter to true.
	if removeColon ~= false then
		link = removeInitialColon(link)
	end

	local namespace = link:match('^(.-):')

	if namespace then
		local nsTable = mw.site.namespaces[namespace]
		if nsTable then
			return nsTable.id
		end
	end

	return 0
end
 
function p.makeLinks(link, display)
	link = removeInitialColon(link)
	local namespace = p.findNamespaceId(link, false)
	local colon

	if namespace == 6 or namespace == 14 then
		colon = ':'
	else
		colon = ''
	end
 
	-- Find whether a faux display value has been added with the {{!}} magic
	-- word.
	if not display then
		local prePipe, postPipe = link:match('^(.-)|(.*)$')
		link = prePipe or link
		display = postPipe
	end
 
	-- Find the display value.
	if not display then
		local page, section = link:match('^(.-)#(.*)$')
		if page then
			display = page .. ' § ' .. section
		end
	end
 
	-- Assemble the link.
	if display then
		return string.format('[[%s%s|%s]]', colon, link, display)
	else
		return string.format('[[%s%s]]', colon, link)
	end
end

-- Produces standard context-link text.
function p.contextlink(frame)
	local args = getArgs(frame)
	local s = args.desc or 'For another page, see'
	local i = 1
	local links = {}
        local link, display

        while true do
		link = args[i]
		display = args['t' .. i]

                if not link then
			break
		end

		links[i] = p.makeLinks(link, display)
		i = i + 1
    	end

	local options = {}
	options.extraclasses = args.class
	options.extrastyles = args.style
	return p._contextlink(s, links, options)
end

function p._contextlink(s, links, options)
        local linktext = table.concat(links, ', ', 1, #links - 1)

	if #links > 1 then
		linktext = linktext .. ' or '
	end

        linktext = linktext .. links[#links] .. '.'

	local container = mw.html.create('div')
		:addClass('context-link')
		:addClass(options.extraclasses)
		:cssText(options.extrastyles)
		:wikitext(s .. ' ' .. linktext)

	return tostring(container)
end

return p