@@ -17,22 +17,23 @@ class DefaultLexer(RegexLexer):
17
17
Simply lex each line as a token.
18
18
"""
19
19
20
- name = ' Default'
21
- aliases = [' default' ]
22
- filenames = ['*' ]
20
+ name = " Default"
21
+ aliases = [" default" ]
22
+ filenames = ["*" ]
23
23
24
24
tokens = {
25
- ' root' : [
26
- (r' .*\n' , Text ),
25
+ " root" : [
26
+ (r" .*\n" , Text ),
27
27
]
28
28
}
29
29
30
30
31
31
class DiffHtmlFormatter (HtmlFormatter ):
32
32
"""
33
- Formats a single source file with pygments and adds diff highlights based on the
33
+ Formats a single source file with pygments and adds diff highlights based on the
34
34
diff details given.
35
35
"""
36
+
36
37
isLeft = False
37
38
diffs = None
38
39
@@ -51,25 +52,21 @@ def getDiffLineNos(self):
51
52
if self .isLeft :
52
53
if change :
53
54
if isinstance (left_no , int ) and isinstance (right_no , int ):
54
- no = '<span class="lineno_q lineno_leftchange">' + \
55
- str (left_no ) + "</span>"
55
+ no = '<span class="lineno_q lineno_leftchange">' + str (left_no ) + "</span>"
56
56
elif isinstance (left_no , int ) and not isinstance (right_no , int ):
57
- no = '<span class="lineno_q lineno_leftdel">' + \
58
- str (left_no ) + "</span>"
57
+ no = '<span class="lineno_q lineno_leftdel">' + str (left_no ) + "</span>"
59
58
elif not isinstance (left_no , int ) and isinstance (right_no , int ):
60
59
no = '<span class="lineno_q lineno_leftadd"> </span>'
61
60
else :
62
61
no = '<span class="lineno_q">' + str (left_no ) + "</span>"
63
62
else :
64
63
if change :
65
64
if isinstance (left_no , int ) and isinstance (right_no , int ):
66
- no = '<span class="lineno_q lineno_rightchange">' + \
67
- str (right_no ) + "</span>"
65
+ no = '<span class="lineno_q lineno_rightchange">' + str (right_no ) + "</span>"
68
66
elif isinstance (left_no , int ) and not isinstance (right_no , int ):
69
67
no = '<span class="lineno_q lineno_rightdel"> </span>'
70
68
elif not isinstance (left_no , int ) and isinstance (right_no , int ):
71
- no = '<span class="lineno_q lineno_rightadd">' + \
72
- str (right_no ) + "</span>"
69
+ no = '<span class="lineno_q lineno_rightadd">' + str (right_no ) + "</span>"
73
70
else :
74
71
no = '<span class="lineno_q">' + str (right_no ) + "</span>"
75
72
@@ -79,7 +76,7 @@ def getDiffLineNos(self):
79
76
80
77
def _wrap_code (self , source ):
81
78
source = list (source )
82
- yield 0 , ''
79
+ yield 0 , ""
83
80
84
81
for idx , ((left_no , left_line ), (right_no , right_line ), change ) in enumerate (self .diffs ):
85
82
# print idx, ((left_no, left_line),(right_no, right_line),change)
@@ -132,7 +129,7 @@ def _wrap_code(self, source):
132
129
except :
133
130
# print "WARNING! failed to enumerate diffs fully!"
134
131
pass # this is expected sometimes
135
- yield 0 , ' \n '
132
+ yield 0 , " \n "
136
133
137
134
def _wrap_tablelinenos (self , inner ):
138
135
dummyoutfile = io .StringIO ()
@@ -150,11 +147,9 @@ def _wrap_tablelinenos(self, inner):
150
147
aln = self .anchorlinenos
151
148
nocls = self .noclasses
152
149
153
-
154
-
155
150
yield 0 , (f'<table width="100%" class="{ self .cssclass } "><tr></div></td><td class="code">' )
156
151
yield 0 , dummyoutfile .getvalue ()
157
- yield 0 , ' </td></tr></table>'
152
+ yield 0 , " </td></tr></table>"
158
153
159
154
160
155
class CodeDiff (object ):
@@ -164,26 +159,24 @@ class CodeDiff(object):
164
159
"""
165
160
166
161
def __init__ (self , fromtxt , totxt , name = None ):
167
-
168
162
self .fromlines = [n + "\n " for n in fromtxt .split ("\n " )]
169
163
self .leftcode = "" .join (self .fromlines )
170
164
171
-
172
165
self .tolines = [n + "\n " for n in totxt .split ("\n " )]
173
166
self .rightcode = "" .join (self .tolines )
174
167
175
- def getDiffDetails (self , fromdesc = '' , todesc = '' , context = False , numlines = 5 , tabSize = 8 ):
168
+ def getDiffDetails (self , fromdesc = "" , todesc = "" , context = False , numlines = 5 , tabSize = 8 ):
176
169
# change tabs to spaces before it gets more difficult after we insert
177
170
# markkup
178
171
def expand_tabs (line ):
179
172
# hide real spaces
180
- line = line .replace (' ' , ' \0 ' )
173
+ line = line .replace (" " , " \0 " )
181
174
# expand tabs into spaces
182
175
line = line .expandtabs (tabSize )
183
176
# replace spaces from expanded tabs back into tab characters
184
177
# (we'll replace them with markup after we do differencing)
185
- line = line .replace (' ' , ' \t ' )
186
- return line .replace (' \0 ' , ' ' ).rstrip (' \n ' )
178
+ line = line .replace (" " , " \t " )
179
+ return line .replace (" \0 " , " " ).rstrip (" \n " )
187
180
188
181
self .fromlines = [expand_tabs (line ) for line in self .fromlines ]
189
182
self .tolines = [expand_tabs (line ) for line in self .tolines ]
@@ -194,29 +187,29 @@ def expand_tabs(line):
194
187
else :
195
188
context_lines = None
196
189
197
- diffs = difflib ._mdiff (self .fromlines , self .tolines , context_lines ,
198
- linejunk = None , charjunk = difflib .IS_CHARACTER_JUNK )
190
+ diffs = difflib ._mdiff (
191
+ self .fromlines , self .tolines , context_lines , linejunk = None , charjunk = difflib .IS_CHARACTER_JUNK
192
+ )
199
193
return list (diffs )
200
194
201
195
def format (self , verbose = False ):
202
- self .diffs = self .getDiffDetails ('a' , 'b' )
196
+ self .diffs = self .getDiffDetails ("a" , "b" )
203
197
204
198
if verbose :
205
199
for diff in self .diffs :
206
200
print ("%-6s %-80s %-80s" % (diff [2 ], diff [0 ], diff [1 ]))
207
201
208
- fields = ((self .leftcode , True ),
209
- (self .rightcode , False ))
202
+ fields = ((self .leftcode , True ), (self .rightcode , False ))
210
203
211
204
codeContents = []
212
- for ( code , isLeft ) in fields :
213
-
214
- inst = DiffHtmlFormatter ( isLeft ,
215
- self .diffs ,
216
- nobackground = False ,
217
- linenos = True ,
218
- # style=options.syntax_css
219
- )
205
+ for code , isLeft in fields :
206
+ inst = DiffHtmlFormatter (
207
+ isLeft ,
208
+ self .diffs ,
209
+ nobackground = False ,
210
+ linenos = True ,
211
+ # style=options.syntax_css
212
+ )
220
213
221
214
self .lexer = DefaultLexer ()
222
215
@@ -225,27 +218,28 @@ def format(self, verbose=False):
225
218
codeContents .append (formatted )
226
219
227
220
answers = {
228
- "left" : codeContents [0 ],
229
- "right" : codeContents [1 ],
221
+ "left" : codeContents [0 ],
222
+ "right" : codeContents [1 ],
230
223
}
231
224
232
225
return answers
233
226
227
+
234
228
def styled_diff (old , new ):
235
229
"""
236
230
returns styled output
237
231
"""
238
232
239
233
if "last_updated" in old :
240
- old .pop ("last_updated" )
234
+ old .pop ("last_updated" )
241
235
if "last_updated" in new :
242
236
new .pop ("last_updated" )
243
237
244
- if get_plugin_setting (' change_log_format' ) == ' yaml' :
238
+ if get_plugin_setting (" change_log_format" ) == " yaml" :
245
239
# old['output_format'] = 'yaml'
246
240
old = yaml .dump (old , sort_keys = False )
247
241
new = yaml .dump (new , sort_keys = False )
248
- elif get_plugin_setting (' change_log_format' ) == ' json' :
242
+ elif get_plugin_setting (" change_log_format" ) == " json" :
249
243
# old['output_format'] = 'json'
250
244
old = json .dumps (old , indent = 2 )
251
245
new = json .dumps (new , indent = 2 )
@@ -255,4 +249,4 @@ def styled_diff(old, new):
255
249
new = yaml .dump (new , sort_keys = False )
256
250
257
251
codeDiff = CodeDiff (old , new )
258
- return codeDiff .format ()
252
+ return codeDiff .format ()
0 commit comments