moduleapi: Add module:get_directory() to get module file's directory, and module:load_resource() to load a file relative to that directory
authorMatthew Wild <mwild1@gmail.com>
Sat, 28 Apr 2012 19:41:04 +0100
changeset 4790 8bf710b19f65
parent 4789 bed0c6117358
child 4791 c01066f27a5d
moduleapi: Add module:get_directory() to get module file's directory, and module:load_resource() to load a file relative to that directory
core/moduleapi.lua
--- a/core/moduleapi.lua	Sat Apr 28 19:38:51 2012 +0100
+++ b/core/moduleapi.lua	Sat Apr 28 19:41:04 2012 +0100
@@ -329,4 +329,14 @@
 	end);
 end
 
+local path_sep = package.config:sub(1,1);
+function api:get_directory()
+	return self.path and (self.path:gsub("%"..path_sep.."[^"..path_sep.."]*$", "")) or nil;
+end
+
+function api:load_resource(path, mode)
+	path = config.resolve_relative_path(self:get_directory(), path);
+	return io.open(path, mode);
+end
+
 return api;