| 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 |
import sys |
| 39 |
import re |
| 40 |
import datetime |
| 41 |
import getopt |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
def outdent(line, n): |
| 47 |
if len(line) <= n: |
| 48 |
return "\n" |
| 49 |
else: |
| 50 |
return line[n:] |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
def matchBeginTitle(line): |
| 57 |
return re.search(r'^\s*//#{3,}\s*([^#]+)#{3,}', line) |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
def matchEndTitle(line): |
| 64 |
return re.search(r'^\s*((//#{3,})|(\s*))$', line) |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
def matchBeginSection(line): |
| 71 |
return re.search(r'^\s*//={3,}\s*([^=]+)={3,}', line) |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
def matchEndSection(line): |
| 78 |
return re.search(r'^\s*((//={3,})|(\s*))$', line) |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
def matchBeginComment(line): |
| 85 |
return re.search(r'^\s*//-{3,}\s*([^-]+)-{3,}', line) |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
def matchEndComment(line): |
| 92 |
return re.search(r'^\s*((//-{3,})|(\s*))$', line) |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
def indentation(line): |
| 99 |
matchComment = re.search(r'(^\s*//\s*\w)', line) |
| 100 |
if matchComment: |
| 101 |
return len(matchComment.group(1))-1 |
| 102 |
else: |
| 103 |
return 0 |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
def isComment(line): |
| 108 |
matchComment = re.search(r'^\s*//', line) |
| 109 |
if matchComment: |
| 110 |
return 1 |
| 111 |
else: |
| 112 |
return 0 |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
def frontMatter(file): |
| 117 |
print('---') |
| 118 |
print('file: %s' % (file,)) |
| 119 |
print('date: %s' % (datetime.date.today(),)) |
| 120 |
print('---') |
| 121 |
print('') |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
tabsize = 4 |
| 128 |
codeflag = 0 |
| 129 |
frontflag = 0 |
| 130 |
mode = 0 |
| 131 |
idt = 0 |
| 132 |
|
| 133 |
|
| 134 |
try: |
| 135 |
opts, args = getopt.getopt(sys.argv[1:], "t:cf") |
| 136 |
if not args: |
| 137 |
raise getopt.error("At least one file argument required") |
| 138 |
except getopt.error as e: |
| 139 |
print(e.msg) |
| 140 |
print("usage: %s [-t tabsize] [-c] [-f] file ..." % (sys.argv[0],)) |
| 141 |
sys.exit(1) |
| 142 |
for optname, optvalue in opts: |
| 143 |
if optname == '-t': |
| 144 |
tabsize = int(optvalue) |
| 145 |
if optname == '-c': |
| 146 |
codeflag = 1 |
| 147 |
if optname == '-f': |
| 148 |
frontflag = 1 |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
for file in args: |
| 153 |
with open(file) as f: |
| 154 |
if frontflag: |
| 155 |
frontMatter(file) |
| 156 |
for text in f: |
| 157 |
line = text.expandtabs(tabsize) |
| 158 |
if isComment(line) == 0: |
| 159 |
if mode == 1: |
| 160 |
|
| 161 |
print('') |
| 162 |
mode = 0 |
| 163 |
if codeflag: |
| 164 |
print('\t%s' % line,) |
| 165 |
else: |
| 166 |
if mode == 0: |
| 167 |
matchComment = matchBeginComment(line) |
| 168 |
matchSection = matchBeginSection(line) |
| 169 |
matchTitle = matchBeginTitle(line) |
| 170 |
if matchComment: |
| 171 |
print('') |
| 172 |
print("### %s" % (matchComment.group(1),)) |
| 173 |
elif matchSection: |
| 174 |
print('') |
| 175 |
print("## %s" % (matchSection.group(1),)) |
| 176 |
elif matchTitle: |
| 177 |
print('') |
| 178 |
print("# %s" % (matchTitle.group(1),)) |
| 179 |
if matchComment or matchSection or matchTitle: |
| 180 |
mode = 1 |
| 181 |
idt = 0 |
| 182 |
else: |
| 183 |
|
| 184 |
|
| 185 |
if codeflag: |
| 186 |
print('\t%s' % (line,)) |
| 187 |
else: |
| 188 |
|
| 189 |
if idt == 0: |
| 190 |
|
| 191 |
idt = indentation(line) |
| 192 |
|
| 193 |
matchComment = matchEndComment(line) |
| 194 |
matchSection = matchEndSection(line) |
| 195 |
matchTitle = matchEndTitle(line) |
| 196 |
if matchComment: |
| 197 |
print('') |
| 198 |
print("---") |
| 199 |
print('') |
| 200 |
if matchComment or matchSection or matchTitle: |
| 201 |
|
| 202 |
mode = 0 |
| 203 |
else: |
| 204 |
|
| 205 |
|
| 206 |
print(outdent(line, idt)), |