3 pygments.lexers.diff |
3 pygments.lexers.diff |
4 ~~~~~~~~~~~~~~~~~~~~ |
4 ~~~~~~~~~~~~~~~~~~~~ |
5 |
5 |
6 Lexers for diff/patch formats. |
6 Lexers for diff/patch formats. |
7 |
7 |
8 :copyright: Copyright 2006-2015 by the Pygments team, see AUTHORS. |
8 :copyright: Copyright 2006-2017 by the Pygments team, see AUTHORS. |
9 :license: BSD, see LICENSE for details. |
9 :license: BSD, see LICENSE for details. |
10 """ |
10 """ |
|
11 |
|
12 import re |
11 |
13 |
12 from pygments.lexer import RegexLexer, include, bygroups |
14 from pygments.lexer import RegexLexer, include, bygroups |
13 from pygments.token import Text, Comment, Operator, Keyword, Name, Generic, \ |
15 from pygments.token import Text, Comment, Operator, Keyword, Name, Generic, \ |
14 Literal |
16 Literal |
15 |
17 |
16 __all__ = ['DiffLexer', 'DarcsPatchLexer'] |
18 __all__ = ['DiffLexer', 'DarcsPatchLexer', 'WDiffLexer'] |
17 |
19 |
18 |
20 |
19 class DiffLexer(RegexLexer): |
21 class DiffLexer(RegexLexer): |
20 """ |
22 """ |
21 Lexer for unified or context-style diffs or patches. |
23 Lexer for unified or context-style diffs or patches. |
102 include('specialText'), |
104 include('specialText'), |
103 (r'\[', Generic.Deleted), |
105 (r'\[', Generic.Deleted), |
104 (r'[^\n\[]+', Generic.Deleted), |
106 (r'[^\n\[]+', Generic.Deleted), |
105 ], |
107 ], |
106 } |
108 } |
|
109 |
|
110 |
|
111 class WDiffLexer(RegexLexer): |
|
112 """ |
|
113 A `wdiff <https://www.gnu.org/software/wdiff/>`_ lexer. |
|
114 |
|
115 Note that: |
|
116 |
|
117 * only to normal output (without option like -l). |
|
118 * if target files of wdiff contain "[-", "-]", "{+", "+}", |
|
119 especially they are unbalanced, this lexer will get confusing. |
|
120 |
|
121 .. versionadded:: 2.2 |
|
122 """ |
|
123 |
|
124 name = 'WDiff' |
|
125 aliases = ['wdiff'] |
|
126 filenames = ['*.wdiff'] |
|
127 mimetypes = [] |
|
128 |
|
129 flags = re.MULTILINE | re.DOTALL |
|
130 |
|
131 # We can only assume "[-" after "[-" before "-]" is `nested`, |
|
132 # for instance wdiff to wdiff outputs. We have no way to |
|
133 # distinct these marker is of wdiff output from original text. |
|
134 |
|
135 ins_op = r"\{\+" |
|
136 ins_cl = r"\+\}" |
|
137 del_op = r"\[\-" |
|
138 del_cl = r"\-\]" |
|
139 normal = r'[^{}[\]+-]+' # for performance |
|
140 tokens = { |
|
141 'root': [ |
|
142 (ins_op, Generic.Inserted, 'inserted'), |
|
143 (del_op, Generic.Deleted, 'deleted'), |
|
144 (normal, Text), |
|
145 (r'.', Text), |
|
146 ], |
|
147 'inserted': [ |
|
148 (ins_op, Generic.Inserted, '#push'), |
|
149 (del_op, Generic.Inserted, '#push'), |
|
150 (del_cl, Generic.Inserted, '#pop'), |
|
151 |
|
152 (ins_cl, Generic.Inserted, '#pop'), |
|
153 (normal, Generic.Inserted), |
|
154 (r'.', Generic.Inserted), |
|
155 ], |
|
156 'deleted': [ |
|
157 (del_op, Generic.Deleted, '#push'), |
|
158 (ins_op, Generic.Deleted, '#push'), |
|
159 (ins_cl, Generic.Deleted, '#pop'), |
|
160 |
|
161 (del_cl, Generic.Deleted, '#pop'), |
|
162 (normal, Generic.Deleted), |
|
163 (r'.', Generic.Deleted), |
|
164 ], |
|
165 } |