local export = {}
local libraryUtil = require("libraryUtil")
local functions = {}
-- based on roundDown in ]
functions.round_down = function (period, verse)
local verse_number = tonumber(verse)
libraryUtil.checkType("round_down", 1, period, "number", false)
-- Check that verse is a number or convertible to a number.
libraryUtil.checkType("verse", 2, verse_number or verse, "number", false)
return math.floor(verse_number / period) * period
end
functions.concat = function(...)
return table.concat{...}
end
local function compare_component(verse1, verse2)
verse1 = tonumber(verse1) or verse1
verse2 = tonumber(verse2) or verse2
if verse1 == verse2 then
return 0
elseif verse1 < verse2 then
return -1
else
return 1
end
end
local function compare_refs(ref1, ref2)
libraryUtil.checkType("compare_refs", 1, ref1, "table", false)
libraryUtil.checkType("compare_refs", 2, ref2, "table", false)
local comp1 = compare_component(ref1, ref2)
if comp1 ~= 0 or not (ref1 and ref2) then
return comp1
end
local comp2 = compare_component(ref1, ref2)
if comp2 ~= 0 or not (ref1 and ref2) then
return comp2
end
return compare_component(ref1, ref2)
end
-- based on chapterSelect in ]
functions.range_search = function(scheme, ref)
for _, v in ipairs(scheme) do
local low_ref, high_ref, value = v, v, v
-- low_ref <= ref <= high_ref
if compare_refs(low_ref, ref) < 1
and compare_refs(ref, high_ref) < 1 then
return value
end
end
end
functions.array = function(...)
return {...}
end
local function eval(env, list)
if type(list) ~= "table" then
return list
end
local name = list
if type(name) ~= "string" then error("First element in list should be string") end
local func = functions
if not func then
local data_table = env
if not data_table then error("Unknown function or table " .. name) end
local val = data_table
for i = 2, #list do
local key = list
if key == nil then return val end -- Can't have nil keys.
val = val
end
return val
end
local parameters = {}
for i = 2, #list do
local arg = list
if not arg then break end -- Do we want nil arguments before the last?
table.insert(parameters, eval(env, arg))
end
return func(unpack(parameters))
end
function export.make_url(format, args)
end
function export.main()
local array = require "Module:array"()
-- based on part of data.Aristotle.BtoC from ]
local aristotle_verses = {
{{486, "a", 5}, {497, "b", 2}, "1"},
{{497, "b", 6}, {509, "a", 23}, "2"}, {{509, "a", 27}, {523, "a", 27}, "3"},
{{523, "a", 31}, {538, "b", 24}, "4"}, {{538, "b", 28}, {558, "b", 4}, "5"},
{{558, "b", 8}, {581, "a", 5}, "6"}, {{581, "a", 9}, {588, "a", 12}, "7"},
{{588, "a", 16}, {608, "a", 7}, "8"}, {{608, "b", 11}, {633, "b", 8}, "9"},
{{633, "b", 12}, {638}, "10"}
}
-- based on data.Aristotle.rlFormat5 from ]
local aristotle_format = {"concat", "s:el:", {"work_data", "wikisource_title"}, "/",
{"range_search",
{"data", "aristotle_verses"},
{"array", {"params", 1}, {"params", 2}, {"params", 3}}},
"#p", {"params", 1}, {"params", 2}
}
local function assert_range_search(ref, expected)
local result = functions.range_search(aristotle_verses, ref)
if result ~= expected then
array:insert("* ref " .. table.concat(ref, ".") .. " evaluated to "
.. tostring(result) .. " rather than " .. tostring(expected))
else
array:insert("* " .. table.concat(ref, ".") .. " -> " .. tostring(expected))
end
end
assert_range_search({486, "a", 5}, "1")
assert_range_search({486, "a"}, "1")
assert_range_search({487, "a"}, "1")
assert_range_search({487}, "1")
assert_range_search({497, "a"}, "1")
assert_range_search({497, "b"}, "1")
assert_range_search({497}, "1") -- iffy
assert_range_search({485, "a"}, nil)
assert_range_search({485}, nil)
array:insert("* [[" .. eval(
{
params = {"486", "a", "5"},
data = {aristotle_verses = aristotle_verses},
work_data = {wikisource_title = "Των περί τα ζώα ιστοριών"}
},
aristotle_format) .. "]]")
return array:concat("\n")
end
return export