[latex3-commits] [git/LaTeX3-latex3-luaotfload] modules: Load parsers as module (21e0426)

Marcel Fabian Krüger tex at 2krueger.de
Sun May 24 04:28:54 CEST 2020


Repository : https://github.com/latex3/luaotfload
On branch  : modules
Link       : https://github.com/latex3/luaotfload/commit/21e0426dfbbd89eda594255c691c0bcc7258eaad

>---------------------------------------------------------------

commit 21e0426dfbbd89eda594255c691c0bcc7258eaad
Author: Marcel Fabian Krüger <tex at 2krueger.de>
Date:   Sun May 24 04:17:19 2020 +0200

    Load parsers as module


>---------------------------------------------------------------

21e0426dfbbd89eda594255c691c0bcc7258eaad
 src/luaotfload-configuration.lua |   2 +-
 src/luaotfload-database.lua      | 172 ++++++++++++++++++++-------------------
 src/luaotfload-diagnostics.lua   |   2 +-
 src/luaotfload-features.lua      |   3 +-
 src/luaotfload-main.lua          |   1 -
 src/luaotfload-tool.lua          |   3 +-
 6 files changed, 92 insertions(+), 91 deletions(-)

diff --git a/src/luaotfload-configuration.lua b/src/luaotfload-configuration.lua
index a419387..b9b702e 100644
--- a/src/luaotfload-configuration.lua
+++ b/src/luaotfload-configuration.lua
@@ -1013,7 +1013,7 @@ end
 
 return function ()
   config.luaotfload = { }
-  local parsers     = luaotfload.parsers
+  local parsers     = require 'luaotfload-parsers'
   config_parser     = parsers.config
   stripslashes      = parsers.stripslashes
 
diff --git a/src/luaotfload-database.lua b/src/luaotfload-database.lua
index 6fdfc91..1fe6990 100644
--- a/src/luaotfload-database.lua
+++ b/src/luaotfload-database.lua
@@ -133,9 +133,7 @@ local report_status_stop       = log.names_status_stop
 
 
 --- Luatex builtins
-local load                     = load
 local next                     = next
-local require                  = require
 local tonumber                 = tonumber
 local unpack                   = table.unpack
 
@@ -144,8 +142,6 @@ local fontshandlers            = fonts.handlers     or { }
 local otfhandler               = fonts.handlers.otf or { }
 fonts.handlers                 = fontshandlers
 
-local gzipload                 = gzip.load
-local gzipsave                 = gzip.save
 local iolines                  = io.lines
 local ioopen                   = io.open
 local kpseexpand_path          = kpse.expand_path
@@ -203,10 +199,12 @@ local names                    = fonts and fonts.names or { }
 local resolversfindfile        = context_environment.resolvers.findfile
 
 --- some of our own
-local unicode                  = require'luaotfload-unicode'
+local unicode                  = require 'luaotfload-unicode'
 local casefold                 = unicode.casefold
 local alphnum_only             = unicode.alphnum_only
 
+local parsers                  = require 'luaotfload-parsers'
+
 local name_index               = nil --> upvalue for names.data
 local lookup_cache             = nil --> for names.lookups
 
@@ -279,7 +277,7 @@ local macroman2utf8 do
         for i=1,#bytes do
             bytes[i] = mapping[bytes[i]] or bytes[i]
         end
-        return utf8.char(table.unpack(bytes))
+        return utf8.char(unpack(bytes))
     end
 end
 local function sanitize_fontname (str)
@@ -488,37 +486,41 @@ end
 --- .luc.
 
 --- string -> (string * table)
-local function load_lua_file (path)
-    local foundname = filereplacesuffix (path, "luc")
-    local code      = nil
-
-    local fh = ioopen (foundname, "rb") -- try bin first
-    if fh then
-        local chunk = fh:read"*all"
-        fh:close()
-        code = load (chunk, "b")
-    end
-
-    if not code then --- fall back to text file
-        foundname = filereplacesuffix (path, "lua")
-        fh = ioopen(foundname, "rb")
+local load_lua_file do
+    local gzipload = gzip.load
+    local load     = load
+    function load_lua_file (path)
+        local foundname = filereplacesuffix (path, "luc")
+        local code      = nil
+
+        local fh = ioopen (foundname, "rb") -- try bin first
         if fh then
             local chunk = fh:read"*all"
             fh:close()
-            code = load (chunk, "t")
+            code = load (chunk, "b")
         end
-    end
 
-    if not code then --- probe gzipped file
-        foundname = filereplacesuffix (path, "lua.gz")
-        local chunk = gzipload (foundname)
-        if chunk then
-            code = load (chunk, "t")
+        if not code then --- fall back to text file
+            foundname = filereplacesuffix (path, "lua")
+            fh = ioopen(foundname, "rb")
+            if fh then
+                local chunk = fh:read"*all"
+                fh:close()
+                code = load (chunk, "t")
+            end
+        end
+
+        if not code then --- probe gzipped file
+            foundname = filereplacesuffix (path, "lua.gz")
+            local chunk = gzipload (foundname)
+            if chunk then
+                code = load (chunk, "t")
+            end
         end
-    end
 
-    if not code then return nil, nil end
-    return foundname, code ()
+        if not code then return nil, nil end
+        return foundname, code ()
+    end
 end
 
 --- define locals in scope
@@ -2126,7 +2128,7 @@ do
             return
         end
 
-        local splitcomma = luaotfload.parsers and luaotfload.parsers.splitcomma
+        local splitcomma = parsers.splitcomma
 
         if stringsub (formats, 1, 1) == "+" then -- add
             formats = lpegmatch (splitcomma, stringsub (formats, 2))
@@ -2333,7 +2335,7 @@ end
 local function filter_out_pwd (dirs)
     local result = { }
     if stripslashes == nil then
-        stripslashes = luaotfload.parsers and luaotfload.parsers.stripslashes
+        stripslashes = parsers.stripslashes
     end
     local pwd = path_normalize (lpegmatch (stripslashes,
                                            lfscurrentdir ()))
@@ -2422,10 +2424,7 @@ local function get_os_dirs ()
             "/usr/local/etc/fonts/fonts.conf",
             "/etc/fonts/fonts.conf",
         }
-        if not luaotfload.parsers then
-            logreport ("log", 0, "db", "Fatal: no fonts.conf parser.")
-        end
-        local os_dirs = luaotfload.parsers.read_fonts_conf(fonts_conves, find_files)
+        local os_dirs = parsers.read_fonts_conf(fonts_conves, find_files)
         return os_dirs
     end
     return {}
@@ -3388,7 +3387,7 @@ end
 --- dry_dun:    don’t write to the db, just scan dirs
 
 --- dbobj? -> bool? -> bool? -> dbobj
-update_names = function (currentnames, force, dry_run)
+function update_names(currentnames, force, dry_run)
     local targetnames
     local n_new = 0
     local n_rem = 0
@@ -3511,7 +3510,7 @@ update_names = function (currentnames, force, dry_run)
 end
 
 --- unit -> bool
-save_lookups = function ( )
+function save_lookups()
     local paths = config.luaotfload.paths
     local luaname, lucname = paths.lookup_path_lua, paths.lookup_path_luc
     if fileiswritable (luaname) and fileiswritable (lucname) then
@@ -3538,57 +3537,60 @@ end
 
 --- save_names() is usually called without the argument
 --- dbobj? -> bool * string option
-save_names = function (currentnames)
-    if not currentnames then
-        currentnames = name_index
-    end
-    if not currentnames or type (currentnames) ~= "table" then
-        return false, "invalid names table"
-    elseif currentnames.meta and currentnames.meta["local"] then
-        return false, "table contains local entries"
-    end
-    local paths = config.luaotfload.paths
-    local luaname, lucname = paths.index_path_lua, paths.index_path_luc
-    if fileiswritable (luaname) and fileiswritable (lucname) then
-        osremove (lucname)
-        local gzname = luaname .. ".gz"
-        if config.luaotfload.db.compress then
-            local serialized = tableserialize (currentnames, true)
-            gzipsave (gzname, serialized)
-            caches.compile (currentnames, "", lucname)
-        else
-            tabletofile (luaname, currentnames, true)
-            caches.compile (currentnames, luaname, lucname)
-        end
-        logreport ("info", 2, "db", "Font index saved at ...")
-        local success = false
-        if lfsisfile (luaname) then
-            logreport ("info", 2, "db", "Text: " .. luaname)
-            success = true
-        end
-        if lfsisfile (gzname) then
-            logreport ("info", 2, "db", "Gzip: " .. gzname)
-            success = true
+do
+    local gzipsave = gzip.save
+    function save_names(currentnames)
+        if not currentnames then
+            currentnames = name_index
+        end
+        if not currentnames or type (currentnames) ~= "table" then
+            return false, "invalid names table"
+        elseif currentnames.meta and currentnames.meta["local"] then
+            return false, "table contains local entries"
+        end
+        local paths = config.luaotfload.paths
+        local luaname, lucname = paths.index_path_lua, paths.index_path_luc
+        if fileiswritable (luaname) and fileiswritable (lucname) then
+            osremove (lucname)
+            local gzname = luaname .. ".gz"
+            if config.luaotfload.db.compress then
+                local serialized = tableserialize (currentnames, true)
+                gzipsave (gzname, serialized)
+                caches.compile (currentnames, "", lucname)
+            else
+                tabletofile (luaname, currentnames, true)
+                caches.compile (currentnames, luaname, lucname)
+            end
+            logreport ("info", 2, "db", "Font index saved at ...")
+            local success = false
+            if lfsisfile (luaname) then
+                logreport ("info", 2, "db", "Text: " .. luaname)
+                success = true
+            end
+            if lfsisfile (gzname) then
+                logreport ("info", 2, "db", "Gzip: " .. gzname)
+                success = true
+            end
+            if lfsisfile (lucname) then
+                logreport ("info", 2, "db", "Byte: " .. lucname)
+                success = true
+            end
+            if success then
+                return true
+            else
+                logreport ("info", 0, "db", "Could not compile font index.")
+                return false
+            end
         end
-        if lfsisfile (lucname) then
-            logreport ("info", 2, "db", "Byte: " .. lucname)
-            success = true
+        logreport ("info", 0, "db", "Index file not writable")
+        if not fileiswritable (luaname) then
+            logreport ("info", 0, "db", "Failed to write %s.", luaname)
         end
-        if success then
-            return true
-        else
-            logreport ("info", 0, "db", "Could not compile font index.")
-            return false
+        if not fileiswritable (lucname) then
+            logreport ("info", 0, "db", "Failed to write %s.", lucname)
         end
+        return false
     end
-    logreport ("info", 0, "db", "Index file not writable")
-    if not fileiswritable (luaname) then
-        logreport ("info", 0, "db", "Failed to write %s.", luaname)
-    end
-    if not fileiswritable (lucname) then
-        logreport ("info", 0, "db", "Failed to write %s.", lucname)
-    end
-    return false
 end
 
 --[[doc--
diff --git a/src/luaotfload-diagnostics.lua b/src/luaotfload-diagnostics.lua
index e7ee76c..3433c7f 100644
--- a/src/luaotfload-diagnostics.lua
+++ b/src/luaotfload-diagnostics.lua
@@ -62,7 +62,7 @@ local function out (...)
     report (false, 0, "diagnose", ...)
 end
 
-local parsers                  = luaotfload.parsers
+local parsers                  = require "luaotfload-parsers"
 local stripslashes             = parsers.stripslashes
 local splitcomma               = parsers.splitcomma
 
diff --git a/src/luaotfload-features.lua b/src/luaotfload-features.lua
index ce4c32e..549712b 100644
--- a/src/luaotfload-features.lua
+++ b/src/luaotfload-features.lua
@@ -356,9 +356,10 @@ do
     extract_subfont   = full_path * sub_expr
 end
 
+local font_request_pattern = require "luaotfload-parsers".font_request
 --- spec -> spec
 local function handle_request (specification)
-    local request = lpegmatch(luaotfload.parsers.font_request,
+    local request = lpegmatch(font_request_pattern,
                               specification.specification)
 ----inspect(request)
     if not request then
diff --git a/src/luaotfload-main.lua b/src/luaotfload-main.lua
index 5f4a10e..2d046fb 100644
--- a/src/luaotfload-main.lua
+++ b/src/luaotfload-main.lua
@@ -181,7 +181,6 @@ luaotfload.main = function ()
 
     local init      = loadmodule "fontloader" --- fontloader initialization
     init (function ()
-        luaotfload.parsers = loadmodule "parsers"         --- fonts.conf and syntax
         initialize "configuration"   --- configuration options
     end)
 
diff --git a/src/luaotfload-tool.lua b/src/luaotfload-tool.lua
index e527b6b..f1da05b 100755
--- a/src/luaotfload-tool.lua
+++ b/src/luaotfload-tool.lua
@@ -115,6 +115,7 @@ local tablekeys                 = table.keys
 local tableserialize            = table.serialize
 local tablesortedkeys           = table.sortedkeys
 local tabletohash               = table.tohash
+local splitcomma                = require "luaotfload-parsers".splitcomma
 
 --[[doc--
 \fileent{luatex-basics-gen.lua} calls functions from the
@@ -185,7 +186,6 @@ end
 require "alt_getopt"
 
 local log = require "luaotfload-log"
-luaotfload.parsers = loadmodule "parsers"       --- fonts.conf, configuration, and request syntax
 loadmodule "configuration" --- configuration file handling
 loadmodule "database"
 loadmodule "resolvers"     --- Font lookup
@@ -1291,7 +1291,6 @@ function actions.list (job)
     local name_index    = fonts.names.data ()
 
     if asked_fields then
-        local splitcomma = luaotfload.parsers.splitcomma
         asked_fields = lpegmatch(splitcomma, asked_fields)
     end
 





More information about the latex3-commits mailing list.