Skip to content

Commit

Permalink
first commit, added files
Browse files Browse the repository at this point in the history
  • Loading branch information
nakajima committed Dec 16, 2008
0 parents commit 06efb1e
Show file tree
Hide file tree
Showing 6 changed files with 200 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.textile
@@ -0,0 +1,3 @@
h1. syntaxily

(c) Copyright 2008 Pat Nakajima, released under MIT License.
5 changes: 5 additions & 0 deletions lib/core_ext/string.rb
@@ -0,0 +1,5 @@
class String
def syntaxify(lexer)
Albino.new(self, lexer).colorize
end
end
20 changes: 20 additions & 0 deletions lib/syntaxily.rb
@@ -0,0 +1,20 @@
$LOAD_PATH << File.dirname(__FILE__) + '/core_ext'
$LOAD_PATH << File.dirname(__FILE__) + '/syntaxily'
$LOAD_PATH << File.dirname(__FILE__) + '/../vendor'

require 'rubygems'
require 'nokogiri'
require 'albino'
require 'string'

module Syntaxily
def self.parse(text)
doc = Nokogiri::HTML.parse(text)
doc.search('pre.code').each do |node|
lexer = node['rel'].to_sym
lexed = node.text.syntaxify(lexer)
node.replace Nokogiri::HTML.parse(lexed)
end
doc.to_html
end
end
6 changes: 6 additions & 0 deletions spec/spec_helper.rb
@@ -0,0 +1,6 @@
require 'rubygems'
require 'spec'
require 'elementor'
require 'elementor/spec'

require File.dirname(__FILE__) + '/../lib/syntaxily'
49 changes: 49 additions & 0 deletions spec/syntaxily_spec.rb
@@ -0,0 +1,49 @@
require File.dirname(__FILE__) + '/spec_helper'

describe Syntaxily do
include Elementor

attr_reader :result

before(:each) do
@result = elements(:from => :render) do |tag|
tag.headers "h1"
tag.keywords ".highlight pre span.k"
tag.symbols ".highlight pre span.ss"
end
end

describe "regular string helper" do
def render
"def foo; :bar end".syntaxify(:ruby)
end

it "adds syntaxify helper to string" do
result.should have(2).keywords
result.should have(1).keywords.with_text('def')
result.should have(1).keywords.with_text('end')
result.should have(1).symbols.with_text(':bar')
end
end



describe "rendering from markup" do
def render
Syntaxily.parse <<-TEXT
<h1>This is normal</h1>
<pre class="code" rel="ruby">def foo; :bar end</pre>
<p>Good bye</p>
TEXT
end

it "syntax highlights within pre tag" do
result.should have(2).keywords
result.should have(1).keywords.with_text('def')
result.should have(1).keywords.with_text('end')
result.should have(1).symbols.with_text(':bar')
end
end
end
117 changes: 117 additions & 0 deletions vendor/albino.rb
@@ -0,0 +1,117 @@
##
# 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('/some/file.rb', :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('/some/file.rb', :ruby, :bbcode)
# puts @syntaxer.colorize
#
# You can also use the #colorize class method:
#
# puts Albino.colorize('/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
#
require 'rubygems'
require 'open4'

class Albino
@@bin = 'pygmentize'

def self.bin=(path)
@@bin = path
end

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

def initialize(target, lexer = :text, format = :html)
@target = File.exists?(target) ? File.read(target) : target rescue target
@options = { :l => lexer, :f => format }
end

def execute(command)
pid, stdin, stdout, stderr = Open4.popen4(command)
stdin.puts @target
stdin.close
stdout.read.strip
end

def colorize(options = {})
execute @@bin + convert_options(options)
end
alias_method :to_s, :colorize

def convert_options(options = {})
@options.merge(options).inject('') do |string, (flag, value)|
string + " -#{flag} #{value}"
end
end
end

if $0 == __FILE__
require 'rubygems'
require 'test/spec'
require 'mocha'
begin require 'redgreen'; rescue LoadError; end

context "Albino" do
setup do
@syntaxer = Albino.new(__FILE__, :ruby)
end

specify "defaults to text" do
syntaxer = Albino.new(__FILE__)
syntaxer.expects(:execute).with('pygmentize -f html -l text').returns(true)
syntaxer.colorize
end

specify "accepts options" do
@syntaxer.expects(:execute).with('pygmentize -f html -l ruby').returns(true)
@syntaxer.colorize
end

specify "works with strings" do
syntaxer = Albino.new('class New; end', :ruby)
assert_match %r(highlight), syntaxer.colorize
end

specify "aliases to_s" do
assert_equal @syntaxer.colorize, @syntaxer.to_s
end

specify "class method colorize" do
assert_equal @syntaxer.colorize, Albino.colorize(__FILE__, :ruby)
end
end
end

0 comments on commit 06efb1e

Please sign in to comment.