Module:MOS modes: Difference between revisions

Ganaram inukshuk (talk | contribs)
No edit summary
Ganaram inukshuk (talk | contribs)
force-collapse, following nick vucis recommendation on not sponsoring mode names
 
(5 intermediate revisions by the same user not shown)
Line 1: Line 1:
local getArgs = require("Module:Arguments").getArgs
local p = {}
local mos     = require("Module:MOS")
 
local rat     = require("Module:Rational")
local mos = require("Module:MOS")
local rat = require("Module:Rational")
local tamnams = require("Module:TAMNAMS")
local tamnams = require("Module:TAMNAMS")
local tip     = require("Module:Template input parse")
local tip = require("Module:Template input parse")
local utils   = require("Module:Utils")
local utils = require("Module:Utils")
local yesno   = require("Module:Yesno")
local yesno = require("Module:Yesno")
 
local p = {}


-- TODO:
-- TODO:
-- - (High priority): Refactor code so instead of string concatenation, lines
--  are appended to a table, where table.concat() is called at the end.
-- - Add ability to autocollapse on large mos pages (say, more than 12 modes)
-- - Add ability to autocollapse on large mos pages (say, more than 12 modes)
-- - Drop/replace delimiter-based mode name input; it's harder to maintain
-- - Limit table entries to ONLY proposed mode names and MAYBE one supplementary
--  column?! Any more is best done with a subst-instance of the table
-- - Think of better naming scheme for entries
-- Extract column info
-- Mode numbering is by BRIGHT GENS DOWN PER PERIOD
-- Nothing is returned; instead, args is modified
function p.preprocess_entries(num_modes, args, col_name)
local col_name = col_name or ""
local col_data = {}
local is_col_empty = true
for i = 1, num_modes do
-- Put together key, then use it to get data from args
local bright_gens_down = i - 1
local key = string.format("%sD", bright_gens_down) .. (col_name ~= "" and string.format(" %s", col_name) or "")
local cell = args[key] or ""
cell = utils.trim(cell)
-- Then transfer data to col_data
table.insert(col_data, cell)
-- Then remove data from args
args[key] = nil
-- Check whether data was present; absence of data is expressed as an
-- empty string.
is_col_empty = is_col_empty and cell == ""
end
if not is_col_empty then
args[string.format("%s Entries", col_name)] = col_data
end
end


-- "Main" function
-- "Main" function
-- To be called by wrapper
-- To be called by wrapper
function p._mos_modes(args)
function p._mos_modes(input_mos, mode_names, headers, entries, is_collapsed)
local is_collapsed = args["Is Collapsed"] == true
local is_collapsed = true
local input_mos   = args["Input MOS"] or mos.new(5,2)
local input_mos = input_mos or mos.new(5,2)
local mode_names   = args["Name Entries"] or args["Mode Names"] or nil
local mode_names = mode_names or {}
local headers     = {}
local headers = headers or {}
local entries     = {}
local entries = entries or {}
-- Get UDPs and CPOs
-- Get UDPs and CPOs
Line 66: Line 28:


-- Check whether to add mode names
-- Check whether to add mode names
local add_mode_names = mode_names ~= nil and #mode_names == #mos_modes
local add_mode_names = #mode_names == #mos_modes


-- Check whether the number of headers times the number of modes equals the
-- Check whether the number of headers times the number of modes equals the
Line 113: Line 75:
-- Add the mode's name, if given
-- Add the mode's name, if given
if add_mode_names then
if add_mode_names then
result = result .. string.format(" || %s", mode_names[i])
result = result .. string.format(" || %s", mode_names[i])
end
end
Line 134: Line 96:
-- Wrapper function; to be called by template
-- Wrapper function; to be called by template
function p.modes_table(frame)
function p.modes_table(frame)
local args = getArgs(frame)
local scale_sig = frame.args["Scale Signature"] or "5L 2s"
-- Process scalesig to input mos
local scale_sig = args["Scale Signature"]
local input_mos = mos.parse(scale_sig)
local input_mos = mos.parse(scale_sig)
args["Input MOS"] = input_mos
args["Scale Signature"] = nil
-- Get collapse options
args["Collapsed"] = yesno(args["Collapsed"], false)
-- Get mode names entered
-- Arg names are formatted as "<bright-gens-down>D <col-name>"
-- These are placed in a table called "<col-name> Data"
local num_modes = mos.period_step_count(input_mos)
p.preprocess_entries(num_modes, args, "Name")
-- Get the mos's mode names, if given
-- Get the mos's mode names, if given
Line 156: Line 104:
local mode_names = nil
local mode_names = nil
if scale_sig == "5L 2s" then
if scale_sig == "5L 2s" then
args["Mode Names"] = {  
mode_names = {  
"Lydian",
"Lydian",
"Ionian (major)",
"Ionian (major)",
Line 165: Line 113:
"Locrian"
"Locrian"
}
}
args["Name Entries"] = args["Mode Names"]
end
end
-- Temporarily re-allow previous input method
-- Get mode names entered
if #frame.args["Mode Names"] ~= 0 then
if #frame.args["Mode Names"] ~= 0 then
args["Mode Names"] = tip.parse_entries(args["Mode Names"], "$")
mode_names = tip.parse_entries(frame.args["Mode Names"], "$")
end
end
args["Table Headers"] = tip.parse_entries(args["Table Headers"], "$")
args["Table Entries"] = tip.parse_entries(args["Table Entries"], "$")


local result = p._mos_modes(args)
-- Get supplementary info
local headers_unparsed = frame.args["Table Headers"]
local headers = tip.parse_entries(headers_unparsed, "$")
local entries_unparsed = frame.args["Table Entries"]
local entries = tip.parse_entries(entries_unparsed, "$")
local is_collapsed = yesno(frame.args["Collapsed"], false)
local debugg = yesno(frame.args["debug"])
local result = p._mos_modes(input_mos, mode_names, headers, entries, is_collapsed)
-- Current means of adding entries is unmaintainable; to be deprecated.
-- Current means of adding entries is unmaintainable; to be deprecated.
Line 183: Line 135:
-- Debugger option
-- Debugger option
local debugg = yesno(args["debug"])
if debugg == true then
if debugg == true then
result = "<syntaxhighlight lang=\"wikitext\">" .. result .. "</syntaxhighlight>"
result = "<syntaxhighlight lang=\"wikitext\">" .. result .. "</syntaxhighlight>"
Line 189: Line 140:
return frame:preprocess(result)
return frame:preprocess(result)
end
-- Tester
function p.tester()
local args = {
["0D Name"] = "AAA",
["1D Name"] = "BBB",
["2D Name"] = "CCC",
["3D Name"] = "DDD"
}
p.preprocess_entries(5, args, "Name")
return args
end
end


return p
return p