Browse files

Merge pull request #5 from simon/pygments.rb

Pygments.rb
  • Loading branch information...
2 parents b6927a0 + e93938f commit 96c7ef2494ed89594af86abdf1e139bf6f3b3c27 @zzak committed May 15, 2012
Showing with 13 additions and 13 deletions.
  1. +2 −0 .gitignore
  2. +1 −1 README.md
  3. +1 −1 glorify.gemspec
  4. +1 −2 lib/glorify.rb
  5. +8 −9 lib/glorify/renderer.rb
View
2 .gitignore
@@ -1,3 +1,5 @@
.rvmrc
pkg/*
*.swp
+.rbx
+Gemfile.lock
View
2 README.md
@@ -2,7 +2,7 @@
Sinatra helper to parse markdown with syntax highlighting like the pros
-Renders via redcarpet with syntax highlighting thanks to albino. Able to use
+Renders via redcarpet with syntax highlighting thanks to [pygments.rb](https://github.com/tmm1/pygments.rb). Able to use
fenced code blocks like github, and includes a default pygments stylesheet.
## install
View
2 glorify.gemspec
@@ -16,6 +16,6 @@ Gem::Specification.new do |s|
s.add_runtime_dependency "sinatra"
s.add_runtime_dependency "redcarpet", "~> 2.0"
- s.add_runtime_dependency "albino"
+ s.add_runtime_dependency "pygments.rb"
s.add_runtime_dependency "nokogiri"
end
View
3 lib/glorify.rb
@@ -6,7 +6,7 @@ module Sinatra
module Glorify
module Helpers
def glorify text
- rndr = Glorify::Renderer.new(:use_albino => settings.use_albino)
+ rndr = Glorify::Renderer.new
Redcarpet::Markdown.new(rndr, settings.glorify_extensions).render(text)
end
@@ -77,7 +77,6 @@ def glorify_css
end
def self.registered(app)
- app.set :use_albino, proc { system 'pygmentize -V' }
app.set :glorify_extensions, { :filter_html => true,
:autolink => true,
:no_intra_emphasis => true,
View
17 lib/glorify/renderer.rb
@@ -1,25 +1,24 @@
require "redcarpet"
-require "albino"
-require "net/http"
+require "pygments.rb"
module Sinatra
module Glorify
class Renderer < Redcarpet::Render::HTML
- attr_reader :use_albino
def initialize(options={})
- @use_albino = options.fetch(:use_albino, true)
+ python = ENV['GLORIFY_PYTHON'] || 'python'
+ RubyPython.configure :python_exe => python
super
end
def block_code(code, lang)
- if use_albino
- Albino.colorize(code, lang)
- else
- Net::HTTP.post_form(URI.parse('http://pygments.appspot.com/'),
- {'code'=>code, 'lang'=>lang}).body
+ begin
+ Pygments.highlight(code, :lexer => lang, :options => {:encoding => "utf-8"})
+ rescue RubyPython::PythonError
+ Pygments.highlight(code, :options => {:encoding => "utf-8"})
end
end
+
end
end
end

0 comments on commit 96c7ef2

Please sign in to comment.