Class: Albino::Multi

Inherits:
Object
  • Object
show all
Includes:
POSIX::Spawn
Defined in:
lib/albino/multi.rb

Overview

Wrapper for a custom multipygmentize script. Passes multiple code fragments in STDIN to Python. This assumes both Python and pygments are installed.

Use like so:

@syntaxer = Albino::Multi.new([ [:ruby, File.open("/some/file.rb")] ])
puts @syntaxer.colorize

It takes an Array of two-element arrays [lexer, code].

You can also use Albino::Multi as a drop-in replacement. It currently has a few limitations however:

  • Only the HTML output format is supported.

  • UTF-8 encoding is forced.

The default lexer is ‘text’. You need to specify a lexer yourself; because we are using STDIN there is no auto-detect.

To see all lexers and formatters available, run ‘pygmentize -L`.

Constant Summary collapse

SEPARATOR =
"\000".freeze

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target, lexer = :text, *args) ⇒ Multi

This method accepts two forms of input:

DEFAULT

target - The Array of two-element [lexer, code] Arrays:

lexer - The String lexer for the upcoming block of code.
code  - The String block of code to highlight.

LEGACY

target - The String block of code to highlight. lexer - The String lexer for the block of code.

Albino#initialize also takes format and encoding which are ignored.



54
55
56
57
58
59
60
61
62
# File 'lib/albino/multi.rb', line 54

def initialize(target, lexer = :text, *args)
  @spec = case target
    when Array
      @multi = true
      target
    else
      [[lexer, target]]
  end
end

Class Attribute Details

.binObject

Returns the value of attribute bin.



29
30
31
# File 'lib/albino/multi.rb', line 29

def bin
  @bin
end

.timeout_thresholdObject

Returns the value of attribute timeout_threshold.



29
30
31
# File 'lib/albino/multi.rb', line 29

def timeout_threshold
  @timeout_threshold
end

Class Method Details

.colorize(*args) ⇒ Object

Initializes a new Albino::Multi and runs #colorize.



36
37
38
# File 'lib/albino/multi.rb', line 36

def self.colorize(*args)
  new(*args).colorize
end

Instance Method Details

#colorize(options = {}) ⇒ Object Also known as: to_s

Colorizes the code blocks.

options - Specify options for the child process:

timeout - A Fixnum timeout for the child process.

Returns an Array of HTML highlighted code block Strings if an array of targets are given to #initialize, or just a single HTML highlighted code block String.



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/albino/multi.rb', line 72

def colorize(options = {})
  options[:timeout] ||= self.class.timeout_threshold
  options[:input]     = @spec.inject([]) do |memo, (lexer, code)|
    memo << lexer << SEPARATOR

    if code.respond_to?(:read)
      out = code.read
      code.close
      code = out
    end

    memo << code << SEPARATOR
  end.join("")

  child  = Child.new(self.class.bin, options)
  pieces = child.out.split(SEPARATOR).each do |code|
    # markdown requires block elements on their own line
    code.sub!(%r{</pre></div>\Z}, "</pre>\n</div>")

    # albino::multi assumes utf8 encoding
    code.force_encoding('UTF-8') if code.respond_to?(:force_encoding)
  end
  @multi ? pieces : pieces.first
end