From 73c5dba96df5b81d7e05ec9a75e81306c709883b Mon Sep 17 00:00:00 2001 From: Aaron Taylor Date: Mon, 8 Jan 2018 19:36:11 -0800 Subject: [PATCH] Added menu functions to template that generate a menu based on the filesystem structure. --- README.md | 3 +- bin/cmless.py | 2 ++ bin/template.py | 57 +++++++++++++++++++++++++++++++ site_sample/data/index.md | 1 + site_sample/template/template.tpl | 7 +++- 5 files changed, 68 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index de8a653..47faa0d 100644 --- 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 ----------------------------------- diff --git a/bin/cmless.py b/bin/cmless.py index 97b944c..f4a5a28 100755 --- a/bin/cmless.py +++ b/bin/cmless.py @@ -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) diff --git a/bin/template.py b/bin/template.py index fc8886f..5950cdf 100644 --- a/bin/template.py +++ b/bin/template.py @@ -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 + 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 += '' + entry[0] + '\n' + else: + menu_text += '' + entry[0] + '\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 += ('
  • ' + + misc.lookup_file_metadata("menu_text",os.path.join(current_path,entry)) + + '
  • \n') + + return template.replace(keyword,menu_text) + def head_meta_description(template): keyword = add_delimiter("meta_description") return template.replace(keyword,' - %%menu%% + %%top_level_menu%%
    +
    %%page_content%%
    -- 2.20.1