lib/slideshow.rb in slideshow-0.7.5 vs lib/slideshow.rb in slideshow-0.7.6
- old
+ new
@@ -13,13 +13,13 @@
# require 'uv'
module Slideshow
- VERSION = '0.7.5'
+ VERSION = '0.7.6'
-class Params
+class ParamsOldDelete
def initialize( name, headers )
@name = name
@headers = headers
end
@@ -193,19 +193,21 @@
@logger = Logger.new(STDOUT)
@logger.level = Logger::INFO
@opts = Opts.new
end
+ # replace w/ attr_reader :logger, :opts ??
+
def logger
@logger
end
def opts
@opts
end
- def check_markdown_libs
+ def load_markdown_libs
# check for available markdown libs/gems
# try to require each lib and remove any not installed
@markdown_libs = []
KNOWN_MARKDOWN_LIBS.each do |lib|
@@ -286,12 +288,12 @@
def load_template( path )
puts " Loading template #{path}..."
return File.read( path )
end
- def render_template( content, b=TOPLEVEL_BINDING )
- ERB.new( content ).result( b )
+ def render_template( content, the_binding )
+ ERB.new( content ).result( the_binding )
end
def load_template_old_delete( name, builtin )
if opts.has_includes?
@@ -354,10 +356,19 @@
dirname = File.dirname( fn )
basename = File.basename( fn, '.*' )
extname = File.extname( fn )
logger.debug "dirname=#{dirname}, basename=#{basename}, extname=#{extname}"
+ # shared variables for templates (binding)
+ @content_for = {} # reset content_for hash
+ @name = basename
+ @headers = @opts
+
+ puts "Preparing slideshow '#{basename}'..."
+
+
+
known_extnames = KNOWN_TEXTILE_EXTNAMES + KNOWN_MARKDOWN_EXTNAMES
if extname.empty? then
extname = ".textile" # default to .textile
@@ -372,22 +383,36 @@
end
inname = "#{dirname}/#{basename}#{extname}"
logger.debug "inname=#{inname}"
+
+ source = File.read( inname )
- content = File.read( inname )
+ # ruby note: .*? is non-greedy (shortest-possible) regex match
+ source.gsub!(/__SKIP__.*?__END__/m, '')
+ source.sub!(/__END__.*/m, '')
+ # allow plugins/helpers; process source (including header) using erb
+
+ # note: include is a ruby keyword; rename to __include__ so we can use it
+ source.gsub!( /<%=[ \t]*include/, '<%= __include__' )
+
+ source = ERB.new( source ).result( binding )
+
+
# todo: read headers before command line options (lets you override options using commandline switch)
# read source document
# strip leading optional headers (key/value pairs) including optional empty lines
read_headers = true
content = ""
+
+ # fix: allow comments in header too (#)
- File.open( inname ).readlines.each do |line|
+ source.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}<"
@@ -409,15 +434,11 @@
content.gsub!( "}}}", "</pre>" )
# restore escaped {{{}}} I'm sure there's a better way! Rubyize this! Anyone?
content.gsub!( "_S9BEGIN_", "{{{" )
content.gsub!( "_S9END_", "}}}" )
- opts.set_defaults
-
- params = Params.new( basename, opts )
-
- puts "Preparing slideshow '#{basename}'..."
+ opts.set_defaults
# convert light-weight markup to hypertext
if KNOWN_MARKDOWN_EXTNAMES.include?( extname )
content = markdown_to_html( content )
@@ -493,15 +514,15 @@
outname = outname.gsub( '__file__', basename )
puts "Preparing #{outname}..."
out = File.new( with_output_path( outname, outpath ), "w+" )
- out << render_template( load_template( entry[1] ), params.params_binding )
+ out << render_template( load_template( entry[1] ), binding )
if entry.size > 2 # more than one source file? assume header and footer with content added inbetween
out << content2
- out << render_template( load_template( entry[2] ), params.params_binding )
+ out << render_template( load_template( entry[2] ), binding )
end
out.flush
out.close
@@ -533,10 +554,31 @@
=end
puts "Done."
end
+def load_plugins
+
+ # use lib folder unless we're in our very own folder
+ # (that use lib for its core functionality), thus, use plugins instead
+ if( File.expand_path( File.dirname(__FILE__) ) == File.expand_path( 'lib' ) )
+ pattern = 'plugins/**/*.rb'
+ else
+ pattern = 'lib/**/*.rb'
+ end
+
+ logger.debug "pattern=#{pattern}"
+
+ Dir.glob( pattern ) do |plugin|
+ begin
+ puts "Loading plugins in '#{plugin}'..."
+ require( plugin )
+ rescue Exception => e
+ puts "** error: failed loading plugins in '#{plugin}': #{e}"
+ end
+ end
+end
def run( args )
opt=OptionParser.new do |cmd|
@@ -599,11 +641,12 @@
puts "Slide Show (S9) Version: #{VERSION} on Ruby #{RUBY_VERSION} (#{RUBY_RELEASE_DATE}) [#{RUBY_PLATFORM}]"
if opts.generate?
create_slideshow_templates
else
- check_markdown_libs
+ load_markdown_libs
+ load_plugins # check for optional plugins/extension in ./lib folder
args.each { |fn| create_slideshow( fn ) }
end
end
@@ -612,7 +655,11 @@
def Slideshow.main
Gen.new.run(ARGV)
end
end # module Slideshow
+
+# load built-in helpers/plugins
+require "#{File.dirname(__FILE__)}/helpers/text_helper.rb"
+require "#{File.dirname(__FILE__)}/helpers/capture_helper.rb"
Slideshow.main if __FILE__ == $0
\ No newline at end of file