Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Module:Dialogue: Difference between revisions

From Very Bad Wiki
Created page with "local p = {} function p.render(frame) local args = frame:getParent().args -- Build alias map (named params that aren't positional) -- e.g. |oz=Wizard of Oz => aliases["oz"] = "Wizard of Oz" local aliases = {} for k, v in pairs(args) do if type(k) == "string" and v ~= "" then aliases[k] = mw.text.trim(v) end end -- Collect ordered positional pairs: |key|value|key|value... local lines = {} local i = 1..."
 
No edit summary
Line 1: Line 1:
local p = {}
--| Creates dialogue definition lists
--- For whatever reason, refuses to make blockquotes
--- <nowiki>
local Dialogue = {}


function p.render(frame)
----
    local args = frame:getParent().args
-- Libraries and Globals
   
----
    -- Build alias map (named params that aren't positional)
-- Parses invocation and template parameters, trims whitespace, and removes blanks.
    -- e.g. |oz=Wizard of Oz  => aliases["oz"] = "Wizard of Oz"
local getArgs = require("Dev:Arguments").getArgs
    local aliases = {}
    for k, v in pairs(args) do
        if type(k) == "string" and v ~= "" then
            aliases[k] = mw.text.trim(v)
        end
    end


    -- Collect ordered positional pairs: |key|value|key|value...
----
    local lines = {}
-- Local Functions
    local i = 1
----
    while args[i] ~= nil do
local function makeInvokeFunc(funcName)
        local key = mw.text.trim(args[i])
return function(frame)
        local val = args[i+1] and mw.text.trim(args[i+1]) or ""
local args = getArgs(frame, { removeBlanks = false })
        table.insert(lines, {key = key, val = val})
        i = i + 2
    end


    -- Render
return Dialogue[funcName](args)
    local out = {}
end
    for _, line in ipairs(lines) do
end
        local key = line.key
 
        local val = line.val
----
        if key == "action" then
-- Public Functions
            table.insert(out, string.format(
----
                '<div class="dialogue-action">[%s]</div>', mw.text.trim(val)
--% Formats dialogue between multiple speakers
            ))
--- Intended to replace {{Dialogue}}.
        else
--- Syntax compliant with [[wikipedia:Template:Dialogue]]
            local speaker = aliases[key] or key
--- with portability and citation modifications
            table.insert(out, string.format(
--@ frame (table) Invokes the frame from {{Dialogue}}. See [[Template:Dialogue/doc]].
                '<div class="dialogue-line"><span class="dialogue-speaker">%s</span><span class="dialogue-text">&#8220;%s&#8221;</span></div>',
--: (string) HTML <dl />
                mw.text.sanitizePattern and speaker or mw.text.trim(speaker),
Dialogue.main = makeInvokeFunc("dialogue")
                val
--% & crafts the Dialogue definition list
            ))
--@ args (table) Arguments passed from Module function call <code>[.main]</code> or other Lua Modules
        end
function Dialogue.dialogue(args)
    end
local block = mw.html.create("dl"):addClass("dialogue-wrapper")
 
for i, v in ipairs(args) do
local next_param = i + 1
 
local mood_line = next_param / 2
 
if i == 1 then
if v == "action" then
block:tag("dd")
:tag("small"):attr("data-dialogue", "action")
:tag("u")
:wikitext("[" .. args[next_param] .. "]")
:done():done():done()
else
local speaker =
(args[i] and args[args[i]])
and args[args[i]]
or (
type(args[i]) == "string"
and mw.text.trim(args[i]):len() > 0
)
and args[i]
or nil
 
speaker = speaker and string.format("'''%s'''", speaker)
 
local mood = args["mood" .. mood_line] or nil
 
mood = mood and string.format("&nbsp;''(%s)''", mood)
 
if speaker or mood then
block
:tag("dt")
:wikitext(("%s%s"):format(speaker or "", mood or "")
)
:done()
end
 
if args[next_param] then
block:tag("dd"):tag("q")
:wikitext(args[next_param])
:done():done()
else
block:tag("dd"):wikitext("..."):done()
end
end
elseif math.fmod(i, 2) == 1 then
if v == "action" then
block:tag("dd")
:tag("small")
:attr("data-dialogue", "action")
:tag("u")
:wikitext("[" .. args[next_param] .. "]")
:done():done():done()
else
local speaker =
(args[i] and args[args[i]])
and args[args[i]]
or (
type(args[i]) == "string"
and mw.text.trim(args[i]):len() > 0
)
and args[i]
or nil
 
speaker = speaker and string.format("'''%s'''", speaker)
 
local mood = args["mood" .. mood_line] or nil
 
mood = mood and string.format("&nbsp;''(%s)''", mood)
 
if speaker or mood then
block
:tag("dt")
:wikitext(("%s%s"):format(speaker or "", mood or "")
)
:done()
end
 
if args[next_param] ~= "" then
block:tag("dd"):tag("q")
:wikitext(args[next_param])
:done():done()
else
block:tag("dd"):wikitext("..."):done()
end
end
end
end
 
block:done()
 
if args["cite"] then
block:tag("cite"):wikitext(string.format("— %s", args["cite"])):done()
end


    return '<div class="dialogue">' .. table.concat(out, "\n") .. '</div>'
return tostring(block)
end
end


return p
----
-- Output
----
return Dialogue

Revision as of 05:19, 20 March 2026

Documentation for this module may be created at Module:Dialogue/doc

--| Creates dialogue definition lists
--- For whatever reason, refuses to make blockquotes
--- <nowiki>
local Dialogue = {}

----
-- Libraries and Globals
----
-- Parses invocation and template parameters, trims whitespace, and removes blanks.
local getArgs = require("Dev:Arguments").getArgs

----
-- Local Functions
----
local function makeInvokeFunc(funcName)
	return function(frame)
		local args = getArgs(frame, { removeBlanks = false })

		return Dialogue[funcName](args)
	end
end

----
-- Public Functions
----
--% Formats dialogue between multiple speakers
--- Intended to replace {{Dialogue}}.
--- Syntax compliant with [[wikipedia:Template:Dialogue]]
--- with portability and citation modifications
--@ frame (table) Invokes the frame from {{Dialogue}}. See [[Template:Dialogue/doc]].
--: (string) HTML <dl />
Dialogue.main = makeInvokeFunc("dialogue")
--% & crafts the Dialogue definition list
--@ args (table) Arguments passed from Module function call <code>[.main]</code> or other Lua Modules
function Dialogue.dialogue(args)
	local block = mw.html.create("dl"):addClass("dialogue-wrapper")

	for i, v in ipairs(args) do
		local next_param = i + 1

		local mood_line = next_param / 2

		if i == 1 then
			if v == "action" then
				block:tag("dd")
:tag("small"):attr("data-dialogue", "action")
:tag("u")
:wikitext("[" .. args[next_param] .. "]")
:done():done():done()
			else
				local speaker =
(args[i] and args[args[i]])
 and args[args[i]]
 or (
type(args[i]) == "string"
 and mw.text.trim(args[i]):len() > 0
)
 and args[i]
 or nil

				speaker = speaker and string.format("'''%s'''", speaker)

				local mood = args["mood" .. mood_line] or nil

				mood = mood and string.format("&nbsp;''(%s)''", mood)

				if speaker or mood then
					block
						:tag("dt")
						:wikitext(("%s%s"):format(speaker or "", mood or "")
)
						:done()
				end

				if args[next_param] then
					block:tag("dd"):tag("q")
:wikitext(args[next_param])
:done():done()
				else
					block:tag("dd"):wikitext("..."):done()
				end
			end
		elseif math.fmod(i, 2) == 1 then
			if v == "action" then
				block:tag("dd")
:tag("small")
:attr("data-dialogue", "action")
:tag("u")
:wikitext("[" .. args[next_param] .. "]")
:done():done():done()
			else
				local speaker =
(args[i] and args[args[i]])
 and args[args[i]]
 or (
type(args[i]) == "string"
 and mw.text.trim(args[i]):len() > 0
)
 and args[i]
 or nil

				speaker = speaker and string.format("'''%s'''", speaker)

				local mood = args["mood" .. mood_line] or nil

				mood = mood and string.format("&nbsp;''(%s)''", mood)

				if speaker or mood then
					block
						:tag("dt")
						:wikitext(("%s%s"):format(speaker or "", mood or "")
)
						:done()
				end

				if args[next_param] ~= "" then
					block:tag("dd"):tag("q")
:wikitext(args[next_param])
:done():done()
				else
					block:tag("dd"):wikitext("..."):done()
				end
			end
		end
	end

	block:done()

	if args["cite"] then
		block:tag("cite"):wikitext(string.format("— %s", args["cite"])):done()
	end

	return tostring(block)
end

----
-- Output
----
return Dialogue