-
Notifications
You must be signed in to change notification settings - Fork 2
/
drift.py
176 lines (148 loc) · 5.65 KB
/
drift.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
#!/usr/bin/env python
#-*- coding: utf-8 -*-
"""Stardrifter building document script.
This script builds HTML documents out of the Markdown files and stores them in
the `build` directory.
Usage:
python drift.py build
You can cleanup the `build` directory by using the "clean" command, too.
:license: BSD, see LICENSE for details
:copyright: 2013 by Bruno Bord
"""
import os
import sys
import shutil
import codecs
import datetime
import logging
import argparse
from jinja2 import Template
from markdown import Markdown
SOURCE_PATH = os.path.abspath(os.path.join('.', 'stardrifter'))
BUILD_PATH = os.path.abspath(os.path.join('.', 'build'))
STATIC_PATH = os.path.join(BUILD_PATH, 'static')
VENDOR_PATH = os.path.join(BUILD_PATH, 'vendor')
now = datetime.datetime.now()
class MarkdownReader(object):
"Reader for markdown documents"
file_extensions = ['md', 'markdown', 'mkd']
extensions = ['extra', 'meta', 'tables', 'toc', 'admonition']
def __init__(self, fragment_path):
self.fragments = {}
for filename in os.listdir(fragment_path):
logging.debug("Fragment: %s" % filename)
if filename.endswith('.md'):
self.fragments[filename] = codecs.open(os.path.join(fragment_path, filename), encoding='utf').read()
def _parse_metadata(self, meta):
"""Return the dict containing document metadata"""
md = Markdown(extensions=self.extensions)
output = {}
for name, value in meta.items():
name = name.lower()
if name == "summary":
summary_values = "\n".join(str(item) for item in value)
summary = md.convert(summary_values)
output[name] = summary
else:
output[name] = value[0]
return output
def read(self, source_path):
"""Parse content and metadata of markdown files"""
text = codecs.open(source_path, encoding='utf').read()
for key in self.fragments:
text = text.replace("~~%s~~" % key, self.fragments[key])
md = Markdown(extensions=self.extensions)
content = md.convert(text)
metadata = self._parse_metadata(md.Meta)
return content, metadata
class HTMLWriter(object):
"HTML Writer, builds documentation"
def __init__(self, build_path, template):
self.build_path = build_path
self.template = template
quiet_mkdir(self.build_path)
def write(self, base, data):
"Write content to the destination path"
path_prefix = '..'
quiet_mkdir(os.path.join(self.build_path, base))
if base != 'intro':
destination = os.path.join(os.path.join(self.build_path, base, 'index.html'))
else:
# special case: intro is the root index
path_prefix = '.'
destination = os.path.join(os.path.join(self.build_path, 'index.html'))
data.update({
'path_prefix': path_prefix,
'date': now.strftime("%a, %d %b %Y %H:%M")
})
with codecs.open(destination, 'w', encoding='utf') as fd:
fd.write(self.template.render(data))
def quiet_mkdir(path):
"Make dirs without warning"
try:
os.makedirs(path)
except OSError:
pass
NAVIGATION = (
{'caption': 'Prepare'},
{'url': "quickstart", 'caption': 'Quickstart'},
{'url': 'you', 'caption': 'Your character'},
{'url': 'ship', 'caption': 'Your ship'},
{'caption': 'Play'},
{'url': 'adventure', 'caption': 'Adventure', 'children': (
{'url': 'marketplace', 'caption': 'Marketplace'},
{'url': 'travel', 'caption': 'Space Travel'},
)},
{'url': 'galaxy', 'caption': 'Galaxy Generator'},
{'url': 'advanced', 'caption': 'Advanced play'},
{'url': "credits", 'caption': "Credits"},
)
def build():
"Build the documents"
logging.info('Start building')
reader = MarkdownReader(os.path.join(SOURCE_PATH, 'fragments'))
writer = HTMLWriter(BUILD_PATH, Template(codecs.open('templates/base.html', encoding='utf').read()))
for filename in os.listdir(SOURCE_PATH):
base, ext = os.path.splitext(filename)
if ext == '.md':
source = os.path.join(SOURCE_PATH, filename)
body, metadata = reader.read(source)
metadata.update({
'body': body,
'navigation': NAVIGATION,
'current': base})
logging.info('Writing %s' % base)
writer.write(base, metadata)
# copy the full static files in build
if os.path.exists(STATIC_PATH):
shutil.rmtree(STATIC_PATH)
if os.path.exists(VENDOR_PATH):
shutil.rmtree(VENDOR_PATH)
logging.info('Copying static files')
shutil.copytree('static', STATIC_PATH)
shutil.copytree('vendor', VENDOR_PATH)
logging.info("Done")
def clean():
"Clean build directories. Warning! there's no 'undo'."
if raw_input('Are you sure? [y/N] ').lower() == 'y':
for item in os.listdir(BUILD_PATH):
fullpath = os.path.join(BUILD_PATH, item)
if item.startswith('.'):
continue
if os.path.isdir(fullpath):
shutil.rmtree(fullpath, ignore_errors=True)
else:
os.unlink(fullpath)
if __name__ == '__main__':
parser = argparse.ArgumentParser('Build Stardrifter and other tools')
parser.add_argument('command', choices=['build', 'clean'])
parser.add_argument('--debug', action="store_true")
args = parser.parse_args()
if args.debug:
logging.basicConfig(level=logging.DEBUG)
if args.command == 'build':
build()
elif args.command == 'clean':
clean()
else:
sys.exit('todo')