Module:MOS tuning spectrum: Difference between revisions

Ganaram inukshuk (talk | contribs)
todo: refactor
Ganaram inukshuk (talk | contribs)
refactor code
Line 8: Line 8:


-- 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.
-- - (Low priority): Force-add comments that lie outside searched depth. If such
-- - (Low priority): Force-add comments that lie outside searched depth. If such
--  ratios are added, they're added in either the last column or their own
--  ratios are added, they're added in either the last column or their own
--  column appended after the last one.
--  column appended after the last one. (to be determined)


-- Split comments into in-range and out-of-range comments.
-- Split comments into in-range and out-of-range comments.
Line 18: Line 16:
-- in the table of step ratios. If so, add the corresponding key-value pair into
-- in the table of step ratios. If so, add the corresponding key-value pair into
-- the table of in-comments; if not, add to the table of out-comments.
-- the table of in-comments; if not, add to the table of out-comments.
function p.preprocess_comments(comments, step_ratios)
function p.preprocess_comments(comments, step_ratios, depths)
local in_comments  = {}
local in_comments  = {}
local out_comments = {}
local out_comments = {}
local out_ratios = {}
local out_ratios   = {}
for key, value in pairs(comments) do
for key, value in pairs(comments) do
Line 75: Line 73:
default_comments["4/1"] = string.format("'''Superhard %s'''", mos_as_string)
default_comments["4/1"] = string.format("'''Superhard %s'''", mos_as_string)
default_comments["1/0"] = string.format("'''Collapsed %s'''", mos_as_string)
default_comments["1/0"] = string.format("'''Collapsed %s'''", mos_as_string)
-- Append boundary of proper scales to basic comment, if applicable
-- Append boundary of proper scales to basic comment, if applicable
Line 99: Line 95:
-- - Step ratio and hardness
-- - Step ratio and hardness
-- - Comments
-- - Comments
local result = "{| class=\"wikitable center-all\"\n"
local result = {}
.. "|+ style=\"font-size: 105%; white-space: nowrap;\" | " .. string.format("Scale tree and tuning spectrum of %s\n", mos_as_string)
table.insert(result, '{| class="wikitable center-all"')
.. "|-\n"
table.insert(result, '|+ style="font-size: 105%; white-space: nowrap;" | Scale tree and tuning spectrum of ' ..  mos_as_string)
.. string.format("! rowspan=\"2\" colspan=\"%d\" | Generator<sup><abbr title=\"In steps of %s.\">(%s)</abbr></sup>\n", deepest + 1, et_suffix, et_suffix)
table.insert(result, '|-')
.. "! colspan=\"2\" | Cents\n"
table.insert(result, string.format('! rowspan="2" colspan="%d" | Generator<sup><abbr title="In steps of %s.">(%s)</abbr></sup>', deepest + 1, et_suffix, et_suffix))
.. "! colspan=\"2\" | Step ratio\n"
table.insert(result, '! colspan="2" | Cents')
.. "! rowspan=\"2\" | " .. comments_header_text .. "\n"
table.insert(result, '! colspan="2" | Step ratio')
.. "|-\n"
table.insert(result, string.format('! rowspan="2" | %s', comments_header_text))
.. "! Bright\n"
table.insert(result, '|-')
.. "! Dark\n"
table.insert(result, '! Bright')
.. "! L:s\n"
table.insert(result, '! Dark')
.. "! Hardness\n"
table.insert(result, '! L:s')
table.insert(result, '! Hardness')


-- Rounding is done using string.format, to 3 decimal places (%.3f)
-- Rounding is done using string.format, to 3 decimal places (%.3f)
Line 132: Line 129:
-- New row
-- New row
result = result .. "|-\n"
table.insert(result, "|-")
-- Cells for bright generator, as steps in et
-- Cells for bright generator, as steps in et
-- Bright gen cell is accompanied by cells to the left and right.
-- The variable current_depth is the number of left cells; the number of
-- right cells is deepest minus current_depth.
local current_depth = depths[i]
local current_depth = depths[i]
local pre_blank_cells  = string.rep("|\n", current_depth) -- Blank cells preceding the bright gen
local num_right_cells = deepest - current_depth
local bright_gen_cell  = string.format("| [[%s|%s]]\n", et_as_string, bright_generator_string) -- Bright gen
for i = 1, current_depth do
local post_blank_cells = string.rep("|\n", deepest - current_depth) -- Blank cells succeeding the bright gen
table.insert(result, "|")
result = result .. pre_blank_cells .. bright_gen_cell .. post_blank_cells
end
table.insert(result, string.format("| [[%s|%s]]", et_as_string, bright_generator_string))
for i = 1, num_right_cells do
table.insert(result, "|")
end
-- Cells for generators in cents
-- Cells for generators in cents
result = result .. string.format("| %.3f\n", bright_generator_cents)
table.insert(result, string.format("| %.3f", bright_generator_cents))
result = result .. string.format("| %.3f\n", dark_generator_cents  )
table.insert(result, string.format("| %.3f", dark_generator_cents  ))
-- Cell for step ratio
-- Cell for step ratio
result = result .. string.format("| %d:%d\n", step_ratio[1], step_ratio[2])
table.insert(result, string.format("| %d:%d", step_ratio[1], step_ratio[2]))
-- Cell for hardness, with divide-by-zero check
-- Cell for hardness, with divide-by-zero check
Line 155: Line 159:
hardness = string.format("%.3f", step_ratio[1] / step_ratio[2])
hardness = string.format("%.3f", step_ratio[1] / step_ratio[2])
end
end
result = result .. string.format("| %s\n", hardness)
table.insert(result, string.format("| %s", hardness))
-- Cell for comment
-- Cell for comment
Line 168: Line 172:
comment = default_comment .. "<br />" .. custom_comment
comment = default_comment .. "<br />" .. custom_comment
end
end
result = result .. string.format("| style=\"text-align: left;\" | %s\n", comment)
table.insert(result, string.format("| style=\"text-align: left;\" | %s", comment))
end
end
result = result .. "|}"
table.insert(result, "|}")
return result
return table.concat(result, "\n")
end
end


Line 276: Line 280:


local args = {}
local args = {}
--return out_comments
return p._mos_tuning_spectrum(args)
return p._mos_tuning_spectrum(args)
end
end


return p
return p