This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

started refactoring the page controller and helpers

  • Loading branch information...
mattetti committed Jan 17, 2009
1 parent 077410c commit c0594ee163156eadd6bccf56071ea260570992fd
Showing with 172 additions and 80 deletions.
  1. +5 −17 app/controllers/pages.rb
  2. +11 −63 app/helpers/global_helpers.rb
  3. +112 −0 app/models/page.rb
  4. +44 −0 spec/models/page_spec.rb
View
@@ -9,26 +9,14 @@ def index
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 => params[:page_name],
+ :chapter => params[:chapter],
+ :language => 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="markdown")
- base = "#{Merb.root}/book-content/#{language}/*-#{@chapter}"
- if @page_name
- Dir["#{base}/*-#{@page_name}.#{format}"].entries.first
- else
- Dir["#{base}/toc.#{format}"].entries.first
- end
- end
def find_toc(format="markdown")
Dir["#{Merb.root}/book-content/#{language}/toc.#{format}"].entries.first
@@ -24,29 +24,28 @@ def rtl_support_class
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 << previous_page(chapter_name, page_name)
-
+ links << previous_page_url
# Stick a link to the TOC in the middle of the array.
links << link_to(language_text(language, 'Home'), url(:toc, :language => language))
-
- chapter_name, page_name = extract_next_page(chapter_number, page_number)
- links << next_page(chapter_name, page_name)
-
+ links << 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 => language))
end
+
+ def next_page_url
+ link_to(language_text(language, 'Next'), url(:page, :language => language, :chapter => @page.next_chapter_name, :page_name => @page.next_page_name))
+ end
- private
+ def previous_page_url
+ link_to(language_text(language, 'Previous'), url(:page, :language => language, :chapter => @page.previous_chapter_name, :page_name => @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 @@ def language_text(language_code, text)
result ||= text
end
- def next_page(chapter_name, page_name)
- link_to(language_text(language, 'Next'), url(:page, :language => language, :chapter => chapter_name, :page_name => page_name))
- end
-
- def previous_page(chapter_name, page_name)
- link_to(language_text(language, 'Previous'), url(:page, :language => language, :chapter => chapter_name, :page_name => 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["#{Merb.root}/book-content/#{language}/#{chapter_number}-*/#{page_number.to_i + 1}-*.#{format}"].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["#{Merb.root}/book-content/#{language}/#{chapter_number.to_i + 1}-*/**"].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["#{Merb.root}/book-content/#{language}/#{chapter_number}-*/#{page_number.to_i - 1}-*.#{format}"].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["#{Merb.root}/book-content/#{language}/#{chapter_number.to_i - 1}-*/**"].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
View
@@ -0,0 +1,112 @@
+class Page
+
+ attr_accessor :file, :name, :chapter, :language
+ attr_reader :chapter_number, :page_number, :next_file, :previous_file
+ attr_reader :previous_file, :next_file
+
+ def initialize(args={})
+ @name = args[:name]
+ @chapter = args[:chapter]
+ @language = args[:language] || 'en'
+ end
+
+ def file
+ @file ||= find_page_file
+ end
+
+ def text
+ @text ||= File.open(file).read
+ end
+
+ def to_html
+ @html ||= ::Maruku::new(text).to_html
+ end
+
+ def chapter_number
+ return @chapter_number unless @chapter_number.nil?
+ extract_chapter_and_page_number
+ @chapter_number
+ end
+
+ def page_number
+ return @page_number unless @page_number.nil?
+ extract_chapter_and_page_number
+ @page_number
+ end
+
+ def next_file
+ return @next_file unless @next_file.nil?
+ n_file = Dir["#{Merb.root}/book-content/#{language}/#{chapter_number}-*/#{page_number + 1}-*.*"].entries.first
+ if n_file.nil?
+ n_file = Dir["#{Merb.root}/book-content/#{language}/#{chapter_number + 1}-*/**"].entries.first
+ # We're on the last page of the last chapter, just return the TOC.
+ n_file = "#{Merb.root}/book-content/#{language}/table-of-contents" if n_file.nil?
+ end
+ @next_file = n_file
+ end
+
+ def next_chapter_name
+ return @next_chapter_name unless @next_chapter_name.nil?
+ extract_next_chapter_and_page_name
+ @next_chapter_name
+ end
+
+ def next_page_name
+ return @next_page_name unless @next_page_name.nil?
+ extract_next_chapter_and_page_name
+ @next_page_name
+ end
+
+ def previous_file
+ return @previous_file unless @previous_file.nil?
+ p_file = Dir["#{Merb.root}/book-content/#{language}/#{chapter_number}-*/#{page_number - 1}-*.*"].entries.first
+ if p_file.nil?
+ p_file = Dir["#{Merb.root}/book-content/#{language}/#{chapter_number - 1}-*/**"].entries.last
+ # We're on the last page of the last chapter, just return the TOC.
+ p_file = "#{Merb.root}/book-content/#{language}/table-of-contents" if p_file.nil?
+ end
+ @previous_file = p_file
+ end
+
+ def previous_chapter_name
+ return @previous_chapter_name unless @previous_chapter_name.nil?
+ extract_previous_chapter_and_page_name
+ @previous_chapter_name
+ end
+
+ def previous_page_name
+ return @previous_page_name unless @previous_page_name.nil?
+ extract_previous_chapter_and_page_name
+ @previous_page_name
+ end
+
+ private
+ def find_page_file(format="markdown")
+ base = "#{Merb.root}/book-content/#{language}/*-#{chapter}"
+ if name
+ Dir["#{base}/*-#{name}.#{format}"].entries.first
+ else
+ Dir["#{base}/toc.#{format}"].entries.first
+ end
+ end
+
+ def extract_chapter_and_page_number
+ file =~ /book-content\/\w{2}\/(\d{1,})-[a-z-]+\/(\d{1,})-[a-z-]+[.]\w+/
+ @chapter_number, @page_number = $1.to_i, $2.to_i
+ end
+
+ def extract_next_chapter_and_page_name
+ @next_chapter_name, @next_page_name = extract_chapter_and_page_number_for_file(next_file)
+ end
+
+ def extract_previous_chapter_and_page_name
+ @previous_chapter_name, @previous_page_name = extract_chapter_and_page_number_for_file(previous_file)
+ end
+
+ # Returns an array with the chapter name and the page name of the file to process
+ def extract_chapter_and_page_number_for_file(file_to_process)
+ file_to_process.grep(/book-content\/\w{2}\/\d{1,}-([a-z-]+)\/\d{1,}-([a-z-]+)[.]\w+/)
+ [$1, $2]
+ end
+
+end
View
@@ -0,0 +1,44 @@
+require File.join(File.dirname(__FILE__), '..', 'spec_helper.rb')
+
+describe "Page" do
+
+ before(:each) do
+ @page = Page.new(:name => "preface", :chapter => "front-matter")
+ end
+
+ it "should have a filename" do
+ @page.file.should_not be_nil
+ @page.file.should include("book-content/en/0-front-matter/1-preface.markdown")
+ end
+
+ it "should have a html version" do
+ @page.to_html.should include("<h1 id='preface'>Preface</h1>")
+ end
+
+ it "should have a previous file" do
+ @page.previous_file.should_not be_nil
+ @page.previous_file.should include("book-content/en/0-front-matter/0-")
+ end
+
+ it "should know the previous chapter name" do
+ @page.previous_chapter_name.should_not be_nil
+ end
+
+ it "should know the previous page name" do
+ @page.previous_page_name.should_not be_nil
+ end
+
+ it "should have a next file" do
+ @page.next_file.should_not be_nil
+ @page.next_file.should include("book-content/en/0-front-matter/2-")
+ end
+
+ it "should know the next chapter name" do
+ @page.next_chapter_name.should_not be_nil
+ end
+
+ it "should know the next page name" do
+ @page.next_page_name.should_not be_nil
+ end
+
+end

0 comments on commit c0594ee

Please sign in to comment.