lib/slideshow.rb in slideshow-0.2 vs lib/slideshow.rb in slideshow-0.3

- old
+ new

@@ -1,235 +1,171 @@ require 'optparse' +require 'erb' require 'RedCloth' +require 'BlueCloth' +require 'logger' + module Slideshow -def Slideshow.create_slideshow( fn ) - - -gradient = <<EOS -<svg xmlns="http://www.w3.org/2000/svg"> +class Params - <defs> - <linearGradient id="dark" x1="0" y1="0" x2="1" y2="1"> - <stop offset="0" style="stop-color: red"/> - <stop offset="1" style="stop-color: black"/> - </linearGradient> + def initialize( title, name ) + @title = title + @svgname = "#{name}.svg" + @cssname = "#{name}.css" + end - <linearGradient id="dark_reverse" x1="0" y1="0" x2="1" y2="1"> - <stop offset="0" style="stop-color: black"/> - <stop offset="1" style="stop-color: red"/> - </linearGradient> - - <linearGradient id="light" x1="0" y1="0" x2="1" y2="1"> - <stop offset="0" style="stop-color: red"/> - <stop offset="1" style="stop-color: orange"/> - </linearGradient> - - <linearGradient id="top_bottom" x1="0" y1="0" x2="0" y2="1"> - <stop offset="0%" style="stop-color: red" /> - <stop offset="100%" style="stop-color: black" /> - </linearGradient> + def params_binding + binding + end - <linearGradient id="left_right" x1="0" y1="0" x2="1" y2="0"> - <stop offset="0%" style="stop-color: red" /> - <stop offset="100%" style="stop-color: orange" /> - </linearGradient> +end - <linearGradient id="repeat" x1="0.4" y1="0.4" x2="0.5" y2="0.5" - spreadMethod="repeat"> - <stop offset="0%" style="stop-color: red" /> - <stop offset="50%" style="stop-color: orange" /> - <stop offset="100%" style="stop-color: red" /> - </linearGradient> +def Slideshow.load_template( name ) - <radialGradient id="radial"> - <stop offset="0%" style="stop-color: black" /> - <stop offset="100%" style="stop-color: red" /> - </radialGradient> - + templatesdir = "#{File.dirname(__FILE__)}/templates" + logger.debug "templatesdir=#{templatesdir}" - <radialGradient id="radial_off_center" fx="0.7" fy="0.7" cx="0.5" cy="0.5" r="0.4"> - <stop offset="0%" style="stop-color: orange" /> - <stop offset="100%" style="stop-color: red" /> - </radialGradient> + File.read( "#{templatesdir}/#{name}" ) +end - <radialGradient id="radial_repeat" fx="0.5" fy="0.5" cx="0.6" cy="0.6" r="0.2" - spreadMethod="repeat"> - <stop offset="0%" style="stop-color: red" /> - <stop offset="50%" style="stop-color: orange" /> - <stop offset="100%" style="stop-color: red" /> - </radialGradient> - - </defs> - - <rect width="100%" height="100%" - style="fill: url(#dark) "/> - -</svg> -EOS +def Slideshow.render_template( content, b=TOPLEVEL_BINDING ) + ERB.new( content ).result( b ) +end -header = <<EOS -<html> - <head> - <meta name="slideselector" content=".slide"> - <meta name="titleselector" content="h1"> - <meta name="stepselector" content=".step"> +def Slideshow.create_slideshow( fn ) -<title>Slideshow</title> + headerdoc = load_template( 'header.html.erb' ) + footerdoc = load_template( 'footer.html.erb' ) + styledoc = load_template( 'style.css.erb' ) + gradientdoc = load_template( 'gradient.svg.erb' ) - <style type="text/css"> - -@media screen { - .layout { display: none; } - - .banner { - display: block; - border: green solid thick; - padding: 1em; - font-family: sans-serif; - font-weight: bold; - margin-bottom: 2em; - } -} - -@media projection { - -body -{ - height: 100%; margin: 0px; padding: 0px; - font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; - color: white; - opacity: .99; -} - -.slide -{ - page-break-after: always; - padding-left: 2em; - padding-top: 2em; -} - -.banner -{ - display: none; -} - -.layout -{ - display: block; -} - -div.background { - position: fixed; - left: 0px; - right: 0px; - top: 0px; - bottom: 0px; - z-index: -1; - } - -a:link, a:visited { - color: white; -} -a:hover { background-color: yellow; } - -h1, h2 { font-size: 36pt; } -h3 { font-size: 25pt; } -p, li, td, th { font-size: 18pt; } - -pre { font-size: 16pt; } - -pre.code { font-size: 16pt; - background-color: black; - color: white; - padding: 5px; - border: silver thick groove; - -moz-border-radius: 11px; - } -} - - </style> - - - </head> - <body> - -<div class="layout"> - <div class="background"> - <object data="$svgname" width="100%" height="100%"> - </div> -</div> - -<div class="banner"> - Turn this document into a (PowerPoint/KeyNote-style) slide show pressing F11. - (Free <a href="https://addons.mozilla.org/en-US/firefox/addon/4650">FullerScreen</a> Firefox addon required). - Learn more at the <a href="http://slideshow.rubyforge.org">Slide Show (S9)</a> - RubyForge project site. - </div> -EOS - - footer = <<EOS -</body> -</html> -EOS - basename = File.basename( fn, '.*' ) extname = File.extname( fn ) - extname = ".textile" if extname.eql?("") + params = Params.new( "Slideshow", basename ) + known_textile_extnames = [ '.textile', '.t' ] + known_markdown_extnames = [ '.markdown', '.mark', '.m', '.txt', '.text' ] + known_extnames = known_textile_extnames + known_markdown_extnames + + if extname.eql?("") then + extname = ".textile" # default to .textile + + known_extnames.each { |e| + logger.debug "File.exists? #{basename}#{e}" + if File.exists?( "#{basename}#{e}" ) then + extname = e + logger.debug "extname=#{extname}" + break + end + } + end + inname = "#{basename}#{extname}" outname = "#{basename}.html" svgname = "#{basename}.svg" + cssname = "#{basename}.css" + logger.debug "inname=#{inname}" + + puts "Preparing slideshow stylesheet '#{cssname}'..." + + out = File.new( cssname, "w+" ) + out << render_template( styledoc, params.params_binding ) + out.flush + out.close + puts "Preparing slideshow theme '#{svgname}'..." - out = File.new( svgname, "w+") - out << gradient + out = File.new( svgname, "w+" ) + out << render_template( gradientdoc, params.params_binding ) out.flush out.close puts "Preparing slideshow '#{outname}'..." - content = '' - slide_counter = 0 + # convert light-weight markup to hypertext + + content = File.read( inname ) + + if known_markdown_extnames.include?( extname ) + content = BlueCloth.new( content ).to_html + else + content = RedCloth.new( content ).to_html + end + - infile = File.new( inname ) - infile.each { |line| - if line.include?( 'h1.' ) then - content << "\n\n</div>" if slide_counter > 0 - content << "<div class='slide'>\n\n" + # post-processing + + slide_counter = 0 + content2 = '' + + # wrap h1's in slide divs + content.each_line { |line| + if line.include?( '<h1>' ) then + content2 << "\n\n</div>" if slide_counter > 0 + content2 << "<div class='slide'>\n\n" slide_counter += 1 end - content << line + content2 << line } + content2 << "\n\n</div>" if slide_counter > 0 - content << "\n\n</div>" if slide_counter > 0 - - out = File.new( outname, "w+") - out << header.gsub( '$svgname', svgname ) - out << RedCloth.new( content ).to_html - out << footer + out = File.new( outname, "w+" ) + out << render_template( headerdoc, params.params_binding ) + out << content2 + out << render_template( footerdoc, params.params_binding ) out.flush out.close puts "Done." end +def Slideshow.logger + if @@logger.nil? + @@logger = Logger.new(STDOUT) + end + @@logger +end def Slideshow.main + @@logger = nil $options = {} + logger.level = Logger::INFO + opt=OptionParser.new do |opts| opts.banner = "Usage: slideshow [options] name" # opts.on( "-s", "--style STYLE", "Select Stylesheet" ) { |s| $options[:style]=s } - opts.on_tail( "-h", "--help", "Show this message" ) { puts opts.help; exit } + # opts.on( "-v", "--version", "Show version" ) {} + opts.on( "-t", "--trace", "Show debug trace" ) { + logger.datetime_format = "%H:%H:%S" + logger.level = Logger::DEBUG + } + opts.on_tail( "-h", "--help", "Show this message" ) { + puts + puts "Slide Show (S9) is a free web alternative to PowerPoint or KeyNote in Ruby" + puts + puts opts.help + puts + puts "Examples:" + puts " slideshow microformats" + puts " slideshow microformats.textile" + puts + puts "Further information:" + puts " http://slideshow.rubyforge.org" + exit + } end opt.parse! + + puts "Slide Show (S9) Version: 0.3 on Ruby #{RUBY_VERSION} (#{RUBY_RELEASE_DATE}) [#{RUBY_PLATFORM}]" + ARGV.each { |fn| Slideshow.create_slideshow( fn ) } end end \ No newline at end of file