Added os.path.join() to a few filesystem related string operations.
[cmless] / bin / template.py
... / ...
CommitLineData
1# (c) 2017 Aaron Taylor <ataylor at subgeniuskitty dot com>
2# See LICENSE file for copyright and license details.
3
4# Python imports
5import os, datetime
6
7# CMless imports
8import config, misc
9
10def add_delimiter(keyword):
11 return config.template_delimiter + keyword + config.template_delimiter
12
13def 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
44def 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
70def head_meta_description(template):
71 keyword = add_delimiter("meta_description")
72 return template.replace(keyword,'<meta name="description" content="' +
73 misc.lookup_current_metadata("meta_description") + '">')
74
75def head_meta_keywords(template):
76 keyword = add_delimiter("meta_keywords")
77 return template.replace(keyword,'<meta name="keywords" content="' +
78 misc.lookup_current_metadata("meta_keywords") + '">')
79
80def page_title(template):
81 keyword = add_delimiter("page_title")
82 return template.replace(keyword, config.site_name + " - " +
83 misc.lookup_current_metadata("page_title"))
84
85def site_title(template):
86 keyword = add_delimiter("site_title")
87 return template.replace(keyword, config.site_name)
88
89def body(template):
90 keyword = add_delimiter("page_content")
91 body = misc.load_file(os.path.join(config.site_data_prefix +
92 os.environ['REQUEST_URI'] + config.markup_file_extension))
93 body = misc.strip_page_metadata(body)
94 body = misc.process_markup(body)
95 return template.replace(keyword, body)
96
97def current_year(template):
98 keyword = add_delimiter("current_year")
99 now = datetime.datetime.now()
100 return template.replace(keyword, str(now.year))