lib/showoff.rb in showoff-0.2.2 vs lib/showoff.rb in showoff-0.2.3
- old
+ new
@@ -11,17 +11,16 @@
rescue LoadError
puts 'image sizing disabled - install RMagick'
end
begin
- require 'prawn'
require 'princely'
rescue LoadError
- puts 'pdf generation disabled - install prawn'
+ puts 'pdf generation disabled - install princely'
end
-begin
+begin
require 'rdiscount'
rescue LoadError
require 'bluecloth'
Markdown = BlueCloth
end
@@ -32,11 +31,11 @@
attr_reader :cached_image_size
set :views, File.dirname(__FILE__) + '/../views'
set :public, File.dirname(__FILE__) + '/../public'
set :pres_dir, 'example'
-
+
def initialize(app=nil)
super(app)
puts dir = File.expand_path(File.join(File.dirname(__FILE__), '..'))
if Dir.pwd == dir
options.pres_dir = dir + '/example'
@@ -75,11 +74,11 @@
end
slides.each do |slide|
md = ''
# extract content classes
lines = slide.split("\n")
- content_classes = lines.shift.split
+ content_classes = lines.shift.split rescue []
slide = lines.join("\n")
# add content class too
content_classes.unshift "content"
# extract transition, defaulting to none
transition = 'none'
@@ -92,11 +91,11 @@
md += "<div class=\"#{content_classes.join(' ')}\" ref=\"#{name}/#{seq.to_s}\">\n"
seq += 1
else
md += "<div class=\"#{content_classes.join(' ')}\" ref=\"#{name}\">\n"
end
- sl = Markdown.new(slide).to_html
+ sl = Markdown.new(slide).to_html
sl = update_image_paths(name, sl, static)
md += sl
md += "</div>\n"
md += "</div>\n"
final += update_commandline_code(md)
@@ -141,11 +140,11 @@
end
end
def update_commandline_code(slide)
html = Nokogiri::XML.parse(slide)
-
+
html.css('pre').each do |pre|
pre.css('code').each do |code|
out = code.text
lines = out.split("\n")
if lines.first[0, 3] == '@@@'
@@ -174,18 +173,16 @@
code << c
end
end
html.root.to_s
end
-
+
def get_slides_html(static=false)
- index = File.join(options.pres_dir, ShowOffUtils::SHOWOFF_JSON_FILE )
+ sections = ShowOffUtils.showoff_sections(options.pres_dir)
files = []
- if File.exists?(index)
- order = JSON.parse(File.read(index))
- order = order.map { |s| s['section'] }
- order.each do |section|
+ if sections
+ sections.each do |section|
files << load_section_files(section)
end
files = files.flatten
files = files.select { |f| f =~ /.md/ }
data = ''
@@ -199,13 +196,13 @@
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)
+ css_file = File.join(File.dirname(__FILE__), '..', pre, css_file)
else
- css_file = File.join(options.pres_dir, css_file)
+ css_file = File.join(options.pres_dir, css_file)
end
css_content += File.read(css_file)
end
css_content += '</style>'
css_content
@@ -213,28 +210,59 @@
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)
+ js_file = File.join(File.dirname(__FILE__), '..', pre, js_file)
else
- js_file = File.join(options.pres_dir, js_file)
+ js_file = File.join(options.pres_dir, js_file)
end
js_content += File.read(js_file)
end
js_content += '</script>'
js_content
end
-
+
def index(static=false)
if static
@slides = get_slides_html(static)
@asset_path = "."
end
erb :index
end
+ def clean_link(href)
+ if href && href[0, 1] == '/'
+ href = href[1, href.size]
+ end
+ href
+ end
+
+ def assets_needed
+ assets = ["index", "slides"]
+
+ index = erb :index
+ html = Nokogiri::XML.parse(index)
+ html.css('head link').each do |link|
+ href = clean_link(link['href'])
+ assets << href if href
+ end
+ html.css('head script').each do |link|
+ href = clean_link(link['src'])
+ assets << href if href
+ end
+
+ slides = get_slides_html
+ html = Nokogiri::XML.parse("<slides>" + slides + "</slides>")
+ html.css('img').each do |link|
+ href = clean_link(link['src'])
+ assets << href if href
+ end
+
+ assets.join("\n")
+ end
+
def slides(static=false)
get_slides_html(static)
end
def onepage(static=false)
@@ -251,15 +279,15 @@
p.pdf_from_string_to_file(html, '/tmp/preso.pdf')
File.new('/tmp/preso.pdf')
end
end
-
-
+
+
def self.do_static(what)
what = "index" if !what
-
+
# Nasty hack to get the actual ShowOff module
showoff = ShowOff.new
while !showoff.is_a?(ShowOff)
showoff = showoff.instance_variable_get(:@app)
end
@@ -280,38 +308,39 @@
my_path = File.join( File.dirname(__FILE__), '..', 'public')
["js", "css"].each { |dir|
FileUtils.copy_entry("#{my_path}/#{dir}", "#{out}/#{dir}")
}
# And copy the directory
- Dir.glob("#{my_path}/#{name}/*").each { |subpath|
+ Dir.glob("#{my_path}/#{name}/*").each { |subpath|
base = File.basename(subpath)
next if "static" == base
next unless File.directory?(subpath) || base.match(/\.(css|js)$/)
FileUtils.copy_entry(subpath, "#{out}/#{base}")
}
end
end
-
+
get %r{(?:image|file)/(.*)} do
path = params[:captures].first
full_path = File.join(options.pres_dir, path)
send_file full_path
end
get %r{/(.*)} do
+ @title = 'testing'
what = params[:captures].first
- what = 'index' if "" == what
+ what = 'index' if "" == what
if (what != "favicon.ico")
data = send(what)
if data.is_a?(File)
send_file data.path
else
data
end
end
end
-
+
end