Sha256: b3839902c2e893f11fe323c694736b8e4c6430fbd00ef71bf8e7550e71e44a41

Contents?: true

Size: 1.84 KB

Versions: 27

Compression:

Stored size: 1.84 KB

Contents

module Rouge
  module Lexers
    class TeX < RegexLexer
      desc "The TeX typesetting system"
      tag 'tex'
      aliases 'TeX', 'LaTeX', 'latex'

      filenames '*.tex', '*.aux', '*.toc'
      mimetypes 'text/x-tex', 'text/x-latex'

      def self.analyze_text(text)
        return 1 if text =~ /\A\s*\\documentclass/
        return 1 if text =~ /\A\s*\\input/
        return 1 if text =~ /\A\s*\\documentstyle/
        return 1 if text =~ /\A\s*\\relax/
      end

      command = /\\([a-z]+|\s+|.)/i

      state :general do
        rule /%.*$/, 'Comment'
        rule /[{}&_^]/, 'Punctuation'
      end

      state :root do
        rule /\\\[/, 'Punctuation', :displaymath
        rule /\\\(/, 'Punctuation', :inlinemath
        rule /\$\$/, 'Punctuation', :displaymath
        rule /\$/, 'Punctuation', :inlinemath
        rule /\\(begin|end)\{.*?\}/, 'Name.Tag'

        rule /(\\verb)\b(\S)(.*?)(\2)/ do |m|
          group 'Name.Builtin'
          group 'Keyword.Pseudo'
          group 'Literal.String.Other'
          group 'Keyword.Pseudo'
        end

        rule command, 'Keyword', :command
        mixin :general
        rule /[^\\$%&_^{}]+/, 'Text'
      end

      state :math do
        rule command, 'Name.Variable'
        mixin :general
        rule /[0-9]+/, 'Literal.Number'
        rule /[-=!+*\/()\[\]]/, 'Operator'
        rule /[^=!+*\/()\[\]\\$%&_^{}0-9-]+/, 'Name.Builtin'
      end

      state :inlinemath do
        rule /\\\)/, 'Punctuation', :pop!
        rule /\$/, 'Punctuation', :pop!
        mixin :math
      end

      state :displaymath do
        rule /\\\]/, 'Punctuation', :pop!
        rule /\$\$/, 'Punctuation', :pop!
        rule /\$/, 'Name.Builtin'
        mixin :math
      end

      state :command do
        rule /\[.*?\]/, 'Name.Attribute'
        rule /\*/, 'Keyword'
        rule(//) { pop! }
      end
    end
  end
end

Version data entries

27 entries across 27 versions & 1 rubygems

Version Path
rouge-0.4.0 lib/rouge/lexers/tex.rb
rouge-0.3.10 lib/rouge/lexers/tex.rb
rouge-0.3.9 lib/rouge/lexers/tex.rb
rouge-0.3.8 lib/rouge/lexers/tex.rb
rouge-0.3.7 lib/rouge/lexers/tex.rb
rouge-0.3.6 lib/rouge/lexers/tex.rb
rouge-0.3.5 lib/rouge/lexers/tex.rb
rouge-0.3.4 lib/rouge/lexers/tex.rb
rouge-0.3.3 lib/rouge/lexers/tex.rb
rouge-0.3.2 lib/rouge/lexers/tex.rb
rouge-0.3.1 lib/rouge/lexers/tex.rb
rouge-0.3.0 lib/rouge/lexers/tex.rb
rouge-0.2.15 lib/rouge/lexers/tex.rb
rouge-0.2.14 lib/rouge/lexers/tex.rb
rouge-0.2.13 lib/rouge/lexers/tex.rb
rouge-0.2.12 lib/rouge/lexers/tex.rb
rouge-0.2.11 lib/rouge/lexers/tex.rb
rouge-0.2.10 lib/rouge/lexers/tex.rb
rouge-0.2.9 lib/rouge/lexers/tex.rb
rouge-0.2.8 lib/rouge/lexers/tex.rb