-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgd
executable file
·107 lines (85 loc) · 2.79 KB
/
gd
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
#!/usr/bin/python
import sys
import getopt
import re
def check_indent(str):
i = 0 # default case -- empty string
for i,c in enumerate(str):
if c != ' ': break
return i, str[i:]
def reindent(level, str):
return " " * level + str
def help(self):
print "Usage: " + self + " [options] file"
print " Options:"
print " -o file, --output file send output to file. default is stdout"
print " -h, --help print this help message"
print " if file is '-' " + self + " will read from stdin."
def load(self, argv):
try:
opts, args = getopt.getopt(argv, "o:h", ["output", "help"])
except getopt.GetoptError as err:
print err
sys.exit(1)
outfile = sys.stdout
for opt, arg in opts:
if opt in ("-o", "--output"):
outfile = open(arg, "w")
if opt in ("-h", "--help"):
help(self)
sys.exit(0)
if len(args) < 1:
print "More args!"
sys.exit(1)
elif len(args) > 1:
print "No! Too much arg!"
sys.exit(1)
else:
if args[0] == "-":
infile = sys.stdin
else:
try:
infile = open(args[0], "r")
except IOError:
print "Not a real file. > <"
sys.exit(2)
return (infile, outfile)
def main():
infile, outfile = load(sys.argv[0], sys.argv[1:])
# main loop
markdown = ""
for line in infile:
line = line[:-1] # strip newl
# handle indent
(level, line) = check_indent(line)
# skip blanks, but keep the spacing
if line == "":
markdown += "\n"
continue
# headers
# georgeheaders are denoted by a terminal colon. replace this with
# an initial octothorpe
if line[-1] == ":":
line = "#" + "#" * level + " " + line[0:-1]
# bullets
# georgebullets are indented two extra spaces. these should be purged
if line[0] == "-":
line = reindent(level - 2, line)
# italics
# replace /word/ with *word*
# points of note: only cases where each / is borded on exact
line = re.sub(r"(^|[\s])/\b([^/]+)\b/([\s]|$)", r"\1*\2*\3", line)
# autolink email
line = re.sub(r"\b([A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4})\b",
r"[\1](mailto:\1)", line, 0, re.IGNORECASE)
# autolink links
link1 = r"\b[A-Z0-9]+\.[A-Z0-9]+\.[A-Z0-9]+(\.[A-Z0-9]+)?\b"
link2 = r"\b(http|ftp|irc)://[^ ]+\b"
line = re.sub("(" + link1 + "|" + link2 + ")",
r"[\1](\1)", line, 0, re.IGNORECASE)
# links
line += "\n" # reinsert newl
markdown += line
outfile.write(markdown)
if __name__ == "__main__":
main()