-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathminimalsite.py
executable file
·401 lines (341 loc) · 14.4 KB
/
minimalsite.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
#!/usr/bin/env python
"""
A fast minimal static website builder.
Minimalsite generates web pages from a source file hierarchy. It supports
markdown and textile syntax, but plain txt and html can be evenly used.
Template files are python modules, providing huge flexibility and keeping the
codebase tiny and simple.
"""
import os
import re
import sys
import imp
import time
import codecs
import argparse
try:
import markdown
import textile
except ImportError:
pass
__author__ = "Marco Squarcina"
__email__ = "lavish@gmail.com"
__license__ = "MIT"
__version__ = "1.00"
# class definitions
class Page:
"""
Meta informations of a page.
Attributes:
src_pathname pathname of the source file
dst_pathname pathname of the generated file
src_file file name of the source file
dst_file file name of the generated file
name file name of the generated file without extension
level depth level in the site hierarchy
last_edit time.struct_time date of source file last edit
"""
def __init__(self, src_pathname, level):
self.src_pathname = src_pathname
self.dst_pathname = self._get_dst_pathname()
self.src_file = os.path.basename(self.src_pathname)
self.dst_file = os.path.basename(self.dst_pathname)
self.name = self._get_name()
self.level = level
self.last_edit = time.localtime(os.path.getmtime(self.src_pathname))
def _get_dst_pathname(self):
"""Get destination pathname from source pathname."""
# replace extension
dst_pathname = os.path.splitext(self.src_pathname)
if dst_pathname[1]:
dst_pathname = os.path.join(dst_pathname[0] + \
'.' + template.DST_EXT)
dst_pathname = ''.join(dst_pathname)
# change destination dir
dst_pathname = '/'.join(template.DST.split('/')
+ dst_pathname.split('/')[len(template.SRC.split('/')):])
# remove index numbers for dirs and files
return re.sub('\/\d+_', '/', dst_pathname)
def _get_name(self):
"""Get page name from destination pathname."""
name = os.path.basename(self.dst_pathname)
name = os.path.splitext(name)[0]
return name.replace('_', ' ')
def __str__(self):
"""Return a textual representation of the page."""
data = "<{}, {}, {}, {}, {}, {}>"
return data.format(self.src_pathname, self.dst_pathname,
self.src_file, self.dst_file, self.name, self.level)
class TreeNode:
"""
Node of the site hierarchy tree structure.
Attributes:
page Page object
parent parent node, None if current node is the root
children list of children
"""
def __init__(self, page, parent = None):
self.page = page
self.parent = parent
self.children = []
def build(self):
"""Create the site tree, representing the source file hierarchy."""
# check if src dir includes an index file
if self.page.level == 0 \
and not has_index(template.SRC):
die('Directory {} does not include a valid index file, aborting.'\
.format(template.SRC), 2)
for file_name in os.listdir(self.page.src_pathname):
pathname = os.path.join(self.page.src_pathname, file_name)
# do not add nodes for links and files starting with '.'
if os.path.islink(pathname) or file_name[0] == ".":
continue
# add nodes for files with an allowed extension
elif os.path.isfile(pathname) and syntax(pathname):
node = TreeNode(Page(pathname, self.page.level + 1), self)
self.children.append(node)
# add nodes for directories and go on building the tree
elif os.path.isdir(pathname) and has_index(pathname):
node = TreeNode(Page(pathname, self.page.level + 1), self)
self.children.append(node)
node.build()
def write(self, margin = ''):
"""Write the generated site on the file system."""
# a directory
if self.children:
# create the destination dir, if possible
try:
os.makedirs(self.page.dst_pathname)
except OSError:
pass
else:
print(margin + self.page.dst_pathname)
# recursively call write_tree against current node
for child in self.children:
child.write(margin + ' ')
# a file
else:
print(margin + self.page.dst_pathname)
self._write_page()
def title(self):
"""Return the title for the current node."""
if self.page.name == 'index':
page_name = self.parent.page.name
else:
page_name = self.page.name
return template.SITE_NAME + ' | ' + page_name
def menu(self):
"""Return the generated code for menu."""
menu_code = '<ul>\n'
# build the 'parent page' menu entry
entry = '\t<li><a href="{}">↵</a></li>\n'
if self.page.name != 'index':
menu_code += entry.format("index." + template.DST_EXT)
elif self.page.level > 1:
menu_code += entry.format("../index." + template.DST_EXT)
# build other entries
for sibling in sorted(self.parent.children,
key=lambda sibling: sibling.page.src_pathname):
# and index page or a hidden file, no need to include them
if sibling.page.dst_file.startswith("index.") \
or sibling.page.src_file in template.HIDDEN:
continue
# a page
elif not sibling.children:
menu_code += '\t<li><a href="{}"'.format(sibling.page.dst_file)
# current page
if self == sibling:
menu_code += ' class="current"'
menu_code += '>{}</a></li>\n'.format(sibling.page.name)
# a directory
else:
menu_code += '\t<li><a href="{}/index.{}">{}</a></li>\n' \
.format(sibling.page.dst_file, template.DST_EXT, \
sibling.page.name)
menu_code += "</ul>"
return menu_code
def path(self):
"""Return the generated code for breadcrumb navigation path."""
path = ""
path_node = []
tmp_node = self
while tmp_node:
path_node.insert(0, tmp_node)
tmp_node = tmp_node.parent
# no need to display "index" in the path
if path_node[-1].page.name == "index":
path_node.pop()
for i in range(len(path_node)):
# last item, it could be current page or current dir
if i == len(path_node) - 1:
path += path_node[i].page.name
# a parent page
else:
traversal = "../" * \
(self.page.level - path_node[i].page.level - 1)
path += '<a href="{}index.{}">{}</a> {} ' \
.format(traversal, template.DST_EXT,
path_node[i].page.name, template.PATH_SEPARATOR)
return path
def write_sitemap(self):
"""Write an XML sitemap to the file system."""
try:
file_desc = open(template.SITEMAP, 'w')
except IOError:
die("Unable to open {} for writing.".format(template.SITEMAP))
file_desc.write('{}\n{}\n'.format(
'<?xml version="1.0" encoding="UTF-8"?>',
'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'))
self._get_sitemap_entries(file_desc, template.URL + template.PREFIX)
file_desc.write('</urlset>')
file_desc.close()
def _get_sitemap_entries(self, file_desc, prefix):
"""Recursively write the XML entries for sitemap."""
if self.children:
for child in self.children:
if not child.page.src_file in template.HIDDEN:
if self.page.level:
# pylint: disable=W0212
child._get_sitemap_entries(file_desc,
prefix + self.page.dst_file + '/')
else:
# pylint: disable=W0212
child._get_sitemap_entries(file_desc, prefix)
else:
file_desc.write(" <url>\n <loc>{}</loc>\n <lastmod>{}</lastmod>\n </url>\n" \
.format(prefix + self.page.dst_file,
time.strftime("%Y-%m-%d", self.page.last_edit)))
def _write_page(self):
"""Write a single page on the file system."""
# open source file
h_src_pathname = codecs.open(self.page.src_pathname, "r", "utf-8")
src_content = h_src_pathname.read()
h_src_pathname.close()
# build page
dst_content = template.header(self)
if syntax(self.page.src_pathname) == "markdown" \
and "markdown" in template.SRC_EXT:
dst_content += markdown.markdown(src_content)
elif syntax(self.page.src_pathname) == "textile" \
and "textile" in template.SRC_EXT:
dst_content += textile.textile(src_content)
elif syntax(self.page.src_pathname) == "plain" \
and "plain" in template.SRC_EXT:
dst_content += src_content
dst_content += template.footer(self)
dst_content = dst_content.replace("%%%TITLE%%%", self.title())
dst_content = dst_content.replace("%%%PATH%%%", self.path())
dst_content = dst_content.replace("%%%MENU%%%", self.menu())
dst_content = dst_content.replace("%%%VERSION%%%", __version__)
# write destination file
h_dst_pathname = codecs.open(self.page.dst_pathname, "w", "utf-8")
h_dst_pathname.write(dst_content)
h_dst_pathname.close()
def __str__(self):
"""Return the entire tree structure."""
tree = " " * self.page.level + str(self.page) + "\n"
for child in self.children:
tree += str(child)
return tree
# function definitions
def syntax(pathname):
"""Return the markup language used in the given pathname."""
for lang in list(template.SRC_EXT.keys()):
if template.SRC_EXT[lang] == pathname.split('.')[-1]:
return lang
return None
def has_index(pathname):
"""Check if there's an index file in the given directory pathname."""
for lang in list(template.SRC_EXT.keys()):
if os.path.isfile(pathname + "/index." + template.SRC_EXT[lang]):
return True
return False
def import_template(pathname):
"""Load the python module in the provided file name as a template."""
if not os.path.isfile(pathname):
die("Template file does not exist. Aborting")
(path, name) = os.path.split(pathname)
(name, ext) = os.path.splitext(name)
if ext == '.py' and name.endswith('_template'):
(file_desc, pathname, data) = imp.find_module(name, [path])
return imp.load_module(name, file_desc, pathname, data)
else:
die("Invalid template file name. Valid templates must terminate with '_template.py'.")
def check_template():
"""Check mandatory variable/function definitions in the provided template."""
template_data = dir(template)
required_data = ['DST', 'DST_EXT', 'HIDDEN', 'HOME', 'PATH_SEPARATOR', \
'PREFIX', 'SITEMAP', 'SITE_NAME', 'SRC', 'SRC_EXT', 'URL', \
'footer', 'header']
for data in required_data:
if not data in template_data:
die("Missing {} definition in template file. Aborting" \
.format(data))
def notice(msg):
"""Write a notice message to stdout."""
print("[*] {}".format(msg))
def die(msg, code=1):
"""Write an error message to stderr and exit."""
sys.stderr.write("[!] {}\n".format(msg))
sys.exit(code)
def main():
"""Main method."""
global template
parser = argparse.ArgumentParser(description = \
'Fast minimal static website builder')
parser.add_argument('-t', '--template', type=str, required=True, \
help="specify a template file name. Valid templates must terminate with '_template.py'")
parser.add_argument('-V', '--verbose', action='store_true', \
default=False, help='verbosely display site structure')
parser.add_argument('-s', '--src', type=str, default=None, \
help='source dir, where the textual hierarchy resides')
parser.add_argument('-d', '--dst', type=str, default=None, \
help='destination dir, where the html pages will be written')
parser.add_argument('-m', '--sitemap', type=str, default=None, \
help='full path name for the XML sitemap')
parser.add_argument('-v', '--version', action='version', \
version='%(prog)s-'+__version__)
args = parser.parse_args()
# load template
template = import_template(args.template)
# check template integrity
check_template()
# check markup modules
for lang in ('markdown', 'textile'):
if not lang in sys.modules:
del template.SRC_EXT[lang]
if args.verbose:
print("[!] Disabling {} support, module not found".format(lang))
if not template.SRC_EXT:
die("No modules for parsing files found. See README for requirements.")
# check src and dst directories
if args.src:
template.SRC = args.src
if args.dst:
template.DST = args.dst
# assign sitemap pathname
if args.sitemap:
template.SITEMAP = args.sitemap
# fix trailing slashes
template.SRC = os.path.abspath(template.SRC)
template.DST = os.path.abspath(template.DST)
for directory in [template.SRC, template.DST]:
if not os.path.isdir(directory):
die('Directory {} does not exist, aborting.'.format(directory), 2)
# start writing pages
notice('Processing files from {}'.format(template.SRC))
root = TreeNode(Page(template.SRC, 0))
root.page.name = template.HOME
root.build()
notice('Writing {} files into {}'.format(template.DST_EXT, template.DST))
root.write()
# write sitemap
if template.SITEMAP:
notice('Writing sitemap to {}'.format(template.SITEMAP))
root.write_sitemap()
if args.verbose:
notice('Printing site structure')
notice('values as: <src_pathname, dst_pathname, src_file, dst_file, name, level>')
print(root)
if __name__ == "__main__":
main()