/
render.py
189 lines (146 loc) · 5.82 KB
/
render.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
"""
Script to render all markdown documents in the repository
Uses the Github Markdown API
"""
import requests
import json
import sys
import os
import logging
import base64
import config.settings as settings
import config.secrets as auth
from config.template_setup import TEMPLATE_ENV
# Configuration
FLAGS = ['-d', '-f']
logging.basicConfig(filename='logs/render.log', level=logging.DEBUG)
def render_markdown(md_file_path):
""" Renders the markdown file with the given filename """
content = None
try:
content = open(md_file_path, 'r').read()
except:
# Some error occurred, log it and return
logging.info('Error occurred rendering markdown file at: ' + md_file_path)
print 'Rendering failed, see logs for details'
return
# Setup and submit request
params = {'text': content, 'mode': 'markdown'}
headers = {'Authorization': 'Basic {:s}'.format(base64.b64encode(auth.AUTH_INFO))}
response = requests.post(settings.GITHUB_API_URL + '/markdown', data=json.dumps(params), headers=headers)
# Change to .html file
md_file_path = md_file_path.replace('.md', '.html')
# Remove source directory from write path
md_file_path = md_file_path.replace(settings.SOURCE_PATH, './')
# Write contents to destination path
write_file = open(os.path.join(settings.RENDER_PATH, md_file_path), 'w')
# TODO: Replace with template specific to notes
template = TEMPLATE_ENV.get_template('note.html')
result = template.render(content=response.text)
write_file.write(result)
# Return path to file created
return md_file_path
def render_all_markdown():
"""
Renders all markdown present in current directory and all subdirectories
Returns a list of paths to the rendered files
"""
print 'Rendering all markdown...'
rendered = []
# Run through and render all .md files in repo
for dirname, subdirs, files in os.walk(settings.SOURCE_PATH):
if any(excluded in dirname for excluded in settings.EXCLUDED):
continue
# Create destination directory, stripping source directory from path
write_path = os.path.join(settings.RENDER_PATH, dirname).replace(settings.SOURCE_PATH, './')
# Make render directory if it doesn't already exist
# NOTE: There's a race condition here. Should be okay though
if not os.path.exists(write_path):
os.makedirs(write_path)
rendered += [render_markdown(os.path.join(dirname, fname)) for fname in files if fname.lower().endswith('.md')]
return rendered
def render_markdown_in_dir(directory):
print 'Rendering markdown in dir: ' + directory
return
def write_out_directory(current_dir, dir_tree, path_to):
"""
Recursively writes out the directory tree
Links to .html files are created, and subdirs written out as lists
within the parent directory list element
"""
content = '<li>'
if '.html' in current_dir:
content += '<a href=\'' + path_to + current_dir + '\'>' + current_dir + '</a>'
else:
directory = current_dir + '/'
content += '<a class=\'open-directory\'>' + directory + '</a>'
sub_files = dir_tree[current_dir]
content += '<div class=\'child_files\'>\n'
content += '<ul>'
for sub_file in sub_files:
content += write_out_directory(sub_file, dir_tree, path_to + directory)
content += '</ul>'
content += '</div>\n'
content += '</li>'
return content
def make_table_of_contents(rendered):
""" Renders the table of contents page """
outfile = open(os.path.join(settings.RENDER_PATH, 'index.html'), 'w')
content = '<h1>Table of Contents</h1>'
# Start recursive writing of directory tree from current directory
content += '<ul id=\'contents-list\'>'
content += write_out_directory('.', rendered, './')
content += '</ul>'
index_template = TEMPLATE_ENV.get_template('index.html');
rendered = index_template.render(content=content)
outfile.write(rendered);
outfile.close()
def sort_by_directories(files_list):
"""
Sorts the given files list based on directories in which they reside
Returns a dict of directories mapped to files/subdirs within them
"""
result = {}
for file_path in files_list:
while '/' in file_path:
# Split path on the first / character
split_path = file_path.split('/', 1)
directory = split_path[0]
file_path = split_path[1]
# Split again to get next piece, either file or subdir
next_file = file_path.split('/', 1)[0]
# Create new set of files to map dir to, or add to existing
result.setdefault(directory, set([])).add(next_file)
return result
def main(arguments):
""" Handles argument parsing """
if len(arguments) > 3:
print 'render.py: Too many arguments'
return
if len(arguments) == 1:
# Called with no additional args, render everything
'render.py: Rendering all markdown files'
rendered = render_all_markdown()
rendered = sort_by_directories(rendered)
make_table_of_contents(rendered)
return
# Otherwise must have been called with specific flags
if arguments[1] not in FLAGS:
print 'render.py: Invalid flag given'
return
if arguments[1] == '-d':
# Render all markdown in given directory
if len(arguments) != 3:
print 'render.py: Directory in which to render markdown not given'
return
render_markdown_in_dir(arguments[2])
return
if arguments[1] == '-f':
# Render specific file
if len(arguments) != 3:
print 'render.py: Path to file to render not given'
return
render_markdown(arguments[2])
return
if __name__ == '__main__':
main(sys.argv)