summaryrefslogtreecommitdiff
path: root/rst_helpers.lua
diff options
context:
space:
mode:
authorPhilipp Gesang <pgesang@ix.urz.uni-heidelberg.de>2010-09-13 23:04:15 +0200
committerPhilipp Gesang <pgesang@ix.urz.uni-heidelberg.de>2010-09-13 23:04:15 +0200
commit89746a438f5f65f969fb46105590a1c5d9c83423 (patch)
treeeb37fc2c0271aaa9cf695fb11376d2a75ccd3f00 /rst_helpers.lua
parent8a2b1fc87836edbbaaebdc3ae8759f843711f95f (diff)
downloadcontext-rst-89746a438f5f65f969fb46105590a1c5d9c83423.tar.gz
moved the list numbering to helpers
Diffstat (limited to 'rst_helpers.lua')
-rw-r--r--rst_helpers.lua111
1 files changed, 111 insertions, 0 deletions
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