Module:Anchor/sandbox

From Wikisource
Jump to navigation Jump to search
--[=[
Implements anchor templates
]=]
require('strict')

local getArgs = require('Module:Arguments').getArgs

local function error_message(message)
	return require('Module:Error')['error']({['message'] = message})
end

local p = {}

-- [[Template:Anchor]]

local function _anchor(args)
	local anchorList = {}
	for k, v in pairs(args) do
		table.insert(anchorList, tostring(mw.html.create('span'):attr('id', mw.uri.anchorEncode(v))))
	end
	return table.concat(anchorList)
end

function p.anchor(frame)
	return _anchor(getArgs(frame))
end

-- [[Template:Anchor link]]

local function _anchor_link(args)
	local anchor = args.anchor or args[1]
	if not anchor then
		return error_message('No anchor specified')
	end
	
	anchor = mw.uri.anchorEncode(anchor)
	local pageno = args.pageno or args[2]
	local subpage = args.subpage or args[3]
	
	local page = mw.title.getCurrentTitle()
	
	if page.nsText == 'Page' and not pageno then
		return error_message('No page number specified')
	end
	
	local title = page.text
	if page.nsText == 'Page' then
		title = page.rootText .. '/' .. pageno
	elseif subpage then
		local rootSubpageTitle = mw.title.makeTitle(page.nsText, page.rootText .. '/' .. subpage)
		local baseSubpageTitle = mw.title.makeTitle(page.nsText, page.baseText .. '/' .. subpage)
		if rootSubpageTitle.exists then
			title = rootSubpageTitle.text
		elseif baseSubpageTitle.exists then
			title = baseSubpageTitle.text
		end
	end
	
	return '[[' .. mw.title.makeTitle(page.nsText, title, anchor).fullText .. '|' .. anchor .. ']]'
end

function p.anchor_link(frame)
	return _anchor_link(getArgs(frame))
end

-- [[Template:Anchor link 2]]

local function _anchor_link_2(args)
	local anchor = args.anchor or args[3]
	if not anchor then
		return error_message('No anchor specified')
	end
	
	local subpage = args.subpage or args[1]
	local pageno = args.pageno or args[2]
	anchor = mw.uri.anchorEncode(anchor)
	local text = args.text or args[4] or anchor
	
	local page = mw.title.getCurrentTitle()
	
	if page.nsText == 'Page' and not pageno then
		return error_message('No page number specified')
	end
	
	local title = page.text
	if page.nsText == 'Page' then
		title = page.rootText .. '/' .. pageno
	elseif subpage then
		local rootSubpageTitle = mw.title.makeTitle(page.nsText, page.rootText .. '/' .. subpage)
		local baseSubpageTitle = mw.title.makeTitle(page.nsText, page.baseText .. '/' .. subpage)
		local baseSubpageBaseTitle = mw.title.makeTitle(page.nsText, baseSubpageTitle.baseText .. '/' .. subpage)
		local subpageTitle = mw.title.makeTitle(page.nsText, subpage)
		if rootSubpageTitle.exists then
			title = rootSubpageTitle.text
		elseif baseSubpageTitle.exists then
			title = baseSubpageTitle.text
		elseif baseSubpageBaseTitle.exists then
			title = baseSubpageBaseTitle.text
		elseif subpageTitle.exists then
			title = subpage
		end
	end
	return '[[' .. mw.title.makeTitle(page.nsText, title, anchor).fullText .. '|' .. text .. ']]'
end

function p.anchor_link_2(frame)
	return _anchor_link_2(getArgs(frame))
end

return p