Added menu functions to template that generate a menu based on the
[cmless] / bin / cmless.py
index ece2313..f4a5a28 100755 (executable)
@@ -4,38 +4,38 @@
 # See LICENSE file for copyright and license details.
 
 # Python imports
 # See LICENSE file for copyright and license details.
 
 # Python imports
-import os, sys, subprocess, cgi
+import sys, os, urllib.parse
 
 # CMless imports
 
 # CMless imports
-import debug, config
-
-# Accepts a string containing a filesystem path to a text file.
-# Returns a string containing the contents of that file.
-def load_file(path):
-    try:
-        with open(path) as f:
-            contents = f.read()
-    except:
-        if debug.print_to_browser: print("Unable to open " + path + " for reading.")
-        sys.exit("Unable to open " + path + " for reading.")
-    return contents
-
-# Accepts a string containing markup'ed text
-# Returns a string containing HTML'ed text
-def process_markup(text):
-    try:
-        p = subprocess.Popen(config.markup_processor,stdout=subprocess.PIPE,stdin=subprocess.PIPE)
-    except:
-        if debug.print_to_browser: print("Unable to open markup processor: " + config.markup_processor)
-        sys.exit("Unable to open markup processor: " + config.markup_processor)
-    text = p.communicate(text.encode('UTF-8'))[0]
-    return(text.decode('UTF-8'))
+import config, misc, template
 
 def main():
     print("Content-Type: text/html;charset=utf-8\n")
 
 
 def main():
     print("Content-Type: text/html;charset=utf-8\n")
 
-    text = load_file(config.site_path_prefix + os.environ['REQUEST_URI'] + config.markup_file_extension)
-    print(process_markup(text))
+    # Since we can't use Apache's DirectoryIndex directive due to a RewriteCond
+    # passing all non-file objects to this script, we implement equivalent
+    # functionality here.
+    if os.environ['REQUEST_URI'][-1] == "/":
+        os.environ['REQUEST_URI'] = os.environ['REQUEST_URI'] + config.default_page
+
+    # Since we use REQUEST_URI to navigate the filesystem we need to unquote it.
+    # In other words, replace things like '%20' with ' ' (a space character).
+    os.environ['REQUEST_URI'] = urllib.parse.unquote(os.environ['REQUEST_URI'])
+
+    # The template engine operates like a micropass compiler with each pass
+    # handling one tag from the template.  Extensions to the template engine
+    # should be executed from here and should not have or create
+    # inter-dependencies.
+    content = misc.load_file(config.site_template_prefix + "/" + config.template_file)
+    content = template.page_title(content)
+    content = template.site_title(content)
+    content = template.current_year(content)
+    content = template.head_meta_description(content)
+    content = template.head_meta_keywords(content)
+    content = template.menu_top_level(content)
+    content = template.menu_current_dir(content)
+    content = template.body(content)
+    print(content)
 
 if __name__ == "__main__":
     sys.exit(main())
 
 if __name__ == "__main__":
     sys.exit(main())