Module:SpringDialogue

From Will You Snail Wiki
Jump to navigation Jump to search

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

--| Creates dialogue boxes
--- Code adapted from Dev:Dialogue.
--- Parameter difference: specifying "action" as the speaker does not create a
--- special row, because WYS dialgoue does not have stage directions
local SpringDialogue = {}

----
-- 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)
    return SpringDialogue[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 />
SpringDialogue.main = makeInvokeFunc('dialogue')
--% & crafts the Dialogue definition list
--@ args (table) Arguments passed from Module function call <code>[.main]</code> or other Lua Modules
function SpringDialogue.dialogue (args)
	local spring_id = tonumber(args['id'])
	
  local block = mw.html.create('div'):cssText('display: grid; grid-template-columns: auto 1fr; column-gap: 0.5em; row-gap: 4px;')
  for i,v in pairs(args) do -- pairs instead of ipairs due to empty unnamed parameters
      if type(i) == 'number' and math.fmod(i, 3) == 1 then
          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
              
          local mood = args[i + 1]
          local message = args[i + 2]
          
          -- mood container
          local img_container = block:tag('div')
          if speaker and mood then
        		img_container
        		:wikitext(
        			string.format('[[File:%s %s.png|56px]]', speaker, mood:lower())
        		)
          end
          img_container:done()
          
          -- dialogue container
          local dialogue_container = block:tag('div')
          
          local speaker_container = dialogue_container:tag('div')
          if speaker then
            speaker_container:wikitext(
            	string.format("'''%s'''", speaker)
            )
          end
          speaker_container:done()
          
          local message_container = dialogue_container:tag('div'):css('margin-left', '24px')
          if message then
            message_container:tag('div')
            :wikitext(message)
            :done()
          else
            message_container:wikitext('...')
          end
          message_container:done()
          
          dialogue_container:done()
          
          -- post-Ellie styling
          if spring_id ~= nil and spring_id >= 40 then
          	img_container:css('transform', 'rotate(-8deg)')
          end
          -- glitchy Unicorn styling
          if speaker:upper() == "UNICORN" and mood:upper() == "GLITCHY" then
          	dialogue_container:css('text-shadow', '2px 0 2px #fff, 0 0 8px #fff')
          	-- img_container:css('filter', 'drop-shadow(2px 0 2px #ff41ff)')
          	-- dallin color is #00dfff
          end
      end
  end
  block:done()
  return tostring(block)
end

----
-- Output
----
return SpringDialogue