parsers.markdown: Make module callable, to allow parsing text as a module draft default tip

Sat, 10 Nov 2012 04:02:30 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Sat, 10 Nov 2012 04:02:30 +0000
changeset 18
a96836139ff9
parent 17
1d0ae741807e

parsers.markdown: Make module callable, to allow parsing text as a module

parsers/markdown.lua file | annotate | diff | comparison | revisions
--- a/parsers/markdown.lua	Sat Nov 10 03:54:44 2012 +0000
+++ b/parsers/markdown.lua	Sat Nov 10 04:02:30 2012 +0000
@@ -119,7 +119,7 @@
 
 -- Set up a table for holding local functions to avoid polluting the global namespace
 local M = {}
-local MT = {__index = _G}
+local MT = {__index = _G, __call = function (M, ...) return M.markdown(...); end }
 setmetatable(M, MT)
 setfenv(1, M)
 
@@ -136,7 +136,14 @@
 	end
 
 	local mt = {__newindex = lock_new_index}
-	if getmetatable(t) then mt.__index = getmetatable(t).__index end
+	local orig_mt = getmetatable(t)
+	if orig_mt then
+		for k, v in pairs(orig_mt) do
+			if k ~= "index" then
+				mt[k] = orig_mt[k]
+			end
+		end
+	end
 	setmetatable(t, mt)
 end
 
@@ -1355,5 +1362,5 @@
 if arg and arg[0]:find("markdown%.lua$") then
 	run_command_line(arg)
 else
-	return markdown
+	return M
 end

mercurial