File: diff.rb

package info (click to toggle)
ruby-rugments 1.0.0~beta8-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 820 kB
  • sloc: ruby: 10,293; makefile: 2
file content (34 lines) | stat: -rw-r--r-- 973 bytes parent folder | download | duplicates (3)
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
module Rugments
  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.analyze_text(text)
        return 1   if text.start_with?('Index: ')
        return 1   if text.start_with?('diff ')

        # TODO: Have a look at pygments here, seems better
        return 0.9 if text =~ /\A---.*?\n\+\+\+/m
      end

      state :root do
        rule(/^ .*\n/, Text)
        rule(/^\+.*\n/, Generic::Inserted)
        # Do not highlight the delimiter line
        # before the diffstat in email patches.
        rule(/^-+ .*\n/, Generic::Deleted)
        rule(/^!.*\n/, Generic::Strong)
        rule(/^@.*\n/, Generic::Subheading)
        rule(/^([Ii]ndex|diff).*\n/, Generic::Heading)
        rule(/^=.*\n/, Generic::Heading)
        rule(/.*\n/, Text)
      end
    end
  end
end