Skip to content

Commit

Permalink
tomdoc'd
Browse files Browse the repository at this point in the history
  • Loading branch information
technoweenie committed Mar 3, 2011
1 parent b4dd24d commit 56b72ec
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions lib/albino/multi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,27 @@ class Albino
class ShellArgumentError < ArgumentError; end
end

# 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`.
class Multi
include POSIX::Spawn

Expand All @@ -15,10 +36,25 @@ class << self
self.timeout_threshold = 10
self.bin = File.join(File.dirname(__FILE__), *%w(.. .. vendor multipygmentize))

# Initializes a new Albino::Multi and runs #colorize.
def self.colorize(*args)
new(*args).colorize
end

# 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.
def initialize(target, lexer = :text, *args)
@spec = case target
when Array
Expand All @@ -29,6 +65,14 @@ def initialize(target, lexer = :text, *args)
end
end

# 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.
def colorize(options = {})
options[:timeout] ||= self.class.timeout_threshold
options[:input] = @spec.inject([]) do |memo, (lexer, code)|
Expand Down

0 comments on commit 56b72ec

Please sign in to comment.