<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array">
    <added>
      <filename>app/models/page.rb</filename>
    </added>
    <added>
      <filename>spec/models/page_spec.rb</filename>
    </added>
  </added>
  <modified type="array">
    <modified>
      <diff>@@ -9,26 +9,14 @@ class Pages &lt; Application
   end
   
   def show
-    @chapter   = params[:chapter]
-    @page_name = params[:page_name]
-    
-    @page_file = find_page_file
-    raise NotFound unless @page_file    
-    
-    text = File.open(@page_file).read
-    render Maruku::new(text).to_html
+    @page = Page.new( :name     =&gt; params[:page_name],
+                      :chapter  =&gt; params[:chapter],
+                      :language =&gt; language)
+    raise NotFound unless @page.file
+    render @page.to_html
   end
   
   private
-    # If no page name is passed, the first page is returned.
-    def find_page_file(format=&quot;markdown&quot;)
-      base = &quot;#{Merb.root}/book-content/#{language}/*-#{@chapter}&quot;
-      if @page_name
-        Dir[&quot;#{base}/*-#{@page_name}.#{format}&quot;].entries.first
-      else
-        Dir[&quot;#{base}/toc.#{format}&quot;].entries.first
-      end
-    end
     
     def find_toc(format=&quot;markdown&quot;)
       Dir[&quot;#{Merb.root}/book-content/#{language}/toc.#{format}&quot;].entries.first</diff>
      <filename>app/controllers/pages.rb</filename>
    </modified>
    <modified>
      <diff>@@ -24,29 +24,28 @@ module Merb
     def page_nav_links(format = 'markdown')
       return if params[:action] != 'show' # Don't need navigation for the TOC (index).
       links = []
-      @current_file =~ /book-content\/\w{2}\/(\d{1,})-[a-z-]+\/(\d{1,})-[a-z-]+[.]\w+/
-      chapter_number, page_number = $1, $2
-
-      chapter_name, page_name = extract_previous_page(chapter_number, page_number)
-      links &lt;&lt; previous_page(chapter_name, page_name)
-
+      links &lt;&lt; previous_page_url
       # Stick a link to the TOC in the middle of the array.
       links &lt;&lt; link_to(language_text(language, 'Home'), url(:toc, :language =&gt; language))
-
-      chapter_name, page_name = extract_next_page(chapter_number, page_number)
-      links &lt;&lt; next_page(chapter_name, page_name)
-
+      links &lt;&lt; next_page_url
       links.join(' | ')
     end
     
     # returns the table of contents link
     def toc_link
-      return unless params[:action] == 'show' # Don't need navigation for the TOC (index).
+      return unless action_name == 'show' # Don't need navigation for the TOC (index).
       link_to(language_text(language, 'Table of Contents'), url(:toc, :language =&gt; language))
     end
+    
+    def next_page_url
+      link_to(language_text(language, 'Next'), url(:page, :language =&gt; language, :chapter =&gt; @page.next_chapter_name, :page_name =&gt; @page.next_page_name))
+    end
 
-    private
+    def previous_page_url
+      link_to(language_text(language, 'Previous'), url(:page, :language =&gt; language, :chapter =&gt; @page.previous_chapter_name, :page_name =&gt; @page.previous_page_name))
+    end
 
+    private
     # returns the translated text based on the current language
     def language_text(language_code, text)
       result = text
@@ -56,56 +55,5 @@ module Merb
       result ||= text
     end
 
-    def next_page(chapter_name, page_name)
-      link_to(language_text(language, 'Next'), url(:page, :language =&gt; language, :chapter =&gt; chapter_name, :page_name =&gt; page_name))
-    end
-
-    def previous_page(chapter_name, page_name)
-      link_to(language_text(language, 'Previous'), url(:page, :language =&gt; language, :chapter =&gt; chapter_name, :page_name =&gt; page_name))
-    end
-
-    # This method returns a Regexp which contains match captures for the chapter and page names.
-    def chapter_and_page_names
-      /book-content\/\w{2}\/\d{1,}-([a-z-]+)\/\d{1,}-([a-z-]+)[.]\w+/
-    end
-
-    # Returns an array of the next chapter and page names.
-    def extract_next_page(chapter_number, page_number, format = 'markdown')
-      next_file = Dir[&quot;#{Merb.root}/book-content/#{language}/#{chapter_number}-*/#{page_number.to_i + 1}-*.#{format}&quot;].entries.first
-      if next_file
-        next_file.grep(chapter_and_page_names)
-      else
-        # We're on the last page, get the first page of the next chapter.
-        next_file = Dir[&quot;#{Merb.root}/book-content/#{language}/#{chapter_number.to_i + 1}-*/**&quot;].entries.first
-        if next_file
-          next_file.grep(chapter_and_page_names)
-        else
-          # We're on the last page of the last chapter, just return the TOC.
-          return 'table-of-contents'
-        end
-      end
-      chapter_name, page_name = $1, $2
-      [chapter_name, page_name]
-    end
-
-    # Returns an array of the previous chapter and page names.
-    def extract_previous_page(chapter_number, page_number, format = 'markdown')
-      next_file = Dir[&quot;#{Merb.root}/book-content/#{language}/#{chapter_number}-*/#{page_number.to_i - 1}-*.#{format}&quot;].entries.first
-      if next_file
-        next_file.grep(chapter_and_page_names)
-      else
-        # We're on the first page, get the last page of the previous chapter.
-        next_file = Dir[&quot;#{Merb.root}/book-content/#{language}/#{chapter_number.to_i - 1}-*/**&quot;].entries.last
-        if next_file
-          next_file.grep(chapter_and_page_names)
-        else
-          # We're on the first page of the first chapter, just return the TOC.
-          return 'table-of-contents'
-        end
-      end
-      chapter_name, page_name = $1, $2
-      [chapter_name, page_name]
-    end
-
   end
 end</diff>
      <filename>app/helpers/global_helpers.rb</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>077410c854a8eacc3965ab6fd1633c8b9b8bb11c</id>
    </parent>
  </parents>
  <author>
    <name>Matt Aimonetti</name>
    <email>mattaimonetti@gmail.com</email>
  </author>
  <url>http://github.com/mattetti/merb-book/commit/c0594ee163156eadd6bccf56071ea260570992fd</url>
  <id>c0594ee163156eadd6bccf56071ea260570992fd</id>
  <committed-date>2009-01-16T22:16:04-08:00</committed-date>
  <authored-date>2009-01-16T22:16:04-08:00</authored-date>
  <message>started refactoring the page controller and helpers</message>
  <tree>6d223c671588b8a3a644d733b25308e807ced0f7</tree>
  <committer>
    <name>Matt Aimonetti</name>
    <email>mattaimonetti@gmail.com</email>
  </committer>
</commit>
