Module:Quote: Difference between revisions
ReisuDesign (talk | contribs) No edit summary |
ReisuDesign (talk | contribs) No edit summary |
||
| (3 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
-- <nowiki> | -- <nowiki> | ||
local Quote = {} | local Quote = {} | ||
local | local getArgs = require('Module:Arguments').getArgs | ||
local i18n = require('Module:I18n').loadMessages('Quote') | |||
-- ========================================================= | |||
-- Parameters | |||
-- ========================================================= | |||
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 | end | ||
local function | |||
local options = {} | -- ========================================================= | ||
-- Options | |||
-- ========================================================= | |||
options. | local function getOptions(args) | ||
options. | |||
local align = | local options = { | ||
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 | |||
-- Alignment | |||
local align = parameter(args, 'align') | |||
if align then | if align then | ||
options. | align = string.lower(align) | ||
if align == 'left' or align == 'right' then | |||
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 | -- ========================================================= | ||
-- Build quote | |||
-- ========================================================= | |||
local function build(quoteContents, quoteSource, options) | |||
local quote = mw.html.create('blockquote') | |||
:addClass('pp-quote') | |||
-- Alignment classes | |||
for _, className in ipairs(options.classes) do | |||
quote:addClass(className) | |||
end | |||
-- Extra classes | |||
if options.extraClasses then | |||
quote:addClass(options.extraClasses) | |||
end | |||
-- Explicit styles | |||
for property, value in pairs(options.styles) do | |||
quote:css(property, value) | |||
end | |||
if options.extraStyles then | |||
quote:cssText(options.extraStyles) | |||
end | |||
-- Quote content | |||
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 = getOptions(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' | |||
) | |||
local | -- Quote text | ||
:addClass(' | local quoteContents = mw.html.create('div') | ||
:wikitext( | :addClass('pp-quote__text') | ||
:wikitext(quoteText) | |||
local | |||
-- Source | |||
local quoteSource | |||
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 = getOptions(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(' ' .. line) | |||
:done() | :done() | ||
end | end | ||
end | end | ||
local context = | |||
local source = | -- Dialogue 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 | ||