summaryrefslogtreecommitdiff
path: root/support/make4ht/make4ht-config.lua
blob: 310f5afd7bfd8e94f61c4542bb5005a3ee260381 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
local m = {}

local mkutils = require "mkutils"

local file_exists = mkutils.file_exists
-- function file_exists(name)
-- 	local f=io.open(name,"r")
-- 	if f~=nil then io.close(f) return true else return false end
-- end


local make_name = function(name)
  return table.concat(name, "/")
  -- return name:gsub("//","/")
end

-- find the config file in XDG_CONFIG_HOME  or in the HOME directry
-- the XDG tree is looked up first, the $HOME is used only when it cannot be
-- find in the former
local xdg_config  = function(filename, xdg_config_name)
  local dotfilename = "." .. filename
  local xdg_config_name = xdg_config_name or "config.lua"
  local xdg = os.getenv("XDG_CONFIG_HOME") or ((os.getenv("HOME") or "") ..  "/.config")
  local home = os.getenv("HOME") or os.getenv("USERPROFILE")
  if xdg then
    -- filename like ~/.config/make4ht/config.lua
    local fn = make_name{ xdg ,filename , xdg_config_name }
    if file_exists(fn) then
      return fn
    end
  end
  if home then
    -- ~/.make4ht
    local fn = make_name{ home, dotfilename }
    if file_exists(fn) then
      return fn
    end
  end
end

local find_config = function(filename)
  local filename = "." .. filename
	local current =  lfs.currentdir()
	local path = {}
	current:gsub("([^/]+)", function(s) table.insert(path,s) end)
	local begin = os.type == "windows" and "" or "/"
	for i = #path, 1, -1 do
		local fn =begin .. table.concat(path,"/") .. "/".. filename
		-- print("testing",fn)
		if file_exists(fn) then return fn end
		table.remove(path)
	end
	return false
end

local function load_config(filename, default)
  local default = default or {}
  default.table = table
  default.string = string
  default.io = io
  default.os = os
  default.math = math
  default.print = print
  default.ipairs = ipairs
  default.pairs = pairs
  local f = io.open(filename, "r")
  local contents = f:read("*all")
  f:close()
  load(contents,"sandbox config","bt", default)()
  return default
end

--[[
local function load_config(filename, default)
	local default = default or {}
	if ~file_exists(filename) then 
		return nil, "Cannot load config file "..filename 
	end
	local section = "default"
	local file  = io.open(filename,  "r")
	if ~file then return nil, "Error opening config file"..filename end
	for line in file:lines() do
		local ts = line:match("")
	end
	file:close()
end
--]]

m.find_config     = find_config
m.find_xdg_config = xdg_config
m.load_config     = load_config
return m