forked from mkdocs/mkdocs
/
utils.py
347 lines (262 loc) · 9.36 KB
/
utils.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
# coding: utf-8
"""
Standalone file utils.
Nothing in this module should have an knowledge of config or the layout
and structure of the site and pages in the site.
"""
import os
import shutil
import markdown
import six
from mkdocs import toc
def reduce_list(data_set):
""" Reduce duplicate items in a list and preserve order """
seen = set()
return [item for item in data_set if item not in seen and not seen.add(item)]
def copy_file(source_path, output_path):
"""
Copy source_path to output_path, making sure any parent directories exist.
"""
output_dir = os.path.dirname(output_path)
if not os.path.exists(output_dir):
os.makedirs(output_dir)
shutil.copy(source_path, output_path)
def write_file(content, output_path):
"""
Write content to output_path, making sure any parent directories exist.
"""
output_dir = os.path.dirname(output_path)
if not os.path.exists(output_dir):
os.makedirs(output_dir)
open(output_path, 'wb').write(content)
def clean_directory(directory):
"""
Remove the content of a directory recursively but not the directory itself.
"""
if not os.path.exists(directory):
return
for entry in os.listdir(directory):
# Don't remove hidden files from the directory. We never copy files
# that are hidden, so we shouldn't delete them either.
if entry.startswith('.'):
continue
path = os.path.join(directory, entry)
if os.path.isdir(path):
shutil.rmtree(path, True)
else:
os.unlink(path)
def copy_media_files(from_dir, to_dir):
"""
Recursively copy all files except markdown and HTML into another directory.
"""
for (source_dir, dirnames, filenames) in os.walk(from_dir):
relative_path = os.path.relpath(source_dir, from_dir)
output_dir = os.path.normpath(os.path.join(to_dir, relative_path))
# Filter filenames starting with a '.'
filenames = [f for f in filenames if not f.startswith('.')]
# Filter the dirnames that start with a '.' and update the list in
# place to prevent us walking these.
dirnames[:] = [d for d in dirnames if not d.startswith('.')]
for filename in filenames:
if not is_markdown_file(filename) and not is_html_file(filename):
source_path = os.path.join(source_dir, filename)
output_path = os.path.join(output_dir, filename)
copy_file(source_path, output_path)
def get_html_path(path):
"""
Map a source file path to an output html path.
Paths like 'index.md' will be converted to 'index.html'
Paths like 'about.md' will be converted to 'about/index.html'
Paths like 'api-guide/core.md' will be converted to 'api-guide/core/index.html'
"""
path = os.path.splitext(path)[0]
if os.path.basename(path) == 'index':
return path + '.html'
return "/".join((path, 'index.html'))
def get_url_path(path, use_directory_urls=True):
"""
Map a source file path to an output html path.
Paths like 'index.md' will be converted to '/'
Paths like 'about.md' will be converted to '/about/'
Paths like 'api-guide/core.md' will be converted to '/api-guide/core/'
If `use_directory_urls` is `False`, returned URLs will include the a trailing
`index.html` rather than just returning the directory path.
"""
path = get_html_path(path)
url = '/' + path.replace(os.path.sep, '/')
if use_directory_urls:
return url[:-len('index.html')]
return url
def is_homepage(path):
return os.path.splitext(path)[0] == 'index'
def is_markdown_file(path):
"""
Return True if the given file path is a Markdown file.
http://superuser.com/questions/249436/file-extension-for-markdown-files
"""
ext = os.path.splitext(path)[1].lower()
return ext in [
'.markdown',
'.mdown',
'.mkdn',
'.mkd',
'.md',
]
def is_css_file(path):
"""
Return True if the given file path is a CSS file.
"""
ext = os.path.splitext(path)[1].lower()
return ext in [
'.css',
]
def is_javascript_file(path):
"""
Return True if the given file path is a Javascript file.
"""
ext = os.path.splitext(path)[1].lower()
return ext in [
'.js',
'.javascript'
]
def is_html_file(path):
"""
Return True if the given file path is an HTML file.
"""
ext = os.path.splitext(path)[1].lower()
return ext in [
'.html',
'.htm',
]
def is_template_file(path):
"""
Return True if the given file path is an HTML file.
"""
ext = os.path.splitext(path)[1].lower()
return ext in [
'.html',
'.htm',
'.xml',
]
def create_media_urls(nav, path_list):
"""
Return a list of URLs that have been processed correctly for inclusion in
a page.
"""
final_urls = []
for path in path_list:
# Allow links to fully qualified URL's
parsed = six.moves.urllib.parse.urlparse(path)
if parsed.netloc:
final_urls.append(path)
continue
# We must be looking at a local path.
url = path_to_url(path)
relative_url = '%s/%s' % (nav.url_context.make_relative('/'), url)
final_urls.append(relative_url)
return final_urls
def create_relative_media_url(nav, url):
"""
For a current page, create a relative url based on the given URL.
On index.md (which becomes /index.html):
image.png -> ./image.png
/image.png -> ./image.png
On sub/page.md (which becomes /sub/page/index.html):
image.png -> ../image.png
/image.png -> ../../image.png
On sub/index.md (which becomes /sub/index.html):
image.png -> ./image.png
/image.png -> ./image.png
"""
# Allow links to fully qualified URL's
parsed = six.moves.urllib.parse.urlparse(url)
if parsed.netloc:
return url
# If the URL we are looking at starts with a /, then it should be
# considered as absolute and will be 'relative' to the root.
if url.startswith('/'):
base = '/'
url = url[1:]
else:
base = nav.url_context.base_path
relative_base = nav.url_context.make_relative(base)
if relative_base == "." and url.startswith("./"):
relative_url = url
else:
relative_url = '%s/%s' % (relative_base, url)
# TODO: Fix this, this is a hack. Relative urls are not being calculated
# correctly for images in the same directory as the markdown. I think this
# is due to us moving it into a directory with index.html, but I'm not sure
if (not nav.file_context.current_file.endswith("/index.md")
and nav.url_context.base_path is not '/'
and relative_url.startswith("./")):
relative_url = ".%s" % relative_url
return relative_url
def path_to_url(path):
"""Convert a system path to a URL."""
if os.path.sep == '/':
return path
return six.moves.urllib.request.pathname2url(path)
def convert_markdown(markdown_source, extensions=None, extension_configs=None):
"""
Convert the Markdown source file to HTML content, and additionally
return the parsed table of contents, and a dictionary of any metadata
that was specified in the Markdown file.
`extensions` is an optional sequence of Python Markdown extensions to add
to the default set.
"""
md = markdown.Markdown(
extensions=extensions or [],
extension_configs=extension_configs or {}
)
html_content = md.convert(markdown_source)
# On completely blank markdown files, no Meta or tox properties are added
# to the generated document.
meta = getattr(md, 'Meta', {})
toc_html = getattr(md, 'toc', '')
# Post process the generated table of contents into a data structure
table_of_contents = toc.TableOfContents(toc_html)
return (html_content, table_of_contents, meta)
def get_theme_names():
"""Return a list containing all the names of all the builtin themes."""
return os.listdir(os.path.join(os.path.dirname(__file__), 'themes'))
def filename_to_title(filename):
title = os.path.splitext(filename)[0]
title = title.replace('-', ' ').replace('_', ' ')
# Capitalize if the filename was all lowercase, otherwise leave it as-is.
if title.lower() == title:
title = title.capitalize()
return title
def find_or_create_node(branch, key):
"""
Given a list, look for dictionary with a key matching key and return it's
value. If it doesn't exist, create it with the value of an empty list and
return that.
"""
for node in branch:
if not isinstance(node, dict):
continue
if key in node:
return node[key]
new_branch = []
node = {key: new_branch}
branch.append(node)
return new_branch
def nest_paths(paths):
"""
Given a list of paths, convert them into a nested structure that will match
the pages config.
"""
nested = []
for path in paths:
if os.path.sep not in path:
nested.append(path)
continue
directory, _ = os.path.split(path)
parts = directory.split(os.path.sep)
branch = nested
for part in parts:
part = filename_to_title(part)
branch = find_or_create_node(branch, part)
branch.append(path)
return nested