Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100755 116 lines (89 sloc) 4.249 kB
07cb8fd @rowyn Draft version of script to create Hyde templates for wiki markdown fi…
rowyn authored
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 import glob
5 import optparse
6 import os
7 import re
3225757 @rowyn Adding image copying to the markdown_to_hyde script.
rowyn authored
8 import shutil
07cb8fd @rowyn Draft version of script to create Hyde templates for wiki markdown fi…
rowyn authored
9 import string
10
11 HYDE_RE = re.compile(r'({%hyde.+?%})(.+)', re.DOTALL)
12
13 TEMPLATE = string.Template("""{% extends "_post.html" %}
14
15 $hyde_tag
16
17 {% block article %}
18
19 {% include 'patterns/$filename' %}
20
21 {% endblock %}""")
22
23
24 def HtmlFilename(basename):
25 return basename.split('.')[0] + '.html'
26
27
28 def ProcessMarkdown(filename):
29 """Takes the name of a markdown file and returns a tuple consisting of two
30 strings, the initial Hyde tag and the remaining contents of the file."""
31 f = open(filename, 'r')
3829b15 @rowyn ACTUALLY making the updates to markdown_to_hyde and the patterns and …
rowyn authored
32 contents = f.read().strip()
3a7de15 @rowyn Adding images and updated copies of patterns.
rowyn authored
33 contents = string.replace(contents, 'media/images/', '/media/images/')
07cb8fd @rowyn Draft version of script to create Hyde templates for wiki markdown fi…
rowyn authored
34 result = HYDE_RE.match(contents)
35 if result and result.groups():
36 return (result.group(1), result.group(2).strip())
37 else:
38 print "No hyde tag found in file: %s" % filename
39 return (None, None)
40
41
42 def ProcessFiles(source_dir, dest_dir):
05322c8 @jlzych Amended markdown_to_hyde.py to copy principles directory into site/co…
jlzych authored
43 patterns_source = os.path.join(source_dir, 'patterns')
44 patterns_dest = os.path.join(dest_dir, 'patterns')
45
46 for filename in glob.glob(os.path.join(patterns_source, '*.md')):
07cb8fd @rowyn Draft version of script to create Hyde templates for wiki markdown fi…
rowyn authored
47 basename = os.path.basename(filename)
48 hyde_tag, contents = ProcessMarkdown(filename)
49 if hyde_tag and contents:
05322c8 @jlzych Amended markdown_to_hyde.py to copy principles directory into site/co…
jlzych authored
50 hyde_file = open(os.path.join(patterns_dest, HtmlFilename(basename)), 'w')
07cb8fd @rowyn Draft version of script to create Hyde templates for wiki markdown fi…
rowyn authored
51 hyde_file.write(
52 TEMPLATE.safe_substitute(hyde_tag=hyde_tag, filename=basename))
53 hyde_file.close()
54
05322c8 @jlzych Amended markdown_to_hyde.py to copy principles directory into site/co…
jlzych authored
55 md_file = open(os.path.join(patterns_dest, basename), 'w')
07cb8fd @rowyn Draft version of script to create Hyde templates for wiki markdown fi…
rowyn authored
56 md_file.write(contents)
57 md_file.close()
58 print "Successfully processed file: %s" % filename
05322c8 @jlzych Amended markdown_to_hyde.py to copy principles directory into site/co…
jlzych authored
59
60 # Process principles
61 principles_source = os.path.join(source_dir, 'principles')
62 principles_temp_dest = os.path.join(dest_dir, 'principles.new')
63 principles_dest = os.path.join(dest_dir, 'principles')
64 try:
65 # Copy princples to new directory, then remove old one and rename new one
66 shutil.copytree(principles_source, principles_temp_dest)
67 shutil.rmtree(principles_dest)
68 os.rename(principles_temp_dest, principles_dest)
69 except:
70 print "Could not copy principles:", sys.exc_info()[0]
71 else:
72 print "Successfully copied %s to %s" % (principles_source, principles_dest)
07cb8fd @rowyn Draft version of script to create Hyde templates for wiki markdown fi…
rowyn authored
73
3225757 @rowyn Adding image copying to the markdown_to_hyde script.
rowyn authored
74 def CopyImages(source_dir, dest_dir):
75 # patterns and media dirs are siblings in privacypatterns.wiki
76 source_dir = os.path.dirname(source_dir) + "/media/images"
77 # from site/content/patterns, go up two directories to find media dir
78 dest_dir = os.path.dirname(os.path.dirname(dest_dir)) + "/media/images"
79 for filename in glob.glob(os.path.join(source_dir, "*.*")):
80 basename = os.path.basename(filename)
81 shutil.copyfile(filename, os.path.join(dest_dir, basename))
82 print "Successfully copied image: %s" % filename
83
07cb8fd @rowyn Draft version of script to create Hyde templates for wiki markdown fi…
rowyn authored
84
85 def main():
86 parser = optparse.OptionParser(usage="%prog [-f] [-q]", version="%prog 0.5.3")
87 parser.add_option("-s", "--source",
05322c8 @jlzych Amended markdown_to_hyde.py to copy principles directory into site/co…
jlzych authored
88 default = "../privacypatterns.wiki",
07cb8fd @rowyn Draft version of script to create Hyde templates for wiki markdown fi…
rowyn authored
89 dest = "source_dir",
05322c8 @jlzych Amended markdown_to_hyde.py to copy principles directory into site/co…
jlzych authored
90 help = "Path of the source wiki directory, where the patterns and principles live.")
07cb8fd @rowyn Draft version of script to create Hyde templates for wiki markdown fi…
rowyn authored
91 parser.add_option("-d", "--destination",
05322c8 @jlzych Amended markdown_to_hyde.py to copy principles directory into site/co…
jlzych authored
92 default = "site/content",
07cb8fd @rowyn Draft version of script to create Hyde templates for wiki markdown fi…
rowyn authored
93 dest = "dest_dir",
05322c8 @jlzych Amended markdown_to_hyde.py to copy principles directory into site/co…
jlzych authored
94 help = "Path of the site content directory, into which patterns and principles will be copied.")
3225757 @rowyn Adding image copying to the markdown_to_hyde script.
rowyn authored
95 parser.add_option("-i", "--copy_images",
96 default = True,
97 dest = "copy_images",
98 help = "Whether or not to copy images. Relative paths to image directories only work for default options.")
07cb8fd @rowyn Draft version of script to create Hyde templates for wiki markdown fi…
rowyn authored
99
100 (options, args) = parser.parse_args()
101
102 if len(args):
103 parser.error("Unexpected arguments encountered.")
104
d936b77 @rowyn Added defaults for source/dest folders and deleted unused var.
rowyn authored
105 options.source_dir = os.path.abspath(options.source_dir)
106 options.dest_dir = os.path.abspath(options.dest_dir)
07cb8fd @rowyn Draft version of script to create Hyde templates for wiki markdown fi…
rowyn authored
107
108 ProcessFiles(options.source_dir, options.dest_dir)
3225757 @rowyn Adding image copying to the markdown_to_hyde script.
rowyn authored
109
110 if options.copy_images:
111 CopyImages(options.source_dir, options.dest_dir)
07cb8fd @rowyn Draft version of script to create Hyde templates for wiki markdown fi…
rowyn authored
112
113
114 if __name__ == "__main__":
115 main()
Something went wrong with that request. Please try again.