-
Notifications
You must be signed in to change notification settings - Fork 0
/
converter.py
100 lines (88 loc) · 3.67 KB
/
converter.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import os,shutil
import constants
from page import Page
from page import Post
class Converter:
"""
A class that handles walking the filesystem and either converting to HTML
or copying non-convertible files to the output directory.
"""
def __init__(self, config):
"""
Initializes instance with configuration details
"""
self.layoutdir = config['layouts']
self.default = config['default_layout']
self.outdir = os.path.abspath(os.path.expanduser(config['output']))
self.blogdir = config['blog']
self.source = os.path.abspath(os.path.expanduser(config['source']))
self.ignores = config['ignores']
self.permalink = config['permalink']
try:
tsfile = open(config['timestamp'])
self.timestamp = pickle.load(tsfile)
tsfile.close()
except:
self.timestamp = 0
def process(self):
"""
Walks through the filesystem and converts all appropriate files to HTML
"""
for root, dirs, files in os.walk(self.source):
if not self.visitable(root):
continue
self.current_outdir = root.replace(self.source,self.outdir)
try:
os.mkdir(self.current_outdir)
except:
pass
for filename in files:
filepath = os.path.join(root,filename)
modify_time = os.path.getmtime(filepath)
if modify_time > self.timestamp:
if root.endswith(self.blogdir) and self.convertable(filename):
blogdir = self.current_outdir.strip(self.blogdir)
self.make_post(blogdir, filepath)
elif self.convertable(filename):
self.make_page(filepath)
elif self.copyable(filename):
outpath = os.path.join(self.current_outdir,filename)
shutil.copy(filepath, outpath)
def visitable(self, directory):
"""Checks if the given directory should be visited """
for each in self.ignores:
if directory.find(each+'/') != -1 or directory.endswith(each):
return False
if '/.' in directory:
return False
else:
return True
def convertable(self, filename):
""" Checks if the given filename can be converted to HTML"""
return filename.endswith(constants.formats) and self.copyable(filename)
def copyable(self, filename):
"""Checks if the given filename is hidden, a backup or a cyblog ignore """
return filename[-1] != '~' and filename[0] != '.' and filename[0] != '_'
def make_post(self, root, filepath):
"""Creates the HTML version of a blog post and then writes it to disk """
post = Post(filepath, self.layoutdir)
post.make_permalink(self.permalink)
html = post.generate()
postpath = os.path.join(root, post.prefix)
try:
os.makedirs(postpath)
except:
pass
htmlfile = os.path.join(postpath, post.htmlname)
self.write_out(html, htmlfile)
def make_page(self, filepath):
"""Creates the HTML version of a Page and writes it to disk """
page = Page(filepath, self.layoutdir)
html = page.generate()
htmlpath = os.path.join(self.current_outdir,page.htmlname)
self.write_out(html, htmlpath)
def write_out(self, html, htmlpath):
"""Given a chunk of HTML and a filepath, writes the HTML to the file """
htmlfile = open(htmlpath, 'w')
htmlfile.write(html)
htmlfile.close()