/
criticParser_CLI.py
executable file
·395 lines (290 loc) · 9.46 KB
/
criticParser_CLI.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
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
#!/usr/bin/env python
import codecs
import sys
import os
import re
import argparse
import subprocess
# Input is quoted UNIX type file path.
# Output is HTML formated text using ins, del, and aside tags
# File is written to the same directory as the source unless specified with the -o flag
# -m2 Uses the markdown2 module
# -o <file_path> Writes file to specified path. Must include file name
#
# -b Opens the output HTML file in the defualt browser
add_pattern = r'''(?s)\{\+\+(?P<value>.*?)\+\+[ \t]*(\[(?P<meta>.*?)\])?[ \t]*\}'''
del_pattern = r'''(?s)\{\-\-(?P<value>.*?)\-\-[ \t]*(\[(?P<meta>.*?)\])?[ \t]*\}'''
comm_pattern = r'''(?s)\{\>\>(?P<value>.*?)\<\<\}'''
gen_comm_pattern = r'''(?s)\{[ \t]*\[(?P<meta>.*?)\][ \t]*\}'''
subs_pattern = r'''(?s)\{\~\~(?P<original>(?:[^\~\>]|(?:\~(?!\>)))+)\~\>(?P<new>(?:[^\~\~]|(?:\~(?!\~\})))+)\~\~\}'''
mark_pattern = r'''(?s)\{\=\=(?P<value>.*?)\=\=\}\{\>\>(?P<comment>.*?)\<\<\}'''
# Considered for future standalone highlight without comment
# mark_pattern = r'''(?s)\{\{(?P<value>.*?)\}\}'''
def deletionProcess(group_object):
replaceString = ''
if group_object.group('value') == '\n\n':
replaceString = "<del> </del>"
else:
replaceString = '<del>' + group_object.group('value').replace("\n\n", " ") + '</del>'
return replaceString
def subsProcess(group_object):
delString = '<del>' + group_object.group('original') + '</del>'
insString = '<ins>' + group_object.group('new') + '</ins>'
newString = delString + insString
return newString
# Converts Addition markup to HTML
def additionProcess(group_object):
replaceString = ''
# Is there a new paragraph followed by new text
if group_object.group('value').startswith('\n\n') and group_object.group('value') != "\n\n":
replaceString = "\n\n<ins class='critic' break> </ins>\n\n"
replaceString = replaceString + '<ins>' + group_object.group('value').replace("\n", " ")
replaceString = replaceString + '</ins>'
# Is the addition just a single new paragraph
elif group_object.group('value') == "\n\n":
replaceString = "\n\n<ins class='critic break'> " + '</ins>\n\n'
# Is it added text followed by a new paragraph?
elif group_object.group('value').endswith('\n\n') and group_object.group('value') != "\n\n":
replaceString = '<ins>' + group_object.group('value').replace("\n", " ") + '</ins>'
replaceString = replaceString + "\n\n<ins class='critic break'> </ins>\n\n"
else:
replaceString = '<ins>' + group_object.group('value').replace("\n", " ") + '</ins>'
return replaceString
def highlightProcess(group_object):
replaceString = '<span class="critic comment">' + group_object.group('value').replace("\n", " ") + '</span>'
return replaceString
def markProcess(group_object):
replaceString = '<mark>' + group_object.group('value') + '</mark><span class="critic comment">' + group_object.group('comment').replace("\n", " ") + '</span>'
return replaceString
a = '''
<style>
#wrapper {
padding-top: 30px !important;
}
#criticnav {
position: fixed;
top: 0;
left: 0;
width: 100%;
box-shadow: 0 1px 1px 1px #777;
margin: 0;
padding: 0;
background-color: white;
font-size: 12px;
}
#criticnav ul {
list-style-type: none;
width: 90%;
margin: 0 auto;
padding: 0;
}
#criticnav ul li {
display: block;
width: 33%;
text-align: center;
padding: 10px 0 5px!important;
margin: 0 !important;
line-height: 1em;
float: left;
border-left: 1px solid #ccc;
text-transform: uppercase;
}
#criticnav ul li:before {
content: none !important;
}
#criticnav ul li#edited-button {
border-right: 1px solid #ccc;
}
#criticnav ul li.active {
background-image: -webkit-linear-gradient(top, white, #cccccc)
}
.original del {
text-decoration: none;
}
.original ins,
.original span.popover,
.original ins.break {
display: none;
}
.edited ins {
text-decoration: none;
}
.edited del,
.edited span.popover,
.edited ins.break {
display: none;
}
.original mark,
.edited mark {
background-color: transparent;
}
.markup mark {
background-color: #fffd38;
text-decoration: none;
}
.markup del {
background-color: #f6a9a9;
text-decoration: none;
}
.markup ins {
background-color: #a9f6a9;
text-decoration: none;
}
.markup ins.break {
display: block;
line-height: 2px;
padding: 0 !important;
margin: 0 !important;
}
.markup ins.break span {
line-height: 1.5em;
}
.markup .popover {
background-color: #4444ff;
color: #fff;
}
.markup .popover .critic.comment {
display: none;
}
.markup .popover:hover span.critic.comment {
display: block;
position: absolute;
width: 200px;
left: 30%;
font-size: 0.8em;
color: #ccc;
background-color: #333;
z-index: 10;
padding: 0.5em 1em;
border-radius: 0.5em;
}
}
</style>
<div id="criticnav">
<ul>
<li id="markup-button">Markup</li>
<li id="original-button">Original</li>
<li id="edited-button">Edited</li>
</ul>
</div>
<script type="text/javascript">
function critic() {
$('#firstdiff').remove();
$('#wrapper').addClass('markup');
$('#markup-button').addClass('active');
$('ins.break').unwrap();
$('span.critic.comment').wrap('<span class="popover" />');
$('span.critic.comment').before('‡');
}
function original() {
$('#original-button').addClass('active');
$('#edited-button').removeClass('active');
$('#markup-button').removeClass('active');
$('#wrapper').addClass('original');
$('#wrapper').removeClass('edited');
$('#wrapper').removeClass('markup');
}
function edited() {
$('#original-button').removeClass('active');
$('#edited-button').addClass('active');
$('#markup-button').removeClass('active');
$('#wrapper').removeClass('original');
$('#wrapper').addClass('edited');
$('#wrapper').removeClass('markup');
}
function markup() {
$('#original-button').removeClass('active');
$('#edited-button').removeClass('active');
$('#markup-button').addClass('active');
$('#wrapper').removeClass('original');
$('#wrapper').removeClass('edited');
$('#wrapper').addClass('markup');
}
var o = document.getElementById("original-button");
var e = document.getElementById("edited-button");
var m = document.getElementById("markup-button");
window.onload = critic;
o.onclick = original;
e.onclick = edited;
m.onclick = markup;
</script>
'''
jq = '''<!DOCTYPE html>
<html>
<head><script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<title>Critic Markup Output</title>'''
head = '''<!DOCTYPE html>
<html>
<head>
<title>Critic Markup Output</title>'''
bodybegin = '''</head><body><div id="wrapper">'''
headend = '''</div></body></html>'''
# Accept input from Marked.app
# h = sys.stdin.read()
parser = argparse.ArgumentParser(description='Convert Critic Markup to HTML')
parser.add_argument('source', help='The source file path, including file name')
parser.add_argument('-m2', help='Use the markdown2 python module. If left blank then markdown module is used', action='store_true')
parser.add_argument('-o','--output', help='Path to store the output file, including file name', metavar='out-file', type=argparse.FileType('wt'), required=False)
parser.add_argument('-css','--css', help='Path to a custom CSS file, including file name', metavar='in-file', type=argparse.FileType('rt'),required=False)
parser.add_argument('-b', '--browser', help='View the output file in the default browser after saving.', action='store_true')
args = parser.parse_args()
try:
if args.source:
inputFile = open(args.source, "r")
inputText = inputFile.read()
inputFile.close()
else:
log("No source file specified")
print "No source file specified"
sys.exit(1)
h = inputText
h = re.sub(del_pattern, deletionProcess, inputText, flags=re.DOTALL)
h = re.sub(add_pattern, additionProcess, h, flags=re.DOTALL)
h = re.sub(mark_pattern, markProcess, h, flags=re.DOTALL)
# comment processing must come after highlights
h = re.sub(comm_pattern, highlightProcess, h, flags=re.DOTALL)
h = re.sub(subs_pattern, subsProcess, h, flags=re.DOTALL)
if (args.m2):
import markdown2
h = markdown2.markdown(h, extras=['footnotes', 'fenced-code-blocks', 'cuddled-lists', 'code-friendly'])
print '\nUsing the Markdown2 module for processing'
else:
import markdown
h = markdown.markdown(h, extensions=['extra', 'codehilite', 'meta'])
if (args.css):
css_file = args.css
cssText = css_file.read()
css_file.close()
h = head + cssText + bodybegin + h + headend
else:
h = jq + a + bodybegin + h + headend
# If an output file is specified, write to it
if args.output:
filesource = args.output
abs_path = os.path.abspath(filesource.name)
output_file = abs_path
print output_file
#file = open(filename, 'wb')
filesource.write(h)
filesource.close()
print "\nOutput file created: ", abs_path
else:
path, filename = os.path.split(args.source)
print "Converting >> " + args.source
output_file = path+'/'+filename.split(os.extsep, 1)[0]+'_CriticParseOut.html'
file = open(output_file, 'w')
file.write(h.encode('utf-8'))
file.close()
print "\nOutput file created: "+ output_file
if (args.browser):
try:
retcode = subprocess.call("open " + output_file, shell=True)
if retcode < 0:
print >>sys.stderr, "Child was terminated by signal", -retcode
else:
print >>sys.stderr, "Child returned", retcode
except OSError, e:
print >>sys.stderr, "Execution failed:", e
except:
print "Unexpected Error: ", sys.exc_info()[0]
raise