Moduuli:Keskustelu

Star Wars Fanon Wikistä
Siirry navigaatioonSiirry hakuun

Tämän moduulin ohjeistuksen voi tehdä sivulle Moduuli:Keskustelu/ohje

-- <nowiki>
--
-- [[Malline:Keskustelu]]

local p = {}

local function makeCategoryLink(cat)
	return string.format('[[Luokka:%s]]', cat)
end

local function makeWikitextError(msg)
	local ret = ''
	ret = ret .. string.format(
		'<strong class="error">[[Malline:Keskustelu]] virhe: %s.</strong>',
		msg
	)
	if mw.title.getCurrentTitle().namespace == 0 then
		ret = ret .. makeCategoryLink('Sivut, joissa on mallineparametrivirheitä')
	end
	return ret
end

function p._main(lines, options)
	-- Validate the lines data.
	if #lines < 2 then
		return makeWikitextError('keskustelussa on oltava vähintään kaksi riviä')
	elseif #lines > 10 then
		return makeWikitextError('keskustelussa ei voi olla yli 10 riviä')
	end
	for i, t in ipairs(lines) do
		if not t.speaker then
			return makeWikitextError(string.format(
				'keskustelun rivillä %d ei ole puhujaa',
				i
			))
		elseif not t.text then
			return makeWikitextError(string.format(
				'keskustelun rivillä %d ei ole tekstiä',
				i
			))
		end
	end

	-- Get the dialogue text.
	local dialogue = {}
	for i, t in ipairs(lines) do
		local text
		if t.format == 'trans' then
			text = string.format("&laquo;''%s''&raquo;", t.text)
		elseif t.format == 'no' then
			text = t.text
		else
			text = string.format("”''%s''”", t.text)
		end
		table.insert(dialogue, string.format(
			":'''%s''': %s", t.speaker, text
		))
	end
	dialogue = table.concat(dialogue, '\n')

	-- Make the footnote.
	local footnote = {}
	table.insert(footnote, ':&#8213;')
	if options.kuvaus then
		table.insert(footnote, options.kuvaus)
	else
		return makeWikitextError("muuttujaa 'kuvaus' ei annettu")
	end
	if options['ääni'] then
		if options.fi then
			table.insert(footnote, string.format(
				'<span class="noprint">&nbsp;&mdash; ' ..
				'[[Tiedosto:Gnome-speakernotes.png|20px|(ääni)|link=]]' ..
				'[[Media:%s|Kuuntele]] ' ..
				'<small>([[:Tiedosto:%s|kuvaus]])</small></span>',
				options['ääni'], options['ääni']
			))
		else
			table.insert(footnote, string.format(
				'<span class="noprint">&nbsp;&mdash; ' ..
				'[[Tiedosto:Gnome-speakernotes.png|20px|(ääni)|link=]]' ..
				'[[Media:%s|Kuuntele]] <span style="color:dimgray">(englanniksi)</span> ' ..
				'<small>([[:Tiedosto:%s|kuvaus]])</small></span>',
				options['ääni'], options['ääni']
			))
		end
	end
	if options['lähde'] then
		local formatString
		if options.url then
			formatString = '<sup class="noprint plainlinks">[%s %s]</sup>'
		else
			formatString = '<sup class="noprint">[[%s|%s]]</sup>'
		end
		table.insert(footnote, string.format(
			formatString,
			options['lähde'],
			mw.text.nowiki('[lähde]')
		))
	elseif mw.title.getCurrentTitle().namespace == 0 then
		table.insert(footnote, makeCategoryLink('Lähteettömät sitaatit'))
	end
	footnote = table.concat(footnote)

	-- Return the assembled output.
	return string.format(
		'<div class="quote">\n%s\n%s</div>',
		dialogue,
		footnote
	)
end

function p.main(frame)
	-- Process arguments from the frame object.
	local args = {}
	for k, v in pairs(frame:getParent().args) do
		v = v:match('^%s*(.-)%s*$') -- trim whitespace
		if v ~= '' then
			args[k] = v
		end
	end

	-- Sort the aguments into a usable format.
	local lines, options = {}, {}
	local function addLineData(line, lineKey, value)
		lines[line] = lines[line] or {}
		lines[line][lineKey] = value
	end
	for k, v in pairs(args) do
		if type(k) == 'number' then
			local line = math.ceil(k / 2)
			local lineKey = k % 2 == 1 and 'speaker' or 'text'
			addLineData(line, lineKey, v)
		else
			-- Assume k is a string.
			local line = k:match('^line([1-9][0-9]*)$')
			if line then
				addLineData(tonumber(line), 'format', v)
			else
				options[k] = v
			end
		end
	end

	-- Remove blanks from the lines data.
	local function removeBlanks(t)
		local ret, nums = {}, {}
		for num in pairs(t) do
			nums[#nums + 1] = num
		end
		for i, num in ipairs(nums) do
			ret[i] = t[num]
		end
		return ret
	end
	lines = removeBlanks(lines)

	return p._main(lines, options)
end

return p

-- </nowiki>