Module:Infobox chord: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
Line 13: | Line 13: | ||
local cats = "" | local cats = "" | ||
local errors = "" | local errors = "" | ||
local name = frame.args["Name"] | |||
if 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 | if utils.value_provided(frame.args["Harmonics"]) then | ||
table.insert(infobox_data, { | |||
"Name" | |||
}) | |||
local harmonics = {} | local harmonics = {} | ||
local i = 0 | local i = 0 | ||
Line 36: | Line 61: | ||
local prime_limit = 1 | local prime_limit = 1 | ||
local odd_limit = 1 | local odd_limit = 1 | ||
local debug_data = "" | |||
for i, h in ipairs(harmonics) do | for i, h in ipairs(harmonics) do | ||
for j, prime in ipairs(utils.prime_factorization_raw(h)) do | for j, prime in ipairs(utils.prime_factorization_raw(h)) do | ||
debug_data = debug_data .. "prime:" .. prime .. "<br/>" | |||
if prime > prime_limit then | if prime > prime_limit then | ||
prime_limit = prime | prime_limit = prime | ||
Line 56: | Line 84: | ||
cats = cats .. "[[Category:" .. prime_limit .. "-limit chords]]" | cats = cats .. "[[Category:" .. prime_limit .. "-limit chords]]" | ||
cats = cats .. "[[Category:" .. odd_limit .. "-odd-limit chords]]" | cats = cats .. "[[Category:" .. odd_limit .. "-odd-limit chords]]" | ||
end | |||
if debug_data ~= "" then | |||
table.insert(infobox_data, { | |||
"Debug", | |||
debug_data, | |||
}) | |||
end | end | ||
Revision as of 02:41, 12 August 2024
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 infobox_data = {}
local cats = ""
local errors = ""
local name = frame.args["Name"]
if 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
table.insert(infobox_data, {
"Name"
})
local harmonics = {}
local i = 0
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
harmonics[i] = h
i = i+1
end
end
local root = harmonics[0]
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 debug_data = ""
for i, h in ipairs(harmonics) do
for j, prime in ipairs(utils.prime_factorization_raw(h)) do
debug_data = debug_data .. "prime:" .. prime .. "<br/>"
if prime > prime_limit then
prime_limit = prime
end
end
local numer = h / utils._gcd(h, root)
local odd = numer
while odd % 2 == 0 do
odd = odd / 2
end
if odd > odd_limit then
odd_limit = odd
end
end
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