Class: Simplabs::Highlight::PygmentsWrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/simplabs/highlight/pygments_wrapper.rb

Overview

Wraps the actual pygments syntax highlighter and exposes its functionality to Ruby code.

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (PygmentsWrapper) initialize(code, language)

Parameters:

  • code (String)

    the actual code to highlight

  • language (String, Symbol)

    the language the code to highlight is in



25
26
27
28
# File 'lib/simplabs/highlight/pygments_wrapper.rb', line 25

def initialize(code, language)
  @code     = code
  @language = language
end

Instance Attribute Details

- (Object) code (readonly)

The code the wrapper highlights



12
13
14
# File 'lib/simplabs/highlight/pygments_wrapper.rb', line 12

def code
  @code
end

- (Object) language (readonly)

The language the #code to highlight is in



16
17
18
# File 'lib/simplabs/highlight/pygments_wrapper.rb', line 16

def language
  @language
end

Instance Method Details

- (String) highlight(options = {})

Highlights the #code.

Returns:

  • (String)

    the highlighted code or simply the HTML-escaped code if the language is not supported.



36
37
38
39
40
41
42
43
# File 'lib/simplabs/highlight/pygments_wrapper.rb', line 36

def highlight(options = {})
  command = "pygmentize -f html -O nowrap=true -l #{@language}"
  IO.popen(command, mode = 'r+') do |pygments|
    pygments << @code
    pygments.close_write
    result = pygments.read.chomp
  end
end