-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminimalsite.py
executable file
·281 lines (247 loc) · 8.55 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
#!/usr/bin/python
# Author: Marco Squarcina <[email protected]>
# License: MIT, see LICENSE for details
import getopt, os, sys
import string, codecs, re
try:
import markdown
except ImportError:
sys.stderr.write("Module markdown not found.\n")
try:
import textile
except ImportError:
sys.stderr.write("Module textile not found.\n")
# class definition
class Node(object):
def __init__(self, src_pathname, level, parent = None):
self.src_pathname = src_pathname
self.dst_pathname = get_dst_pathname(src_pathname)
self.src_file = os.path.basename(self.src_pathname)
self.dst_file = os.path.basename(self.dst_pathname)
self.name = get_name(self.dst_pathname)
self.level = level
self.parent = parent
self.children = []
def __str__(self):
s = '<%s:%s, ' % ('src_pathname', self.src_pathname)
s += '%s:%s, ' % ('dst_pathname', self.dst_pathname)
s += '%s:%s, ' % ('src_file', self.src_file)
s += '%s:%s, ' % ('dst_file', self.dst_file)
s += '%s:%s, ' % ('name', self.name)
s += '%s:%d>\n' % ('level', self.level)
return s
def add_child(self, obj):
self.children.append(obj)
# functions definition
def syntax(pathname):
"""Returns 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 ''
def hasindex(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 get_dst_pathname(src_pathname):
"""Get destionation pathname from source pathname."""
# replace extension
dst_pathname = os.path.splitext(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_dir.split('/')
+ dst_pathname.split('/')[len(template.src_dir.split('/')):])
# remove index numbers for dirs and files
return re.sub('\/\d+_', '/', dst_pathname)
def get_name(dst_pathname):
"""Get page name from destionation pathname."""
name = os.path.basename(dst_pathname)
name = os.path.splitext(name)[0]
return name.replace('_', ' ')
def menu(node):
"""Given a node, returns a multine string of the menu code."""
menu_code = "<ul>\n"
for sibling in sorted(node.parent.children, key=lambda sibling: sibling.src_pathname):
# and index page or a hidden file, no need to include them
if sibling.dst_file.startswith("index.") or sibling.src_file in template.hidden:
continue
# a page
elif not sibling.children:
menu_code += '\t<li><a href='
menu_code += '"' + sibling.dst_file + '"'
# current page
if node == sibling:
menu_code += ' class="current"'
menu_code += '>' + sibling.name + '</a></li>\n'
# a directory
else:
menu_code += '\t<li><a href="'
menu_code += sibling.dst_file
menu_code += '/index.' + template.dst_ext + '">'
menu_code += sibling.name + '</a></li>\n'
menu_code += "</ul>"
return menu_code
def path(node):
"""Given a node, returns a string of the breadcrumb navigation path code."""
path = ""
path_node = []
tmp_node = node
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].name == "index":
path_node.pop()
for i in range(0, len(path_node)):
# last item, it could be current page or current dir
if i == len(path_node) - 1:
path += path_node[i].name
# a parent page
else:
path += '<a href="'
path += "../" * (node.level - path_node[i].level - 1)
path += "index." + template.dst_ext + '">'
path += path_node[i].name
path += '</a> ' + template.path_separator + ' '
return path
def write_page(node):
"""Given a node, write on the file system its corresponding page."""
# open source file
h_src_pathname = codecs.open(node.src_pathname, "r", "utf-8");
src_content = h_src_pathname.read()
h_src_pathname.close()
# build page
dst_content = template.header(node)
if(syntax(node.src_pathname) == "markdown"
and "markdown" in template.src_ext):
dst_content += markdown.markdown(src_content)
elif(syntax(node.src_pathname) == "textile"
and "textile" in template.src_ext):
dst_content += textile.textile(src_content)
dst_content += template.footer(node)
dst_content = dst_content.replace("%%%PATH%%%", path(node))
dst_content = dst_content.replace("%%%MENU%%%", menu(node))
# write destionation file
h_dst_pathname = codecs.open(node.dst_pathname, "w", "utf-8")
h_dst_pathname.write(dst_content)
h_dst_pathname.close()
def print_tree(node, margin = ''):
"""Given a node, display the entire tree structure from that node."""
if node:
sys.stderr.write(margin + str(node))
for child in node.children:
print_tree(child, margin + ' ')
def build_tree(node):
"""Given a node, recursively create a tree representing the sources file
hierarchy starting from that node."""
for file in os.listdir(node.src_pathname):
pathname = os.path.join(node.src_pathname, file)
# do not add nodes for links and files starting with '.'
if os.path.islink(pathname) or file[0] == ".":
continue
# add nodes for files with an allowed extension
elif os.path.isfile(pathname) and syntax(pathname):
node.add_child(Node(pathname, node.level + 1, node))
# add nodes for directories and go on building the tree
elif os.path.isdir(pathname) and hasindex(pathname):
node.add_child(Node(pathname, node.level + 1, node))
# -1 used to specify the last added node
build_tree(node.children[-1])
def write_tree(node, margin = ''):
"""Given a node, recursively write all the corresponding pages on the file
system."""
# a directory
if node.children:
# create the destination dir, if possible
try:
os.makedirs(node.dst_pathname)
except OSError:
pass
else:
print(margin + "creating -> " + node.dst_pathname)
# recursivly call write_tree against current node
for child in node.children:
write_tree(child, margin + ' ')
# a file
else:
print(margin + "writing -> " + node.dst_pathname)
write_page(node)
def main():
global template
# set default arguments
src_dir = dst_dir = None
template_module = 'templates.default'
verbose = False
# parse options
try:
opts, args = getopt.getopt(sys.argv[1:],
"ht:s:d:Vv",
["help", "template=", "src_dir=", "dst_dir=", "version", "verbose"])
except getopt.GetoptError as err:
sys.stderr.write('Incorrect usage, see -h for help\n')
sys.exit(1)
for o, a in opts:
if o in ("-V", "--version"):
print('minimalsite-0.8 by Marco Squarcina, see LICENSE for details')
sys.exit(0)
elif o in ("-h", "--help"):
print('''Usage: minimalsite.py [options]
Options:
-h, --help Show help options
-V, --version Display minimalsite version
-v, --verbose Display the entire tree structure
-t, --template=TEMPLATE Specify a template
-s, --src_dir=SOURCE_DIR Specify source dir to use
-d, --dst_dir=DESTINATION_DIR Specify destionation dir to use''')
sys.exit(0)
elif o in ("-t", "--template"):
template_module = 'templates.' + a
elif o in ("-s", "--src_dir"):
src_dir = a
elif o in ("-d", "--dst_dir"):
dst_dir = a
elif o in ("-v", "--verbose"):
verbose = True
else:
assert False, "unhandled option"
# load template
__import__(template_module)
template = sys.modules[template_module]
# check markup modules
for markup in ('markdown', 'textile'):
if not markup in sys.modules:
del template.src_ext[markup]
if not template.src_ext:
sys.stderr.write("No modules for parsing files found. See README for requirements\n")
sys.exit(3)
# check src and dst directories
if src_dir:
template.src_dir = os.path.abspath(src_dir)
if dst_dir:
template.dst_dir = os.path.abspath(dst_dir)
if not os.path.isdir(template.src_dir):
sys.stderr.write('"' + template.src_dir + '" is not a directory, aborting\n')
sys.exit(2)
if not os.path.isdir(template.dst_dir):
sys.stderr.write('"' + template.dst_dir + '" is not a directory, aborting\n')
sys.exit(2)
# check if src dir includes an index file
# if not hasindex(template.src_dir):
# sys.stderr.write('"' + template.src_dir + '" does not include a valid index file, aborting\n')
# sys.exit(2)
# start writing pages
print('Processing files in "' + template.src_dir + '":\n')
root = Node(template.src_dir, 0)
root.name = template.home
build_tree(root)
write_tree(root)
print('\n... Done!')
if verbose:
sys.stderr.write("\nSite structure:\n\n")
print_tree(root)
if __name__ == "__main__":
main()