fixes and pygments support

This commit is contained in:
John Bintz 2012-03-23 15:43:00 -04:00
parent 94540f5c24
commit 446dc88244
3 changed files with 41 additions and 11 deletions

View File

@ -1,10 +1,7 @@
#!/usr/bin/env ruby #!/usr/bin/env ruby
if !ENV['BUNDLE_GEMFILE'] && File.file?('Gemfile') if !ENV['BUNDLE_GEMFILE'] && File.file?('Gemfile')
require 'rubygems' Kernel.exec("bundle", "exec", $0, *ARGV)
require 'bundler/setup'
$: << File.expand_path('../../lib', __FILE__)
end end
require 'thor' require 'thor'

View File

@ -4,12 +4,16 @@ module Attentive
autoload :Server, 'attentive/server' autoload :Server, 'attentive/server'
class << self class << self
attr_accessor :title, :has_presentation attr_accessor :title, :has_presentation, :use_pygments_command_line
def has_presentation? def has_presentation?
@has_presentation == true @has_presentation == true
end end
def use_pygments_command_line?
(@use_pygments_command_line ||= true) == true
end
def middleware def middleware
@middleware ||= [] @middleware ||= []
end end

View File

@ -6,7 +6,6 @@ require 'attentive/compass_patches'
require 'sinatra' require 'sinatra'
require 'nokogiri' require 'nokogiri'
require 'rdiscount' require 'rdiscount'
require 'pygments'
require 'sinatra/base' require 'sinatra/base'
require 'rack/builder' require 'rack/builder'
@ -33,14 +32,16 @@ module Attentive
def self.start(options) def self.start(options)
require 'rack' require 'rack'
require 'pygments'
require 'coffee_script' require 'coffee_script'
require 'sass' require 'sass'
require 'tilt/coffee' require 'tilt/coffee'
require 'pygments'
# make sure pygments is ready before starting a new thread if !Attentive.use_pygments_command_line?
Pygments.highlight("attentive") # make sure pygments is ready before starting a new thread
Pygments.highlight("attentive")
end
Rack::Handler.default.run(Attentive::Server, :Port => options[:port]) do |server| Rack::Handler.default.run(Attentive::Server, :Port => options[:port]) do |server|
trap(:INT) do trap(:INT) do
@ -79,6 +80,32 @@ module Attentive
end end
end end
class Highlight
attr_reader :code, :lang
def self.run(*args)
new(*args).run
end
def initialize(code, lang)
@code, @lang = code, lang
end
def run
if Attentive.use_pygments_command_line?
require 'tempfile'
temp = Tempfile.new('pygments')
temp.print code
temp.close
%x{pygmentize -l #{lang} -f html #{temp.path}}
else
Pygments.highlight(code, :lexer => language)
end
end
end
class Slide class Slide
extend Forwardable extend Forwardable
@ -104,7 +131,9 @@ module Attentive
lines.each do |line| lines.each do |line|
if line[%r{^```}] if line[%r{^```}]
if code_block if code_block
new_lines << Pygments.highlight(code_block.join, :lexer => code_language)
new_lines << Highlight.run(code_block.join, code_language)
code_block = nil code_block = nil
else else
code_block = [] code_block = []
@ -131,7 +160,7 @@ module Attentive
end end
def markdown_output def markdown_output
RDiscount.new(code_output.join).to_html RDiscount.new(code_output.collect(&:to_s).join).to_html
end end
def to_html def to_html