-
Notifications
You must be signed in to change notification settings - Fork 0
/
webtastic.py
executable file
·306 lines (255 loc) · 8.75 KB
/
webtastic.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#!/usr/bin/python
import jinja2
from jinja2.exceptions import TemplateNotFound
import markdown
import os
import datetime
import re
import argparse
import sys
from yapsy.PluginManager import PluginManagerSingleton
from yapsy.VersionedPluginManager import VersionedPluginManager
import logging
import shutil, errno
import yaml
from yaml import Loader, SafeLoader
def construct_yaml_str(self, node):
# Override the default string handling function
# to always return unicode objects
return self.construct_scalar(node)
Loader.add_constructor(u'tag:yaml.org,2002:str', construct_yaml_str)
SafeLoader.add_constructor(u'tag:yaml.org,2002:str', construct_yaml_str)
def utf8 (func):
def inner(*args, **kwargs):
output = func(*args, **kwargs)
if isinstance(output, str):
if sys.version_info.major < 3:
output = output.decode('utf8')
else:
output = output
return output
return inner
TEMPLATE_OPTIONS = {}
BASE_URL = ""
class Webtastic(object):
""" Class doc """
def __init__ (self):
""" Class initialiser """
# Get singleton instance
PluginManagerSingleton.setBehaviour([
VersionedPluginManager,
])
manager = PluginManagerSingleton.get()
# A referrence for plugins to access the app
manager.app = self
# Set environment
self.env = jinja2.Environment(loader=jinja2.FileSystemLoader("./template/"),
**TEMPLATE_OPTIONS)
# Set ArgParser
self.parser = argparse.ArgumentParser(description='Simple Static Web Generator')
self.parser.add_argument('--cache', dest='cache', action='store_true')
# A referrence for plugins to access the env object
# manager.env = self.env
# Set plugin's directories path
manager.setPluginPlaces(['plugins'])
# Locates and Loads the plugins
manager.collectPlugins()
def write_file (self, path, content):
""" Function doc """
f = open(path, 'w')
# TODO: return by Exceptions
try:
f.write(content)
except UnicodeEncodeError:
f.write(content.encode("utf8"))
f.close()
return True
def run (self):
""" Function doc """
# Activating all the plugins
manager = PluginManagerSingleton.get()
for plugin in manager.getAllPlugins():
plugin.plugin_object.activate()
# Get Arguments
self.args = vars(self.parser.parse_args())
print (self.args)
# OUTPUT and
base_url = self.args['base_url']
OUTPUT_DIR = os.path.join(os.path.abspath('html'), base_url)
TEMPLATE_DIR = os.path.abspath('template')
# Create SourceTree object from source directory
self.sources = WebtasticSourceTree('source')
# remove OUTPUT directory
# os.system("rm -r html/")
# Create OUTPUT directory
os.system("mkdir -p %s" % (OUTPUT_DIR))
# Copy TEMPLATE's content directories/files into OUTPUT directory
# TODO: exclude partials and layout
os.system("rsync --update -a %s/* %s" % (TEMPLATE_DIR, OUTPUT_DIR))
# Create Directory for every subdirectory exists in source directory
for directory in self.sources.directories('source/'):
directory = re.sub(r"^([^/]+)", OUTPUT_DIR, directory)
os.system("mkdir -p %s" % directory)
# Copy every not_source files to relative location at OUTPUT directory
for rel_file in self.sources.rel_file_paths('source/'):
src = rel_file
dst = re.sub(r"^([^/]+)", OUTPUT_DIR, rel_file)
# TODO: use rsync instead of cp
os.system("cp %s %s" % (src, dst))
# read every source file and compile it into OUTPUT directory
for src_file in self.sources.src_files():
print ('html: ' + src_file.path)
self.src_file = src_file
# TODO: add output variable
output_file = os.path.join('html/', self.src_file.link[1:])
if self.args['cache'] and os.path.exists(output_file):
if max(int(os.path.getctime(src_file.path)), int(os.path.getmtime(src_file.path))) <= int(os.path.getmtime(output_file)):
# print 'cached', src_file.path
continue
# print 'html' + src_file.link
# TODO: plugin's BEFORE_LOAD_TEMPLATE
# TODO: set default layout
try:
# check if layout exists
# FIXME: there should be a better way to do this you Silly.
template = self.env.get_template(self.src_file.layout)
except TemplateNotFound as e:
template = self.env.get_template('%s.html' % self.src_file.layout)
# TODO: plugin's AFTER_LOAD_TEMPLATE
# TODO: plugin's BEFORE_TEMPLATE_RENDER
output_content = template.render(page=self.src_file, source=self.sources)
output_content = re.sub("$BASE_URL", "html/", output_content)
# TODO: plugin's AFTER_TEMPLATE_RENDER
# TODO: plugin's BEFORE_WRITE_OUTPUT
self.write_file(output_file, output_content)
# TODO: plugin's AFTER_WRITE_OUTPUT
# Deactivating the plugins
for plugin in manager.getAllPlugins():
plugin.plugin_object.deactivate()
class WebtasticSourceTree(object):
""" Class doc """
# path = None
tree = {}
hash_tree = {}
__files__ = []
def __init__ (self, path):
""" Class initialiser """
# TODO: ValueError check
# FIXME: is self needed?
self.path = path
for dirname, dirnames, filenames in os.walk(self.path):
for filename in filenames:
filepath = os.path.join(dirname, filename)
self.__files__.append(filepath)
def file_paths (self, path='', recursive=True):
""" Function doc """
filtered = []
for f in self.__files__:
if not recursive:
regex_search = "(%s[^/]+$)" % path
else:
regex_search = "(%s.*)" % path
matched = re.search(regex_search, f)
if matched:
filtered.append(matched.group())
return filtered
def src_file_paths (self, path='', recursive=True, meta=False):
""" Function doc """
filtered = []
for f in self.file_paths(path, recursive):
# print f, meta, re.search("^_.*.md$", f), re.search("^([^_].*.md)$", f)
filename = os.path.split(f)[1]
if meta and re.search(".md$", filename):
filtered.append(f)
elif not meta and re.search("^([^_].+.md)$", filename):
filtered.append(f)
# pass
return filtered
def rel_file_paths (self, path='', recursive=True):
""" Function doc """
filtered = []
for f in self.file_paths(path, recursive):
if not re.search(".md$", f):
filtered.append(f)
return filtered
def directories (self, path='', recursive=True):
""" Function doc """
filtered = []
for f in self.file_paths(path, recursive):
directory = re.sub(r"([^/])+$", "", f)
if directory not in filtered:
filtered.append(directory)
return filtered
def src_files (self, path='', recursive=True, meta=False):
""" Function doc """
res = []
for file_path in self.src_file_paths(path, recursive, meta):
f = WebtasticSourceFile(file_path)
res.append(f)
return res
class WebtasticSourceFile(object):
""" Class doc """
__attributes__ = None
__content__ = None
def __init__ (self, path):
""" Class initialiser """
# TODO: add ERROR handlers for bad path
self.path = path
f = open(path)
self.raw_data =f.read()
f.close()
try:
# FIXME: possible only yaml content is broken
raw_attr, raw_content = self.raw_data.split("-"*10)[1:]
self.__attributes__ = yaml.load(raw_attr)
self.__content__ = raw_content
except Exception as e:
# TODO: Raise an Exception
print ("ERROR HAPPENDED ", e.message)
pass
def __getattr__ (self, key):
""" Function doc """
return self.__attributes__.get(key, None)
@property
@utf8
def content (self):
""" Function doc """
if self.__content__:
return self.__content__
@property
def basename (self):
""" Function doc """
return os.path.split(self.path)[1][:-3]
@property
def filename (self):
""" Function doc """
return os.path.split(self.path)[1]
@property
def name (self):
""" Function doc """
return os.path.split(self.path)[1]
@property
def link (self):
""" Function doc """
# remove `source` from the path
if self.output is not None:
return self.output
else:
root_directory = self.path.split("/")[0]
return self.path.replace(root_directory, '').replace('.md', '.html')
@property
def ctime (self):
""" Function doc """
return int(os.path.getctime(self.path))
@property
def mtime (self):
""" Function doc """
return int(os.path.getmtime(self.path))
@property
def atime (self):
""" Function doc """
return int(os.path.getatime(self.path))
if __name__ == '__main__':
logging.basicConfig(level=logging.DEBUG)
app = Webtastic()
app.run()