Module:Sandbox/Spacing form/Point

From TestWiki

Note: This Module is under construction.

General documentation

Module:Point is a supporting module for Hvtari, an upcoming constructed language project. A list of "pivots" define the shape and pronunciation of each Hvtari morpheme. Because Hvtari script is generative (containing complex information such as conjugation), uploading an image for each morpheme is not feasible. Therefore, we decided to generate Hvtari script dynamically.

Currently I'm exploring other potential solutions of the issue, including to simply use a romanized writing system.

Functions

Functions here are tentative.

  • searchName(name) - Searches forthe matching point for a string in a static dataset.
  • findNearest(x, y) - Given an arbitrary coordinate, find the nearest point in the static dataset.

--[[
Module:Point (or nothing really, in its current state)
Preview with "User:Spacing form/sandbox"
]]

local pt = {}

-- A fluent HTML interface
pt.printDiv = function(frame)
	local div = mw.html.create("div")
	div:wikitext("''Don't'' reinvent the wheel.")
	div:newline()
	div:newline()
	div:wikitext("<small>Exceptions can be made.</small>")
	div:css("border", "1px solid black")
	return tostring(div)
end

-- Take two strings and display them in two boxes
pt.printParagraphs = function(frame) 	-- use ... for variable number of paragraphs
	local args = frame.args
	local div = mw.html.create("div")
	local p1 = div:tag("p")
	p1:addClass("kename")
	p1:wikitext(args[1])
	local p2 = div:tag("p")
	p2:addClass("larame")
	p2:wikitext(args[2])
	-- call p2:allDone() to return to root node
	return tostring(div)
end

-- Repeat a string n times and display them with decreasing font size
pt.printRepeat = function(frame)
	local args = frame.args
	local s = args[1]
	local n = tonumber(args[2])
	local div = mw.html.create("div")
	for i = 1, n do
		local span = div:tag("span")
		local font_size = mw.ustring.format("%dpx", 16+2*n-2*i)
		span:css("font-size", font_size)
		span:wikitext(s)
	end
	return tostring(div)
end

-- Example of loading and using data from ./data
pt.searchName = function(frame)
	local coords = mw.loadData("Module:Sandbox/Spacing form/Point/data")
	local name = frame.args[1]
	local res = coords[name]
	if res ~= nil then
		return "(" .. res[1] .. ", " .. res[2] .. ")"
	else
		return "[nothing found]"
	end
end

pt.findNearest = function(frame)
	local args = frame.args
	local coords = mw.loadData("Module:Sandbox/Spacing form/Point/data")	-- loaded only once
	local pt = { tonumber(args[1]), tonumber(args[2]) }
	local nearest_distance
	local nearest_name
	for name in pairs(coords) do
		local pt_goal = coords[name]
		local distance = ((pt[1]-pt_goal[1])^2 + (pt[2]-pt_goal[2])^2) ^ (1/2)
		if nearest_distance == nil or distance < nearest_distance then
			nearest_distance = distance
			nearest_name = name
		end
	end
	local ret = mw.ustring.format("Closest Ndoto is N. %s, %.1fm away.", nearest_name, nearest_distance)
	return ret
end

-- Alternative formatting. Use mw.ustring, not string
pt.printCoordsFormat = function( frame )
	local args = frame.args
	local ret = mw.ustring.format("The coordinates are (%.2f, %.2f), rounded to nearest hundredth.", args[1], args[2])
	return ret
end

-- Takes three arguments `name`, `x`, and `y` and returns a string

pt.printRepr = function(frame)
	local params = {"x", "y", "name"}
	local args = pt._getArgs(frame.args, params)
	local ret = mw.ustring.format("(%d,%d) is %s's latest position\n", args.x, args.y, args.name)
	return ret
end

-- Helper function that populates the argument list (edited)
pt._getArgs = function(frame_args, params)
	local new_args = {}
	for k, v in ipairs(frame_args) do
		new_args[params[k]] = frame_args[k]
	end
	for k in pairs(frame_args) do
		new_args[k] = frame_args[k]
	end
	return new_args
end

return pt