forked from privacypatterns/privacypatterns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
markdown_to_hyde.py
executable file
·115 lines (89 loc) · 4.15 KB
/
markdown_to_hyde.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import glob
import optparse
import os
import re
import shutil
import string
HYDE_RE = re.compile(r'({%hyde.+?%})(.+)', re.DOTALL)
TEMPLATE = string.Template("""{% extends "_post.html" %}
$hyde_tag
{% block article %}
{% include 'patterns/$filename' %}
{% endblock %}""")
def HtmlFilename(basename):
return basename.split('.')[0] + '.html'
def ProcessMarkdown(filename):
"""Takes the name of a markdown file and returns a tuple consisting of two
strings, the initial Hyde tag and the remaining contents of the file."""
f = open(filename, 'r')
contents = f.read().strip()
contents = string.replace(contents, 'media/images/', '/media/images/')
result = HYDE_RE.match(contents)
if result and result.groups():
return (result.group(1), result.group(2).strip())
else:
print "No hyde tag found in file: %s" % filename
return (None, None)
def ProcessFiles(source_dir, dest_dir):
patterns_source = os.path.join(source_dir, 'patterns')
patterns_dest = os.path.join(dest_dir, 'patterns')
for filename in glob.glob(os.path.join(patterns_source, '*.md')):
basename = os.path.basename(filename)
hyde_tag, contents = ProcessMarkdown(filename)
if hyde_tag and contents:
hyde_file = open(os.path.join(patterns_dest, HtmlFilename(basename)), 'w')
hyde_file.write(
TEMPLATE.safe_substitute(hyde_tag=hyde_tag, filename=basename))
hyde_file.close()
md_file = open(os.path.join(patterns_dest, basename), 'w')
md_file.write(contents)
md_file.close()
print "Successfully processed file: %s" % filename
# Process principles
principles_source = os.path.join(source_dir, 'principles')
principles_temp_dest = os.path.join(dest_dir, 'principles.new')
principles_dest = os.path.join(dest_dir, 'principles')
try:
# Copy princples to new directory, then remove old one and rename new one
shutil.copytree(principles_source, principles_temp_dest)
shutil.rmtree(principles_dest)
os.rename(principles_temp_dest, principles_dest)
except:
print "Could not copy principles:", sys.exc_info()[0]
else:
print "Successfully copied %s to %s" % (principles_source, principles_dest)
def CopyImages(source_dir, dest_dir):
# patterns and media dirs are siblings in privacypatterns.wiki
source_dir = os.path.dirname(source_dir) + "/media/images"
# from site/content/patterns, go up two directories to find media dir
dest_dir = os.path.dirname(os.path.dirname(dest_dir)) + "/media/images"
for filename in glob.glob(os.path.join(source_dir, "*.*")):
basename = os.path.basename(filename)
shutil.copyfile(filename, os.path.join(dest_dir, basename))
print "Successfully copied image: %s" % filename
def main():
parser = optparse.OptionParser(usage="%prog [-f] [-q]", version="%prog 0.5.3")
parser.add_option("-s", "--source",
default = "../privacypatterns.wiki",
dest = "source_dir",
help = "Path of the source wiki directory, where the patterns and principles live.")
parser.add_option("-d", "--destination",
default = "site/content",
dest = "dest_dir",
help = "Path of the site content directory, into which patterns and principles will be copied.")
parser.add_option("-i", "--copy_images",
default = True,
dest = "copy_images",
help = "Whether or not to copy images. Relative paths to image directories only work for default options.")
(options, args) = parser.parse_args()
if len(args):
parser.error("Unexpected arguments encountered.")
options.source_dir = os.path.abspath(options.source_dir)
options.dest_dir = os.path.abspath(options.dest_dir)
ProcessFiles(options.source_dir, options.dest_dir)
if options.copy_images:
CopyImages(options.source_dir, options.dest_dir)
if __name__ == "__main__":
main()