Class: Pygments::Lexer

Inherits:
Struct
  • Object
show all
Defined in:
lib/pygments/lexer.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#aliasesObject

Returns the value of attribute aliases

Returns:

  • (Object)

    the current value of aliases



2
3
4
# File 'lib/pygments/lexer.rb', line 2

def aliases
  @aliases
end

#filenamesObject

Returns the value of attribute filenames

Returns:

  • (Object)

    the current value of filenames



2
3
4
# File 'lib/pygments/lexer.rb', line 2

def filenames
  @filenames
end

#mimetypesObject

Returns the value of attribute mimetypes

Returns:

  • (Object)

    the current value of mimetypes



2
3
4
# File 'lib/pygments/lexer.rb', line 2

def mimetypes
  @mimetypes
end

#nameObject

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name



2
3
4
# File 'lib/pygments/lexer.rb', line 2

def name
  @name
end

Class Method Details

.[](name) ⇒ Object

Public: Alias for find.



72
73
74
# File 'lib/pygments/lexer.rb', line 72

def self.[](name)
  find(name)
end

.allObject

Public: Get all Lexers

Returns an Array of Lexers



55
56
57
# File 'lib/pygments/lexer.rb', line 55

def self.all
  @lexers
end

.create(hash) ⇒ Object

Internal: Create a new Lexer object

hash - A hash of attributes

Returns a Lexer object



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
48
49
50
# File 'lib/pygments/lexer.rb', line 15

def self.create(hash)
  lexer = new(hash[:name], hash[:aliases], hash[:filenames], hash[:mimetypes])

  @lexers << lexer

  @index[lexer.name.downcase] = @name_index[lexer.name] = lexer

  lexer.aliases.each do |name|
    @alias_index[name] = lexer
    @index[name.downcase] ||= lexer
  end

  lexer.filenames.each do |filename|
    extnames = []

    extname = File.extname(filename)
    if m = extname.match(/\[(.+)\]/)
      m[1].scan(/./).each do |s|
        extnames << extname.sub(m[0], s)
      end
    elsif extname != ""
      extnames << extname
    end

    extnames.each do |the_extname|
      @extname_index[the_extname] = lexer
      @index[the_extname.downcase.sub(/^\./, "")] ||= lexer
    end
  end

  lexer.mimetypes.each do |type|
    @mimetypes_index[type] = lexer
  end

  lexer
end

.find(name) ⇒ Object

Public: Look up Lexer by name or alias.

name - A String name or alias

Lexer.find('Ruby')
=> #<Lexer name="Ruby">

Returns the Lexer or nil if none was found.



67
68
69
# File 'lib/pygments/lexer.rb', line 67

def self.find(name)
  @index[name.to_s.downcase]
end

.find_by_alias(name) ⇒ Object

Public: Look up Lexer by one of its aliases.

name - A String alias of the Lexer

Examples

Lexer.find_by_alias('rb')
# => #<Lexer name="Ruby">

Returns the Lexer or nil if none was found.



100
101
102
# File 'lib/pygments/lexer.rb', line 100

def self.find_by_alias(name)
  @alias_index[name]
end

.find_by_extname(extname) ⇒ Object

Public: Look up Lexer by one of it’s file extensions.

extname - A String file extension.

Examples

Lexer.find_by_extname('.rb')
# => #<Lexer name="Ruby">

Returns the Lexer or nil if none was found.



114
115
116
# File 'lib/pygments/lexer.rb', line 114

def self.find_by_extname(extname)
  @extname_index[extname]
end

.find_by_mimetype(type) ⇒ Object

Public: Look up Lexer by one of it’s mime types.

type - A mime type String.

Examples

Lexer.find_by_mimetype('application/x-ruby')
# => #<Lexer name="Ruby">

Returns the Lexer or nil if none was found.



128
129
130
# File 'lib/pygments/lexer.rb', line 128

def self.find_by_mimetype(type)
  @mimetypes_index[type]
end

.find_by_name(name) ⇒ Object

Public: Look up Lexer by its proper name.

name - The String name of the Lexer

Examples

Lexer.find_by_name('Ruby')
# => #<Lexer name="Ruby">

Returns the Lexer or nil if none was found.



86
87
88
# File 'lib/pygments/lexer.rb', line 86

def self.find_by_name(name)
  @name_index[name]
end

Instance Method Details

#highlight(text, options = {}) ⇒ Object

Public: Highlight syntax of text

text - String of code to be highlighted options - Hash of options (defaults to {})

Returns html String



138
139
140
141
# File 'lib/pygments/lexer.rb', line 138

def highlight(text, options = {})
  options[:lexer] = aliases.first
  Pygments.highlight(text, options)
end