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



4
5
6
# File 'lib/pygments/lexer.rb', line 4

def aliases
  @aliases
end

#filenamesObject

Returns the value of attribute filenames

Returns:

  • (Object)

    the current value of filenames



4
5
6
# File 'lib/pygments/lexer.rb', line 4

def filenames
  @filenames
end

#mimetypesObject

Returns the value of attribute mimetypes

Returns:

  • (Object)

    the current value of mimetypes



4
5
6
# File 'lib/pygments/lexer.rb', line 4

def mimetypes
  @mimetypes
end

#nameObject

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name



4
5
6
# File 'lib/pygments/lexer.rb', line 4

def name
  @name
end

Class Method Details

.[](name) ⇒ Object

Public: Alias for find.



74
75
76
# File 'lib/pygments/lexer.rb', line 74

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

.allObject

Public: Get all Lexers

Returns an Array of Lexers



57
58
59
# File 'lib/pygments/lexer.rb', line 57

def self.all
  @lexers
end

.create(hash) ⇒ Object

Internal: Create a new Lexer object

hash - A hash of attributes

Returns a Lexer object



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
51
52
# File 'lib/pygments/lexer.rb', line 17

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.



69
70
71
# File 'lib/pygments/lexer.rb', line 69

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.



102
103
104
# File 'lib/pygments/lexer.rb', line 102

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.



116
117
118
# File 'lib/pygments/lexer.rb', line 116

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.



130
131
132
# File 'lib/pygments/lexer.rb', line 130

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.



88
89
90
# File 'lib/pygments/lexer.rb', line 88

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



140
141
142
143
# File 'lib/pygments/lexer.rb', line 140

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