Module:MOS scalesig

Revision as of 10:20, 8 March 2025 by Ganaram inukshuk (talk | contribs) (module now calls link functions from module:mos)
Module documentation[view] [edit] [history] [purge]
Note: Do not invoke this module directly; use the corresponding template instead: Template:MOS scalesig.

This module formats a string into a mos scale signature, with non-breaking spaces. Scale signatures can optionally be formatted as a link to the scale's wiki page.

To include this module in other modules, use local mos_scalesig = require("Module:MOS scalesig")._mos_scalesig.

local mos = require("Module:MOS")
local yesno = require("Module:Yesno")
local getArgs = require("Module:Arguments").getArgs
local p = {}

-- Preprocess unparsed scalesig; if it's raw text, then it's a scalesig to be
-- parsed into a mos; if it's a mos as defined by the mos module, return it.
function p.preprocess_unparsed_scalesig(unparsed)
	if unparsed.nL ~= nil and unparsed.ns ~= nil and unparsed.equave ~= nil then
		return unparsed
	else
		return mos.parse(unparsed)
	end
end

-- Function to format a string into a mos scalesig, with nonbreaking spaces.
-- Options are:
-- - is_long - Whether to use "xL ys<p/q>" or "xL ys (p/q-equivalent)". Only
--   applies for nonoctave mosses, as a scalesig without an equave is assumed to
--   be 2/1-equivalent.
-- - is_link - Whether the scalesig is a link to its wiki page.
function p._mos_scalesig(unparsed, is_link, is_long)
	local input_mos = p.preprocess_unparsed_scalesig(unparsed)
	local is_link = yesno(is_link, false)
	local is_long = yesno(is_long, false)
	
	if is_link then
		return is_long and mos.as_long_link(input_mos) or mos.as_link(input_mos)
	else
		return is_long and mos.as_long_string(input_mos) or mos.as_string(input_mos)
	end
end

-- Wrapper function for use with a template
function p.mos_scalesig(frame)
	local args = getArgs(frame)
	
	local unparsed = args["scalesig"]
	local is_link = yesno(args["link"], false)
	local is_long = yesno(args["long"], false)
	
	return p._mos_scalesig(unparsed, is_link, is_long)
end

function p.tester()
	local input_mos = mos.new(5,2)
	return p._mos_scalesig(input_mos, true, true)
end

return p