brad-toto 0.4.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 cloudhead
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,156 @@
1
+ toto
2
+ ====
3
+
4
+ the tiniest blogging engine in Oz!
5
+
6
+ introduction
7
+ ------------
8
+
9
+ toto is a git-powered, minimalist blog engine for the hackers of Oz. The engine weighs around ~300 sloc at its worse.
10
+ There is no toto client, at least for now; everything goes through git.
11
+
12
+ blog in 10 seconds
13
+ ------------------
14
+
15
+ $ git clone git://github.com/cloudhead/dorothy.git myblog
16
+ $ cd myblog
17
+ $ heroku create myblog
18
+ $ git push heroku master
19
+
20
+ philosophy
21
+ ----------
22
+
23
+ Everything that can be done better with another tool should be, but one should not have too much pie to stay fit.
24
+ In other words, toto does away with web frameworks or DSLs such as sinatra, and is built right on top of **rack**.
25
+ There is no database or ORM either, we use plain text files.
26
+
27
+ Toto was designed to be used with a reverse-proxy cache, such as [Varnish](http://varnish-cache.org).
28
+ This makes it an ideal candidate for [heroku](http://heroku.com).
29
+
30
+ Oh, and everything that can be done with git, _is_.
31
+
32
+ how it works
33
+ ------------
34
+
35
+ - content is entirely managed through **git**; you get full fledged version control for free.
36
+ - articles are stored as _.txt_ files, with embeded metadata (in yaml format).
37
+ - articles are processed through a markdown converter (rdiscount) by default.
38
+ - templating is done through **ERB**.
39
+ - toto is built right on top of **Rack**.
40
+ - toto was built to take advantage of _HTTP caching_.
41
+ - toto was built with heroku in mind.
42
+ - comments are handled by [disqus](http://disqus.com)
43
+ - individual articles can be accessed through urls such as _/2009/11/21/blogging-with-toto_
44
+ - the archives can be accessed by year, month or day, wih the same format as above.
45
+ - arbitrary metadata can be included in articles files, and accessed from the templates.
46
+ - summaries are generated intelligently by toto, following the `:max` setting you give it.
47
+ - you can also define how long your summary is, by adding `~` at the end of it (`:delim`).
48
+
49
+ dorothy
50
+ -------
51
+
52
+ Dorothy is toto's default template, you can get it at <http://github.com/cloudhead/dorothy>. It
53
+ comes with a very minimalistic but functional template, and a _config.ru_ file to get you started.
54
+ It also includes a _.gems_ file, for heroku.
55
+
56
+ synopsis
57
+ --------
58
+
59
+ One would start by installing _toto_, with `sudo gem install toto`, and then forking or
60
+ cloning the `dorothy` repo, to get a basic skeleton:
61
+
62
+ $ git clone git://github.com/cloudhead/dorothy.git weblog
63
+ $ cd weblog/
64
+
65
+ One would then edit the template at will, it has the following structure:
66
+
67
+ templates/
68
+ |
69
+ +- layout.rhtml # the main site layout, shared by all pages
70
+ |
71
+ +- feed.builder # the builder template for the atom feed
72
+ |
73
+ +- pages/ # pages, such as home, about, etc go here
74
+ |
75
+ +- index.rhtml # the default page loaded from `/`, it displays the list of articles
76
+ |
77
+ +- article.rhtml # the article (post) partial and page
78
+ |
79
+ +- about.rhtml
80
+
81
+ One could then create a .txt article file in the `articles/` folder, and make sure it has the following format:
82
+
83
+ title: The Wonderful Wizard of Oz
84
+ author: Lyman Frank Baum
85
+ date: 1900/05/17
86
+
87
+ Dorothy lived in the midst of the great Kansas prairies, with Uncle Henry,
88
+ who was a farmer, and Aunt Em, who was the farmer's wife.
89
+
90
+ If one is familiar with webby or aerial, this shouldn't look funny. Basically the top of the file is in YAML format,
91
+ and the rest of it is the blog post. They are delimited by an empty line `/\n\n/`, as you can see above.
92
+ None of the information is compulsory, but it's strongly encouraged you specify it.
93
+ Note that one can also use `rake` to create an article stub, with `rake new`.
94
+
95
+ Once he finishes writing his beautiful tale, one can push to the git repo, as usual:
96
+
97
+ $ git add articles/wizard-of-oz.txt
98
+ $ git commit -m 'wrote the wizard of oz.'
99
+ $ git push remote master
100
+
101
+ Where `remote` is the name of your remote git repository. The article is now published.
102
+
103
+ ### deployment
104
+
105
+ Toto is built on top of **Rack**, and hence has a **rackup** file: _config.ru_.
106
+
107
+ #### on your own server
108
+
109
+ Once you have created the remote git repo, and pushed your changes to it, you can run toto with any Rack compliant web server,
110
+ such as **thin**, **mongrel** or **unicorn**.
111
+
112
+ With thin, you would do something like:
113
+
114
+ $ thin start -R config.ru
115
+
116
+ With unicorn, you can just do:
117
+
118
+ $ unicorn
119
+
120
+ #### on heroku
121
+
122
+ Toto was designed to work well with [heroku](http://heroku.com), it makes the most out of it's state-of-the-art caching,
123
+ by setting the _Cache-Control_ and _Etag_ HTTP headers. Deploying on Heroku is really easy, just get the heroku gem,
124
+ create a heroku app with `heroku create`, and push with `git push heroku master`.
125
+
126
+ $ heroku create weblog
127
+ $ git push heroku master
128
+ $ heroku open
129
+
130
+ ### configuration
131
+
132
+ You can configure toto, by modifying the _config.ru_ file. For example, if you want to set the blog author to 'John Galt',
133
+ you could add `set :author, 'John Galt'` inside the `Toto::Server.new` block. Here are the defaults, to get you started:
134
+
135
+ set :author, ENV['USER'] # blog author
136
+ set :title, Dir.pwd.split('/').last # site title
137
+ set :url, 'http://example.com' # site root URL
138
+ set :root, "index" # page to load on /
139
+ set :date, lambda {|now| now.strftime("%d/%m/%Y") } # date format for articles
140
+ set :markdown, :smart # use markdown + smart-mode
141
+ set :disqus, false # disqus id, or false
142
+ set :summary, :max => 150, :delim => /~\n/ # length of article summary and delimiter
143
+ set :ext, 'txt' # file extension for articles
144
+ set :cache, 28800 # cache site for 8 hours
145
+ set :to_html do |path, page, ctx| # returns an html, from a path & context
146
+ ERB.new(File.read("#{path}/#{page}.rhtml")).result(ctx)
147
+ end
148
+
149
+ thanks
150
+ ------
151
+
152
+ To heroku for making this easy as pie.
153
+ To adam wiggins, as I stole a couple of ideas from Scanty.
154
+ To the developpers of Rack, for making such an awesome platform.
155
+
156
+ Copyright (c) 2009 cloudhead. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,31 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "brad-toto"
8
+ gem.summary = %Q{the tiniest blog-engine in Oz}
9
+ gem.description = %Q{the tiniest blog-engine in Oz.}
10
+ gem.email = "self@cloudhead.net"
11
+ gem.homepage = "http://github.com/cloudhead/toto"
12
+ gem.authors = ["cloudhead"]
13
+ gem.add_development_dependency "riot"
14
+ gem.add_dependency "builder"
15
+ gem.add_dependency "rack"
16
+ gem.add_dependency "rdiscount"
17
+ end
18
+ Jeweler::GemcutterTasks.new
19
+ rescue LoadError
20
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
21
+ end
22
+
23
+ require 'rake/testtask'
24
+ Rake::TestTask.new(:test) do |test|
25
+ test.libs << 'lib' << 'test'
26
+ test.pattern = 'test/**/*_test.rb'
27
+ test.verbose = true
28
+ end
29
+
30
+ task :test => :check_dependencies
31
+ task :default => :test
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.4.3
data/lib/ext/ext.rb ADDED
@@ -0,0 +1,46 @@
1
+ class Object
2
+ def meta_def name, &blk
3
+ (class << self; self; end).instance_eval do
4
+ define_method(name, &blk)
5
+ end
6
+ end
7
+ end
8
+
9
+ class String
10
+ def slugize
11
+ self.downcase.gsub(/&/, 'and').gsub(/\s+/, '-').gsub(/[^a-z0-9-]/, '')
12
+ end
13
+
14
+ def humanize
15
+ self.capitalize.gsub(/[-_]+/, ' ')
16
+ end
17
+ end
18
+
19
+ class Fixnum
20
+ def ordinal
21
+ # 1 => 1st
22
+ # 2 => 2nd
23
+ # 3 => 3rd
24
+ # ...
25
+ case self % 100
26
+ when 11..13; "#{self}th"
27
+ else
28
+ case self % 10
29
+ when 1; "#{self}st"
30
+ when 2; "#{self}nd"
31
+ when 3; "#{self}rd"
32
+ else "#{self}th"
33
+ end
34
+ end
35
+ end
36
+ end
37
+
38
+ class Date
39
+ # This check is for people running Toto with ActiveSupport, avoid a collision
40
+ unless respond_to? :iso8601
41
+ # Return the date as a String formatted according to ISO 8601.
42
+ def iso8601
43
+ ::Time.utc(year, month, day, 0, 0, 0, 0).iso8601
44
+ end
45
+ end
46
+ end
data/lib/toto.rb ADDED
@@ -0,0 +1,360 @@
1
+ require 'yaml'
2
+ require 'date'
3
+ require 'erb'
4
+ require 'rack'
5
+ require 'digest'
6
+ require 'open-uri'
7
+
8
+ require 'rdiscount'
9
+ require 'builder'
10
+
11
+ $:.unshift File.dirname(__FILE__)
12
+
13
+ require 'ext/ext'
14
+
15
+ module Toto
16
+ Paths = {
17
+ :templates => "templates",
18
+ :pages => "templates/pages",
19
+ :articles => "articles"
20
+ }
21
+
22
+ def self.env
23
+ ENV['RACK_ENV'] || 'production'
24
+ end
25
+
26
+ def self.env= env
27
+ ENV['RACK_ENV'] = env
28
+ end
29
+
30
+ module Template
31
+ def to_html page, config, &blk
32
+ path = ([:layout, :repo].include?(page) ? Paths[:templates] : Paths[:pages])
33
+ config[:to_html].call(path, page, binding)
34
+ end
35
+
36
+ def markdown text
37
+ if (options = @config[:markdown])
38
+ Markdown.new(text.to_s.strip, *(options.eql?(true) ? [] : options)).to_html
39
+ else
40
+ text.strip
41
+ end
42
+ end
43
+
44
+ def method_missing m, *args, &blk
45
+ self.keys.include?(m) ? self[m] : super
46
+ end
47
+
48
+ def self.included obj
49
+ obj.class_eval do
50
+ define_method(obj.to_s.split('::').last.downcase) { self }
51
+ end
52
+ end
53
+ end
54
+
55
+ class Site
56
+ def initialize config
57
+ @config = config
58
+ end
59
+
60
+ def [] *args
61
+ @config[*args]
62
+ end
63
+
64
+ def []= key, value
65
+ @config.set key, value
66
+ end
67
+
68
+ def index type = :html
69
+ case type
70
+ when :html
71
+ {:articles => self.articles.reverse.map do |article|
72
+ Article.new article, @config
73
+ end }.merge archives
74
+ when :xml, :json
75
+ return :articles => self.articles.map do |article|
76
+ Article.new article, @config
77
+ end
78
+ else return {}
79
+ end
80
+ end
81
+
82
+ def archives filter = ""
83
+ entries = ! self.articles.empty??
84
+ self.articles.select do |a|
85
+ filter !~ /^\d{4}/ || File.basename(a) =~ /^#{filter}/
86
+ end.reverse.map do |article|
87
+ Article.new article, @config
88
+ end : []
89
+
90
+ return :archives => Archives.new(entries, @config)
91
+ end
92
+
93
+ def article route
94
+ begin
95
+ Article.new("#{Paths[:articles]}/#{route.join('-')}.#{self[:ext]}", @config).load
96
+ rescue
97
+ Site.articles(@config[:ext]).reverse.map do |a|
98
+ Article.new(a, @config)
99
+ end.first do |article|
100
+ article.title == route
101
+ end.load
102
+ end
103
+ end
104
+
105
+
106
+ def /
107
+ self[:root]
108
+ end
109
+
110
+ def go route, type = :html
111
+ route << self./ if route.empty?
112
+ type, path = type =~ /html|xml|json/ ? type.to_sym : :html, route.join('/')
113
+ context = lambda do |data, page|
114
+ Context.new(data, @config, path).render(page, type)
115
+ end
116
+
117
+ body, status = if Context.new.respond_to?(:"to_#{type}")
118
+ if route.first =~ /\d{4}/
119
+ case route.size
120
+ when 1..3
121
+ context[archives(route * '-'), :archives]
122
+ when 4
123
+ context[article(route), :article]
124
+ else http 400
125
+ end
126
+ elsif respond_to?(path)
127
+ context[send(path, type), path.to_sym]
128
+ elsif (repo = @config[:github][:repos].grep(/#{path}/).first) &&
129
+ !@config[:github][:user].empty?
130
+ context[Repo.new(repo, @config), :repo]
131
+ else
132
+ context[{}, path.to_sym]
133
+ end
134
+ else
135
+ http 400
136
+ end
137
+
138
+ rescue Errno::ENOENT => e
139
+ return :body => http(404).first, :type => :html, :status => 404
140
+ else
141
+ return :body => body || "", :type => type, :status => status || 200
142
+ end
143
+
144
+ protected
145
+
146
+ def http code
147
+ return ["<font style='font-size:300%'>toto, we're not in Kansas anymore (#{code})</font>", code]
148
+ end
149
+
150
+ def articles
151
+ self.class.articles self[:ext]
152
+ end
153
+
154
+ def self.articles ext
155
+ Dir["#{Paths[:articles]}/*.#{ext}"].sort_by {|entry| File.basename(entry) }
156
+ end
157
+
158
+ class Context
159
+ include Template
160
+
161
+ def initialize ctx = {}, config = {}, path = "/"
162
+ @config, @context, @path = config, ctx, path
163
+ @articles = Site.articles(@config[:ext]).reverse.map do |a|
164
+ Article.new(a, @config)
165
+ end
166
+
167
+ ctx.each do |k, v|
168
+ meta_def(k) { ctx.instance_of?(Hash) ? v : ctx.send(k) }
169
+ end
170
+ end
171
+
172
+ def title
173
+ @config[:title]
174
+ end
175
+
176
+ def render page, type
177
+ type == :html ? to_html(:layout, @config, &Proc.new { to_html page, @config }) : send(:"to_#{type}", :feed)
178
+ end
179
+
180
+ def to_xml page
181
+ xml = Builder::XmlMarkup.new(:indent => 2)
182
+ instance_eval File.read("#{Paths[:templates]}/#{page}.builder")
183
+ end
184
+ alias :to_atom to_xml
185
+
186
+ def method_missing m, *args, &blk
187
+ @context.respond_to?(m) ? @context.send(m) : super
188
+ end
189
+ end
190
+ end
191
+
192
+ class Repo < Hash
193
+ include Template
194
+
195
+ README = "http://github.com/%s/%s/raw/master/README.%s"
196
+
197
+ def initialize name, config
198
+ self[:name], @config = name, config
199
+ end
200
+
201
+ def readme
202
+ markdown open(README %
203
+ [@config[:github][:user], self[:name], @config[:github][:ext]]).read
204
+ rescue Timeout::Error, OpenURI::HTTPError => e
205
+ "This page isn't available."
206
+ end
207
+ alias :content readme
208
+ end
209
+
210
+ class Archives < Array
211
+ include Template
212
+
213
+ def initialize articles, config
214
+ self.replace articles
215
+ @config = config
216
+ end
217
+
218
+ def [] a
219
+ a.is_a?(Range) ? self.class.new(self.slice(a) || [], @config) : super
220
+ end
221
+
222
+ def to_html
223
+ super(:archives, @config)
224
+ end
225
+ alias :to_s to_html
226
+ alias :archive archives
227
+ end
228
+
229
+ class Article < Hash
230
+ include Template
231
+
232
+ def initialize obj, config = {}
233
+ @obj, @config = obj, config
234
+ self.load if obj.is_a? Hash
235
+ end
236
+
237
+ def load
238
+ data = if @obj.is_a? String
239
+ meta, self[:body] = File.read(@obj).split(/\n\n/, 2)
240
+
241
+ # use the date from the filename, or else toto won't find the article
242
+ @obj =~ /\/(\d{4}-\d{2}-\d{2})[^\/]*$/
243
+ ($1 ? {:date => $1} : {}).merge(YAML.load(meta))
244
+ elsif @obj.is_a? Hash
245
+ @obj
246
+ end.inject({}) {|h, (k,v)| h.merge(k.to_sym => v) }
247
+
248
+ self.taint
249
+ self.update data
250
+ self[:date] = Date.parse(self[:date].gsub('/', '-')) rescue Date.today
251
+ self
252
+ end
253
+
254
+ def [] key
255
+ self.load unless self.tainted?
256
+ super
257
+ end
258
+
259
+ def slug
260
+ self[:slug] || self[:title].slugize
261
+ end
262
+
263
+ def summary length = nil
264
+ config = @config[:summary]
265
+ sum = if self[:body] =~ config[:delim]
266
+ self[:body].split(config[:delim]).first
267
+ else
268
+ self[:body].match(/(.{1,#{length || config[:length] || config[:max]}}.*?)(\n|\Z)/m).to_s
269
+ end
270
+ markdown(sum.length == self[:body].length ? sum : sum.strip.sub(/\.\Z/, '&hellip;'))
271
+ end
272
+
273
+ def url
274
+ "http://#{(@config[:url].sub("http://", '') + self.path).squeeze('/')}"
275
+ end
276
+ alias :permalink url
277
+
278
+ def body
279
+ markdown self[:body].sub(@config[:summary][:delim], '') rescue markdown self[:body]
280
+ end
281
+
282
+ def title() self[:title] || "an article" end
283
+ def date() @config[:date].call(self[:date]) end
284
+ def path() self[:date].strftime("/%Y/%m/%d/#{slug}/") end
285
+ def author() self[:author] || @config[:author] end
286
+ def to_html() self.load; super(:article, @config) end
287
+
288
+ alias :to_s to_html
289
+
290
+ end
291
+
292
+ class Config < Hash
293
+ Defaults = {
294
+ :author => ENV['USER'], # blog author
295
+ :title => Dir.pwd.split('/').last, # site title
296
+ :root => "index", # site index
297
+ :url => "http://127.0.0.1",
298
+ :date => lambda {|now| now.strftime("%d/%m/%Y") }, # date function
299
+ :markdown => :smart, # use markdown
300
+ :disqus => false, # disqus name
301
+ :summary => {:max => 150, :delim => /~\n/}, # length of summary and delimiter
302
+ :ext => 'txt', # extension for articles
303
+ :cache => 28800, # cache duration (seconds)
304
+ :github => {:user => "", :repos => [], :ext => 'md'}, # Github username and list of repos
305
+ :to_html => lambda {|path, page, ctx| # returns an html, from a path & context
306
+ ERB.new(File.read("#{path}/#{page}.rhtml")).result(ctx)
307
+ }
308
+ }
309
+ def initialize obj
310
+ self.update Defaults
311
+ self.update obj
312
+ end
313
+
314
+ def set key, val = nil, &blk
315
+ if val.is_a? Hash
316
+ self[key].update val
317
+ else
318
+ self[key] = block_given?? blk : val
319
+ end
320
+ end
321
+ end
322
+
323
+ class Server
324
+ attr_reader :config
325
+
326
+ def initialize config = {}, &blk
327
+ @config = config.is_a?(Config) ? config : Config.new(config)
328
+ @config.instance_eval(&blk) if block_given?
329
+ end
330
+
331
+ def call env
332
+ @request = Rack::Request.new env
333
+ @response = Rack::Response.new
334
+
335
+ return [400, {}, []] unless @request.get?
336
+
337
+ path, mime = @request.path_info.split('.')
338
+ route = (path || '/').split('/').reject {|i| i.empty? }
339
+
340
+ response = Toto::Site.new(@config).go(route, *(mime ? mime : []))
341
+
342
+ @response.body = [response[:body]]
343
+ @response['Content-Length'] = response[:body].length.to_s unless response[:body].empty?
344
+ @response['Content-Type'] = Rack::Mime.mime_type(".#{response[:type]}")
345
+
346
+ # Set http cache headers
347
+ @response['Cache-Control'] = if Toto.env == 'production'
348
+ "public, max-age=#{@config[:cache]}"
349
+ else
350
+ "no-cache, must-revalidate"
351
+ end
352
+
353
+ @response['Etag'] = Digest::SHA1.hexdigest(response[:body])
354
+
355
+ @response.status = response[:status]
356
+ @response.finish
357
+ end
358
+ end
359
+ end
360
+
@@ -0,0 +1,5 @@
1
+ title: The Wonderful Wizard of Oz
2
+ date: 17/05/1900
3
+
4
+ _Once upon a time_...
5
+
@@ -0,0 +1,5 @@
1
+ title: the wizard of oz
2
+ date: 12/10/1932
3
+
4
+ Once upon a time...
5
+
@@ -0,0 +1,5 @@
1
+ title: the wizard of oz
2
+ date: 12/10/1932
3
+
4
+ Once upon a time...
5
+
@@ -0,0 +1,5 @@
1
+ title: the wizard of oz
2
+ date: 12/10/1932
3
+
4
+ Once upon a time...
5
+
@@ -0,0 +1,5 @@
1
+ title: the wizard of oz
2
+ date: 12/10/1932
3
+
4
+ Once upon a time...
5
+
data/test/autotest.rb ADDED
@@ -0,0 +1,34 @@
1
+ #
2
+ # Convenience Methods
3
+ #
4
+ def run(cmd)
5
+ print "\n\n"
6
+ puts(cmd)
7
+ system(cmd)
8
+ print "\n\n"
9
+ end
10
+
11
+ def run_all_tests
12
+ # see Rakefile for the definition of the test:all task
13
+ system("rake -s test:all VERBOSE=true")
14
+ end
15
+
16
+ #
17
+ # Watchr Rules
18
+ #
19
+ watch('^test/.*?_test\.rb' ) {|m| run("ruby -rubygems %s" % m[0]) }
20
+ watch('^lib/(.*)\.rb' ) {|m| run("ruby -rubygems test/%s_test.rb" % m[1]) }
21
+ watch('^lib/toto/(.*)\.rb' ) {|m| run("ruby -rubygems test/%s_test.rb" % m[1]) }
22
+ watch('^test/test_helper\.rb') { run_all_tests }
23
+
24
+ #
25
+ # Signal Handling
26
+ #
27
+ # Ctrl-\
28
+ Signal.trap('QUIT') do
29
+ puts " --- Running all tests ---\n\n"
30
+ run_all_tests
31
+ end
32
+
33
+ # Ctrl-C
34
+ Signal.trap('INT') { abort("\n") }
@@ -0,0 +1 @@
1
+ <span id="count"><%= @articles.length %></span>
@@ -0,0 +1,5 @@
1
+ <h1><%= @path %></h1>
2
+ <% for entry in archives %>
3
+ <li class="entry"><%= entry.title %></li>
4
+ <% end %>
5
+
@@ -0,0 +1,4 @@
1
+ <h2><%= title %></h2>
2
+ <span><%= date %></h2>
3
+ <p><%= body %></p>
4
+
@@ -0,0 +1,21 @@
1
+ xml.instruct!
2
+ xml.feed "xmlns" => "http://www.w3.org/2005/Atom" do
3
+ xml.title @config[:title]
4
+ xml.id @config[:url]
5
+ xml.updated articles.first[:date].iso8601 unless articles.empty?
6
+ xml.author { xml.name @config[:author] }
7
+
8
+ articles.each do |article|
9
+ xml.entry do
10
+ xml.title article.title
11
+ xml.link "rel" => "alternate", "href" => article.url
12
+ xml.id article.url
13
+ xml.published article[:date].iso8601
14
+ xml.updated article[:date].iso8601
15
+ xml.author { xml.name @config[:author] }
16
+ xml.summary article.summary, "type" => "html"
17
+ xml.content article.body, "type" => "html"
18
+ end
19
+ end
20
+ end
21
+
@@ -0,0 +1,9 @@
1
+ <ul id="articles">
2
+ <% for article in articles[0...3] %>
3
+ <li><%= article %></li>
4
+ <% end %>
5
+ </ul>
6
+ <div id="archives">
7
+ <%= archives[3...5] %>
8
+ </div>
9
+
@@ -0,0 +1,4 @@
1
+ <html>
2
+ <%= yield %>
3
+ </html>
4
+
@@ -0,0 +1 @@
1
+ <%= readme %>
@@ -0,0 +1,44 @@
1
+ require 'rubygems'
2
+ require 'hpricot'
3
+ require 'riot'
4
+
5
+ $:.unshift File.dirname(__FILE__)
6
+ $:.unshift File.join(File.dirname(__FILE__), '..', 'lib')
7
+
8
+ require 'toto'
9
+
10
+ module Toto
11
+ class IncludesHTMLMacro < Riot::AssertionMacro
12
+ register :includes_html
13
+
14
+ def evaluate(actual, expected)
15
+ doc = Hpricot.parse(actual)
16
+ expected = expected.to_a.flatten
17
+
18
+ if (doc/expected.first).empty?
19
+ fail("expected #{actual} to contain a <#{expected.first}>")
20
+ elsif !(doc/expected.first).inner_html.match(expected.last)
21
+ fail("expected <#{expected.first}> to contain #{expected.last}")
22
+ else
23
+ pass
24
+ end
25
+ end
26
+ end
27
+
28
+ class IncludesElementsMacro < Riot::AssertionMacro
29
+ register :includes_elements
30
+
31
+ def evaluate(actual, selector, count)
32
+ doc = Hpricot.parse(actual)
33
+ (doc/selector).size == count ? pass : fail("expected #{actual} to contain #{count} #{selector}(s)")
34
+ end
35
+ end
36
+
37
+ class WithinMacro < Riot::AssertionMacro
38
+ register :within
39
+
40
+ def evaluate(actual, expected)
41
+ expected.include?(actual) ? pass : fail("expected #{actual} to be within #{expected}")
42
+ end
43
+ end
44
+ end
data/test/toto_test.rb ADDED
@@ -0,0 +1,216 @@
1
+ require 'test/test_helper'
2
+ require 'date'
3
+
4
+ URL = "http://toto.oz"
5
+ AUTHOR = "toto"
6
+
7
+ context Toto do
8
+ setup do
9
+ @config = Toto::Config.new(:markdown => true, :author => AUTHOR, :url => URL)
10
+ @toto = Rack::MockRequest.new(Toto::Server.new(@config))
11
+ Toto::Paths[:articles] = "test/articles"
12
+ Toto::Paths[:pages] = "test/templates"
13
+ Toto::Paths[:templates] = "test/templates"
14
+ end
15
+
16
+ context "GET /" do
17
+ setup { @toto.get('/') }
18
+
19
+ asserts("returns a 200") { topic.status }.equals 200
20
+ asserts("body is not empty") { not topic.body.empty? }
21
+ asserts("content type is set properly") { topic.content_type }.equals "text/html"
22
+ should("include a couple of article") { topic.body }.includes_elements("#articles li", 3)
23
+ should("include an archive") { topic.body }.includes_elements("#archives li", 2)
24
+
25
+ context "with no articles" do
26
+ setup { Rack::MockRequest.new(Toto::Server.new(@config.merge(:ext => 'oxo'))).get('/') }
27
+
28
+ asserts("body is not empty") { not topic.body.empty? }
29
+ asserts("returns a 200") { topic.status }.equals 200
30
+ end
31
+
32
+ context "with a user-defined to_html" do
33
+ setup do
34
+ @config[:to_html] = lambda do |path, page, binding|
35
+ ERB.new(File.read("#{path}/#{page}.rhtml")).result(binding)
36
+ end
37
+ @toto.get('/')
38
+ end
39
+
40
+ asserts("returns a 200") { topic.status }.equals 200
41
+ asserts("body is not empty") { not topic.body.empty? }
42
+ asserts("content type is set properly") { topic.content_type }.equals "text/html"
43
+ should("include a couple of article") { topic.body }.includes_elements("#articles li", 3)
44
+ should("include an archive") { topic.body }.includes_elements("#archives li", 2)
45
+ end
46
+ end
47
+
48
+ context "GET /about" do
49
+ setup { @toto.get('/about') }
50
+ asserts("returns a 200") { topic.status }.equals 200
51
+ asserts("body is not empty") { not topic.body.empty? }
52
+ should("have access to @articles") { topic.body }.includes_html("#count" => /5/)
53
+ end
54
+
55
+ context "GET a single article" do
56
+ setup { @toto.get("/1900/05/17/the-wonderful-wizard-of-oz") }
57
+ asserts("returns a 200") { topic.status }.equals 200
58
+ asserts("content type is set properly") { topic.content_type }.equals "text/html"
59
+ should("contain the article") { topic.body }.includes_html("p" => /<em>Once upon a time<\/em>/)
60
+ end
61
+
62
+ context "GET to the archive" do
63
+ context "through a year" do
64
+ setup { @toto.get('/2009') }
65
+ asserts("returns a 200") { topic.status }.equals 200
66
+ should("includes the entries for that year") { topic.body }.includes_elements("li.entry", 3)
67
+ end
68
+
69
+ context "through a year & month" do
70
+ setup { @toto.get('/2009/12') }
71
+ asserts("returns a 200") { topic.status }.equals 200
72
+ should("includes the entries for that month") { topic.body }.includes_elements("li.entry", 2)
73
+ should("includes the year & month") { topic.body }.includes_html("h1" => /2009\/12/)
74
+ end
75
+
76
+ context "through /archive" do
77
+ setup { @toto.get('/archive') }
78
+ end
79
+ end
80
+
81
+ context "GET to an unknown route" do
82
+ setup { @toto.get('/unknown') }
83
+ should("returns a 404") { topic.status }.equals 404
84
+ end
85
+
86
+ context "Request is invalid" do
87
+ setup { @toto.delete('/invalid') }
88
+ should("returns a 400") { topic.status }.equals 400
89
+ end
90
+
91
+ context "GET /index.xml (atom feed)" do
92
+ setup { @toto.get('/index.xml') }
93
+ asserts("content type is set properly") { topic.content_type }.equals "application/xml"
94
+ asserts("body should be valid xml") { topic.body }.includes_html("feed > entry" => /.+/)
95
+ asserts("summary shouldn't be empty") { topic.body }.includes_html("summary" => /.{10,}/)
96
+ end
97
+
98
+ context "GET to a repo name" do
99
+ setup do
100
+ class Toto::Repo
101
+ def readme() "#{self[:name]}'s README" end
102
+ end
103
+ end
104
+
105
+ context "when the repo is in the :repos array" do
106
+ setup do
107
+ @config[:github] = {:user => "cloudhead", :repos => ['the-repo']}
108
+ @toto.get('/the-repo')
109
+ end
110
+ should("return the-repo's README") { topic.body }.includes("the-repo's README")
111
+ end
112
+
113
+ context "when the repo is not in the :repos array" do
114
+ setup do
115
+ @config[:github] = {:user => "cloudhead", :repos => []}
116
+ @toto.get('/the-repo')
117
+ end
118
+ should("return a 404") { topic.status }.equals 404
119
+ end
120
+ end
121
+
122
+ context "creating an article" do
123
+ setup do
124
+ @config[:markdown] = true
125
+ @config[:date] = lambda {|t| "the time is #{t.strftime("%Y/%m/%d %H:%M")}" }
126
+ @config[:summary] = {:length => 50}
127
+ end
128
+
129
+ context "with the bare essentials" do
130
+ setup do
131
+ Toto::Article.new({
132
+ :title => "Toto & The Wizard of Oz.",
133
+ :body => "#Chapter I\nhello, *stranger*."
134
+ }, @config)
135
+ end
136
+
137
+ should("have a title") { topic.title }.equals "Toto & The Wizard of Oz."
138
+ should("parse the body as markdown") { topic.body }.equals "<h1>Chapter I</h1>\n\n<p>hello, <em>stranger</em>.</p>\n"
139
+ should("create an appropriate slug") { topic.slug }.equals "toto-and-the-wizard-of-oz"
140
+ should("set the date") { topic.date }.equals "the time is #{Date.today.strftime("%Y/%m/%d %H:%M")}"
141
+ should("create a summary") { topic.summary == topic.body }
142
+ should("have an author") { topic.author }.equals AUTHOR
143
+ should("have a path") { topic.path }.equals Date.today.strftime("/%Y/%m/%d/toto-and-the-wizard-of-oz/")
144
+ should("have a url") { topic.url }.equals Date.today.strftime("#{URL}/%Y/%m/%d/toto-and-the-wizard-of-oz/")
145
+ end
146
+
147
+ context "with a user-defined summary" do
148
+ setup do
149
+ Toto::Article.new({
150
+ :title => "Toto & The Wizard of Oz.",
151
+ :body => "Well,\nhello ~\n, *stranger*."
152
+ }, @config.merge(:markdown => false, :summary => {:max => 150, :delim => /~\n/}))
153
+ end
154
+
155
+ should("split the article at the delimiter") { topic.summary }.equals "Well,\nhello"
156
+ should("not have the delimiter in the body") { topic.body !~ /~/ }
157
+ end
158
+
159
+ context "with everything specified" do
160
+ setup do
161
+ Toto::Article.new({
162
+ :title => "The Wizard of Oz",
163
+ :body => ("a little bit of text." * 5) + "\n" + "filler" * 10,
164
+ :date => "19/10/1976",
165
+ :slug => "wizard-of-oz",
166
+ :author => "toetoe"
167
+ }, @config)
168
+ end
169
+
170
+ should("parse the date") { [topic[:date].month, topic[:date].year] }.equals [10, 1976]
171
+ should("use the slug") { topic.slug }.equals "wizard-of-oz"
172
+ should("use the author") { topic.author }.equals "toetoe"
173
+
174
+ context "and long first paragraph" do
175
+ should("create a valid summary") { topic.summary }.equals "<p>" + ("a little bit of text." * 5).chop + "&hellip;</p>\n"
176
+ end
177
+
178
+ context "and a short first paragraph" do
179
+ setup do
180
+ @config[:markdown] = false
181
+ Toto::Article.new({:body => "there ain't such thing as a free lunch\n" * 10}, @config)
182
+ end
183
+
184
+ should("create a valid summary") { topic.summary.size }.within 75..80
185
+ end
186
+ end
187
+ end
188
+
189
+ context "using Config#set with a hash" do
190
+ setup do
191
+ conf = Toto::Config.new({})
192
+ conf.set(:summary, {:delim => /%/})
193
+ conf
194
+ end
195
+
196
+ should("set summary[:delim] to /%/") { topic[:summary][:delim].source }.equals "%"
197
+ should("leave the :max intact") { topic[:summary][:max] }.equals 150
198
+ end
199
+
200
+ context "using Config#set with a block" do
201
+ setup do
202
+ conf = Toto::Config.new({})
203
+ conf.set(:to_html) {|path, p, _| path + p }
204
+ conf
205
+ end
206
+
207
+ should("set the value to a proc") { topic[:to_html] }.respond_to :call
208
+ end
209
+
210
+ context "extensions to the core Ruby library" do
211
+
212
+ should("respond to iso8601") { Date.today }.respond_to?(:iso8601)
213
+ end
214
+ end
215
+
216
+
data/toto.gemspec ADDED
@@ -0,0 +1,78 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{toto}
8
+ s.version = "0.4.3"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["cloudhead"]
12
+ s.date = %q{2010-03-15}
13
+ s.description = %q{the tiniest blog-engine in Oz.}
14
+ s.email = %q{self@cloudhead.net}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.md"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.md",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "lib/ext/ext.rb",
27
+ "lib/toto.rb",
28
+ "test/articles/1900-05-17-the-wonderful-wizard-of-oz.txt",
29
+ "test/articles/2001-01-01-two-thousand-and-one.txt",
30
+ "test/articles/2009-04-01-tilt-factor.txt",
31
+ "test/articles/2009-12-04-some-random-article.txt",
32
+ "test/articles/2009-12-11-the-dichotomy-of-design.txt",
33
+ "test/autotest.rb",
34
+ "test/templates/about.rhtml",
35
+ "test/templates/archives.rhtml",
36
+ "test/templates/article.rhtml",
37
+ "test/templates/feed.builder",
38
+ "test/templates/index.rhtml",
39
+ "test/templates/layout.rhtml",
40
+ "test/templates/repo.rhtml",
41
+ "test/test_helper.rb",
42
+ "test/toto_test.rb",
43
+ "toto.gemspec"
44
+ ]
45
+ s.homepage = %q{http://github.com/cloudhead/toto}
46
+ s.rdoc_options = ["--charset=UTF-8"]
47
+ s.require_paths = ["lib"]
48
+ s.rubygems_version = %q{1.3.5}
49
+ s.summary = %q{the tiniest blog-engine in Oz}
50
+ s.test_files = [
51
+ "test/test_helper.rb",
52
+ "test/toto_test.rb",
53
+ "test/autotest.rb"
54
+ ]
55
+
56
+ if s.respond_to? :specification_version then
57
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
58
+ s.specification_version = 3
59
+
60
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
61
+ s.add_development_dependency(%q<riot>, [">= 0"])
62
+ s.add_runtime_dependency(%q<builder>, [">= 0"])
63
+ s.add_runtime_dependency(%q<rack>, [">= 0"])
64
+ s.add_runtime_dependency(%q<rdiscount>, [">= 0"])
65
+ else
66
+ s.add_dependency(%q<riot>, [">= 0"])
67
+ s.add_dependency(%q<builder>, [">= 0"])
68
+ s.add_dependency(%q<rack>, [">= 0"])
69
+ s.add_dependency(%q<rdiscount>, [">= 0"])
70
+ end
71
+ else
72
+ s.add_dependency(%q<riot>, [">= 0"])
73
+ s.add_dependency(%q<builder>, [">= 0"])
74
+ s.add_dependency(%q<rack>, [">= 0"])
75
+ s.add_dependency(%q<rdiscount>, [">= 0"])
76
+ end
77
+ end
78
+
metadata ADDED
@@ -0,0 +1,120 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: brad-toto
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.4.3
5
+ platform: ruby
6
+ authors:
7
+ - cloudhead
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-03-15 00:00:00 +01:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: riot
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: builder
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "0"
34
+ version:
35
+ - !ruby/object:Gem::Dependency
36
+ name: rack
37
+ type: :runtime
38
+ version_requirement:
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: "0"
44
+ version:
45
+ - !ruby/object:Gem::Dependency
46
+ name: rdiscount
47
+ type: :runtime
48
+ version_requirement:
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: "0"
54
+ version:
55
+ description: the tiniest blog-engine in Oz.
56
+ email: self@cloudhead.net
57
+ executables: []
58
+
59
+ extensions: []
60
+
61
+ extra_rdoc_files:
62
+ - LICENSE
63
+ - README.md
64
+ files:
65
+ - .document
66
+ - .gitignore
67
+ - LICENSE
68
+ - README.md
69
+ - Rakefile
70
+ - VERSION
71
+ - lib/ext/ext.rb
72
+ - lib/toto.rb
73
+ - test/articles/1900-05-17-the-wonderful-wizard-of-oz.txt
74
+ - test/articles/2001-01-01-two-thousand-and-one.txt
75
+ - test/articles/2009-04-01-tilt-factor.txt
76
+ - test/articles/2009-12-04-some-random-article.txt
77
+ - test/articles/2009-12-11-the-dichotomy-of-design.txt
78
+ - test/autotest.rb
79
+ - test/templates/about.rhtml
80
+ - test/templates/archives.rhtml
81
+ - test/templates/article.rhtml
82
+ - test/templates/feed.builder
83
+ - test/templates/index.rhtml
84
+ - test/templates/layout.rhtml
85
+ - test/templates/repo.rhtml
86
+ - test/test_helper.rb
87
+ - test/toto_test.rb
88
+ - toto.gemspec
89
+ has_rdoc: true
90
+ homepage: http://github.com/cloudhead/toto
91
+ licenses: []
92
+
93
+ post_install_message:
94
+ rdoc_options:
95
+ - --charset=UTF-8
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: "0"
103
+ version:
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: "0"
109
+ version:
110
+ requirements: []
111
+
112
+ rubyforge_project:
113
+ rubygems_version: 1.3.5
114
+ signing_key:
115
+ specification_version: 3
116
+ summary: the tiniest blog-engine in Oz
117
+ test_files:
118
+ - test/test_helper.rb
119
+ - test/toto_test.rb
120
+ - test/autotest.rb