Module:Quote: Difference between revisions
ReisuDesign (talk | contribs) No edit summary Tags: Mobile edit Mobile web edit |
ReisuDesign (talk | contribs) No edit summary |
||
| Line 1: | Line 1: | ||
-- <nowiki> | -- <nowiki> | ||
local Quote = {} | local Quote = {} | ||
local getArgs = require('Module:Arguments').getArgs | local getArgs = require('Module:Arguments').getArgs | ||
local i18n = require('Module:I18n').loadMessages('Quote') | local i18n = require('Module:I18n').loadMessages('Quote') | ||
-- ========================================================= | |||
-- Helpers | |||
-- ========================================================= | |||
local function parameter(args, ...) | |||
local names = { ... } | |||
for _, name in ipairs(names) do | |||
local value = i18n:parameter(name, args) | |||
if value ~= nil and value ~= '' then | |||
return value | |||
end | |||
end | end | ||
return | return nil | ||
end | |||
local function paragraph(text) | |||
return '\n' .. text .. '\n' | |||
end | end | ||
local function | |||
local options = {} | -- ========================================================= | ||
-- Options | |||
-- ========================================================= | |||
options. | local function parseOptions(args) | ||
options. | local options = { | ||
local align = | classes = {}, | ||
styles = {} | |||
} | |||
local extraClasses = parameter(args, 'class') | |||
if extraClasses then | |||
options.extraClasses = extraClasses | |||
end | |||
local extraStyles = parameter(args, 'style') | |||
if extraStyles then | |||
options.extraStyles = extraStyles | |||
end | |||
local align = parameter(args, 'align') | |||
if align then | if align then | ||
options.align = ' | align = string.lower(align) | ||
if align == 'left' or align == 'right' then | |||
options.align = align | |||
options.classes[#options.classes + 1] = 'pp-quote--' .. align | |||
local width = parameter(args, 'width', 'quotewidth') | |||
if width then | |||
options.styles['width'] = width | |||
end | |||
end | |||
end | end | ||
return options | return options | ||
end | end | ||
-- | |||
local function | -- ========================================================= | ||
-- Quote builder | |||
-- ========================================================= | |||
local function build(quoteContents, quoteSource, options) | |||
local quote = mw.html.create('div') | |||
:addClass('pp-quote') | |||
-- Alignment | |||
for _, className in ipairs(options.classes) do | |||
quote:addClass(className) | |||
end | |||
-- User-supplied classes | |||
if options.extraClasses then | |||
quote:addClass(options.extraClasses) | |||
end | |||
-- User-supplied styles | |||
for property, value in pairs(options.styles) do | |||
quote:css(property, value) | |||
end | |||
if options.extraStyles then | |||
quote:cssText(options.extraStyles) | |||
end | |||
-- Quote text | |||
quote:node(quoteContents) | |||
-- Source | |||
if quoteSource and quoteSource ~= '' then | |||
quote | |||
:tag('div') | |||
:addClass('pp-quote__source') | |||
:wikitext(quoteSource) | |||
:done() | |||
end | |||
return quote | |||
end | end | ||
-- ========================================================= | |||
-- Normal quote | |||
-- ========================================================= | |||
function Quote.quote(frame) | function Quote.quote(frame) | ||
local args = getArgs(frame) | local args = getArgs(frame) | ||
local options = parseOptions(args) | |||
local quoteText = | |||
args[1] | |||
or parameter(args, 'quotetext', 'quote', 'text') | |||
or '' | |||
local person = | |||
args[2] | |||
or parameter(args, 'person', 'speaker', 'personquoted') | |||
local source = | |||
args[3] | |||
or parameter(args, 'source', 'quote_source') | |||
-- Quote text container | |||
local quoteContents = mw.html.create('div') | |||
:addClass('pp-quote__text') | |||
:wikitext(quoteText) | |||
-- Build source | |||
local quoteSource | |||
local | |||
if person and source then | if person and source then | ||
quoteSource = person .. ', ' .. source | |||
elseif person then | |||
quoteSource = person | |||
elseif source then | elseif source then | ||
quoteSource = source | |||
end | end | ||
return build( | return build( | ||
quoteContents, | |||
quoteSource, | |||
options | |||
) | |||
end | end | ||
-- ========================================================= | |||
-- Dialogue | |||
-- ========================================================= | |||
function Quote.dialogue(frame) | function Quote.dialogue(frame) | ||
local args = getArgs(frame) | local args = getArgs(frame) | ||
local options = parseOptions(args) | |||
local options = | |||
local quoteContents = mw.html.create('div') | |||
local | :addClass('pp-quote__text') | ||
:addClass(' | :addClass('pp-quote__text--dialogue') | ||
local | -- Dialogue lines | ||
for i = 1, #args, 2 do | |||
local speaker = args[i] | |||
local line = args[i + 1] | |||
if speaker and line then | |||
quoteContents | |||
:tag('div') | |||
:addClass('pp-quote__line') | |||
:tag('strong') | |||
:addClass('pp-quote__speaker') | |||
:wikitext(speaker .. ':') | |||
:done() | |||
:wikitext(' ' .. paragraph(line)) | |||
:done() | :done() | ||
end | end | ||
end | end | ||
local context = | -- Dialogue source | ||
local source = | local context = parameter(args, 'context') | ||
local source = parameter(args, 'source') | |||
local quoteSource | |||
if context and source then | if context and source then | ||
quoteSource = context .. ', ' .. source | |||
elseif context | elseif context then | ||
quoteSource = context | |||
elseif source | elseif source then | ||
quoteSource = source | |||
end | end | ||
return build( | return build( | ||
quoteContents, | |||
quoteSource, | |||
options | |||
) | |||
end | end | ||
return Quote | return Quote | ||