Commit | Line | Data |
---|---|---|
d955cfad AT |
1 | # (c) 2017 Aaron Taylor <ataylor at subgeniuskitty dot com> |
2 | # See LICENSE file for copyright and license details. | |
3 | ||
4 | # Python imports | |
990c6f79 | 5 | import os, datetime |
d955cfad AT |
6 | |
7 | # CMless imports | |
8 | import config, misc | |
9 | ||
1debf034 AT |
10 | def add_delimiter(keyword): |
11 | return config.template_delimiter + keyword + config.template_delimiter | |
12 | ||
73c5dba9 AT |
13 | def menu_top_level(template): |
14 | """Generate a menu based on the directories contained in config.site_data_prefix. | |
15 | ||
16 | Only top level directories are included, i.e. no recursion. The root | |
17 | directory '.' is included with the menu name "Home". All other directories | |
18 | use the directory name as the menu name. Each entry is wrapped in an HTML | |
19 | <a> tag for direct use as a menu. | |
20 | """ | |
21 | keyword = add_delimiter("top_level_menu") | |
22 | ||
23 | dir_list = [["Home","/"]] | |
24 | ||
25 | for entry in os.listdir(config.site_data_prefix): | |
26 | if os.path.isdir(os.path.join(config.site_data_prefix, entry)): | |
27 | dir_list.append([entry,os.path.join("/", entry, "")]) | |
28 | ||
29 | menu_text = "" | |
30 | ||
31 | for entry in dir_list: | |
32 | # Since we highlight (adding class=thisSite) whenever we are inside one | |
33 | # of these menu directories it is necessary to test for the "Home" or | |
34 | # "/" entry separately as every page on the site is inside that | |
35 | # directory. | |
36 | if ((entry[1] == "/" and os.path.dirname(os.environ['REQUEST_URI']) == "/") | |
37 | or (entry[1] != "/" and entry[1] == os.environ['REQUEST_URI'][:len(entry[1])])): | |
38 | menu_text += '<a class="thisSite" href="' + entry[1] + '">' + entry[0] + '</a>\n' | |
39 | else: | |
40 | menu_text += '<a href="' + entry[1] + '">' + entry[0] + '</a>\n' | |
41 | ||
42 | return template.replace(keyword,menu_text) | |
43 | ||
44 | def menu_current_dir(template): | |
45 | """Generate a menu based files in the currently requested directory. | |
46 | ||
47 | Files are only included in the menu if they have valid menu_text metadata entries. | |
48 | """ | |
49 | keyword = add_delimiter("current_dir_menu") | |
50 | ||
51 | menu_text = "" | |
52 | ||
53 | current_path = os.path.dirname(os.path.join(config.site_data_prefix, | |
54 | os.environ['REQUEST_URI'][1:])) | |
55 | for entry in os.listdir(current_path): | |
56 | # We exclude files without valid menu_text metadata (i.e. equal to an | |
57 | # empty string) so that we can create pages which do not appear in the menu. | |
58 | # | |
59 | # TODO: Should this implicit behavior be made explicit by addition of a | |
60 | # metadata tag like "visible_in_menu"? | |
61 | if (entry[-3:] == config.markup_file_extension | |
62 | and misc.lookup_file_metadata("menu_text",os.path.join(current_path,entry)) != ""): | |
63 | menu_text += ('<li><a href="' + | |
64 | os.path.join(os.path.dirname(os.environ['REQUEST_URI']),entry[:-3]) +'">' + | |
65 | misc.lookup_file_metadata("menu_text",os.path.join(current_path,entry)) + | |
66 | '</a></li>\n') | |
67 | ||
68 | return template.replace(keyword,menu_text) | |
69 | ||
1debf034 AT |
70 | def head_meta_description(template): |
71 | keyword = add_delimiter("meta_description") | |
78e2ec48 AT |
72 | return template.replace(keyword,'<meta name="description" content="' + |
73 | misc.lookup_current_metadata("meta_description") + '">') | |
1debf034 AT |
74 | |
75 | def head_meta_keywords(template): | |
76 | keyword = add_delimiter("meta_keywords") | |
78e2ec48 AT |
77 | return template.replace(keyword,'<meta name="keywords" content="' + |
78 | misc.lookup_current_metadata("meta_keywords") + '">') | |
1debf034 | 79 | |
0a58f7f1 | 80 | def page_title(template): |
1debf034 | 81 | keyword = add_delimiter("page_title") |
78e2ec48 AT |
82 | return template.replace(keyword, config.site_name + " - " + |
83 | misc.lookup_current_metadata("page_title")) | |
d955cfad | 84 | |
0a58f7f1 | 85 | def site_title(template): |
1debf034 AT |
86 | keyword = add_delimiter("site_title") |
87 | return template.replace(keyword, config.site_name) | |
0a58f7f1 AT |
88 | |
89 | def body(template): | |
1debf034 | 90 | keyword = add_delimiter("page_content") |
fafb94d2 AT |
91 | body = misc.load_file(os.path.join(config.site_data_prefix + |
92 | os.environ['REQUEST_URI'] + config.markup_file_extension)) | |
1debf034 | 93 | body = misc.strip_page_metadata(body) |
d955cfad | 94 | body = misc.process_markup(body) |
1debf034 | 95 | return template.replace(keyword, body) |
990c6f79 AT |
96 | |
97 | def current_year(template): | |
1debf034 | 98 | keyword = add_delimiter("current_year") |
990c6f79 | 99 | now = datetime.datetime.now() |
1debf034 | 100 | return template.replace(keyword, str(now.year)) |