Added menu functions to template that generate a menu based on the
authorAaron Taylor <ataylor@subgeniuskitty.com>
Tue, 9 Jan 2018 03:36:11 +0000 (19:36 -0800)
committerAaron Taylor <ataylor@subgeniuskitty.com>
Tue, 9 Jan 2018 03:42:29 +0000 (19:42 -0800)
filesystem structure.

README.md
bin/cmless.py
bin/template.py
site_sample/data/index.md
site_sample/template/template.tpl

index de8a653..47faa0d 100644 (file)
--- a/README.md
+++ b/README.md
@@ -16,7 +16,8 @@ CMless is not yet functional. The stages required to reach basic functionality a
 2. ~~Interpret URL and locate/serve appropriate page~~
 3. ~~Feed pages through a document processor~~
 4. ~~Feed pages through a template engine~~
-5. Generate menu based on directory structure
+5. ~~Generate menu based on directory structure~~
+6. Write documentation, including a sample site.
 
 Installation Example - Debian Linux
 -----------------------------------
index 97b944c..f4a5a28 100755 (executable)
@@ -32,6 +32,8 @@ def main():
     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)
 
index fc8886f..5950cdf 100644 (file)
@@ -10,6 +10,63 @@ import config, misc
 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")
     return template.replace(keyword,'<meta name="description" content="' +
index ed61039..df56c70 100644 (file)
@@ -3,6 +3,7 @@
 page_title = Markdown Reference
 meta_keywords = Words,that,describe,the,page
 meta_description = A description of the page.
+menu_text = Markdown Reference
 %%END_PAGE_METADATA%%
 
 Markdown Test
index d42acb4..5f06626 100644 (file)
     </div>
     <div id="menu">
         <span class="left">
-            %%menu%%
+            %%top_level_menu%%
         </span>
     </div>
     <div id="content">
+        <div id="nav">
+            <ul>
+            %%current_dir_menu%%
+            </ul>
+        </div>
         <div id="main">
             %%page_content%%
         </div>