Module:Classement musical
Aller à la navigation
Aller à la recherche
La documentation pour ce module peut être créée à Module:Classement musical/doc
-- Ce module implémente les modèles {{Albumchart}} et {{Singlechart}}. local p = {} -- Useful definitions. local format = mw.ustring.format local insert = table.insert local Chart = {} Chart.__index = Chart function Chart.new(args, chartType) local args = args or {} local isAlbumChart = chartType == 'album' local chartName = mw.ustring.lower(args[1] or '') -- Loading the data module. local data = require('Module:Classement musical/Données') local cfg = data.cfg local moduleName = isAlbumChart and cfg.albumData or cfg.singleData local chartData = mw.loadData(moduleName)[chartName] or {} local root = mw.html.create() -- To reproduce the behavior of the wikicode, the HTML code is deliberately -- raw and does not use mw.html. if args[cfg.rowHeader] then root:wikitext('! scope="row" style="font-weight:normal;"') end root:wikitext('|') local fields = { args = args, cfg = cfg, isAlbumChart = isAlbumChart, chartName = chartName, chartData = chartData, requiredParams = data.formats[chartData.format] or {}, errorDetect = false, root = root } return setmetatable(fields, Chart) end function Chart:__tostring() return tostring(self.root) end function Chart:formattingError(textError) self.errorDetect = true local catName = self.isAlbumChart and self.cfg.albumDetectCat or self.cfg.singleDetectCat self.root :tag('span') :addClass('error') :css('font-size', '100%') :wikitext(format( '%s %s[[%s]]', self.cfg.errorPrefix, textError, catName)) end function Chart:checkFormat() -- Non matching chart type error. if not self.chartData.chart then self:formattingError(format(self.cfg.noChartError, self.chartName)) return self end -- No error raised in case of optional parameters. if self.requiredParams.optionalParams then return self end -- Missing parameters type error. local missingParams = {} for _, requiredParam in ipairs(self.requiredParams) do if not self.args[requiredParam] and not missingParams[requiredParam] then -- Put the values as the table's keys to not insert a duplicate. missingParams[requiredParam] = true insert(missingParams, '<code>' .. requiredParam .. '</code>') end end if next(missingParams) then self:formattingError(format( self.cfg.noParamsError, table.concat(missingParams, ', '))) end return self end -- Produces a reference link in the format "[link title]" by applying in the -- order the required parameters. If an insert note is specified, the output -- format is "[link title]. note". function Chart:buildRefLink() local requiredArgs, encodeArgs = {}, {} local requiredParams = self.requiredParams local encoder = requiredParams.encoder or mw.uri.encode for _, param in ipairs(requiredParams) do local requiredParam = self.args[param] if requiredParam then insert(encodeArgs, encoder(requiredParam)) insert(requiredArgs, requiredParam) end end local link = format(self.chartData.link, unpack(encodeArgs)) local title = format(self.chartData.title, unpack(requiredArgs)) local refLink = format('[%s %s]', link, title) if self.chartData.insertNote then local noteArgs = requiredArgs -- Clear [requiredParams] table for [unpack] function. if #noteArgs < #requiredParams then noteArgs = {} for _, param in ipairs(requiredParams) do insert(noteArgs, self.args[param] or param) end end local note = format(self.chartData.insertNote, unpack(noteArgs)) refLink = format("%s. ''%s''", refLink, note) end return refLink end function Chart:buildRef() local res = mw.html.create() local frame = mw.getCurrentFrame() local args = self.args local cfg = self.cfg if self.chartData.lang then local lang = frame:expandTemplate{title = self.chartData.lang} res:wikitext(format('%s ', lang)) end res:wikitext(format('%s. %s. %s.', self:buildRefLink(), self.chartData.chartNote or self.chartData.chart, self.chartData.publisher)) if args[cfg.publishDate] then res:wikitext(format(' %s. ', args[cfg.publishDate])) end if args[cfg.accessDate] then res:wikitext(format(' %s %s.', cfg.accessDatePrefix, args[cfg.accessDate])) end return frame:extensionTag('ref', tostring(res), { name = args.refname, group = args.refgroup }) end function Chart:buildChartRow() if self.errorDetect then return self end self.root :wikitext(mw.getCurrentFrame():expandTemplate{title = self.chartData.flag}) :wikitext(format(' (%s)', self.chartData.chart)) :wikitext(self:buildRef()) if self.args[self.cfg.note] then self.root :tag('br') :done() :tag('span') :css('font-style', 'italic') :css('font-size', '89%') :wikitext(self.args.note) end return self end function Chart:buildPositionRow() self.root :newline() :wikitext('| style="text-align:center;"|') :wikitext(self.args[2] or '{{{2}}}') return self end -- Access in the module space. function p._main(args, chartType) local chart = Chart.new(args, chartType) return tostring(chart:checkFormat():buildChartRow():buildPositionRow()) end -- Access outside the module space. function p.main(frame) local argsParent = frame:getParent().args local cleanArgs = {} for k, v in pairs(argsParent) do if v ~= '' then cleanArgs[k] = mw.text.trim(v) end end return p._main(cleanArgs, frame.args[1]) end return p