lib/showoff.rb in showoff-0.0.7 vs lib/showoff.rb in showoff-0.1.0
- old
+ new
@@ -2,10 +2,17 @@
require 'sinatra/base'
require 'json'
require 'nokogiri'
require 'showoff_utils'
+begin
+ require 'prawn'
+ require 'princely'
+rescue LoadError
+ puts 'pdf generation disabled - install prawn'
+end
+
begin
require 'rdiscount'
rescue LoadError
require 'bluecloth'
Markdown = BlueCloth
@@ -43,11 +50,11 @@
def js_files
Dir.glob("#{options.pres_dir}/*.js").map { |path| File.basename(path) }
end
- def process_markdown(name, content)
+ def process_markdown(name, content, wrap = false)
slides = content.split(/^!SLIDE/)
slides.delete('')
final = ''
if slides.size > 1
seq = 1
@@ -55,20 +62,22 @@
slides.each do |slide|
md = ''
lines = slide.split("\n")
classes = lines.shift
slide = lines.join("\n")
+ md += '<div class="' + wrap + '">' if wrap
if seq
md += "<div class=\"slide #{classes}\" ref=\"#{name}/#{seq.to_s}\">\n"
seq += 1
else
md += "<div class=\"slide #{classes}\" ref=\"#{name}\">\n"
end
sl = Markdown.new(slide).to_html
sl = update_image_paths(name, sl)
md += sl
md += "</div>\n"
+ md += "</div>\n" if wrap
final += update_commandline_code(md)
end
final
end
@@ -112,10 +121,59 @@
code << c
end
end
html.root.to_s
end
+
+ def get_slides_html(wrap = false)
+ index = File.join(options.pres_dir, 'showoff.json')
+ files = []
+ if File.exists?(index)
+ order = JSON.parse(File.read(index))
+ order = order.map { |s| s['section'] }
+ order.each do |section|
+ files << load_section_files(section)
+ end
+ files = files.flatten
+ files = files.select { |f| f =~ /.md/ }
+ data = ''
+ files.each do |f|
+ fname = f.gsub(options.pres_dir + '/', '').gsub('.md', '')
+ data += process_markdown(fname, File.read(f), wrap)
+ end
+ end
+ data
+ end
+
+ def inline_css(csses, pre = nil)
+ css_content = '<style type="text/css">'
+ csses.each do |css_file|
+ if pre
+ css_file = File.join(File.dirname(__FILE__), '..', pre, css_file)
+ else
+ css_file = File.join(options.pres_dir, css_file)
+ end
+ css_content += File.read(css_file)
+ end
+ css_content += '</style>'
+ css_content
+ end
+
+ def inline_js(jses, pre = nil)
+ js_content = '<script type="text/javascript">'
+ jses.each do |js_file|
+ if pre
+ js_file = File.join(File.dirname(__FILE__), '..', pre, js_file)
+ else
+ js_file = File.join(options.pres_dir, js_file)
+ end
+ js_content += File.read(js_file)
+ end
+ js_content += '</script>'
+ js_content
+ end
+
end
get '/' do
erb :index
end
@@ -125,26 +183,23 @@
full_path = File.join(options.pres_dir, path)
send_file full_path
end
get '/slides' do
- index = File.join(options.pres_dir, 'showoff.json')
- files = []
- if File.exists?(index)
- order = JSON.parse(File.read(index))
- order = order.map { |s| s['section'] }
- order.each do |section|
- files << load_section_files(section)
+ get_slides_html
+ end
- end
- files = files.flatten
- files = files.select { |f| f =~ /.md/ }
- data = ''
- files.each do |f|
- fname = f.gsub(options.pres_dir + '/', '').gsub('.md', '')
- data += process_markdown(fname, File.read(f))
- end
- end
- data
+ get '/onepage' do
+ @slides = get_slides_html('preso')
+ erb :onepage
+ end
+
+ get '/pdf' do
+ @slides = get_slides_html('preso')
+ @no_js = true
+ html = erb :onepage
+ p = Princely.new
+ p.pdf_from_string_to_file(html, '/tmp/preso.pdf')
+ send_file '/tmp/preso.pdf'
end
end