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
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'

View File

@ -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

View File

@ -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