Module:Quote: Difference between revisions
ReisuDesign (talk | contribs) Created page with "-- This Module is used for making templates based in the Lua language. -- See more details about Lua in w:Help:Lua. -- The Fandom Developer's Wiki hosts Global Lua Modules that can be imported and locally overridden. -- The next line imports the Quote module from the w:c:dev:Global Lua Modules. local Quote = require('Dev:Quote') -- See more details about this module at w:c:dev:Global_Lua_Modules/Quote -- The last line produces the output for the template ret..." |
ReisuDesign (talk | contribs) No edit summary |
||
| (4 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
-- | -- <nowiki> | ||
-- | local Quote = {} | ||
-- | |||
-- | local getArgs = require('Module:Arguments').getArgs | ||
local Quote = | 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 | |||
return nil | |||
end | |||
-- ========================================================= | |||
-- Options | |||
-- ========================================================= | |||
local function getOptions(args) | |||
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 | |||
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 | |||
return options | |||
end | |||
-- ========================================================= | |||
-- 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 | |||
-- ========================================================= | |||
-- Normal quote | |||
-- ========================================================= | |||
function Quote.quote(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' | |||
) | |||
-- Quote text | |||
local quoteContents = mw.html.create('div') | |||
:addClass('pp-quote__text') | |||
:wikitext(quoteText) | |||
-- Source | |||
local quoteSource | |||
if person and source then | |||
quoteSource = person .. ', ' .. source | |||
elseif person then | |||
quoteSource = person | |||
elseif source then | |||
quoteSource = source | |||
end | |||
return build( | |||
quoteContents, | |||
quoteSource, | |||
options | |||
) | |||
end | |||
-- ========================================================= | |||
-- Dialogue | |||
-- ========================================================= | |||
function Quote.dialogue(frame) | |||
local args = getArgs(frame) | |||
local options = getOptions(args) | |||
local quoteContents = mw.html.create('div') | |||
:addClass('pp-quote__text') | |||
:addClass('pp-quote__text--dialogue') | |||
-- 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() | |||
end | |||
end | |||
-- Dialogue source | |||
local context = | |||
parameter(args, 'context') | |||
local source = | |||
parameter(args, 'source') | |||
local quoteSource | |||
if context and source then | |||
quoteSource = context .. ', ' .. source | |||
elseif context then | |||
quoteSource = context | |||
elseif source then | |||
quoteSource = source | |||
end | |||
return build( | |||
quoteContents, | |||
quoteSource, | |||
options | |||
) | |||
end | |||
return Quote | return Quote | ||