From 99415564741ca849c0771a3cdd3c18a72b74a373 Mon Sep 17 00:00:00 2001
From: Tom Christie
Date: Sat, 1 Sep 2012 21:23:50 +0100
Subject: Get docs ready to deploy
---
 mkdocs.py | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 69 insertions(+)
 create mode 100755 mkdocs.py
(limited to 'mkdocs.py')
diff --git a/mkdocs.py b/mkdocs.py
new file mode 100755
index 00000000..8a151572
--- /dev/null
+++ b/mkdocs.py
@@ -0,0 +1,69 @@
+#!/usr/bin/env python
+
+import markdown
+import os
+import re
+import shutil
+import sys
+
+root_dir = os.path.dirname(__file__)
+docs_dir = os.path.join(root_dir, 'docs')
+html_dir = os.path.join(root_dir, 'html')
+
+local = not '--deploy' in sys.argv
+
+if local:
+    base_url = 'file://%s/' % os.path.normpath(os.path.join(os.getcwd(), html_dir))
+    suffix = '.html'
+    index = 'index.html'
+else:
+    base_url = 'http://tomchristie.github.com/restframeworkdocs'
+    suffix = ''
+    index = ''
+
+
+main_header = '
{{ title }}'
+sub_header = '{{ title }}'
+
+page = open(os.path.join(docs_dir, 'template.html'), 'r').read()
+
+# Copy static files
+for static in ['css', 'js']:
+    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)
+
+for (dirpath, dirnames, filenames) in os.walk(docs_dir):
+    for filename in filenames:
+        if not filename.endswith('.md'):
+            continue
+
+        toc = ''
+        text = open(os.path.join(dirpath, filename), 'r').read().decode('utf-8')
+        for line in text.splitlines():
+            if line.startswith('# '):
+                title = line[2:].strip()
+                template = main_header
+            elif line.startswith('## '):
+                title = line[3:].strip()
+                template = sub_header
+            else:
+                continue
+
+            anchor = title.lower().replace(' ', '-').replace(':-', '-').replace("'", '').replace('?', '').replace('.', '')
+            template = template.replace('{{ title }}', title)
+            template = template.replace('{{ anchor }}', anchor)
+            toc += template + '\n'
+
+        content = markdown.markdown(text, ['headerid'])
+
+        build_dir = os.path.join(html_dir, dirpath.lstrip(docs_dir))
+        build_file = os.path.join(build_dir, filename[:-3] + '.html')
+
+        if not os.path.exists(build_dir):
+            os.makedirs(build_dir)
+        output = page.replace('{{ content }}', content).replace('{{ toc }}', toc).replace('{{ base_url }}', base_url).replace('{{ suffix }}', suffix).replace('{{ index }}', index)
+        output = re.sub(r'a href="([^"]*)\.md"', r'a href="\1.html"', output)
+        open(build_file, 'w').write(output.encode('utf-8'))
-- 
cgit v1.2.3