Module:Short title

From Wikisource
Jump to navigation Jump to search

local p = {}

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

function p.main(frame)
	local args = getArgs(frame, {wrappers = 'Template:Short-title', valueFunc = function(k, v)
		if v then
			v = mw.text.trim(v)
		end
		if v ~= '' or k == 'sn' or k == 'm' then
			return v
		end
	end })

	if not args.s then
		return '<span class="error">Error: No title passed to {{[[Template:Short-title|Short-title]]}}</span>'
	end
	local retval
	if false then -- if args.style == 'ref' then
		-- The original template has an error in its code that prevents this from ever working, so I'm not going to bother converting it for now.
		-- {{tag:ref|{{#if: {{{c|}}}|[[{{#if: {{{y|}}}|{{{s}}} {{{y}}}|{{{s}}} }}{{!}}{{{c}}}]]|{{#if: {{{the|}}}|The&nbsp;|}}{{#if: {{{link|}}}|[[{{#if: {{{y|}}}|{{{s}}} {{{y}}}|{{{s}}}}}{{!}}{{{s}}} {{#if: {{{y|}}}|{{#ifeq: {{{comma}}}|yes|,|}} {{{y}}}|}}]]| {{{s}}}  {{#if: {{{y|}}}|{{#ifeq: {{{comma}}}|yes|,|}} {{{y}}}|}}}}}}}}
	else
		if args.c then
			retval = string.format('[[%s %s%s%s|%s]]',
				args.s,
				args.y or '',
				args.a and '#' or '',
				args.a or '',
				args.c
			)
		else
			local maybeThe = args.the and 'The&nbsp;' or ''
			local maybeComma = args.comma == 'yes' and ',' or ''
			local maybeDisambig = not (args.j == nil)
			if args.link then
				retval = string.format('%s[[%s %s|%s%s]]',
					maybeThe,
					args.s,
					args.y or '', -- Needs a disambig insertion here for (Scotland, N.I., B.I, etc.) --
					args.s,
					args.y and string.format('%s&nbsp;%s', maybeComma, args.y) or ''
				)
			else if args.page then
				retval = string.format('%s[[%s|%s%s]]',
					maybeThe,
					args.page,
					args.s,
					args.y and string.format('%s&nbsp;%s', maybeComma, args.y) or ''
				)
			else
				retval = string.format('%s%s %s',
					maybeThe,
					args.s,
					args.y and string.format('%s %s', maybeComma, args.y) or ''
				)
				end
			end
		end
	end
	if not args.sn or args.sn == 'none' then
		if args.regnal == 'yes' then
			retval = string.format('%s (%s%s c.%s)', retval, args.r, args.m, args.chapter)
		end
--	Code that would have interacted with the cl-act-p family, which was never fully implemented or worked correctly.
--	elseif args.sn == 'cl' then
--		retval = retval .. frame:expandTemplate{title = 'cl-act-title', args = {
--			'',
--			string.format('%s%s c.%s', args.r, args.m, args.chapter),
--			layout = args.layout or ''
--		}} 
	elseif args.sn ~= '' then
		retval = retval .. frame:expandTemplate{title = args.sn .. ' sidenote' , args = {
			string.format('%s c.%s', args.r, args.chapter)
		}}
	end
	if args.style then retval = string.format('%s[[Category:Pages using Module:Short title with style|%s]]', retval, args.style) end
	return retval
end

return p