diff.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. """
  2. pygments.lexers.diff
  3. ~~~~~~~~~~~~~~~~~~~~
  4. Lexers for diff/patch formats.
  5. :copyright: Copyright 2006-2021 by the Pygments team, see AUTHORS.
  6. :license: BSD, see LICENSE for details.
  7. """
  8. import re
  9. from pygments.lexer import RegexLexer, include, bygroups
  10. from pygments.token import Text, Comment, Operator, Keyword, Name, Generic, \
  11. Literal
  12. __all__ = ['DiffLexer', 'DarcsPatchLexer', 'WDiffLexer']
  13. class DiffLexer(RegexLexer):
  14. """
  15. Lexer for unified or context-style diffs or patches.
  16. """
  17. name = 'Diff'
  18. aliases = ['diff', 'udiff']
  19. filenames = ['*.diff', '*.patch']
  20. mimetypes = ['text/x-diff', 'text/x-patch']
  21. tokens = {
  22. 'root': [
  23. (r' .*\n', Text),
  24. (r'\+.*\n', Generic.Inserted),
  25. (r'-.*\n', Generic.Deleted),
  26. (r'!.*\n', Generic.Strong),
  27. (r'@.*\n', Generic.Subheading),
  28. (r'([Ii]ndex|diff).*\n', Generic.Heading),
  29. (r'=.*\n', Generic.Heading),
  30. (r'.*\n', Text),
  31. ]
  32. }
  33. def analyse_text(text):
  34. if text[:7] == 'Index: ':
  35. return True
  36. if text[:5] == 'diff ':
  37. return True
  38. if text[:4] == '--- ':
  39. return 0.9
  40. class DarcsPatchLexer(RegexLexer):
  41. """
  42. DarcsPatchLexer is a lexer for the various versions of the darcs patch
  43. format. Examples of this format are derived by commands such as
  44. ``darcs annotate --patch`` and ``darcs send``.
  45. .. versionadded:: 0.10
  46. """
  47. name = 'Darcs Patch'
  48. aliases = ['dpatch']
  49. filenames = ['*.dpatch', '*.darcspatch']
  50. DPATCH_KEYWORDS = ('hunk', 'addfile', 'adddir', 'rmfile', 'rmdir', 'move',
  51. 'replace')
  52. tokens = {
  53. 'root': [
  54. (r'<', Operator),
  55. (r'>', Operator),
  56. (r'\{', Operator),
  57. (r'\}', Operator),
  58. (r'(\[)((?:TAG )?)(.*)(\n)(.*)(\*\*)(\d+)(\s?)(\])',
  59. bygroups(Operator, Keyword, Name, Text, Name, Operator,
  60. Literal.Date, Text, Operator)),
  61. (r'(\[)((?:TAG )?)(.*)(\n)(.*)(\*\*)(\d+)(\s?)',
  62. bygroups(Operator, Keyword, Name, Text, Name, Operator,
  63. Literal.Date, Text), 'comment'),
  64. (r'New patches:', Generic.Heading),
  65. (r'Context:', Generic.Heading),
  66. (r'Patch bundle hash:', Generic.Heading),
  67. (r'(\s*)(%s)(.*\n)' % '|'.join(DPATCH_KEYWORDS),
  68. bygroups(Text, Keyword, Text)),
  69. (r'\+', Generic.Inserted, "insert"),
  70. (r'-', Generic.Deleted, "delete"),
  71. (r'.*\n', Text),
  72. ],
  73. 'comment': [
  74. (r'[^\]].*\n', Comment),
  75. (r'\]', Operator, "#pop"),
  76. ],
  77. 'specialText': [ # darcs add [_CODE_] special operators for clarity
  78. (r'\n', Text, "#pop"), # line-based
  79. (r'\[_[^_]*_]', Operator),
  80. ],
  81. 'insert': [
  82. include('specialText'),
  83. (r'\[', Generic.Inserted),
  84. (r'[^\n\[]+', Generic.Inserted),
  85. ],
  86. 'delete': [
  87. include('specialText'),
  88. (r'\[', Generic.Deleted),
  89. (r'[^\n\[]+', Generic.Deleted),
  90. ],
  91. }
  92. class WDiffLexer(RegexLexer):
  93. """
  94. A `wdiff <https://www.gnu.org/software/wdiff/>`_ lexer.
  95. Note that:
  96. * It only works with normal output (without options like ``-l``).
  97. * If the target files contain "[-", "-]", "{+", or "+}",
  98. especially they are unbalanced, the lexer will get confused.
  99. .. versionadded:: 2.2
  100. """
  101. name = 'WDiff'
  102. aliases = ['wdiff']
  103. filenames = ['*.wdiff']
  104. mimetypes = []
  105. flags = re.MULTILINE | re.DOTALL
  106. # We can only assume "[-" after "[-" before "-]" is `nested`,
  107. # for instance wdiff to wdiff outputs. We have no way to
  108. # distinct these marker is of wdiff output from original text.
  109. ins_op = r"\{\+"
  110. ins_cl = r"\+\}"
  111. del_op = r"\[\-"
  112. del_cl = r"\-\]"
  113. normal = r'[^{}[\]+-]+' # for performance
  114. tokens = {
  115. 'root': [
  116. (ins_op, Generic.Inserted, 'inserted'),
  117. (del_op, Generic.Deleted, 'deleted'),
  118. (normal, Text),
  119. (r'.', Text),
  120. ],
  121. 'inserted': [
  122. (ins_op, Generic.Inserted, '#push'),
  123. (del_op, Generic.Inserted, '#push'),
  124. (del_cl, Generic.Inserted, '#pop'),
  125. (ins_cl, Generic.Inserted, '#pop'),
  126. (normal, Generic.Inserted),
  127. (r'.', Generic.Inserted),
  128. ],
  129. 'deleted': [
  130. (del_op, Generic.Deleted, '#push'),
  131. (ins_op, Generic.Deleted, '#push'),
  132. (ins_cl, Generic.Deleted, '#pop'),
  133. (del_cl, Generic.Deleted, '#pop'),
  134. (normal, Generic.Deleted),
  135. (r'.', Generic.Deleted),
  136. ],
  137. }