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
3232end
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 ' ./'
4044end
4145
4246local 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,24 @@ if os.getenv('THEMEPARK_DEBUG') then
6671 themepark .debug = true
6772end
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:
82+ -- * 'themes' directory in the themepark repo
83+ -- * current directory
84+ local themes_dir = themepark .dir :gsub (' /lua/$' , ' /themes' )
85+ table.insert (themepark .theme_search_path , themes_dir )
86+ table.insert (themepark .theme_search_path , ' .' )
87+
88+ if themepark .debug then
89+ print (" Themepark: Theme search path: " .. table.concat (themepark .theme_search_path , ' :' ))
90+ end
91+ end )()
7092
7193-- ---------------------------------------------------------------------------
7294-- set_option(NAME, VALUE)
@@ -117,17 +139,19 @@ end
117139-- ---------------------------------------------------------------------------
118140-- add_theme_dir(DIR)
119141--
120- -- Append DIR to search path for themes. If DIR is a relative path,
142+ -- Prepend DIR to search path for themes. If DIR is a relative path,
121143-- interpret it relative to the file the function was called from.
122144-- ---------------------------------------------------------------------------
123145function themepark :add_theme_dir (dir )
124146 if string.find (dir , ' /' ) ~= 1 then
125- dir = script_path (5 ) .. dir .. ' / '
147+ dir = script_dir (5 ) .. dir
126148 end
149+ table.insert (self .theme_search_path , 1 , dir )
150+
127151 if self .debug then
128- print (" Themepark: Add theme directory at '" .. dir .. " '." )
152+ print (" Themepark: Added theme directory at '" .. dir .. " '." )
153+ print (" Themepark: Theme search path: " .. table.concat (themepark .theme_search_path , ' :' ))
129154 end
130- table.insert (self .theme_path , dir .. ' /' )
131155end
132156
133157-- ---------------------------------------------------------------------------
@@ -144,7 +168,7 @@ function themepark:init_theme(theme)
144168 end
145169
146170 if theme == ' ' then
147- local dir = script_path (2 )
171+ local dir = script_dir (2 )
148172 self .themes [' ' ] = { dir = dir }
149173 if self .debug then
150174 print (" Themepark: Loading theme '' with path '" .. dir .. " ' ..." )
@@ -160,8 +184,8 @@ function themepark:init_theme(theme)
160184 print (" Themepark: Loading theme '" .. theme .. " ' ..." )
161185 end
162186
163- for _ , dir in ipairs (self .theme_path ) do
164- local theme_dir = dir .. theme
187+ for _ , dir in ipairs (self .theme_search_path ) do
188+ local theme_dir = dir .. ' / ' .. theme
165189 local theme_file = theme_dir .. ' /init.lua'
166190 if self .debug then
167191 print (" Themepark: Trying to load from '" .. theme_file .. " ' ..." )
0 commit comments