Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fixed up the paginator so that it works properly now. Resources deter…
…mine

their own destination filename based on the current page number.

A "cairn" file is used to mark when the content was last compiled into the
output directory. The mtime of the output directory is modified when a file
is added; this was causing issues with layout modifications.
  • Loading branch information
TwP committed Oct 15, 2007
1 parent 7d1606f commit 2ea5274
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 31 deletions.
12 changes: 12 additions & 0 deletions lib/webby.rb
Expand Up @@ -10,6 +10,7 @@
Logging::Logger['Webby'].add(Logging::Appender.stdout)
Logging::Logger['Webby'].level = :info


module Webby

VERSION = '0.5.0' # :nodoc:
Expand Down Expand Up @@ -73,6 +74,17 @@ def self.exclude
@exclude ||= Regexp.new(config['exclude'].join('|'))
end

# call-seq:
# cairn => filename
#
# The Webby _cairn_ file is used to mark the last time the content was
# built into the output directory. It is an empty file; only the
# modification time of the file is important.
#
def self.cairn
@cairn ||= File.join(config['output_dir'], '.cairn')
end

end # module Webby


Expand Down
7 changes: 3 additions & 4 deletions lib/webby/builder.rb
Expand Up @@ -107,11 +107,10 @@ def run( opts = {} )
else Renderer.write(page) end
end

# touch the output directory so we know when the
# website was last generated
FileUtils.touch output_dir
# touch the cairn so we know when the website was last generated
FileUtils.touch ::Webby.cairn

return nil
nil
end


Expand Down
23 changes: 9 additions & 14 deletions lib/webby/renderer.rb
Expand Up @@ -38,7 +38,7 @@ def self.write( page )
fd.write renderer.layout_page
end
break unless renderer.next_page
}
}
end

# call-seq:
Expand Down Expand Up @@ -121,31 +121,26 @@ def render_page
# number of items in the current page.
#
def paginate( items, count, &block )
@pager ||= Paginator.new(items.length, count) do |offset, per_page|
@pager ||= Paginator.new(items.length, count, @page) do |offset, per_page|
items[offset,per_page]
end.first

@pager.each &block

rescue NameError
@log.error 'pagination failed (Paginator not installed?)'
exit
end

# call-seq:
# next_page
# next_page => true or false
#
def next_page
return false unless defined? @pager and @pager

# go to the next page; break out if there is no next page
@pager = @pager.next
return false if @pager.nil?

# set filename based on pager number
fn = "%s%d" % [@page.filename, @pager.number]
@page.instance_variable_set :@filename, fn
@page.instance_variable_set :@dest, nil
if @pager.next?
@pager = @pager.next
else
@page.number = nil
return false
end

true
end
Expand Down
19 changes: 18 additions & 1 deletion lib/webby/resource.rb
Expand Up @@ -54,6 +54,9 @@ def clear
# Resource file modification time
attr_reader :mtime

# Resource page number (if needed)
attr_reader :number

# call-seq:
# Resource.new( filename ) => resource
#
Expand All @@ -66,6 +69,7 @@ def initialize( fn )
@ext = ::File.extname(@path).sub(%r/\A\.?/o, '')
@mtime = ::File.mtime @path

@number = nil
@rendering = false

# deal with the meta-data
Expand Down Expand Up @@ -139,17 +143,30 @@ def extension
#
def destination
return @dest if defined? @dest and @dest
return @dest = ::Webby.config['output_dir'] if is_layout?
return @dest = ::Webby.cairn if is_layout?

@dest = if @mdata.has_key? 'destination' then @mdata['destination']
else File.join(dir, filename) end

@dest = File.join(::Webby.config['output_dir'], @dest)
@dest << @number.to_s if @number
@dest << '.'
@dest << extension
@dest
end

# call-seq:
# resource.number = Integer
#
# Sets the page number for the current resource to the given integer. This
# number is used to modify the output destination for resources that
# require pagination.
#
def number=( num )
@number = num
@dest = nil
end

# call-seq:
# render => string
#
Expand Down
25 changes: 18 additions & 7 deletions lib/webby/stelan/paginator.rb
@@ -1,16 +1,24 @@
# This code was originally written by Bruce Williams, and it is available
# as the Paginator gem. I've added a few helper methods and modifications so
# it plays a little more nicely with Webby. Specifically, a Webby::Resource
# can be given to the Page and used to generate links to the previous and
# next pages.
#
# Many thanks to Bruce Williams for letting me use his work. Drop him a note
# of praise scribbled on the back of a $100 bill. He'd appreciate it.

require 'forwardable'

module Webby
class Paginator

VERSION = '1.1.0'

include Enumerable

class ArgumentError < ::ArgumentError; end
class MissingCountError < ArgumentError; end
class MissingSelectError < ArgumentError; end

attr_reader :per_page, :count
attr_reader :per_page, :count, :resource

# Instantiate a new Paginator object
#
Expand All @@ -21,8 +29,8 @@ class MissingSelectError < ArgumentError; end
# * The block is passed the item offset
# (and the number of items to show per page, for
# convenience, if the arity is 2)
def initialize(count, per_page, &select)
@count, @per_page = count, per_page
def initialize(count, per_page, resource, &select)
@count, @per_page, @resource = count, per_page, resource
unless select
raise MissingSelectError, "Must provide block to select data for each page"
end
Expand Down Expand Up @@ -75,6 +83,8 @@ def initialize(pager, number, select) #:nodoc:
@pager, @number = pager, number
@offset = (number - 1) * pager.per_page
@select = select

@pager.resource.number = number
end

# Retrieve the items for this page
Expand Down Expand Up @@ -132,7 +142,8 @@ def method_missing(meth, *args, &block) #:nodoc:
super
end
end

end

end
end # class Paginator
end # module Webby
10 changes: 5 additions & 5 deletions website/layouts/default.rhtml
Expand Up @@ -29,11 +29,11 @@ filter: erb
<h1>Webby &#9775; <%= h(@page.title) %></h1>
<hr />
<ul id="navigation"
><li><a href="index.html">Home</a>&middot;</li
><li><a href="download.html">Download</a>&middot;</li
><li><a href="tutorial.html">Tutorial</a>&middot;</li
><li><a href="manual.html">Manual</a>&middot;</li
><li><a href="tips_and_tricks.html">Tips &amp; Tricks</a></li
><li><a href="/index.html">Home</a>&middot;</li
><li><a href="/download.html">Download</a>&middot;</li
><li><a href="/tutorial.html">Tutorial</a>&middot;</li
><li><a href="/manual.html">Manual</a>&middot;</li
><li><a href="/tips_and_tricks.html">Tips &amp; Tricks</a></li
></ul>
</div>

Expand Down

0 comments on commit 2ea5274

Please sign in to comment.