django-rest-framework/mkdocs.py

185 lines
6.2 KiB
Python
Raw Normal View History

2012-09-01 23:26:27 +04:00
#!/usr/bin/env python
import markdown
import os
import re
2012-09-02 00:23:50 +04:00
import shutil
import sys
2012-09-01 23:26:27 +04:00
root_dir = os.path.abspath(os.path.dirname(__file__))
2012-09-02 00:23:50 +04:00
docs_dir = os.path.join(root_dir, 'docs')
html_dir = os.path.join(root_dir, 'html')
local = not '--deploy' in sys.argv
preview = '-p' in sys.argv
2012-09-01 23:26:27 +04:00
if local:
2012-09-02 00:23:50 +04:00
base_url = 'file://%s/' % os.path.normpath(os.path.join(os.getcwd(), html_dir))
2012-09-01 23:26:27 +04:00
suffix = '.html'
index = 'index.html'
else:
base_url = 'http://django-rest-framework.org'
2012-10-30 18:48:21 +04:00
suffix = '.html'
2012-09-01 23:26:27 +04:00
index = ''
main_header = '<li class="main"><a href="#{{ anchor }}">{{ title }}</a></li>'
sub_header = '<li><a href="#{{ anchor }}">{{ title }}</a></li>'
code_label = r'<a class="github" href="https://github.com/tomchristie/django-rest-framework/tree/master/rest_framework/\1"><span class="label label-info">\1</span></a>'
2012-09-01 23:26:27 +04:00
2012-09-02 00:23:50 +04:00
page = open(os.path.join(docs_dir, 'template.html'), 'r').read()
# Copy static files
2012-10-09 17:12:38 +04:00
# for static in ['css', 'js', 'img']:
# source = os.path.join(docs_dir, 'static', static)
# target = os.path.join(html_dir, static)
# if os.path.exists(target):
# shutil.rmtree(target)
# shutil.copytree(source, target)
2012-09-01 23:26:27 +04:00
2013-03-28 19:58:53 +04:00
# Hacky, but what the hell, it'll do the job
path_list = [
'index.md',
'tutorial/quickstart.md',
'tutorial/1-serialization.md',
'tutorial/2-requests-and-responses.md',
'tutorial/3-class-based-views.md',
'tutorial/4-authentication-and-permissions.md',
'tutorial/5-relationships-and-hyperlinked-apis.md',
2013-03-31 14:36:58 +04:00
'tutorial/6-viewsets-and-routers.md',
2013-03-28 19:58:53 +04:00
'api-guide/requests.md',
'api-guide/responses.md',
'api-guide/views.md',
'api-guide/generic-views.md',
2013-04-09 14:54:51 +04:00
'api-guide/viewsets.md',
'api-guide/routers.md',
2013-03-28 19:58:53 +04:00
'api-guide/parsers.md',
'api-guide/renderers.md',
'api-guide/serializers.md',
'api-guide/fields.md',
'api-guide/relations.md',
'api-guide/authentication.md',
'api-guide/permissions.md',
'api-guide/throttling.md',
'api-guide/filtering.md',
'api-guide/pagination.md',
'api-guide/content-negotiation.md',
'api-guide/format-suffixes.md',
'api-guide/reverse.md',
'api-guide/exceptions.md',
'api-guide/status-codes.md',
'api-guide/settings.md',
'topics/ajax-csrf-cors.md',
'topics/browser-enhancements.md',
'topics/browsable-api.md',
'topics/rest-hypermedia-hateoas.md',
'topics/contributing.md',
'topics/rest-framework-2-announcement.md',
'topics/2.2-announcement.md',
2013-04-25 23:43:37 +04:00
'topics/2.3-announcement.md',
2013-03-28 19:58:53 +04:00
'topics/release-notes.md',
'topics/credits.md',
]
prev_url_map = {}
next_url_map = {}
for idx in range(len(path_list)):
path = path_list[idx]
rel = '../' * path.count('/')
if idx > 0:
prev_url_map[path] = rel + path_list[idx - 1][:-3] + suffix
if idx < len(path_list) - 1:
next_url_map[path] = rel + path_list[idx + 1][:-3] + suffix
2012-09-02 00:23:50 +04:00
for (dirpath, dirnames, filenames) in os.walk(docs_dir):
relative_dir = dirpath.replace(docs_dir, '').lstrip(os.path.sep)
build_dir = os.path.join(html_dir, relative_dir)
2012-10-09 17:12:38 +04:00
if not os.path.exists(build_dir):
os.makedirs(build_dir)
2012-09-01 23:26:27 +04:00
for filename in filenames:
path = os.path.join(dirpath, filename)
2013-03-28 19:58:53 +04:00
relative_path = os.path.join(relative_dir, filename)
2012-10-09 17:12:38 +04:00
if not filename.endswith('.md'):
if relative_dir:
output_path = os.path.join(build_dir, filename)
shutil.copy(path, output_path)
2012-09-01 23:26:27 +04:00
continue
output_path = os.path.join(build_dir, filename[:-3] + '.html')
2012-09-01 23:26:27 +04:00
toc = ''
text = open(path, 'r').read().decode('utf-8')
2013-03-15 17:41:22 +04:00
main_title = None
description = 'Django, API, REST'
2012-09-01 23:26:27 +04:00
for line in text.splitlines():
if line.startswith('# '):
title = line[2:].strip()
template = main_header
2013-03-15 17:41:22 +04:00
description = description + ', ' + title
2012-09-01 23:26:27 +04:00
elif line.startswith('## '):
title = line[3:].strip()
template = sub_header
else:
continue
2013-03-15 17:41:22 +04:00
if not main_title:
main_title = title
2012-09-01 23:26:27 +04:00
anchor = title.lower().replace(' ', '-').replace(':-', '-').replace("'", '').replace('?', '').replace('.', '')
template = template.replace('{{ title }}', title)
template = template.replace('{{ anchor }}', anchor)
toc += template + '\n'
2013-03-15 17:41:22 +04:00
if filename == 'index.md':
2013-03-28 19:58:53 +04:00
main_title = 'Django REST framework - Web Browseable APIs'
2013-03-15 17:41:22 +04:00
else:
main_title = 'Django REST framework - ' + main_title
2013-03-28 19:58:53 +04:00
prev_url = prev_url_map.get(relative_path)
next_url = next_url_map.get(relative_path)
2012-09-01 23:26:27 +04:00
content = markdown.markdown(text, ['headerid'])
output = page.replace('{{ content }}', content).replace('{{ toc }}', toc).replace('{{ base_url }}', base_url).replace('{{ suffix }}', suffix).replace('{{ index }}', index)
2013-03-15 17:41:22 +04:00
output = output.replace('{{ title }}', main_title)
output = output.replace('{{ description }}', description)
output = output.replace('{{ page_id }}', filename[:-3])
2013-03-28 19:58:53 +04:00
if prev_url:
output = output.replace('{{ prev_url }}', prev_url)
output = output.replace('{{ prev_url_disabled }}', '')
else:
output = output.replace('{{ prev_url }}', '#')
output = output.replace('{{ prev_url_disabled }}', 'disabled')
if next_url:
output = output.replace('{{ next_url }}', next_url)
output = output.replace('{{ next_url_disabled }}', '')
else:
output = output.replace('{{ next_url }}', '#')
output = output.replace('{{ next_url_disabled }}', 'disabled')
2012-09-05 21:25:30 +04:00
output = re.sub(r'a href="([^"]*)\.md"', r'a href="\1%s"' % suffix, output)
2012-09-08 23:23:32 +04:00
output = re.sub(r'<pre><code>:::bash', r'<pre class="prettyprint lang-bsh">', output)
output = re.sub(r'<pre>', r'<pre class="prettyprint lang-py">', output)
2012-09-09 01:06:13 +04:00
output = re.sub(r'<a class="github" href="([^"]*)"></a>', code_label, output)
open(output_path, 'w').write(output.encode('utf-8'))
if preview:
import subprocess
url = 'html/index.html'
try:
subprocess.Popen(["open", url]) # Mac
except OSError:
subprocess.Popen(["xdg-open", url]) # Linux
except:
os.startfile(url) # Windows