slideshow 0.7.5 → 0.7.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,134 @@
1
+ # Originally based on code from Rails and Merb; adapted from Webby.
2
+
3
+ module CaptureHelper
4
+
5
+ # Called in pages and partials to store up content for later use. Takes a
6
+ # string and/or a block. First, the string is evaluated, and then the
7
+ # block is captured using the capture() helper provided by the template
8
+ # languages. The two are concatenated together.
9
+ #
10
+ # Content is retrieved by calling the method without a string or a block.
11
+ #
12
+ # ==== Parameters
13
+ # obj<Object>:: The key in the content_for hash.
14
+ # string<String>:: Textual content. Defaults to nil.
15
+ # &block:: A block to be evaluated and concatenated to string.
16
+ #
17
+ # ==== Returns
18
+ # Any content associated with the key (or nil).
19
+ #
20
+ # ==== Example
21
+ # content_for(:foo, "Foo")
22
+ # content_for(:foo) #=> "Foo"
23
+ # content_for(:foo, "Bar")
24
+ # content_for(:foo) #=> "FooBar"
25
+ #
26
+ def content_for( obj, string = nil, &block )
27
+ return @content_for[obj] unless string || block_given?
28
+
29
+ cur = @content_for[obj].to_s
30
+ new = string.to_s + (block_given? ? capture_erb(&block) : "")
31
+ @content_for[obj] = cur + new
32
+ end
33
+
34
+ # Returns true if there is content for the given key. Otherwise returns
35
+ # false.
36
+ #
37
+ # ==== Parameters
38
+ # obj<Object>:: The key in the conetnt_for hash.
39
+ #
40
+ # ==== Example
41
+ # content_for(:foo, "Foo")
42
+ # content_for?(:foo) #=> true
43
+ # content_for?(:bar) #=> false
44
+ #
45
+ def content_for?( obj )
46
+ @content_for.key?(obj)
47
+ end
48
+
49
+ # Deletes any content associated with the given object in the content_for
50
+ # hash.
51
+ #
52
+ # ==== Parameters
53
+ # obj<Object>:: The key in the conetnt_for hash.
54
+ #
55
+ # ==== Returns
56
+ # Any content associated with the key (or nil).
57
+ #
58
+ # ==== Example
59
+ # content_for(:foo, "Foo")
60
+ # content_for?(:foo) #=> true
61
+ # delete_content_for(:foo)
62
+ # content_for?(:foo) #=> false
63
+ #
64
+ def delete_content_for( obj )
65
+ @content_for.delete(obj)
66
+ end
67
+
68
+ # This method is used to capture content from an ERB filter evaluation. It
69
+ # is useful to helpers that need to process chunks of data during ERB filter
70
+ # processing.
71
+ #
72
+ # ==== Parameters
73
+ # *args:: Arguments to pass to the block.
74
+ # &block:: The ERB block to call.
75
+ #
76
+ # ==== Returns
77
+ # String:: The output of the block.
78
+ #
79
+ # ==== Examples
80
+ # Capture being used in an ERB page:
81
+ #
82
+ # <% @foo = capture_erb do %>
83
+ # <p>Some Foo content!</p>
84
+ # <% end %>
85
+ #
86
+ def capture_erb( *args, &block )
87
+ # get the buffer from the block's binding
88
+ buffer = _erb_buffer(block.binding) rescue nil
89
+
90
+ # If there is no buffer, just call the block and get the contents
91
+ if buffer.nil?
92
+ block.call(*args)
93
+ # If there is a buffer, execute the block, then extract its contents
94
+ else
95
+ pos = buffer.length
96
+ block.call(*args)
97
+
98
+ # extract the block
99
+ data = buffer[pos..-1]
100
+
101
+ # replace it in the original with empty string
102
+ buffer[pos..-1] = ""
103
+
104
+ data
105
+ end
106
+ end
107
+
108
+ # This method is used to concatenate content into the ERB output buffer.
109
+ # It is usefule to helpers that need to insert transformed text back into
110
+ # the ERB output buffer.
111
+ #
112
+ # ==== Parameters
113
+ # string<String>:: The string to insert into the ERB output.
114
+ # the_binding<Binding>:: The binding to pass to the buffer.
115
+ #
116
+ def concat_erb( string, the_binding )
117
+ _erb_buffer(the_binding) << string
118
+ end
119
+
120
+ # Provides direct acccess to the ERB buffer in the conext of the binding.
121
+ #
122
+ # ==== Parameters
123
+ # the_binding<Binding>:: The binding to pass to the buffer.
124
+ #
125
+ # ==== Returns
126
+ # The current ERB output buffer.
127
+ #
128
+ def _erb_buffer( the_binding )
129
+ eval('_erbout', the_binding, __FILE__, __LINE__)
130
+ end
131
+
132
+ end # module CaptureHelper
133
+
134
+ Slideshow::Gen.__send__( :include, CaptureHelper )
@@ -0,0 +1,10 @@
1
+ module TextHelper
2
+
3
+ def __include__( name, opts = {} )
4
+ puts " Including '#{name}'..."
5
+ content = File.read( name )
6
+ end
7
+
8
+ end # module TextHelper
9
+
10
+ Slideshow::Gen.__send__( :include, TextHelper )
@@ -15,9 +15,9 @@ require 'pp'
15
15
 
16
16
  module Slideshow
17
17
 
18
- VERSION = '0.7.5'
18
+ VERSION = '0.7.6'
19
19
 
20
- class Params
20
+ class ParamsOldDelete
21
21
 
22
22
  def initialize( name, headers )
23
23
  @name = name
@@ -195,6 +195,8 @@ class Gen
195
195
  @opts = Opts.new
196
196
  end
197
197
 
198
+ # replace w/ attr_reader :logger, :opts ??
199
+
198
200
  def logger
199
201
  @logger
200
202
  end
@@ -203,7 +205,7 @@ class Gen
203
205
  @opts
204
206
  end
205
207
 
206
- def check_markdown_libs
208
+ def load_markdown_libs
207
209
  # check for available markdown libs/gems
208
210
  # try to require each lib and remove any not installed
209
211
  @markdown_libs = []
@@ -288,8 +290,8 @@ class Gen
288
290
  return File.read( path )
289
291
  end
290
292
 
291
- def render_template( content, b=TOPLEVEL_BINDING )
292
- ERB.new( content ).result( b )
293
+ def render_template( content, the_binding )
294
+ ERB.new( content ).result( the_binding )
293
295
  end
294
296
 
295
297
  def load_template_old_delete( name, builtin )
@@ -356,6 +358,15 @@ class Gen
356
358
  extname = File.extname( fn )
357
359
  logger.debug "dirname=#{dirname}, basename=#{basename}, extname=#{extname}"
358
360
 
361
+ # shared variables for templates (binding)
362
+ @content_for = {} # reset content_for hash
363
+ @name = basename
364
+ @headers = @opts
365
+
366
+ puts "Preparing slideshow '#{basename}'..."
367
+
368
+
369
+
359
370
  known_extnames = KNOWN_TEXTILE_EXTNAMES + KNOWN_MARKDOWN_EXTNAMES
360
371
 
361
372
  if extname.empty? then
@@ -374,8 +385,20 @@ class Gen
374
385
  inname = "#{dirname}/#{basename}#{extname}"
375
386
 
376
387
  logger.debug "inname=#{inname}"
388
+
389
+ source = File.read( inname )
390
+
391
+ # ruby note: .*? is non-greedy (shortest-possible) regex match
392
+ source.gsub!(/__SKIP__.*?__END__/m, '')
393
+ source.sub!(/__END__.*/m, '')
394
+
395
+ # allow plugins/helpers; process source (including header) using erb
396
+
397
+ # note: include is a ruby keyword; rename to __include__ so we can use it
398
+ source.gsub!( /<%=[ \t]*include/, '<%= __include__' )
399
+
400
+ source = ERB.new( source ).result( binding )
377
401
 
378
- content = File.read( inname )
379
402
 
380
403
  # todo: read headers before command line options (lets you override options using commandline switch)
381
404
 
@@ -384,8 +407,10 @@ class Gen
384
407
 
385
408
  read_headers = true
386
409
  content = ""
410
+
411
+ # fix: allow comments in header too (#)
387
412
 
388
- File.open( inname ).readlines.each do |line|
413
+ source.each do |line|
389
414
  if read_headers && line =~ /^\s*(\w[\w-]*)[ \t]*:[ \t]*(.*)/
390
415
  key = $1.downcase
391
416
  value = $2.strip
@@ -411,11 +436,7 @@ class Gen
411
436
  content.gsub!( "_S9BEGIN_", "{{{" )
412
437
  content.gsub!( "_S9END_", "}}}" )
413
438
 
414
- opts.set_defaults
415
-
416
- params = Params.new( basename, opts )
417
-
418
- puts "Preparing slideshow '#{basename}'..."
439
+ opts.set_defaults
419
440
 
420
441
  # convert light-weight markup to hypertext
421
442
 
@@ -495,11 +516,11 @@ class Gen
495
516
 
496
517
  out = File.new( with_output_path( outname, outpath ), "w+" )
497
518
 
498
- out << render_template( load_template( entry[1] ), params.params_binding )
519
+ out << render_template( load_template( entry[1] ), binding )
499
520
 
500
521
  if entry.size > 2 # more than one source file? assume header and footer with content added inbetween
501
522
  out << content2
502
- out << render_template( load_template( entry[2] ), params.params_binding )
523
+ out << render_template( load_template( entry[2] ), binding )
503
524
  end
504
525
 
505
526
  out.flush
@@ -535,6 +556,27 @@ class Gen
535
556
  puts "Done."
536
557
  end
537
558
 
559
+ def load_plugins
560
+
561
+ # use lib folder unless we're in our very own folder
562
+ # (that use lib for its core functionality), thus, use plugins instead
563
+ if( File.expand_path( File.dirname(__FILE__) ) == File.expand_path( 'lib' ) )
564
+ pattern = 'plugins/**/*.rb'
565
+ else
566
+ pattern = 'lib/**/*.rb'
567
+ end
568
+
569
+ logger.debug "pattern=#{pattern}"
570
+
571
+ Dir.glob( pattern ) do |plugin|
572
+ begin
573
+ puts "Loading plugins in '#{plugin}'..."
574
+ require( plugin )
575
+ rescue Exception => e
576
+ puts "** error: failed loading plugins in '#{plugin}': #{e}"
577
+ end
578
+ end
579
+ end
538
580
 
539
581
  def run( args )
540
582
 
@@ -601,7 +643,8 @@ def run( args )
601
643
  if opts.generate?
602
644
  create_slideshow_templates
603
645
  else
604
- check_markdown_libs
646
+ load_markdown_libs
647
+ load_plugins # check for optional plugins/extension in ./lib folder
605
648
 
606
649
  args.each { |fn| create_slideshow( fn ) }
607
650
  end
@@ -615,4 +658,8 @@ end
615
658
 
616
659
  end # module Slideshow
617
660
 
661
+ # load built-in helpers/plugins
662
+ require "#{File.dirname(__FILE__)}/helpers/text_helper.rb"
663
+ require "#{File.dirname(__FILE__)}/helpers/capture_helper.rb"
664
+
618
665
  Slideshow.main if __FILE__ == $0
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: slideshow
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.5
4
+ version: 0.7.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gerald Bauer
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-02-19 00:00:00 +01:00
12
+ date: 2009-02-24 00:00:00 +01:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -41,6 +41,9 @@ extensions: []
41
41
  extra_rdoc_files: []
42
42
 
43
43
  files:
44
+ - lib/helpers
45
+ - lib/helpers/capture_helper.rb
46
+ - lib/helpers/text_helper.rb
44
47
  - lib/slideshow.rb
45
48
  - lib/templates
46
49
  - lib/templates/footer.html.erb