/
index.py
45 lines (35 loc) · 1.51 KB
/
index.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
from mako.template import Template
from mako.lookup import TemplateLookup
import util
import os
class BlogIndex:
def __init__(self):
self.root = os.path.abspath(os.path.dirname(__file__))
self.entries = []
self._load()
myLookup = TemplateLookup(directories=['.'],
output_encoding='utf-8', encoding_errors='replace')
self.template = Template(filename=os.path.join("design", 'index.html'), lookup=myLookup)
self._walk()
self._render()
def _load(self):
config_filename = "config.ini"
self.config = util.parse_config(config_filename)
#Set up logging
self.logger = util.logger(self.config['loglevel'], self.config['logfile'])
self.logger.info("Loaded config")
def _render(self):
# Send date, permalink, filepath, title
with open("index.html", 'w') as outfh:
outfh.write(self.template.render(entries=self.entries,
select_theme=self.config['theme']))
self.logger.info("Done")
def _walk(self):
# Walk through /drafts to check for published posts and add its context
listing = os.listdir('drafts')
for infile in listing:
with open(os.path.join('drafts', infile), 'r') as f:
raw_header, raw_content = f.read().split('---')
header = util.parse_header(raw_header)
if util.isdraft(header['publish']):
self.entries.append(header)