Module:Dialogue

-- This module implements Template:Dialogue.

local p = {}

function p._main(lines, options) -- Validate the lines data. if #lines < 2 then error('dialogues must have at least two lines', 2) end for i, t in ipairs(lines) do		if not t.speaker then error(string.format( 'no speaker was specified for line %d of the dialogue', i ), 2)		elseif not t.text then error(string.format( 'no text was specified for line %d of the dialogue', i ), 2)		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;') table.insert(footnote, options.attr or error( "no 'attr' argument was specified", 2 ))	if options.audio then table.insert(string.format( ' &mdash; ' .. '' ..			'[[Media:%s|Listen]] ' .. ' (file info) ', options.audio, options.audio ))	end if options.src then local formatString if options.url then formatString = '[%s %s] ' else formatString = ' %s ' end table.insert(footnote, string.format( formatString, options.src, mw.text.nowiki('[src]') ))	else table.insert(footnote, '') end footnote = table.concat(footnote)

-- Return the assembled output. return string.format(		' \n%s\n%s ',		dialogue,		footnote	) end

function p.main(frame) -- Process arguments from the frame object. local args = {} for k, v in ipairs(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