Module:SB tree: Difference between revisions
Preparing module for use with a template |
ArrowHead294 (talk | contribs) mNo edit summary |
||
| (9 intermediate revisions by 2 users not shown) | |||
| Line 1: | Line 1: | ||
local p = {} | local p = {} | ||
local yesno = require("Module:Yesno") | |||
-- Function that constructs a sequence of ratios according to the Stern-Brocot tree | -- Function that constructs a sequence of ratios according to the Stern-Brocot tree | ||
| Line 117: | Line 119: | ||
-- Test function that produces the ratios of the SB tree as a multi-column table, using different arguments | -- Test function that produces the ratios of the SB tree as a multi-column table, using different arguments | ||
-- To try this out, add the following text (not as a lua comment): | -- To try this out, add the following text (not as a lua comment): | ||
-- {{#invoke:SB_tree|sb_table}} | -- {{#invoke: SB_tree|sb_table}} | ||
function p.sb_table(frame) | function p.sb_table(frame) | ||
-- Call the sb function | -- Call the sb function | ||
-- Start/stop ratios are the same, depth is deeper, and edge-extend is allowed | -- Start/stop ratios are the same, depth is deeper, and edge-extend is allowed | ||
local depth = frame.args[ | local depth = frame.args["Depth"] or 4 | ||
local edge_extend = frame.args[ | local edge_extend = frame.args["Edge Extend"] or 2 | ||
local start_ratio_unparsed = frame.args[ | local start_ratio_unparsed = frame.args["Start Ratio"] or "1/1" | ||
local stop_ratio_unparsed = frame.args[ | local stop_ratio_unparsed = frame.args["Stop Ratio"] or "1/0" | ||
local stagger_ratios = (frame.args["Stagger Ratios"] and string.lower(frame.args["Stagger Ratios"])) == "true" and true or false | |||
-- Parse ratios | -- Parse ratios | ||
local start_ratio = p.parse_ratio(start_ratio_unparsed) | local start_ratio = p.parse_ratio(start_ratio_unparsed) | ||
local stop_ratio = p.parse_ratio( | local stop_ratio = p.parse_ratio(stop_ratio_unparsed) | ||
-- Get the ratios and depths | -- Get the ratios and depths | ||
| Line 136: | Line 138: | ||
-- Create the table | -- Create the table | ||
result = | result = "{| class=\"wikitable\"\n|-\n" | ||
.. "! " | |||
-- Create the header cell | |||
if stagger_ratios then | |||
-- Create the | result = result .. "colspan=\"" .. depth + edge_extend + 1 .. "\" | " | ||
result = result .. | end | ||
result = result .. "Ratios\n" | |||
-- Create the individual rows | -- Create the individual rows | ||
| Line 148: | Line 151: | ||
result = result .. "|-\n" | result = result .. "|-\n" | ||
-- Create the cells for each row | -- Create the cells for each row | ||
-- | if stagger_ratios then | ||
-- Stagger ratios | |||
for j = 1, depth + edge_extend + 1 do | |||
result = result .. "| " | |||
if j == sb_tree_depths[i] then | |||
result = result .. " | result = result .. sb_tree_ratios[i][1] .. "/" .. sb_tree_ratios[i][2] | ||
end | |||
result = result .. "\n" | |||
end | end | ||
else | |||
-- Don't stagger ratios | |||
result = result .. "| " .. sb_tree_ratios[i][1] .. "/" .. sb_tree_ratios[i][2] .. "\n" | |||
end | end | ||
end | end | ||
| Line 161: | Line 169: | ||
result = result .. "|}" | result = result .. "|}" | ||
return result | -- Debugger option | ||
local debugg = yesno(frame.args["debug"]) | |||
if debugg == true then | |||
result = "<syntaxhighlight lang=\"wikitext\">" .. result .. "</syntaxhighlight>" | |||
end | |||
return frame:preprocess(result) | |||
end | end | ||
return p | return p | ||