-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsquirrel.py
More file actions
executable file
·60 lines (49 loc) · 2.39 KB
/
squirrel.py
File metadata and controls
executable file
·60 lines (49 loc) · 2.39 KB
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
#!/usr/bin/env python
import os
import argparse
import re
import datetime
import markdown2
import pygments
parser = argparse.ArgumentParser()
parser.add_argument("-m", dest='markdown', help="Markdown file to be converted to html.")
parser.add_argument("-t", dest='template', required=False, help="Template to be used for formatting of markdown file.")
#parser.add_argument("-d", required=False, action='store_true')
args = parser.parse_args()
def convert():
markdownInput = open(args.markdown).read() # Read markdown file into markdownInput
title = markdownInput.split('\n')[:2] # Take the first line and make it the <title>
block = markdownInput.split('\n')[2:] # Strip the title out of the entry
block = '\n'.join(block)
markdownHTML = markdown2.markdown(block.decode('utf8'), extras=['fenced-code-blocks', 'wiki-tables']) # Convert markdown to HTML
if args.template:
title = str(title[0])
template = open('templates/' + args.template).read() # Load template specified by -t flag
else:
title = str(title[0]) + '\n' + str(title[1])
title = markdown2.markdown(title)
template = "{{title}}\n{{block}}"
htmlOutput = ''
for line in template:
htmlOutput += line # Turns converted read template html to a multi line string, helps for regex subs
htmlOutput = re.sub("{{title}}", title, htmlOutput) # Sets <title> if not index
htmlOutput = re.sub("{{datetime}}", datetime.datetime.now().ctime(), htmlOutput) # Insert date and time i.e., Wed Apr 16 17:18:56 2014
htmlOutput = re.sub("{{block}}", markdownHTML, htmlOutput) # Insert converted markdown to location of {{block}} in template
try:
if not os.path.exists('webserver'): # Check foe existence of webserver folder
os.mkdir('webserver')
htmlPath = args.markdown.strip(".md").replace(' ', '-') + '/'
if not os.path.exists('webserver/' + htmlPath): # Check for existence of child folder
os.mkdir('webserver/' + htmlPath)
open('webserver/' + htmlPath + "/index.html", "w").write(htmlOutput.encode('ascii', 'ignore')) # Create html and write it
print "Generated %s" % (args.markdown.strip('.md'))
except IOError:
print """Path could not be found. Please make sure the folder hierarchy matches in
the local 'webserver' folder, and that the 'webserver' folder exists."""
if args.markdown:
if os.path.isfile(str(args.markdown)):
convert()
else:
print "File " + args.markdown + " could not be found."
else:
print parser.parse_args(['-h'])