Skip to content

Commit e4c03ba

Browse files
committed
New env var THEMEPARK_PATH can be used to set theme search path
As usual use a colon (:) to seperate items in the path. Initial search path is now whetever is in THEMEPARK_PATH and then the "themes" directory of the repo. The themepark:add_theme_dir() function now prepends the specified directory to the search path instead of appending it. In debug mode the search path is printed at the start and whenever it changes.
1 parent 6df5864 commit e4c03ba

File tree

1 file changed

+36
-14
lines changed

1 file changed

+36
-14
lines changed

lua/themepark.lua

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,26 @@
2626
--
2727
-- ---------------------------------------------------------------------------
2828

29-
local function script_path_impl(num)
30-
local str = debug.getinfo(num, "S").source:sub(2)
31-
return str:match("(.*/)")
29+
local function script_dir_impl(num)
30+
local src = debug.getinfo(num, "S").source
31+
return src:match("^@(.*/)") -- return directory
3232
end
3333

34-
local function script_path(num)
35-
local success, value = pcall(script_path_impl, num)
34+
-- Return the directory of the script calling this function directly or
35+
-- indirectly. Goes 'num' levels up the call stack, and returns the directory
36+
-- of the script running the function found in that way. Returns the current
37+
-- directory ('./') if that fails.
38+
local function script_dir(num)
39+
local success, value = pcall(script_dir_impl, num)
3640
if success and value then
3741
return value
3842
end
3943
return './'
4044
end
4145

4246
local themepark = {
43-
dir = script_path(2),
47+
dir = script_dir(1),
48+
theme_search_path = {},
4449
debug = false,
4550
options = {
4651
schema = 'public',
@@ -66,7 +71,22 @@ if os.getenv('THEMEPARK_DEBUG') then
6671
themepark.debug = true
6772
end
6873

69-
themepark.theme_path = { script_path(3) .. '../themes/', themepark.dir .. 'themes/' }
74+
(function()
75+
-- Use search path from THEMEPARK_PATH env variable if available
76+
local search_path_from_env = os.getenv('THEMEPARK_PATH')
77+
if search_path_from_env then
78+
themepark.theme_search_path = osm2pgsql.split_string(search_path_from_env, ':')
79+
end
80+
81+
-- Theme search path always contains the 'themes' directory in the
82+
-- themepark repo.
83+
local themes_dir = themepark.dir:gsub('/lua/$', '/themes')
84+
table.insert(themepark.theme_search_path, themes_dir)
85+
86+
if themepark.debug then
87+
print("Themepark: Theme search path: " .. table.concat(themepark.theme_search_path, ':'))
88+
end
89+
end)()
7090

7191
-- ---------------------------------------------------------------------------
7292
-- set_option(NAME, VALUE)
@@ -117,17 +137,19 @@ end
117137
-- ---------------------------------------------------------------------------
118138
-- add_theme_dir(DIR)
119139
--
120-
-- Append DIR to search path for themes. If DIR is a relative path,
140+
-- Prepend DIR to search path for themes. If DIR is a relative path,
121141
-- interpret it relative to the file the function was called from.
122142
-- ---------------------------------------------------------------------------
123143
function themepark:add_theme_dir(dir)
124144
if string.find(dir, '/') ~= 1 then
125-
dir = script_path(5) .. dir .. '/'
145+
dir = script_dir(5) .. dir
126146
end
147+
table.insert(self.theme_search_path, 1, dir)
148+
127149
if self.debug then
128-
print("Themepark: Add theme directory at '" .. dir .. "'.")
150+
print("Themepark: Added theme directory at '" .. dir .. "'.")
151+
print("Themepark: Theme search path: " .. table.concat(themepark.theme_search_path, ':'))
129152
end
130-
table.insert(self.theme_path, dir .. '/')
131153
end
132154

133155
-- ---------------------------------------------------------------------------
@@ -144,7 +166,7 @@ function themepark:init_theme(theme)
144166
end
145167

146168
if theme == '' then
147-
local dir = script_path(2)
169+
local dir = script_dir(2)
148170
self.themes[''] = { dir = dir }
149171
if self.debug then
150172
print("Themepark: Loading theme '' with path '" .. dir .. "' ...")
@@ -160,8 +182,8 @@ function themepark:init_theme(theme)
160182
print("Themepark: Loading theme '" .. theme .. "' ...")
161183
end
162184

163-
for _, dir in ipairs(self.theme_path) do
164-
local theme_dir = dir .. theme
185+
for _, dir in ipairs(self.theme_search_path) do
186+
local theme_dir = dir .. '/' .. theme
165187
local theme_file = theme_dir .. '/init.lua'
166188
if self.debug then
167189
print("Themepark: Trying to load from '" .. theme_file .. "' ...")

0 commit comments

Comments
 (0)