Skip to content
Browse files

Upgrade to Redcarpet 2

Syntax highlighting is now via a custom renderer instead.  Also,
configuration options exposed via application settings.
  • Loading branch information...
1 parent 385b7aa commit 1f4f23e9ba424a3a23cb6e8082ad5bca1a009d5d @namelessjon namelessjon committed with Jan 11, 2012
Showing with 34 additions and 40 deletions.
  1. +9 −40 lib/glorify.rb
  2. +25 −0 lib/glorify/renderer.rb
View
49 lib/glorify.rb
@@ -1,17 +1,13 @@
require "sinatra/base"
require "glorify/version"
-require "redcarpet"
-require "albino"
-require "nokogiri"
-require "net/http"
+require "glorify/renderer"
module Sinatra
module Glorify
module Helpers
def glorify text
- options = [:filter_html, :autolink,
- :no_intraemphasis, :fenced_code, :gh_blockcode]
- highlighter(Redcarpet.new(text, *options).to_html)
+ rndr = Glorify::Renderer.new(:use_albino => settings.use_albino)
+ Redcarpet::Markdown.new(rndr, settings.glorify_extensions).render(text)
end
def glorify_css
@@ -78,42 +74,15 @@ def glorify_css
.il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
css
end
-
- private
- def highlighter html
- doc = Nokogiri::HTML(html)
- doc.search("//pre[@lang]").each do |pre|
- pre.replace colorize(pre.text.rstrip, pre[:lang])
- end
- doc.search('pre').each do |pre|
- pre.children.each do |c|
- c.parent = pre.parent
- end
- pre.remove
- end
- doc.search('div').each do |div|
- if div['class'] == 'highlight'
- div.replace(Nokogiri.make("<pre>#{div.to_html}</pre>"))
- end
- end
- doc.to_s
- end
-
- def colorize(code, lang)
- if(pygmentize?)
- Albino.colorize(code, lang)
- else
- Net::HTTP.post_form(URI.parse('http://pygments.appspot.com/'),
- {'code'=>code, 'lang'=>lang}).body
- end
- end
-
- def pygmentize?
- system 'pygmentize -V'
- end
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,
+ :fenced_code_blocks => true
+ }
app.helpers Glorify::Helpers
app.get '/pygments.css' do
glorify_css
View
25 lib/glorify/renderer.rb
@@ -0,0 +1,25 @@
+require "redcarpet"
+require "albino"
+require "net/http"
+
+module Sinatra
+ module Glorify
+ class Renderer < Redcarpet::Render::HTML
+ attr_reader :use_albino
+
+ def initialize(options={})
+ @use_albino = options.fetch(:use_albino, true)
+ 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
+ end
+ end
+ end
+ end
+end

0 comments on commit 1f4f23e

Please sign in to comment.
Something went wrong with that request. Please try again.