-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomment-book.py
69 lines (50 loc) · 2.44 KB
/
comment-book.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
#!/usr/bin/python
# http://rubular.com/ is helpful for the regexes.
# to bring the markdown into indesign, download pandoc from https://github.com/jgm/pandoc/releases/
# then run:
# pandoc -s -f markdown -t icml -o my-book.icml my-book.md
# the .icml file can be imported and styled by indesign.
import os
import re
import sys
output = open("my-book.md", "w")
def find_comments_in_file(file_path):
global output
try:
buffer = open(file_path).read()
file_path_no_ext, file_ext = os.path.splitext(file_path)
block_comments = slash_comments = angle_comments = smarty_comments = quote_comments = pound_comments = None
if file_ext == ".js" or file_ext == ".json":
block_comments = re.findall("(\/\*.*?\*\/)", buffer, re.DOTALL)
slash_comments = re.findall("(?<!http:)\/\/.*\n", buffer)
elif file_ext == ".css":
block_comments = re.findall("(\/\*.*?\*\/)", buffer, re.DOTALL)
elif file_ext == ".html":
angle_comments = re.findall("(\<!--.*?--\>)", buffer, re.DOTALL)
smarty_comments = re.findall("({\*.*?\*})", buffer, re.DOTALL)
elif file_ext == ".py":
pound_comments = re.findall("(#.*\n)", buffer)
quote_comments = re.findall("('''.*?''')", buffer, re.DOTALL)
elif file_ext == ".php":
pound_comments = re.findall("(#.*\n)", buffer)
block_comments = re.findall("(\/\*.*?\*\/)", buffer, re.DOTALL)
slash_comments = re.findall("(?<!http:)\/\/.*\n", buffer)
else:
pound_comments = re.findall("(#.*\n)", buffer)
for comment_grp in [pound_comments,block_comments,slash_comments,smarty_comments,quote_comments,angle_comments]:
if comment_grp:
output.write("## `%s`\n" % file_path.replace('../',''))
for comment in comment_grp:
if not comment.endswith("\n"):
comment = "%s\n" % comment
output.write("```\n%s```\n\n" % comment)
buffer = None
except Exception, e:
print e
pass
for root, dirs, files in os.walk("./"):
for file in files:
file_path = os.path.join(root, file)
# add any folders or file extensions you want to ignore here
if re.search("(\.md)|(git)|(\.eot)|(\.woff)|(\.svg)|(\.ttf)|(\.jpg)|(\.gif)|(\.png)", file_path) is None:
find_comments_in_file(file_path)