From 55dd2a99475afa980f0d2f4c2e4f962c4aee32df Mon Sep 17 00:00:00 2001 From: Shameer C Date: Tue, 18 Oct 2011 14:33:04 +0530 Subject: [PATCH 01/10] Added url suffix option in config and updated articles listing paths --- lib/toto.rb | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/toto.rb b/lib/toto.rb index d74b920..1d175e9 100644 --- a/lib/toto.rb +++ b/lib/toto.rb @@ -103,7 +103,6 @@ def go route, env = {}, type = :html context = lambda do |data, page| Context.new(data, @config, path, env).render(page, type) end - body, status = if Context.new.respond_to?(:"to_#{type}") if route.first =~ /\d{4}/ case route.size @@ -272,7 +271,11 @@ def body end def path - "/#{@config[:prefix]}#{self[:date].strftime("/%Y/%m/%d/#{slug}/")}".squeeze('/') + if(@config[:suffix]=='/') + return "/#{@config[:prefix]}#{self[:date].strftime("/%Y/%m/%d/#{slug}/")}".squeeze('/') + else + return "/#{@config[:prefix]}#{self[:date].strftime("/%Y/%m/%d/#{slug}/")}".squeeze('/').chomp('/') + @config[:suffix] + end end def title() self[:title] || "an article" end @@ -301,7 +304,8 @@ class Config < Hash }, :error => lambda {|code| # The HTML for your error page "toto, we're not in Kansas anymore (#{code})" - } + }, + :suffix => '/' } def initialize obj self.update Defaults @@ -338,6 +342,7 @@ def call env response = @site.go(route, env, *(mime ? mime : [])) @response.body = [response[:body]] + @response.body << mime @response['Content-Length'] = response[:body].length.to_s unless response[:body].empty? @response['Content-Type'] = Rack::Mime.mime_type(".#{response[:type]}") From 51cc13cb506f82cef109589eb883d21664691d9d Mon Sep 17 00:00:00 2001 From: Shameer C Date: Wed, 19 Oct 2011 11:10:22 +0530 Subject: [PATCH 02/10] Added new class --- lib/toto.rb | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/lib/toto.rb b/lib/toto.rb index 1d175e9..99bc448 100644 --- a/lib/toto.rb +++ b/lib/toto.rb @@ -359,5 +359,26 @@ def call env @response.finish end end + class Route + attr_accessor :name,:regex + def initialize name,regex + @name = name + @regex = regex + end + end + class Router + def initialize + @routes = Array.new + end + def add_route route + @routes.push(route) + end + def match_route path + @routes.each do |route| + return route if(path =~ route.regex) + end + return nil + end + end end From 4c7aa26e1ade6216303949ee6f8601460712dbfe Mon Sep 17 00:00:00 2001 From: Shameer C Date: Wed, 19 Oct 2011 21:02:06 +0530 Subject: [PATCH 03/10] Updated with date changes --- lib/toto.rb | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/lib/toto.rb b/lib/toto.rb index 99bc448..0b8c3ed 100644 --- a/lib/toto.rb +++ b/lib/toto.rb @@ -107,7 +107,14 @@ def go route, env = {}, type = :html if route.first =~ /\d{4}/ case route.size when 1..3 - context[archives(route * '-'), :archives] + last_item = Integer(route.last) rescue route.last + if(last_item.is_a? Fixnum) + puts route.last + route.last.class.to_s + ":archives" + context[archives(route * '-'), :archives] + else + puts route.last + route.last.class.to_s + ":article" + context[article(route), :article] + end when 4 context[article(route), :article] else http 400 @@ -272,9 +279,9 @@ def body def path if(@config[:suffix]=='/') - return "/#{@config[:prefix]}#{self[:date].strftime("/%Y/%m/%d/#{slug}/")}".squeeze('/') + return "/#{@config[:prefix]}#{self[:date].strftime("/%Y/%m/#{slug}/")}".squeeze('/') else - return "/#{@config[:prefix]}#{self[:date].strftime("/%Y/%m/%d/#{slug}/")}".squeeze('/').chomp('/') + @config[:suffix] + return "/#{@config[:prefix]}#{self[:date].strftime("/%Y/%m/#{slug}/")}".squeeze('/').chomp('/') + @config[:suffix] end end From 648b3eacdbecb40ae661b10fdb177a1b0987f4ef Mon Sep 17 00:00:00 2001 From: Shameer C Date: Wed, 19 Oct 2011 21:19:55 +0530 Subject: [PATCH 04/10] taking date format from config --- lib/toto.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/toto.rb b/lib/toto.rb index 0b8c3ed..84b9b54 100644 --- a/lib/toto.rb +++ b/lib/toto.rb @@ -279,9 +279,9 @@ def body def path if(@config[:suffix]=='/') - return "/#{@config[:prefix]}#{self[:date].strftime("/%Y/%m/#{slug}/")}".squeeze('/') + return "/#{@config[:prefix]}#{self[:date].strftime("/#{@config[:dateformat]}/#{slug}/")}".squeeze('/') else - return "/#{@config[:prefix]}#{self[:date].strftime("/%Y/%m/#{slug}/")}".squeeze('/').chomp('/') + @config[:suffix] + return "/#{@config[:prefix]}#{self[:date].strftime("/#{@config[:dateformat]}/#{slug}/")}".squeeze('/').chomp('/') + @config[:suffix] end end From efadc7f9fa441652dc4ce7c6b6ba3bc730950cc6 Mon Sep 17 00:00:00 2001 From: Shameer C Date: Thu, 20 Oct 2011 15:02:10 +0530 Subject: [PATCH 05/10] Removed debugging sttmnts --- lib/toto.rb | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/toto.rb b/lib/toto.rb index 84b9b54..7c93498 100644 --- a/lib/toto.rb +++ b/lib/toto.rb @@ -109,10 +109,8 @@ def go route, env = {}, type = :html when 1..3 last_item = Integer(route.last) rescue route.last if(last_item.is_a? Fixnum) - puts route.last + route.last.class.to_s + ":archives" context[archives(route * '-'), :archives] else - puts route.last + route.last.class.to_s + ":article" context[article(route), :article] end when 4 @@ -312,7 +310,8 @@ class Config < Hash :error => lambda {|code| # The HTML for your error page "toto, we're not in Kansas anymore (#{code})" }, - :suffix => '/' + :suffix => '/', + :dateformat => '%Y/%m/%d' } def initialize obj self.update Defaults From e8a479105a7da31e71887d6714fcb3c95fe75fe6 Mon Sep 17 00:00:00 2001 From: Administrator Date: Mon, 31 Oct 2011 10:01:59 +0530 Subject: [PATCH 06/10] Removed routing classes --- lib/toto.rb | 24 +----------------------- 1 file changed, 1 insertion(+), 23 deletions(-) diff --git a/lib/toto.rb b/lib/toto.rb index 7c93498..d81ccf6 100644 --- a/lib/toto.rb +++ b/lib/toto.rb @@ -365,26 +365,4 @@ def call env @response.finish end end - class Route - attr_accessor :name,:regex - def initialize name,regex - @name = name - @regex = regex - end - end - class Router - def initialize - @routes = Array.new - end - def add_route route - @routes.push(route) - end - def match_route path - @routes.each do |route| - return route if(path =~ route.regex) - end - return nil - end - end -end - +end \ No newline at end of file From 8a75e8a8f2586769cc344ab4c15998f495ed798d Mon Sep 17 00:00:00 2001 From: Shameer C Date: Mon, 31 Oct 2011 10:03:25 +0530 Subject: [PATCH 07/10] username --- lib/toto.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/toto.rb b/lib/toto.rb index d81ccf6..836ea2c 100644 --- a/lib/toto.rb +++ b/lib/toto.rb @@ -24,7 +24,7 @@ module Toto :pages => "templates/pages", :articles => "articles" } - + def self.env ENV['RACK_ENV'] || 'production' end From 8bc6a7f2ebad7bf9394bef3d9a4d35d8b39d18e3 Mon Sep 17 00:00:00 2001 From: Shameer C Date: Fri, 11 Nov 2011 23:47:45 +0530 Subject: [PATCH 08/10] Added categories and tagcloud in index and article --- lib/toto.rb | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/lib/toto.rb b/lib/toto.rb index 836ea2c..52f4636 100644 --- a/lib/toto.rb +++ b/lib/toto.rb @@ -9,7 +9,7 @@ require 'maruku' Markdown = Maruku else - require 'rdiscount' + require 'redcarpet' end require 'builder' @@ -24,7 +24,7 @@ module Toto :pages => "templates/pages", :articles => "articles" } - + def self.env ENV['RACK_ENV'] || 'production' end @@ -61,6 +61,7 @@ def self.included obj class Site def initialize config @config = config + @tagcloud = Array.new end def [] *args @@ -85,12 +86,22 @@ def archives filter = "" end.reverse.map do |article| Article.new article, @config end : [] - - return :archives => Archives.new(entries, @config) + return :archives => Archives.new(entries, @config), + :categories => self.categories(entries) + end + + def categories entries + cats = Array.new + entries.each { |i| + cat = (i.category!=nil) ? i.category : '' + cats = cats | cat.split('/') + } + return cats end def article route - Article.new("#{Paths[:articles]}/#{route.join('-')}.#{self[:ext]}", @config).load + articles = self.index + Article.new("#{Paths[:articles]}/#{route.join('-')}.#{self[:ext]}", @config).load(articles) end def / @@ -225,22 +236,23 @@ def to_html class Article < Hash include Template - def initialize obj, config = {} + def initialize obj, config = {} @obj, @config = obj, config self.load if obj.is_a? Hash end - def load + def load articles='' data = if @obj.is_a? String meta, self[:body] = File.read(@obj).split(/\n\n/, 2) # use the date from the filename, or else toto won't find the article @obj =~ /\/(\d{4}-\d{2}-\d{2})[^\/]*$/ - ($1 ? {:date => $1} : {}).merge(YAML.load(meta)) + meta = YAML.load(meta) + ($1 ? {:date => $1} : {}).merge(meta) elsif @obj.is_a? Hash @obj end.inject({}) {|h, (k,v)| h.merge(k.to_sym => v) } - + self[:categories] = articles[:categories] self.taint self.update data self[:date] = Date.parse(self[:date].gsub('/', '-')) rescue Date.today @@ -287,9 +299,11 @@ def title() self[:title] || "an article" end def date() @config[:date].call(self[:date]) end def author() self[:author] || @config[:author] end def to_html() self.load; super(:article, @config) end + def category() self[:category] || nil end + def tags() self[:tags] ? self[:tags].split(',').map { |i| i.strip } : '' end + alias :to_s to_html end - class Config < Hash Defaults = { :author => ENV['USER'], # blog author From 0afd31b106b880f15743583ebf3a05cbdfc94f3f Mon Sep 17 00:00:00 2001 From: Shameer C Date: Wed, 14 Dec 2011 23:19:11 +0530 Subject: [PATCH 09/10] Added categories, tags and custom routes for them --- lib/toto.rb | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/lib/toto.rb b/lib/toto.rb index 52f4636..71838cf 100644 --- a/lib/toto.rb +++ b/lib/toto.rb @@ -94,6 +94,7 @@ def categories entries cats = Array.new entries.each { |i| cat = (i.category!=nil) ? i.category : '' + puts cat cats = cats | cat.split('/') } return cats @@ -103,7 +104,13 @@ def article route articles = self.index Article.new("#{Paths[:articles]}/#{route.join('-')}.#{self[:ext]}", @config).load(articles) end - + + def category route + index + end + def tag + index + end def / self[:root] end @@ -128,8 +135,11 @@ def go route, env = {}, type = :html context[article(route), :article] else http 400 end - elsif respond_to?(path) + elsif respond_to?(path) context[send(path, type), path.to_sym] + elsif respond_to?(path.split('/').first) + m = path.split('/').first + context[archives(route * '-'), m] elsif (repo = @config[:github][:repos].grep(/#{path}/).first) && !@config[:github][:user].empty? context[Repo.new(repo, @config), :repo] @@ -157,7 +167,7 @@ def articles end def self.articles ext - Dir["#{Paths[:articles]}/*.#{ext}"].sort_by {|entry| File.basename(entry) } + Dir["#{Paths[:articles]}/*.#{ext}"].sort_by {|entry| test(?M ,entry) } end class Context @@ -379,4 +389,5 @@ def call env @response.finish end end -end \ No newline at end of file +end + From c0be454a5dbc9a1f004ee865753938f8e1fa5c0c Mon Sep 17 00:00:00 2001 From: Shameer C Date: Sat, 17 Dec 2011 12:48:16 +0530 Subject: [PATCH 10/10] Fixed some bugs in categories and tagcloud --- lib/toto.rb | 65 ++++++++++++++++++++++++++++++++++------------------- 1 file changed, 42 insertions(+), 23 deletions(-) diff --git a/lib/toto.rb b/lib/toto.rb index 71838cf..7134082 100644 --- a/lib/toto.rb +++ b/lib/toto.rb @@ -75,8 +75,9 @@ def []= key, value def index type = :html articles = type == :html ? self.articles.reverse : self.articles {:articles => articles.map do |article| - Article.new article, @config - end}.merge archives + Article.new article, @config + end.sort_by{|article| article.date} + }.merge archives end def archives filter = "" @@ -86,23 +87,11 @@ def archives filter = "" end.reverse.map do |article| Article.new article, @config end : [] - return :archives => Archives.new(entries, @config), - :categories => self.categories(entries) + return :archives => Archives.new(entries, @config) end - def categories entries - cats = Array.new - entries.each { |i| - cat = (i.category!=nil) ? i.category : '' - puts cat - cats = cats | cat.split('/') - } - return cats - end - def article route - articles = self.index - Article.new("#{Paths[:articles]}/#{route.join('-')}.#{self[:ext]}", @config).load(articles) + Article.new("#{Paths[:articles]}/#{route.join('-')}.#{self[:ext]}", @config).load end def category route @@ -167,19 +156,20 @@ def articles end def self.articles ext - Dir["#{Paths[:articles]}/*.#{ext}"].sort_by {|entry| test(?M ,entry) } + Dir["#{Paths[:articles]}/*.#{ext}"].sort_by {|entry| File.basename(entry) } end class Context include Template - attr_reader :env + attr_reader :env, :categories, :tags_cloud def initialize ctx = {}, config = {}, path = "/", env = {} @config, @context, @path, @env = config, ctx, path, env @articles = Site.articles(@config[:ext]).reverse.map do |a| Article.new(a, @config) end - + @categories = Categories.new(@articles,@config) + @tags_cloud = TagCloud.new(@articles,@config) ctx.each do |k, v| meta_def(k) { ctx.instance_of?(Hash) ? v : ctx.send(k) } end @@ -228,7 +218,7 @@ class Archives < Array include Template def initialize articles, config - self.replace articles + self.replace articles.sort_by{|article| article.date} @config = config end @@ -251,7 +241,7 @@ def initialize obj, config = {} self.load if obj.is_a? Hash end - def load articles='' + def load data = if @obj.is_a? String meta, self[:body] = File.read(@obj).split(/\n\n/, 2) @@ -262,7 +252,6 @@ def load articles='' elsif @obj.is_a? Hash @obj end.inject({}) {|h, (k,v)| h.merge(k.to_sym => v) } - self[:categories] = articles[:categories] self.taint self.update data self[:date] = Date.parse(self[:date].gsub('/', '-')) rescue Date.today @@ -372,7 +361,6 @@ def call env response = @site.go(route, env, *(mime ? mime : [])) @response.body = [response[:body]] - @response.body << mime @response['Content-Length'] = response[:body].length.to_s unless response[:body].empty? @response['Content-Type'] = Rack::Mime.mime_type(".#{response[:type]}") @@ -389,5 +377,36 @@ def call env @response.finish end end + + # tagcloud class, @todo add more features + class TagCloud < Array + def initialize articles,config + @articles = articles + @config = config + end + + def snowball + @articles.map {|article| article.tags.to_s.split('/')}.flatten.uniq.sort + end + + def path obj + '/tag/' + obj.to_s + end + end + ## category class, need to add more features + class Categories < Array + def initialize articles,config + @articles = articles + @config = config + end + + def cloud + @articles.map { |article| article.category.split('/')}.flatten.uniq.sort + end + + def path obj + '/category/' + obj.to_s + end + end end