Commits

matthijs committed 1c5b5cf

Restructure macros.py handling

This makes the code a bit simpler and allows poole to preset functions
in the macros module (unlike the previous "class nomod" approach, which
caused a function to be treated as an unbound method).

  • Participants
  • Parent commits 53aaec3

Comments (0)

Files changed (1)

         os.mkdir(dir_out)
 
     # macro module
-    class nomod: pass # dummy module, something we can set attributes on
     fname = opj(opts.project, "macros.py")
-    macros = {"__encoding__": opts.output_enc}
-    macmod = opx(fname) and imp.load_source("macros", fname) or nomod
-    setattr(macmod, "options", opts)
-    setattr(macmod, "project", project)
-    setattr(macmod, "input", dir_in)
-    setattr(macmod, "output", dir_out)
-    for attr in dir(macmod):
-        if not attr.startswith("_"):
-            macros[attr] = getattr(macmod, attr)
+    macmod = opx(fname) and imp.load_source("macros", fname)
+    if macmod:
+        macros = macmod.__dict__
+    else:
+        # No macros module was defined, but we still need a dictionary
+        # to use as globals when evaluating expressions
+        macros = {}
+
+    macros["__encoding__"] = opts.output_enc
+    macros["options"] = opts
+    macros["project"] = project
+    macros["input"] = dir_in
+    macros["output"] = dir_out
 
     # -------------------------------------------------------------------------
     # process input files
     pages.sort(key=lambda p: int(p.get("sval", "0")))
 
     macros["pages"] = pages
-    macmod.pages = pages
 
     # -------------------------------------------------------------------------
     # run pre-convert hooks in macro module (named 'once' before)
     # -------------------------------------------------------------------------
 
-    hooks = [a for a in dir(macmod) if re.match(r'hook_preconvert_|once_', a)]
+    hooks = [a for a in macros if re.match(r'hook_preconvert_|once_', a)]
     for fn in sorted(hooks):
-        getattr(macmod, fn)()
+        macros[fn]()
 
     # -------------------------------------------------------------------------
     # convert pages (markdown to HTML)
         print("info   : convert %s" % page.fname)
 
         # replace expressions and statements in page source
-        macmod.page = page
         macros["page"] = page
         out = regx_eval.sub(repl_eval, page.source)
         out = regx_exec.sub(repl_exec, out)
     # run post-convert hooks in macro module
     # -------------------------------------------------------------------------
 
-    hooks = [a for a in dir(macmod) if a.startswith("hook_postconvert_")]
+    hooks = [a for a in macros if a.startswith("hook_postconvert_")]
     for fn in sorted(hooks):
-        getattr(macmod, fn)()
+        macros[fn]()
 
     # -------------------------------------------------------------------------
     # render complete HTML pages
         print("info   : render %s" % page.url)
 
         # replace expressions and statements in page.html
-        macmod.page = page
         macros["page"] = page
         macros["__content__"] = page.html
         out = regx_eval.sub(repl_eval, skeleton)