From c10ee230c83a5dfcda077652c2b16bc8f8a9d221 Mon Sep 17 00:00:00 2001 From: Khaled Hosny Date: Mon, 10 May 2010 21:19:21 +0300 Subject: Sync with ConTeXt stable (stable 2010.05.08) No functions removed, few new additions. --- lualibs-dimen.lua | 28 ++++++++++++---------- lualibs-dir.lua | 70 ++++++++++++++++++++++++++++++++++++------------------- lualibs-lpeg.lua | 2 +- lualibs-os.lua | 3 ++- 4 files changed, 65 insertions(+), 38 deletions(-) diff --git a/lualibs-dimen.lua b/lualibs-dimen.lua index a8faa27..da5ab14 100644 --- a/lualibs-dimen.lua +++ b/lualibs-dimen.lua @@ -128,9 +128,9 @@ capture takes place.

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 + Cc(1)) -- tonumber is new +local dimenpair = amount/tonumber * (unit^1/dimenfactors + Cc(1)) -- tonumber is new -lpeg.patterns.dimenpair = pattern +lpeg.patterns.dimenpair = dimenpair --[[ldx--

We use a metatable to intercept errors. When no key is found in @@ -149,7 +149,7 @@ function string:todimen() if type(self) == "number" then return self else - local value, unit = lpegmatch(pattern,self) + local value, unit = lpegmatch(dimenpair,self) return value/unit end end @@ -158,7 +158,7 @@ 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 +local validdimen = amount * unit lpeg.patterns.validdimen = pattern @@ -166,12 +166,12 @@ lpeg.patterns.validdimen = pattern

This converter accepts calls like:

-string.todimen("10")) -string.todimen(".10")) -string.todimen("10.0")) -string.todimen("10.0pt")) -string.todimen("10pt")) -string.todimen("10.0pt")) +string.todimen("10") +string.todimen(".10") +string.todimen("10.0") +string.todimen("10.0pt") +string.todimen("10pt") +string.todimen("10.0pt")

And of course the often more efficient:

@@ -371,7 +371,7 @@ function dimen(a) if k then a = k else - local value, unit = lpegmatch(pattern,a) + local value, unit = lpegmatch(dimenpair,a) if type(unit) == "function" then k = value/unit() else @@ -395,7 +395,7 @@ function string:todimen() else local k = known[self] if not k then - local value, unit = lpegmatch(pattern,self) + local value, unit = lpegmatch(dimenpair,self) if value and unit then k = value/unit else @@ -408,6 +408,10 @@ function string:todimen() end end +function number.toscaled(d) + return format("0.5f",d/2^16) +end + --[[ldx--

In a similar fashion we can define a glue datatype. In that case we probably use a hash instead of a one-element table.

diff --git a/lualibs-dir.lua b/lualibs-dir.lua index 0d08362..5828d99 100644 --- a/lualibs-dir.lua +++ b/lualibs-dir.lua @@ -14,6 +14,12 @@ local lpegmatch = lpeg.match dir = dir or { } +-- handy + +function dir.current() + return (gsub(lfs.currentdir(),"\\","/")) +end + -- optimizing for no string.find (*) does not save time local attributes = lfs.attributes @@ -92,29 +98,48 @@ local filter = Cs ( ( )^0 ) local function glob(str,t) - if type(str) == "table" then - local t = t or { } - for s=1,#str do - glob(str[s],t) + if type(t) == "function" then + if type(str) == "table" then + for s=1,#str do + glob(str[s],t) + end + elseif lfs.isfile(str) then + t(str) + else + local split = lpegmatch(pattern,str) + if split then + local root, path, base = split[1], split[2], split[3] + local recurse = find(base,"%*%*") + local start = root .. path + local result = lpegmatch(filter,start .. base) + glob_pattern(start,result,recurse,t) + end end - return t - elseif lfs.isfile(str) then - local t = t or { } - t[#t+1] = str - return t else - local split = lpegmatch(pattern,str) - if split then + if type(str) == "table" then local t = t or { } - local action = action or function(name) t[#t+1] = name end - local root, path, base = split[1], split[2], split[3] - local recurse = find(base,"%*%*") - local start = root .. path - local result = lpegmatch(filter,start .. base) - glob_pattern(start,result,recurse,action) + for s=1,#str do + glob(str[s],t) + end + return t + elseif lfs.isfile(str) then + local t = t or { } + t[#t+1] = str return t else - return { } + local split = lpegmatch(pattern,str) + if split then + local t = t or { } + local action = action or function(name) t[#t+1] = name end + local root, path, base = split[1], split[2], split[3] + local recurse = find(base,"%*%*") + local start = root .. path + local result = lpegmatch(filter,start .. base) + glob_pattern(start,result,recurse,action) + return t + else + return { } + end end end end @@ -246,8 +271,7 @@ if string.find(os.getenv("PATH"),";") then -- os.type == "windows" function dir.expand_name(str) -- will be merged with cleanpath and collapsepath local first, nothing, last = match(str,"^(//)(//*)(.*)$") if first then - first = lfs.currentdir() .. "/" - first = gsub(first,"\\","/") + first = dir.current() .. "/" end if not first then first, last = match(str,"^(//)/*(.*)$") @@ -257,15 +281,13 @@ if string.find(os.getenv("PATH"),";") then -- os.type == "windows" if first and not find(last,"^/") then local d = lfs.currentdir() if lfs.chdir(first) then - first = lfs.currentdir() - first = gsub(first,"\\","/") + first = dir.current() end lfs.chdir(d) end end if not first then - first, last = lfs.currentdir(), str - first = gsub(first,"\\","/") + first, last = dir.current(), str end last = gsub(last,"//","/") last = gsub(last,"/%./","/") diff --git a/lualibs-lpeg.lua b/lualibs-lpeg.lua index 2e366a9..f060f3b 100644 --- a/lualibs-lpeg.lua +++ b/lualibs-lpeg.lua @@ -120,7 +120,7 @@ function string:checkedsplit(separator) return match(c,self) end ---~ function lpeg.L(list,pp) +--~ function lpeg.append(list,pp) --~ local p = pp --~ for l=1,#list do --~ if p then diff --git a/lualibs-os.lua b/lualibs-os.lua index 4f0c0c1..fba2cd3 100644 --- a/lualibs-os.lua +++ b/lualibs-os.lua @@ -87,12 +87,13 @@ end --~ print(os.date("%H:%M:%S",os.time())) -- no need for function anymore as we have more clever code and helpers now +-- this metatable trickery might as well disappear os.resolvers = os.resolvers or { } local resolvers = os.resolvers -local osmt = getmetatable(os) or { __index = function(t,k) t[k] = "unset" return "unset" end } +local osmt = getmetatable(os) or { __index = function(t,k) t[k] = "unset" return "unset" end } -- maybe nil local osix = osmt.__index osmt.__index = function(t,k) -- cgit v1.2.3