From 22fa4cc9381179fdd43ca8a251cb49dd2703079d Mon Sep 17 00:00:00 2001 From: Khaled Hosny Date: Mon, 22 Mar 2010 17:49:04 +0200 Subject: Sync with ConTeXt --- luaextra-dimen.lua | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) (limited to 'luaextra-dimen.lua') diff --git a/luaextra-dimen.lua b/luaextra-dimen.lua index e15e294..a8faa27 100644 --- a/luaextra-dimen.lua +++ b/luaextra-dimen.lua @@ -16,7 +16,7 @@ table.

--ldx]]-- local format, match, gsub, type, setmetatable = string.format, string.match, string.gsub, type, setmetatable -local lpegmatch = lpeg.match +local P, S, R, Cc, lpegmatch = lpeg.P, lpeg.S, lpeg.R, lpeg.Cc, lpeg.match number = number or { } @@ -100,6 +100,7 @@ end

We collect a bunch of converters in the number namespace.

--ldx]]-- +number.maxdimen = 1073741823 number.todimen = todimen number.dimenfactors = dimenfactors @@ -124,10 +125,12 @@ a number and optionally a unit. When no unit is given a constant capture takes place.

--ldx]]-- -local amount = (lpeg.S("+-")^0 * lpeg.R("09")^0 * lpeg.P(".")^0 * lpeg.R("09")^0) + lpeg.Cc("0") -local unit = lpeg.R("az")^1 +local amount = (S("+-")^0 * R("09")^0 * P(".")^0 * R("09")^0) + Cc("0") +local unit = R("az")^1 -local pattern = amount/tonumber * (unit^1/dimenfactors + lpeg.Cc(1)) -- tonumber is new +local pattern = amount/tonumber * (unit^1/dimenfactors + Cc(1)) -- tonumber is new + +lpeg.patterns.dimenpair = pattern --[[ldx--

We use a metatable to intercept errors. When no key is found in @@ -147,11 +150,18 @@ function string:todimen() return self else local value, unit = lpegmatch(pattern,self) - print(value,unit) return value/unit end end +local amount = S("+-")^0 * R("09")^0 * S(".,")^0 * R("09")^0 +local unit = P("pt") + P("cm") + P("mm") + P("sp") + P("bp") + P("in") + + P("pc") + P("dd") + P("cc") + P("nd") + P("nc") + +local pattern = amount * unit + +lpeg.patterns.validdimen = pattern + --[[ldx--

This converter accepts calls like:

-- cgit v1.2.3