From a62a199034a1bd4e52ee2fc6385575ec72b32dde Mon Sep 17 00:00:00 2001 From: Endel Date: Wed, 27 Apr 2011 00:19:38 -0300 Subject: [PATCH] Support tagged posts. --- TODO | 2 ++ lib/toto.rb | 56 ++++++++++++++++++++++++++++++++++++-- test/templates/index.rhtml | 3 ++ toto.gemspec | 8 +++--- 4 files changed, 63 insertions(+), 6 deletions(-) diff --git a/TODO b/TODO index 74e5cc0..77b6537 100644 --- a/TODO +++ b/TODO @@ -1,2 +1,4 @@ TODO ==== + +* Missing a tag-cloud helper for sidebar. \ No newline at end of file diff --git a/lib/toto.rb b/lib/toto.rb index d74b920..3999af4 100644 --- a/lib/toto.rb +++ b/lib/toto.rb @@ -92,7 +92,17 @@ def archives filter = "" def article route Article.new("#{Paths[:articles]}/#{route.join('-')}.#{self[:ext]}", @config).load end - + + def tagged tag + articles = self.articles.collect do |article| + Article.new article, @config + end.select do |article| + !article[:tags].index(tag.humanize).nil? + end + + {:articles => articles, :tag => tag} + end + def / self[:root] end @@ -113,6 +123,8 @@ def go route, env = {}, type = :html context[article(route), :article] else http 400 end + elsif route.first == "tagged" + context[tagged(route.last), :tagged] elsif respond_to?(path) context[send(path, type), path.to_sym] elsif (repo = @config[:github][:repos].grep(/#{path}/).first) && @@ -236,9 +248,10 @@ def load elsif @obj.is_a? Hash @obj end.inject({}) {|h, (k,v)| h.merge(k.to_sym => v) } - + self.taint self.update data + self[:tags] = self[:tags].split(',').collect{|tag| tag.strip.humanize } self[:date] = Date.parse(self[:date].gsub('/', '-')) rescue Date.today self end @@ -274,6 +287,10 @@ def body def path "/#{@config[:prefix]}#{self[:date].strftime("/%Y/%m/%d/#{slug}/")}".squeeze('/') end + + def tags + self[:tags].collect {|tag| "#{tag}" }.join(@config[:tag_separator]) + end def title() self[:title] || "an article" end def date() @config[:date].call(self[:date]) end @@ -281,6 +298,40 @@ def author() self[:author] || @config[:author] end def to_html() self.load; super(:article, @config) end alias :to_s to_html end + + class TagCloud < Hash + def initialize articles + # Count all article tags + articles.each do |article| + article[:tags].each do |word| + unless self.key? word + self[word] = 0 + else + self[word] = self[word] + 1 + end + end + end + end + + def font_ratio + min, max = 1000000, -1000000 + self.each_key do |word| + max = self[word] if self[word] > max + min = self[word] if self[word] < min + end + 18.0 / (max - min) + end + + def build + cloud = String.new + ratio = font_ratio + self.each_key do |word| + font_size = (9 + (self[word] * ratio)) + cloud << %Q{#{word} } + end + cloud + end + end class Config < Hash Defaults = { @@ -294,6 +345,7 @@ class Config < Hash :disqus => false, # disqus name :summary => {:max => 150, :delim => /~\n/}, # length of summary and delimiter :ext => 'txt', # extension for articles + :tag_separator => ' ', # tag separator for articles :cache => 28800, # cache duration (seconds) :github => {:user => "", :repos => [], :ext => 'md'}, # Github username and list of repos :to_html => lambda {|path, page, ctx| # returns an html, from a path & context diff --git a/test/templates/index.rhtml b/test/templates/index.rhtml index 07184eb..38bf3fc 100644 --- a/test/templates/index.rhtml +++ b/test/templates/index.rhtml @@ -5,6 +5,9 @@
<%= archives[3...5] %> +
+
+

env passed: <%= env != nil %>

diff --git a/toto.gemspec b/toto.gemspec index 03b032e..6f0e3e1 100644 --- a/toto.gemspec +++ b/toto.gemspec @@ -8,10 +8,10 @@ Gem::Specification.new do |s| s.version = "0.4.9" s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version= - s.authors = ["cloudhead"] - s.date = %q{2010-12-01} + s.authors = ["cloudhead", "endel"] + s.date = %q{2011-04-27} s.description = %q{the tiniest blog-engine in Oz.} - s.email = %q{self@cloudhead.net} + s.email = %q{talk@endel.me} s.extra_rdoc_files = [ "LICENSE", "README.md" @@ -44,7 +44,7 @@ Gem::Specification.new do |s| "test/toto_test.rb", "toto.gemspec" ] - s.homepage = %q{http://github.com/cloudhead/toto} + s.homepage = %q{http://github.com/endel/toto} s.rdoc_options = ["--charset=UTF-8"] s.require_paths = ["lib"] s.rubygems_version = %q{1.3.5}