Module:Scale

local std = require('Module:std') local table = require('Module:std/table') local fp = require('Module:std/functional') local getArgs = require('Module:Arguments').getArgs local fileLink = require('Module:File link')._main local data = require('Module:Scale/Data')

local function validate(system) local validSystems = std.tostring(table.keys(data.notes)) if(system == nil or system == '') then error('No system provided. Must be one of: ' .. validSystems) end if(data.notes[system] == nil) then error('Invalid system "' .. system .. '". Must be one of: ' .. validSystems) end end

-- For backwards compatibility with the old version of Template:Scale local function concatNoteArgs(args, noteSymbols) for i,v in ipairs(args) do       if(i > 2) then noteSymbols = noteSymbols .. v       end end return noteSymbols end

local function symbolToNote(system) return function(symbol) local result = data.notes[system][symbol] if(result == nil) then local validNotes = std.tostring(table.keys(data.notes[system])) error('Invalid symbol "' .. symbol .. '". Valid symbols for ' .. system .. ' are ' .. validNotes) end return result end end

local function addFileLink(note) n = mw.clone(note) n.file= fileLink{ file= note.file, link= '', size= '12px', }   return n end

local function noteToPositionedElement(system) local numPositions = table.size(data.notes[system]) return function(note) local position = data.relativePositions[numPositions][note.position] return mw.html.create('span') :css({               position= 'relative',                top= position .. 'px',            }) :wikitext(note.file, ' ') end end

local function symbolToPositionedElement(system) return fp.compose(       symbolToNote(system),        addFileLink,        noteToPositionedElement(system)    ) end

local function makeDiv(noteElems) local div = mw.html.create('div') :css({           display= 'inline-block',            ['line-height']= '28px',        }) for _, elem in ipairs(noteElems) do       div:node(elem) end return div end

- -- MAIN -

local function _main(args) local system = args[1] validate(system) local noteSymbols = args[2] if(args[3]) then noteSymbols = concatNoteArgs(args, noteSymbols) end noteSymbols = mw.text.split(noteSymbols, '') local notes = fp.map(symbolToPositionedElement(system), noteSymbols) return makeDiv(notes) end

local function main(frame) return _main(getArgs(frame, { wrappers= {'Template:Scale', 'Template:Scale/sandbox'} })) end

- -- EXPORTS - return { main=main, _main=_main, }