File: diff.rb

package info (click to toggle)
ruby-rouge 4.7.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,844 kB
  • sloc: ruby: 38,489; sed: 2,071; perl: 152; makefile: 8
file content (47 lines) | stat: -rw-r--r-- 1,181 bytes parent folder | download | duplicates (2)
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
# frozen_string_literal: true

module Rouge
  module Lexers
    class Diff < RegexLexer
      title 'diff'
      desc 'Lexes unified diffs or patches'

      tag 'diff'
      aliases 'patch', 'udiff'
      filenames '*.diff', '*.patch'
      mimetypes 'text/x-diff', 'text/x-patch'

      def self.detect?(text)
        return true if text.start_with?('Index: ')
        return true if text =~ %r(\Adiff[^\n]*?\ba/[^\n]*\bb/)
        return true if text =~ /---.*?\n[+][+][+]/ || text =~ /[+][+][+].*?\n---/
      end

      state :root do
        rule(/^ .*$\n?/, Text)
        rule(/^---$\n?/, Punctuation)

        rule %r(
          (^\++.*$\n?) |
          (^>+[ \t]+.*$\n?) |
          (^>+$\n?)
        )x, Generic::Inserted

        rule %r(
          (^-+.*$\n?) |
          (^<+[ \t]+.*$\n?) |
          (^<+$\n?)
        )x, Generic::Deleted

        rule(/^!.*$\n?/, Generic::Strong)
        rule(/^([Ii]ndex|diff).*$\n?/, Generic::Heading)
        rule(/^(@@[^@]*@@)([^\n]*\n)/) do
          groups Punctuation, Text
        end
        rule(/^\w.*$\n?/, Punctuation)
        rule(/^=.*$\n?/, Generic::Heading)
        rule(/.+$\n?/, Text)
      end
    end
  end
end