-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathb.rb
116 lines (97 loc) · 2.57 KB
/
b.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# encoding: utf-8
require 'htmlentities/string'
Camping.goes :B
module B::Models
class Book
@@books = []
def initialize(title, author, pages)
@title, @author, @pages = title, author, pages
@@books << self
end
attr_reader :title, :author, :pages
def url
Book.url(title)
end
def self.url(title)
title.to_s.downcase.gsub(/[^A-z0-9]/, '_')
end
def self.all
@@books
end
def self.find_by_title(title)
@@books.detect { |b| b.title == title }
end
def self.find_by_url(url)
@@books.detect { |b| b.url == url }
end
end
end
module B::Controllers
class Index < R '/'
def get
@books = Book.all
render :index
end
end
class Page < R '/(.+)/(.+)'
def get(url, id)
@book = Book.find_by_url(url)
@page_id = id.to_i
@page = @book.pages[@page_id - 1]
render :page
end
end
class Style < R '/style.css'
def get
@headers["Content-Type"] = "text/css; charset=utf-8"
@body = %{
body { background-color: #ffffff; color: #000000; margin: 0; padding: 0 1em; font-family: "Lucida Grande", Helvetica, Arial, sans-serif; }
p, ul { margin: 1em 0; padding: 0; }
ul { list-style-type: none; }
h1 { margin: 0.7692em 0; padding: 0; font-size: 130%; }
}
end
end
class TOC < R '/(.+)'
def get(url)
@book = Book.find_by_url(url)
render :toc
end
end
end
module B::Views
def layout
xhtml_transitional do
head do
title([email protected]? ? "#{@book.title} by #{@book.author}" : "Books")
link :rel => 'stylesheet', :type => 'text/css', :href => '/style.css'
end
body do
self << yield
end
end
end
def index
h1 { "Books" }
ul { @books.each { |b| li { a(b.title, :href => "/#{b.url}") } } }
p { "Created by #{a "Brenton Fletcher", :href => "http://blog.bloople.net"}." }
end
def toc
h1 { "#{@book.title} by #{@book.author}" }
ul { @book.pages.each_with_index { |p, i| li { a("Page #{i + 1}", :href => "/#{@book.url}/#{i + 1}") } } }
end
def page
h1 { "#{@book.title} by #{@book.author} - Page #{@page_id}" }
text @page
pages = []
pages << a('<< Previous page', :href => "#{@page_id - 1}") if @page_id > 1
pages << a('Next page >>', :href => "#{@page_id + 1}") if @page_id < @book.pages.length
p { pages.join(' | ') }
end
end
def B.create
Dir.glob("b/*.yml") do |filename|
f = YAML.load_file(filename)
B::Models::Book.new(f['title'].to_s, f['author'], f['parts'])
end
end