Skip to content
This repository
Browse code

started refactoring the page controller and helpers

  • Loading branch information...
commit c0594ee163156eadd6bccf56071ea260570992fd 1 parent 077410c
Matt Aimonetti authored
22 app/controllers/pages.rb
@@ -9,26 +9,14 @@ def index
9 9 end
10 10
11 11 def show
12   - @chapter = params[:chapter]
13   - @page_name = params[:page_name]
14   -
15   - @page_file = find_page_file
16   - raise NotFound unless @page_file
17   -
18   - text = File.open(@page_file).read
19   - render Maruku::new(text).to_html
  12 + @page = Page.new( :name => params[:page_name],
  13 + :chapter => params[:chapter],
  14 + :language => language)
  15 + raise NotFound unless @page.file
  16 + render @page.to_html
20 17 end
21 18
22 19 private
23   - # If no page name is passed, the first page is returned.
24   - def find_page_file(format="markdown")
25   - base = "#{Merb.root}/book-content/#{language}/*-#{@chapter}"
26   - if @page_name
27   - Dir["#{base}/*-#{@page_name}.#{format}"].entries.first
28   - else
29   - Dir["#{base}/toc.#{format}"].entries.first
30   - end
31   - end
32 20
33 21 def find_toc(format="markdown")
34 22 Dir["#{Merb.root}/book-content/#{language}/toc.#{format}"].entries.first
74 app/helpers/global_helpers.rb
@@ -24,29 +24,28 @@ def rtl_support_class
24 24 def page_nav_links(format = 'markdown')
25 25 return if params[:action] != 'show' # Don't need navigation for the TOC (index).
26 26 links = []
27   - @current_file =~ /book-content\/\w{2}\/(\d{1,})-[a-z-]+\/(\d{1,})-[a-z-]+[.]\w+/
28   - chapter_number, page_number = $1, $2
29   -
30   - chapter_name, page_name = extract_previous_page(chapter_number, page_number)
31   - links << previous_page(chapter_name, page_name)
32   -
  27 + links << previous_page_url
33 28 # Stick a link to the TOC in the middle of the array.
34 29 links << link_to(language_text(language, 'Home'), url(:toc, :language => language))
35   -
36   - chapter_name, page_name = extract_next_page(chapter_number, page_number)
37   - links << next_page(chapter_name, page_name)
38   -
  30 + links << next_page_url
39 31 links.join(' | ')
40 32 end
41 33
42 34 # returns the table of contents link
43 35 def toc_link
44   - return unless params[:action] == 'show' # Don't need navigation for the TOC (index).
  36 + return unless action_name == 'show' # Don't need navigation for the TOC (index).
45 37 link_to(language_text(language, 'Table of Contents'), url(:toc, :language => language))
46 38 end
  39 +
  40 + def next_page_url
  41 + link_to(language_text(language, 'Next'), url(:page, :language => language, :chapter => @page.next_chapter_name, :page_name => @page.next_page_name))
  42 + end
47 43
48   - private
  44 + def previous_page_url
  45 + link_to(language_text(language, 'Previous'), url(:page, :language => language, :chapter => @page.previous_chapter_name, :page_name => @page.previous_page_name))
  46 + end
49 47
  48 + private
50 49 # returns the translated text based on the current language
51 50 def language_text(language_code, text)
52 51 result = text
@@ -56,56 +55,5 @@ def language_text(language_code, text)
56 55 result ||= text
57 56 end
58 57
59   - def next_page(chapter_name, page_name)
60   - link_to(language_text(language, 'Next'), url(:page, :language => language, :chapter => chapter_name, :page_name => page_name))
61   - end
62   -
63   - def previous_page(chapter_name, page_name)
64   - link_to(language_text(language, 'Previous'), url(:page, :language => language, :chapter => chapter_name, :page_name => page_name))
65   - end
66   -
67   - # This method returns a Regexp which contains match captures for the chapter and page names.
68   - def chapter_and_page_names
69   - /book-content\/\w{2}\/\d{1,}-([a-z-]+)\/\d{1,}-([a-z-]+)[.]\w+/
70   - end
71   -
72   - # Returns an array of the next chapter and page names.
73   - def extract_next_page(chapter_number, page_number, format = 'markdown')
74   - next_file = Dir["#{Merb.root}/book-content/#{language}/#{chapter_number}-*/#{page_number.to_i + 1}-*.#{format}"].entries.first
75   - if next_file
76   - next_file.grep(chapter_and_page_names)
77   - else
78   - # We're on the last page, get the first page of the next chapter.
79   - next_file = Dir["#{Merb.root}/book-content/#{language}/#{chapter_number.to_i + 1}-*/**"].entries.first
80   - if next_file
81   - next_file.grep(chapter_and_page_names)
82   - else
83   - # We're on the last page of the last chapter, just return the TOC.
84   - return 'table-of-contents'
85   - end
86   - end
87   - chapter_name, page_name = $1, $2
88   - [chapter_name, page_name]
89   - end
90   -
91   - # Returns an array of the previous chapter and page names.
92   - def extract_previous_page(chapter_number, page_number, format = 'markdown')
93   - next_file = Dir["#{Merb.root}/book-content/#{language}/#{chapter_number}-*/#{page_number.to_i - 1}-*.#{format}"].entries.first
94   - if next_file
95   - next_file.grep(chapter_and_page_names)
96   - else
97   - # We're on the first page, get the last page of the previous chapter.
98   - next_file = Dir["#{Merb.root}/book-content/#{language}/#{chapter_number.to_i - 1}-*/**"].entries.last
99   - if next_file
100   - next_file.grep(chapter_and_page_names)
101   - else
102   - # We're on the first page of the first chapter, just return the TOC.
103   - return 'table-of-contents'
104   - end
105   - end
106   - chapter_name, page_name = $1, $2
107   - [chapter_name, page_name]
108   - end
109   -
110 58 end
111 59 end
112 app/models/page.rb
... ... @@ -0,0 +1,112 @@
  1 +class Page
  2 +
  3 + attr_accessor :file, :name, :chapter, :language
  4 + attr_reader :chapter_number, :page_number, :next_file, :previous_file
  5 + attr_reader :previous_file, :next_file
  6 +
  7 + def initialize(args={})
  8 + @name = args[:name]
  9 + @chapter = args[:chapter]
  10 + @language = args[:language] || 'en'
  11 + end
  12 +
  13 + def file
  14 + @file ||= find_page_file
  15 + end
  16 +
  17 + def text
  18 + @text ||= File.open(file).read
  19 + end
  20 +
  21 + def to_html
  22 + @html ||= ::Maruku::new(text).to_html
  23 + end
  24 +
  25 + def chapter_number
  26 + return @chapter_number unless @chapter_number.nil?
  27 + extract_chapter_and_page_number
  28 + @chapter_number
  29 + end
  30 +
  31 + def page_number
  32 + return @page_number unless @page_number.nil?
  33 + extract_chapter_and_page_number
  34 + @page_number
  35 + end
  36 +
  37 + def next_file
  38 + return @next_file unless @next_file.nil?
  39 + n_file = Dir["#{Merb.root}/book-content/#{language}/#{chapter_number}-*/#{page_number + 1}-*.*"].entries.first
  40 + if n_file.nil?
  41 + n_file = Dir["#{Merb.root}/book-content/#{language}/#{chapter_number + 1}-*/**"].entries.first
  42 + # We're on the last page of the last chapter, just return the TOC.
  43 + n_file = "#{Merb.root}/book-content/#{language}/table-of-contents" if n_file.nil?
  44 + end
  45 + @next_file = n_file
  46 + end
  47 +
  48 + def next_chapter_name
  49 + return @next_chapter_name unless @next_chapter_name.nil?
  50 + extract_next_chapter_and_page_name
  51 + @next_chapter_name
  52 + end
  53 +
  54 + def next_page_name
  55 + return @next_page_name unless @next_page_name.nil?
  56 + extract_next_chapter_and_page_name
  57 + @next_page_name
  58 + end
  59 +
  60 + def previous_file
  61 + return @previous_file unless @previous_file.nil?
  62 + p_file = Dir["#{Merb.root}/book-content/#{language}/#{chapter_number}-*/#{page_number - 1}-*.*"].entries.first
  63 + if p_file.nil?
  64 + p_file = Dir["#{Merb.root}/book-content/#{language}/#{chapter_number - 1}-*/**"].entries.last
  65 + # We're on the last page of the last chapter, just return the TOC.
  66 + p_file = "#{Merb.root}/book-content/#{language}/table-of-contents" if p_file.nil?
  67 + end
  68 + @previous_file = p_file
  69 + end
  70 +
  71 + def previous_chapter_name
  72 + return @previous_chapter_name unless @previous_chapter_name.nil?
  73 + extract_previous_chapter_and_page_name
  74 + @previous_chapter_name
  75 + end
  76 +
  77 + def previous_page_name
  78 + return @previous_page_name unless @previous_page_name.nil?
  79 + extract_previous_chapter_and_page_name
  80 + @previous_page_name
  81 + end
  82 +
  83 + private
  84 + def find_page_file(format="markdown")
  85 + base = "#{Merb.root}/book-content/#{language}/*-#{chapter}"
  86 + if name
  87 + Dir["#{base}/*-#{name}.#{format}"].entries.first
  88 + else
  89 + Dir["#{base}/toc.#{format}"].entries.first
  90 + end
  91 + end
  92 +
  93 + def extract_chapter_and_page_number
  94 + file =~ /book-content\/\w{2}\/(\d{1,})-[a-z-]+\/(\d{1,})-[a-z-]+[.]\w+/
  95 + @chapter_number, @page_number = $1.to_i, $2.to_i
  96 + end
  97 +
  98 + def extract_next_chapter_and_page_name
  99 + @next_chapter_name, @next_page_name = extract_chapter_and_page_number_for_file(next_file)
  100 + end
  101 +
  102 + def extract_previous_chapter_and_page_name
  103 + @previous_chapter_name, @previous_page_name = extract_chapter_and_page_number_for_file(previous_file)
  104 + end
  105 +
  106 + # Returns an array with the chapter name and the page name of the file to process
  107 + def extract_chapter_and_page_number_for_file(file_to_process)
  108 + file_to_process.grep(/book-content\/\w{2}\/\d{1,}-([a-z-]+)\/\d{1,}-([a-z-]+)[.]\w+/)
  109 + [$1, $2]
  110 + end
  111 +
  112 +end
44 spec/models/page_spec.rb
... ... @@ -0,0 +1,44 @@
  1 +require File.join(File.dirname(__FILE__), '..', 'spec_helper.rb')
  2 +
  3 +describe "Page" do
  4 +
  5 + before(:each) do
  6 + @page = Page.new(:name => "preface", :chapter => "front-matter")
  7 + end
  8 +
  9 + it "should have a filename" do
  10 + @page.file.should_not be_nil
  11 + @page.file.should include("book-content/en/0-front-matter/1-preface.markdown")
  12 + end
  13 +
  14 + it "should have a html version" do
  15 + @page.to_html.should include("<h1 id='preface'>Preface</h1>")
  16 + end
  17 +
  18 + it "should have a previous file" do
  19 + @page.previous_file.should_not be_nil
  20 + @page.previous_file.should include("book-content/en/0-front-matter/0-")
  21 + end
  22 +
  23 + it "should know the previous chapter name" do
  24 + @page.previous_chapter_name.should_not be_nil
  25 + end
  26 +
  27 + it "should know the previous page name" do
  28 + @page.previous_page_name.should_not be_nil
  29 + end
  30 +
  31 + it "should have a next file" do
  32 + @page.next_file.should_not be_nil
  33 + @page.next_file.should include("book-content/en/0-front-matter/2-")
  34 + end
  35 +
  36 + it "should know the next chapter name" do
  37 + @page.next_chapter_name.should_not be_nil
  38 + end
  39 +
  40 + it "should know the next page name" do
  41 + @page.next_page_name.should_not be_nil
  42 + end
  43 +
  44 +end

0 comments on commit c0594ee

Please sign in to comment.
Something went wrong with that request. Please try again.