lib/slideshow.rb in slideshow-0.3.1 vs lib/slideshow.rb in slideshow-0.4

- old
+ new

@@ -1,10 +1,13 @@ require 'optparse' require 'erb' require 'redcloth' require 'maruku' require 'logger' +require 'fileutils' +require 'hpricot' +require 'uv' module Slideshow class Params @@ -19,10 +22,24 @@ binding end end +def Slideshow.cache_dir + PLATFORM =~ /win32/ ? win32_cache_dir : File.join(File.expand_path("~"), ".slideshow") +end + +def Slideshow.win32_cache_dir + unless File.exists?(home = ENV['HOMEDRIVE'] + ENV['HOMEPATH']) + puts "No HOMEDRIVE or HOMEPATH environment variable. Set one to save a" + + "local cache of stylesheets for syntax highlighting and more." + return false + else + return File.join(home, 'slideshow') + end +end + def Slideshow.load_template( name ) templatesdir = "#{File.dirname(__FILE__)}/templates" logger.debug "templatesdir=#{templatesdir}" @@ -67,17 +84,33 @@ outname = "#{basename}.html" svgname = "#{basename}.svg" cssname = "#{basename}.css" logger.debug "inname=#{inname}" + + content = File.read( inname ) + + # read source document + # strip leading optional headers (key/value pairs) including optional empty lines - puts "Preparing slideshow stylesheet '#{cssname}'..." + read_headers = true + content = "" - out = File.new( cssname, "w+" ) - out << render_template( styledoc, params.params_binding ) - out.flush - out.close + File.open( inname ).readlines.each do |line| + if read_headers && line =~ /^\s*(\w[\w-]*)[ \t]*:[ \t]*(.*)/ + key = $1.downcase + value = $2.strip + + logger.debug " adding option: key=>#{key}< value=>#{value}<" + store_option( key, value ) + elsif line =~ /^\s*$/ + content << line unless read_headers + else + read_headers =false + content << line + end + end puts "Preparing slideshow theme '#{svgname}'..." out = File.new( svgname, "w+" ) out << render_template( gradientdoc, params.params_binding ) @@ -85,12 +118,10 @@ out.close puts "Preparing slideshow '#{outname}'..." # convert light-weight markup to hypertext - - content = File.read( inname ) if known_markdown_extnames.include?( extname ) content = Maruku.new( content, {:on_error => :raise} ).to_html # content = BlueCloth.new( content ).to_html else @@ -112,27 +143,93 @@ end content2 << line } content2 << "\n\n</div>" if slide_counter > 0 + include_code_stylesheet = false + # syntax highlight code + # todo: can the code handle escaped entities? e.g. &gt; + doc = Hpricot(content2) + doc.search("pre.code, pre > code").each do |e| + if e.inner_html =~ /^\s*#!(\w+)/ + lang = $1.downcase + logger.debug " lang=#{lang}" + if Uv.syntaxes.include?(lang) + e.inner_html = Uv.parse( e.inner_html.sub(/^\s*#!\w+/, '').strip, "xhtml", lang, get_boolean_option( 'code-line-numbers', true ), get_option( 'code-theme', 'amy' )) + include_code_stylesheet = true + end + end + end + + content2 = doc.to_s + + 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 "Preparing slideshow stylesheet '#{cssname}'..." + + out = File.new( cssname, "w+" ) + out << render_template( styledoc, params.params_binding ) + + if include_code_stylesheet + logger.debug "cache_dir=#{cache_dir}" + + FileUtils.mkdir(cache_dir) unless File.exists?(cache_dir) if cache_dir + Uv.copy_files "xhtml", cache_dir + + theme = get_option( 'code-theme', 'amy' ) + + theme_content = File.read( "#{cache_dir}/css/#{theme}.css" ) + + out << "/* styles for code syntax highlighting theme '#{theme}' */\n" + out << "\n" + out << theme_content + end + + out.flush + out.close + + puts "Done." end def Slideshow.logger if @@logger.nil? @@logger = Logger.new(STDOUT) end @@logger end + + +def Slideshow.store_option( key, value ) + $options[ key ] = value +end + +def Slideshow.get_option( key, default ) + value = $options[ key ] + value.nil? ? default : value +end + +def Slideshow.get_boolean_option( key, default ) + value = $options[ key ] + if value.nil? + default + else + if( value.downcase == 'true' || value.downcase == 'yes' || value.downcase == 'on' ) + true + else + false + end + end +end + def Slideshow.main @@logger = nil $options = {} @@ -162,10 +259,10 @@ } end opt.parse! - puts "Slide Show (S9) Version: 0.3.1 on Ruby #{RUBY_VERSION} (#{RUBY_RELEASE_DATE}) [#{RUBY_PLATFORM}]" + puts "Slide Show (S9) Version: 0.4 on Ruby #{RUBY_VERSION} (#{RUBY_RELEASE_DATE}) [#{RUBY_PLATFORM}]" ARGV.each { |fn| Slideshow.create_slideshow( fn ) } end end \ No newline at end of file