From 446dc88244120e2e9714a750f0a39f00796edb5c Mon Sep 17 00:00:00 2001 From: John Bintz Date: Fri, 23 Mar 2012 15:43:00 -0400 Subject: [PATCH] fixes and pygments support --- bin/attentive | 5 +---- lib/attentive.rb | 6 +++++- lib/attentive/server.rb | 41 +++++++++++++++++++++++++++++++++++------ 3 files changed, 41 insertions(+), 11 deletions(-) diff --git a/bin/attentive b/bin/attentive index 23454e2..f4c1239 100755 --- a/bin/attentive +++ b/bin/attentive @@ -1,10 +1,7 @@ #!/usr/bin/env ruby if !ENV['BUNDLE_GEMFILE'] && File.file?('Gemfile') - require 'rubygems' - require 'bundler/setup' - - $: << File.expand_path('../../lib', __FILE__) + Kernel.exec("bundle", "exec", $0, *ARGV) end require 'thor' diff --git a/lib/attentive.rb b/lib/attentive.rb index c759be5..037fb96 100644 --- a/lib/attentive.rb +++ b/lib/attentive.rb @@ -4,12 +4,16 @@ module Attentive autoload :Server, 'attentive/server' class << self - attr_accessor :title, :has_presentation + attr_accessor :title, :has_presentation, :use_pygments_command_line def has_presentation? @has_presentation == true end + def use_pygments_command_line? + (@use_pygments_command_line ||= true) == true + end + def middleware @middleware ||= [] end diff --git a/lib/attentive/server.rb b/lib/attentive/server.rb index 3c5aa78..d523526 100644 --- a/lib/attentive/server.rb +++ b/lib/attentive/server.rb @@ -6,7 +6,6 @@ require 'attentive/compass_patches' require 'sinatra' require 'nokogiri' require 'rdiscount' -require 'pygments' require 'sinatra/base' require 'rack/builder' @@ -33,14 +32,16 @@ module Attentive def self.start(options) require 'rack' - require 'pygments' require 'coffee_script' require 'sass' require 'tilt/coffee' + require 'pygments' - # make sure pygments is ready before starting a new thread - Pygments.highlight("attentive") + if !Attentive.use_pygments_command_line? + # 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| trap(:INT) do @@ -79,6 +80,32 @@ module Attentive 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 extend Forwardable @@ -104,7 +131,9 @@ module Attentive lines.each do |line| if line[%r{^```}] 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 else code_block = [] @@ -131,7 +160,7 @@ module Attentive end def markdown_output - RDiscount.new(code_output.join).to_html + RDiscount.new(code_output.collect(&:to_s).join).to_html end def to_html