From 89746a438f5f65f969fb46105590a1c5d9c83423 Mon Sep 17 00:00:00 2001 From: Philipp Gesang Date: Mon, 13 Sep 2010 23:04:15 +0200 Subject: moved the list numbering to helpers --- rst_helpers.lua | 111 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) (limited to 'rst_helpers.lua') diff --git a/rst_helpers.lua b/rst_helpers.lua index a635996..b30370a 100644 --- a/rst_helpers.lua +++ b/rst_helpers.lua @@ -590,5 +590,116 @@ function helpers.table.simple(raw) return rows end +helpers.list = {} + +do + local c = {} + c.roman = S"ivxlcdm"^1 + c.Roman = S"IVXLCDM"^1 + c.alpha = R"az" - P"i" + c.Alpha = R"AZ" - P"I" + c.digit = R"09"^1 + c.auto = P"#" + + local stripme = S" ()." + local dontstrip = 1 - stripme + local itemstripper = stripme^0 * C(dontstrip^1) * stripme^0 + + local con = function (str) + --print("This is it: >"..str.."<") + str = itemstripper:match(str) + for conv, pat in next, c do + if pat:match(str) then + return conv + end + end + return false + end + helpers.list.conversion = con + + local rnums = { + i = 1, + v = 5, + x = 10, + l = 50, + c = 100, + d = 500, + m = 1000, + } + + local function roman_to_arab (str) + local n = 1 + local curr, succ + local max_three = { } + local value = 0 + while n <= #str do + if curr and curr == max_three[#max_three] then + if #max_three >= 3 then + return "Not a number" + else + max_three[#max_three+1] = curr + end + else + max_three = { curr } + end + + curr = rnums[str:sub(n,n)] + + n = n + 1 + succ = str:sub(n,n) + + if succ and succ ~= "" then + succ = rnums[succ] + if curr < succ then + --n = n + 1 + --value = value + succ - curr + value = value - curr + else + value = value + curr + end + else + value = value + curr + end + end + return value + end + helpers.list.roman_to_arab = roman_to_arab + + local suc = function (str, old) + str, old = itemstripper:match(str), itemstripper:match(old) + local n_str, n_old = tonumber(str), tonumber(old) + if n_str and n_old then -- arabic numeral + return n_str == n_old + 1 + end + + local con_str, con_old = con(str), con(old) + if con_str == "alpha" or + con_str == "Alpha" then + return str:byte() == old:byte() + 1 + else -- “I'm a Roman!” - “A woman?” - “No, *Roman*! - Au!” - “So your father was a woman?” + if not (str:lower() == str or + str:upper() == str) then -- uneven cased --> fail + return false + end + + + local trc = state.roman_cache + n_str = trc[str] or nil + n_old = trc[old] or nil + if not n_str then + n_str = roman_to_arab(str:lower()) + trc[str] = n_str + end + if not n_old then + n_old = roman_to_arab(old:lower()) + trc[old] = n_old + end + --print(n_str, n_old, n_str == n_old + 1 ) + return n_str == n_old + 1 + end + end + helpers.list.successor = suc +end + return helpers -- cgit v1.2.3