albino-1.3.3/ 0000775 0001750 0001750 00000000000 11657751300 012625 5 ustar uwabami uwabami albino-1.3.3/metadata.yml 0000664 0001750 0001750 00000003661 11657751300 015136 0 ustar uwabami uwabami --- !ruby/object:Gem::Specification
name: albino
version: !ruby/object:Gem::Version
hash: 29
prerelease:
segments:
- 1
- 3
- 3
version: 1.3.3
platform: ruby
authors:
- Chris Wanstrath
autorequire:
bindir: bin
cert_chain: []
date: 2011-04-19 00:00:00 -07:00
default_executable:
dependencies:
- !ruby/object:Gem::Dependency
name: posix-spawn
prerelease: false
requirement: &id001 !ruby/object:Gem::Requirement
none: false
requirements:
- - ">="
- !ruby/object:Gem::Version
hash: 31
segments:
- 0
- 3
- 6
version: 0.3.6
type: :runtime
version_requirements: *id001
- !ruby/object:Gem::Dependency
name: mocha
prerelease: false
requirement: &id002 !ruby/object:Gem::Requirement
none: false
requirements:
- - ">="
- !ruby/object:Gem::Version
hash: 3
segments:
- 0
version: "0"
type: :development
version_requirements: *id002
description: Ruby wrapper for pygmentize.
email: chris@wanstrath.com
executables: []
extensions: []
extra_rdoc_files: []
files:
- Gemfile
- LICENSE
- README.md
- Rakefile
- albino.gemspec
- lib/albino.rb
- lib/albino/multi.rb
- test/albino_test.rb
- test/multi_albino_test.rb
- vendor/multipygmentize
has_rdoc: true
homepage: http://github.com/github/albino
licenses: []
post_install_message:
rdoc_options: []
require_paths:
- lib
required_ruby_version: !ruby/object:Gem::Requirement
none: false
requirements:
- - ">="
- !ruby/object:Gem::Version
hash: 3
segments:
- 0
version: "0"
required_rubygems_version: !ruby/object:Gem::Requirement
none: false
requirements:
- - ">="
- !ruby/object:Gem::Version
hash: 3
segments:
- 0
version: "0"
requirements: []
rubyforge_project: albino
rubygems_version: 1.4.2
signing_key:
specification_version: 2
summary: Ruby wrapper for pygmentize.
test_files: []
albino-1.3.3/README.md 0000644 0001750 0001750 00000001070 11657751300 014100 0 ustar uwabami uwabami # Albino: a ruby wrapper for pygmentize
This project is an extraction from GitHub.
For this and other extractions, see [http://github.com/github]()
## Installation
sudo easy_install pygments
gem install albino
## Usage
### Simple
require 'albino'
puts Albino.colorize('puts "Hello World"', :ruby)
### Advanced
require 'albino'
@syntaxer = Albino.new(File.new('albino.rb'), :ruby, :bbcode)
puts @syntaxer.colorize
### Multi
require 'albino/multi'
ruby, python = Albino::Multi.colorize([ ['1+2',:ruby], ['1-2',:python] ])
albino-1.3.3/albino.gemspec 0000644 0001750 0001750 00000004611 11657751300 015436 0 ustar uwabami uwabami ## This is the rakegem gemspec template. Make sure you read and understand
## all of the comments. Some sections require modification, and others can
## be deleted if you don't need them. Once you understand the contents of
## this file, feel free to delete any comments that begin with two hash marks.
## You can find comprehensive Gem::Specification documentation, at
## http://docs.rubygems.org/read/chapter/20
Gem::Specification.new do |s|
s.specification_version = 2 if s.respond_to? :specification_version=
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.rubygems_version = '1.3.5'
## Leave these as is they will be modified for you by the rake gemspec task.
## If your rubyforge_project name is different, then edit it and comment out
## the sub! line in the Rakefile
s.name = 'albino'
s.version = '1.3.3'
s.date = '2011-04-19'
s.rubyforge_project = 'albino'
## Make sure your summary is short. The description may be as long
## as you like.
s.summary = "Ruby wrapper for pygmentize."
s.description = "Ruby wrapper for pygmentize."
## List the primary authors. If there are a bunch of authors, it's probably
## better to set the email to an email list or something. If you don't have
## a custom homepage, consider using your GitHub URL or the like.
s.authors = ["Chris Wanstrath"]
s.email = 'chris@wanstrath.com'
s.homepage = 'http://github.com/github/albino'
## This gets added to the $LOAD_PATH so that 'lib/NAME.rb' can be required as
## require 'NAME.rb' or'/lib/NAME/file.rb' can be as require 'NAME/file.rb'
s.require_paths = %w[lib]
s.add_dependency('posix-spawn', '>= 0.3.6')
s.add_development_dependency('mocha')
## Leave this section as-is. It will be automatically generated from the
## contents of your Git repository via the gemspec task. DO NOT REMOVE
## THE MANIFEST COMMENTS, they are used as delimiters by the task.
# = MANIFEST =
s.files = %w[
Gemfile
LICENSE
README.md
Rakefile
albino.gemspec
lib/albino.rb
lib/albino/multi.rb
test/albino_test.rb
test/multi_albino_test.rb
vendor/multipygmentize
]
# = MANIFEST =
## Test files will be grabbed from the file list. Make sure the path glob
## matches what you actually use.
s.test_files = s.files.select { |path| path =~ /^test\/test_.*\.rb/ }
end
albino-1.3.3/test/ 0000775 0001750 0001750 00000000000 11657751300 013604 5 ustar uwabami uwabami albino-1.3.3/test/multi_albino_test.rb 0000644 0001750 0001750 00000003573 11657751300 017654 0 ustar uwabami uwabami require 'rubygems'
require 'albino/multi'
require 'test/unit'
require 'tempfile'
require 'mocha'
class MultiTest < Test::Unit::TestCase
def setup
@syntaxer = Albino::Multi.new(File.new(__FILE__), :ruby)
end
def test_defaults_to_text
syntaxer = Albino::Multi.new('abc')
regex = /span/
assert_no_match regex, syntaxer.colorize
end
def test_markdown_compatible
code = Albino::Multi.colorize('1+2', :ruby)
assert_no_match %r{\Z}, code
end
def test_forces_utf8
code = Albino::Multi.colorize('1+2', :ruby)
if code.respond_to?(:encoding)
assert_equal 'UTF-8', code.encoding.to_s
end
end
def test_works_with_strings
syntaxer = Albino::Multi.new("class New\nend", :ruby)
assert_match %r(highlight), code=syntaxer.colorize
assert_match %(New\n), code
end
def test_works_with_multiple_code_fragments
syntaxer = Albino::Multi.new [
['ruby', "class New\nend"],
['python', "class New:\n pass"]]
codes = syntaxer.colorize
assert_equal 2, codes.size
assert_match %r(highlight), codes[0]
assert_match %r(highlight), codes[1]
assert_match %(New\n), codes[0]
assert_match %(:), codes[1]
end
def test_works_with_files
contents = "class New\nend"
syntaxer = Albino::Multi.new(contents, :ruby)
file_output = syntaxer.colorize
Tempfile.open 'albino-test' do |tmp|
tmp << contents
tmp.flush
syntaxer = Albino::Multi.new(File.new(tmp.path), :ruby)
assert_equal file_output, syntaxer.colorize
end
end
def test_aliases_to_s
syntaxer = Albino::Multi.new(File.new(__FILE__), :ruby)
assert_equal @syntaxer.colorize, syntaxer.to_s
end
def test_class_method_colorize
assert_equal @syntaxer.colorize, Albino::Multi.colorize(File.new(__FILE__), :ruby)
end
end
albino-1.3.3/test/albino_test.rb 0000644 0001750 0001750 00000004744 11657751300 016443 0 ustar uwabami uwabami # coding: utf-8
require 'rubygems'
require 'albino'
require 'test/unit'
require 'tempfile'
require 'mocha'
class AlbinoTest < Test::Unit::TestCase
def setup
@syntaxer = Albino.new(File.new(__FILE__), :ruby)
end
def test_defaults_to_text
syntaxer = Albino.new('abc')
regex = /span/
assert_no_match regex, syntaxer.colorize
end
def test_accepts_options
assert_match /span/, @syntaxer.colorize
end
def test_accepts_non_alpha_options
assert_equal '', @syntaxer.colorize(:f => 'html+c#-dump')
end
def test_markdown_compatible
code = Albino.colorize('1+2', :ruby)
assert_no_match %r{\Z}, code
end
def test_works_with_strings
syntaxer = Albino.new("class New\nend", :ruby)
assert_match %r(highlight), code=syntaxer.colorize
assert_match %(New\n), code
end
def test_works_with_utf8_strings
code = Albino.new("# é", :bash).colorize
assert_match %r(highlight), code
assert_match %(# é), code
end
def test_works_with_files
contents = "class New\nend"
syntaxer = Albino.new(contents, :ruby)
file_output = syntaxer.colorize
Tempfile.open 'albino-test' do |tmp|
tmp << contents
tmp.flush
syntaxer = Albino.new(File.new(tmp.path), :ruby)
assert_equal file_output, syntaxer.colorize
end
end
def test_default_encoding
assert_equal Albino.default_encoding, 'utf-8'
end
def test_change_encoding
before = Albino.default_encoding
assert_equal Albino.default_encoding, 'utf-8'
Albino.default_encoding = 'ascii'
assert_equal Albino.default_encoding, 'ascii'
ensure
Albino.default_encoding = before
end
def test_invalid_encoding
before = Albino.default_encoding
Albino.default_encoding = 'binary'
assert_equal Albino.colorize('class Baño; end', :ruby), ''
ensure
Albino.default_encoding = before
end
def test_custom_encoding
code = Albino.new('1+2', :ruby, :html, 'ascii').colorize
if code.respond_to?(:encoding)
assert_equal code.encoding.to_s, 'US-ASCII'
end
end
def test_aliases_to_s
syntaxer = Albino.new(File.new(__FILE__), :ruby)
assert_equal @syntaxer.colorize, syntaxer.to_s
end
def test_class_method_colorize
assert_equal @syntaxer.colorize, Albino.colorize(File.new(__FILE__), :ruby)
end
def test_escaped_shell_args
assert_raises Albino::ShellArgumentError do
@syntaxer.convert_options(:l => "'abc;'")
end
end
end
albino-1.3.3/lib/ 0000775 0001750 0001750 00000000000 11657751300 013373 5 ustar uwabami uwabami albino-1.3.3/lib/albino/ 0000775 0001750 0001750 00000000000 11657751300 014637 5 ustar uwabami uwabami albino-1.3.3/lib/albino/multi.rb 0000644 0001750 0001750 00000005672 11657751300 016326 0 ustar uwabami uwabami require 'albino'
class Albino
# 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
class << self
attr_accessor :bin, :timeout_threshold
end
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
@multi = true
target
else
[[lexer, target]]
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)|
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{\Z}, "\n")
# albino::multi assumes utf8 encoding
code.force_encoding('UTF-8') if code.respond_to?(:force_encoding)
end
@multi ? pieces : pieces.first
end
alias_method :to_s, :colorize
SEPARATOR = "\000".freeze
end
end
albino-1.3.3/lib/albino.rb 0000644 0001750 0001750 00000006371 11657751300 015171 0 ustar uwabami uwabami require 'posix-spawn'
##
# Wrapper for the Pygments command line tool, pygmentize.
#
# Pygments: http://pygments.org/
#
# Assumes pygmentize is in the path. If not, set its location
# with Albino.bin = '/path/to/pygmentize'
#
# Use like so:
#
# @syntaxer = Albino.new('puts "Hello World"', :ruby)
# puts @syntaxer.colorize
#
# This'll print out an HTMLized, Ruby-highlighted version
# of '/some/file.rb'.
#
# To use another formatter, pass it as the third argument:
#
# @syntaxer = Albino.new('puts "Hello World"', :ruby, :bbcode)
# puts @syntaxer.colorize
#
# You can also use the #colorize class method:
#
# puts Albino.colorize('puts "Hello World"', :ruby)
#
# To format a file, pass a file stream:
#
# puts Albino.colorize(File.new('/some/file.rb'), :ruby)
#
# Another also: you get a #to_s, for somewhat nicer use in Rails views.
#
# ... helper file ...
# def highlight(text)
# Albino.new(text, :ruby)
# end
#
# ... view file ...
# <%= highlight text %>
#
# 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`.
#
# Chris Wanstrath // chris@ozmm.org
# GitHub // http://github.com
#
class Albino
class ShellArgumentError < ArgumentError; end
include POSIX::Spawn
VERSION = '1.3.3'
class << self
attr_accessor :bin, :timeout_threshold
attr_reader :default_encoding
def default_encoding=(encoding)
# make sure the encoding is valid
Encoding.find(encoding) if defined?(Encoding)
@default_encoding = encoding
end
end
self.timeout_threshold = 10
self.default_encoding = 'utf-8'
self.bin = 'pygmentize'
def self.colorize(*args)
new(*args).colorize
end
def initialize(target, lexer = :text, format = :html, encoding = self.class.default_encoding)
@target = target
@options = { :l => lexer, :f => format, :O => "encoding=#{encoding}" }
@encoding = encoding
end
def execute(options = {})
proc_options = {}
proc_options[:timeout] = options.delete(:timeout) || self.class.timeout_threshold
command = convert_options(options)
command.unshift(bin)
Child.new(*(command + [proc_options.merge(:input => write_target)]))
end
def colorize(options = {})
out = execute(options).out
# markdown requires block elements on their own line
out.sub!(%r{\Z}, "\n")
# covert output to the encoding we told pygmentize to use
out.force_encoding(@encoding) if out.respond_to?(:force_encoding)
out
end
alias_method :to_s, :colorize
def convert_options(options = {})
@options.merge(options).inject([]) do |memo, (flag, value)|
validate_shell_args(flag.to_s, value.to_s)
memo << "-#{flag}" << value.to_s
end
end
def write_target
if @target.respond_to?(:read)
out = @target.read
@target.close
out
else
@target.to_s
end
end
def validate_shell_args(flag, value)
if flag !~ /^[a-z]+$/i
raise ShellArgumentError, "Flag is invalid: #{flag.inspect}"
end
if value !~ /^[a-z0-9\-\_\+\=\#\,\s]+$/i
raise ShellArgumentError, "Flag value is invalid: -#{flag} #{value.inspect}"
end
end
def bin
self.class.bin
end
end
albino-1.3.3/Gemfile 0000644 0001750 0001750 00000000031 11657751300 014110 0 ustar uwabami uwabami source :rubygems
gemspec
albino-1.3.3/LICENSE 0000644 0001750 0001750 00000002056 11657751300 013633 0 ustar uwabami uwabami The MIT License
Copyright (c) Chris Wanstrath
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE. albino-1.3.3/Rakefile 0000644 0001750 0001750 00000006170 11657751300 014274 0 ustar uwabami uwabami require 'rubygems'
require 'rake'
require 'date'
#############################################################################
#
# Helper functions
#
#############################################################################
def name
@name ||= Dir['*.gemspec'].first.split('.').first
end
def version
line = File.read("lib/#{name}.rb")[/^\s*VERSION\s*=\s*.*/]
line.match(/.*VERSION\s*=\s*['"](.*)['"]/)[1]
end
def date
Date.today.to_s
end
def rubyforge_project
name
end
def gemspec_file
"#{name}.gemspec"
end
def gem_file
"#{name}-#{version}.gem"
end
def replace_header(head, header_name)
head.sub!(/(\.#{header_name}\s*= ').*'/) { "#{$1}#{send(header_name)}'"}
end
#############################################################################
#
# Standard tasks
#
#############################################################################
task :default => :test
require 'rake/testtask'
Rake::TestTask.new(:test) do |test|
test.libs << 'lib' << 'test'
test.pattern = 'test/**/*_test.rb'
test.verbose = true
end
desc "Open an irb session preloaded with this library"
task :console do
sh "irb -rubygems -r ./lib/#{name}.rb"
end
#############################################################################
#
# Custom tasks (add your own tasks here)
#
#############################################################################
#############################################################################
#
# Packaging tasks
#
#############################################################################
task :release => :build do
unless `git branch` =~ /^\* master$/
puts "You must be on the master branch to release!"
exit!
end
sh "git commit --allow-empty -a -m 'Release #{version}'"
sh "git tag v#{version}"
sh "git push origin master"
sh "git push --tags"
sh "gem push pkg/#{name}-#{version}.gem"
end
task :build => :gemspec do
sh "mkdir -p pkg"
sh "gem build #{gemspec_file}"
sh "mv #{gem_file} pkg"
end
task :gemspec => :validate do
# read spec file and split out manifest section
spec = File.read(gemspec_file)
head, manifest, tail = spec.split(" # = MANIFEST =\n")
# replace name version and date
replace_header(head, :name)
replace_header(head, :version)
replace_header(head, :date)
#comment this out if your rubyforge_project has a different name
replace_header(head, :rubyforge_project)
# determine file list from git ls-files
files = `git ls-files`.
split("\n").
sort.
reject { |file| file =~ /^\./ }.
reject { |file| file =~ /^(rdoc|pkg)/ }.
map { |file| " #{file}" }.
join("\n")
# piece file back together and write
manifest = " s.files = %w[\n#{files}\n ]\n"
spec = [head, manifest, tail].join(" # = MANIFEST =\n")
File.open(gemspec_file, 'w') { |io| io.write(spec) }
puts "Updated #{gemspec_file}"
end
task :validate do
libfiles = Dir['lib/*'] - ["lib/#{name}.rb", "lib/#{name}"]
unless libfiles.empty?
puts "Directory `lib` should only contain a `#{name}.rb` file and `#{name}` dir."
exit!
end
unless Dir['VERSION*'].empty?
puts "A `VERSION` file at root level violates Gem best practices."
exit!
end
end albino-1.3.3/vendor/ 0000775 0001750 0001750 00000000000 11657751300 014122 5 ustar uwabami uwabami albino-1.3.3/vendor/multipygmentize 0000755 0001750 0001750 00000002636 11657751300 017323 0 ustar uwabami uwabami #!/usr/bin/env python
# This script allows you to highlight multiple chunks of code with a single
# invocation. Expected input is on STDIN and takes the form of:
#
# \000\000\000...
#
# where is the shortname of a Pygments lexer and is the source
# code to be highlighted. Each lexer and code pair is separated with a NULL
# byte and pairs of lexer/code are also separated with NULL bytes.
#
# Output is a list of highlighted code blocks separated by NULL bytes in the
# same order in which they were received.
import sys, os, codecs
sys.stdout = codecs.getwriter('UTF-8')(sys.stdout)
vpath = os.path.realpath(__file__).split("/")
vpath.pop()
vpath.pop()
vpath = "/".join(vpath)
from pygments import highlight
from pygments.lexers import get_lexer_by_name
from pygments.formatters import HtmlFormatter
parts = sys.stdin.read().split("\000")
newparts = []
for i in range(len(parts) / 2):
lang = parts[i * 2]
code = parts[i * 2 + 1]
try:
lexer = get_lexer_by_name(lang)
except:
lexer = get_lexer_by_name('text')
newparts.append([code, lexer])
def hl(spec):
code = spec[0]
lexer = spec[1]
try:
return highlight(code, lexer, HtmlFormatter())
except:
lexer = get_lexer_by_name('text')
return highlight(code, lexer, HtmlFormatter())
for spec in newparts:
sys.stdout.write(hl(spec))
sys.stdout.write("\000")