Module:Infobox chord
Note: Do not invoke this module directly; use the corresponding template instead: Template:Infobox chord.
This module implements {{Infobox chord}}
to generate an infobox providing information about a given chord.
local p = {}
local rat = require("Module:Rational")
local utils = require("Module:Utils")
local infobox = require("Module:Infobox")
function p.infobox_chord(frame)
local debug_mode = frame.args["debug"]
local page_name = frame:preprocess("{{PAGENAME}}")
local debug_data = ""
local infobox_data = {}
local cats = ""
local errors = ""
local name = frame.args["Name"]
if utils.value_provided(name) then
local caption = "Name"
if name:match(",") then
caption = "Names"
-- removing manual line breaks
local matches
name, matches = name:gsub("<br/?>", "")
if matches > 0 then
cats = cats .. "[[Category:Todo:remove manual line breaks]]"
end
-- removing whitespaces after commas
name = name:gsub(",%s+", ",")
-- placing line breaks after commas
name = name:gsub(",", ",<br/>")
end
table.insert(infobox_data, {
caption,
name,
})
end
if utils.value_provided(frame.args["Harmonics"]) then
local harmonics = {}
for hs in string.gmatch(frame.args["Harmonics"], "[^:]+") do
h = tonumber(hs) -- TODO: support rational entries?
if h == nil or h < 1 then
errors = errors .. "<p>error: invalid harmonic " .. hs .. "</p>"
else
table.insert(harmonics, h)
end
end
local root = harmonics[1]
if root == nil and errors == "" then
errors = errors .. "<p>error: no harmonics found</p>"
end
if errors ~= "" then
return errors
end
local prime_limit = 1
local odd_limit = 1
local ratio_string = ""
for i, h in ipairs(harmonics) do
for prime, n in pairs(utils.prime_factorization_raw(h)) do
if prime > prime_limit then
prime_limit = prime
end
end
local gcd = utils._gcd(h, root)
local numer = h / gcd
local denom = root / gcd
if i == 1 then
ratio_string = ratio_string .. numer .. "⁄" .. denom
else
ratio_string = ratio_string .. " – " .. numer .. "⁄" .. denom
end
local odd = numer
while odd % 2 == 0 do
odd = odd / 2
end
if odd > odd_limit then
odd_limit = odd
end
end
table.insert(infobox_data, {"Ratios", ratio_string})
table.insert(infobox_data, {"[[Prime limit]]", prime_limit})
table.insert(infobox_data, {"[[Odd limit]]", odd_limit})
cats = cats .. "[[Category:" .. prime_limit .. "-limit chords]]"
cats = cats .. "[[Category:" .. odd_limit .. "-odd-limit chords]]"
end
if debug_data ~= "" then
table.insert(infobox_data, {
"Debug",
debug_data,
})
end
local s = infobox.build("<u>Chord information</u>", infobox_data)
if not debug_mode then
s = s .. cats
end
return s
end
return p