Added menu functions to template that generate a menu based on the
[cmless] / bin / template.py
index cdc7fe7..5950cdf 100644 (file)
@@ -10,17 +10,77 @@ import config, misc
 def add_delimiter(keyword):
     return config.template_delimiter + keyword + config.template_delimiter
 
 def add_delimiter(keyword):
     return config.template_delimiter + keyword + config.template_delimiter
 
+def menu_top_level(template):
+    """Generate a menu based on the directories contained in config.site_data_prefix.
+
+    Only top level directories are included, i.e. no recursion.  The root
+    directory '.' is included with the menu name "Home".  All other directories
+    use the directory name as the menu name.  Each entry is wrapped in an HTML
+    <a> tag for direct use as a menu.
+    """
+    keyword = add_delimiter("top_level_menu")
+
+    dir_list = [["Home","/"]]
+
+    for entry in os.listdir(config.site_data_prefix):
+        if os.path.isdir(os.path.join(config.site_data_prefix, entry)):
+            dir_list.append([entry,os.path.join("/", entry, "")])
+
+    menu_text = ""
+
+    for entry in dir_list:
+        # Since we highlight (adding class=thisSite) whenever we are inside one
+        # of these menu directories it is necessary to test for the "Home" or
+        # "/" entry separately as every page on the site is inside that
+        # directory.
+        if ((entry[1] == "/" and os.path.dirname(os.environ['REQUEST_URI']) == "/")
+                or (entry[1] != "/" and entry[1] == os.environ['REQUEST_URI'][:len(entry[1])])):
+            menu_text += '<a class="thisSite" href="' + entry[1] + '">' + entry[0] + '</a>\n'
+        else:
+            menu_text += '<a href="' + entry[1] + '">' + entry[0] + '</a>\n'
+
+    return template.replace(keyword,menu_text)
+
+def menu_current_dir(template):
+    """Generate a menu based files in the currently requested directory.
+
+    Files are only included in the menu if they have valid menu_text metadata entries.
+    """
+    keyword = add_delimiter("current_dir_menu")
+
+    menu_text = ""
+
+    current_path = os.path.dirname(os.path.join(config.site_data_prefix,
+            os.environ['REQUEST_URI'][1:]))
+    for entry in os.listdir(current_path):
+        # We exclude files without valid menu_text metadata (i.e. equal to an
+        # empty string) so that we can create pages which do not appear in the menu.
+        #
+        # TODO: Should this implicit behavior be made explicit by addition of a
+        # metadata tag like "visible_in_menu"?
+        if (entry[-3:] == config.markup_file_extension
+                and misc.lookup_file_metadata("menu_text",os.path.join(current_path,entry)) != ""):
+            menu_text += ('<li><a href="' +
+                    os.path.join(os.path.dirname(os.environ['REQUEST_URI']),entry[:-3]) +'">' +
+                    misc.lookup_file_metadata("menu_text",os.path.join(current_path,entry)) +
+                    '</a></li>\n')
+
+    return template.replace(keyword,menu_text)
+
 def head_meta_description(template):
     keyword = add_delimiter("meta_description")
 def head_meta_description(template):
     keyword = add_delimiter("meta_description")
-    return template.replace(keyword,'<meta name="description" content="' + misc.lookup_page_metadata("meta_description") + '">')
+    return template.replace(keyword,'<meta name="description" content="' +
+            misc.lookup_current_metadata("meta_description") + '">')
 
 def head_meta_keywords(template):
     keyword = add_delimiter("meta_keywords")
 
 def head_meta_keywords(template):
     keyword = add_delimiter("meta_keywords")
-    return template.replace(keyword,'<meta name="keywords" content="' + misc.lookup_page_metadata("meta_keywords") + '">')
+    return template.replace(keyword,'<meta name="keywords" content="' +
+            misc.lookup_current_metadata("meta_keywords") + '">')
 
 def page_title(template):
     keyword = add_delimiter("page_title")
 
 def page_title(template):
     keyword = add_delimiter("page_title")
-    return template.replace(keyword, config.site_name + " - " + misc.lookup_page_metadata("page_title"))
+    return template.replace(keyword, config.site_name + " - " +
+            misc.lookup_current_metadata("page_title"))
 
 def site_title(template):
     keyword = add_delimiter("site_title")
 
 def site_title(template):
     keyword = add_delimiter("site_title")