|
1 # -*- coding: utf-8 -*- |
|
2 """ |
|
3 pygments.lexers.diff |
|
4 ~~~~~~~~~~~~~~~~~~~~ |
|
5 |
|
6 Lexers for diff/patch formats. |
|
7 |
|
8 :copyright: Copyright 2006-2014 by the Pygments team, see AUTHORS. |
|
9 :license: BSD, see LICENSE for details. |
|
10 """ |
|
11 |
|
12 from pygments.lexer import RegexLexer, include, bygroups |
|
13 from pygments.token import Text, Comment, Operator, Keyword, Name, Generic, \ |
|
14 Literal |
|
15 |
|
16 __all__ = ['DiffLexer', 'DarcsPatchLexer'] |
|
17 |
|
18 |
|
19 class DiffLexer(RegexLexer): |
|
20 """ |
|
21 Lexer for unified or context-style diffs or patches. |
|
22 """ |
|
23 |
|
24 name = 'Diff' |
|
25 aliases = ['diff', 'udiff'] |
|
26 filenames = ['*.diff', '*.patch'] |
|
27 mimetypes = ['text/x-diff', 'text/x-patch'] |
|
28 |
|
29 tokens = { |
|
30 'root': [ |
|
31 (r' .*\n', Text), |
|
32 (r'\+.*\n', Generic.Inserted), |
|
33 (r'-.*\n', Generic.Deleted), |
|
34 (r'!.*\n', Generic.Strong), |
|
35 (r'@.*\n', Generic.Subheading), |
|
36 (r'([Ii]ndex|diff).*\n', Generic.Heading), |
|
37 (r'=.*\n', Generic.Heading), |
|
38 (r'.*\n', Text), |
|
39 ] |
|
40 } |
|
41 |
|
42 def analyse_text(text): |
|
43 if text[:7] == 'Index: ': |
|
44 return True |
|
45 if text[:5] == 'diff ': |
|
46 return True |
|
47 if text[:4] == '--- ': |
|
48 return 0.9 |
|
49 |
|
50 |
|
51 class DarcsPatchLexer(RegexLexer): |
|
52 """ |
|
53 DarcsPatchLexer is a lexer for the various versions of the darcs patch |
|
54 format. Examples of this format are derived by commands such as |
|
55 ``darcs annotate --patch`` and ``darcs send``. |
|
56 |
|
57 .. versionadded:: 0.10 |
|
58 """ |
|
59 |
|
60 name = 'Darcs Patch' |
|
61 aliases = ['dpatch'] |
|
62 filenames = ['*.dpatch', '*.darcspatch'] |
|
63 |
|
64 DPATCH_KEYWORDS = ('hunk', 'addfile', 'adddir', 'rmfile', 'rmdir', 'move', |
|
65 'replace') |
|
66 |
|
67 tokens = { |
|
68 'root': [ |
|
69 (r'<', Operator), |
|
70 (r'>', Operator), |
|
71 (r'\{', Operator), |
|
72 (r'\}', Operator), |
|
73 (r'(\[)((?:TAG )?)(.*)(\n)(.*)(\*\*)(\d+)(\s?)(\])', |
|
74 bygroups(Operator, Keyword, Name, Text, Name, Operator, |
|
75 Literal.Date, Text, Operator)), |
|
76 (r'(\[)((?:TAG )?)(.*)(\n)(.*)(\*\*)(\d+)(\s?)', |
|
77 bygroups(Operator, Keyword, Name, Text, Name, Operator, |
|
78 Literal.Date, Text), 'comment'), |
|
79 (r'New patches:', Generic.Heading), |
|
80 (r'Context:', Generic.Heading), |
|
81 (r'Patch bundle hash:', Generic.Heading), |
|
82 (r'(\s*)(%s)(.*\n)' % '|'.join(DPATCH_KEYWORDS), |
|
83 bygroups(Text, Keyword, Text)), |
|
84 (r'\+', Generic.Inserted, "insert"), |
|
85 (r'-', Generic.Deleted, "delete"), |
|
86 (r'.*\n', Text), |
|
87 ], |
|
88 'comment': [ |
|
89 (r'[^\]].*\n', Comment), |
|
90 (r'\]', Operator, "#pop"), |
|
91 ], |
|
92 'specialText': [ # darcs add [_CODE_] special operators for clarity |
|
93 (r'\n', Text, "#pop"), # line-based |
|
94 (r'\[_[^_]*_]', Operator), |
|
95 ], |
|
96 'insert': [ |
|
97 include('specialText'), |
|
98 (r'\[', Generic.Inserted), |
|
99 (r'[^\n\[]+', Generic.Inserted), |
|
100 ], |
|
101 'delete': [ |
|
102 include('specialText'), |
|
103 (r'\[', Generic.Deleted), |
|
104 (r'[^\n\[]+', Generic.Deleted), |
|
105 ], |
|
106 } |