1. Koen Van der Auwera
  2. amanda

Commits

Koen Van der Auwera  committed f7f0e37

do tags

  • Participants
  • Parent commits a3fd362
  • Branches master

Comments (0)

Files changed (3)

File amanda.rb

View file
  • Ignore whitespace
 Encoding.default_external = Encoding::UTF_8
 Encoding.default_internal = Encoding::UTF_8
 
+require_relative 'amanda/helper'
 require_relative 'amanda/post'
 require_relative 'amanda/store'
 require_relative 'amanda/feed'
     end
   end
 
+  class Tag < R '/tag/(.+)'
+    def get(tag)
+      @posts = $store.posts_for_tag(tag)
+      render :multiple
+    end
+  end
+
+  class Tags < R '/tags'
+    def get
+      @tags = $store.tags
+      render :tags
+    end
+  end
+
   class Refresh < R '/refresh'
     def get
       $store.refresh_from_dropbox
       if session = @state["dropbox"]
         session.get_access_token
         $store.dropbox_session(session)
-        redirect "/"
+        redirect "/refresh"
       else
         "TUUT"
       end
 end
 
 module Amanda::Views
+  include Amanda::Helper
+
   def layout
     doctype!
     html do
   end
 
   def render_footer
+  end
 
+  def render_post(post)
+    div.post! do
+      h2 post.title
+      div.content! { post.html }
+      div.meta! post.id
+    end
   end
 
   def index
   end
 
   def single
-    div.post! do
-      h2 @post.title
-      div.content! { @post.html }
-      div.meta! @post.id
+    render_post(@post)
+  end
+
+  def multiple
+    @posts.each do |post|
+      render_post post
+    end
+  end
+
+  def tags
+    ul class: "tag-list" do
+      @tags.map {|t| li {a(href: URL("tag/#{parameterize(t)}").to_s, title: t) { t }}}
     end
   end
 

File amanda/post.rb

View file
  • Ignore whitespace
   class Error < StandardError; end;
 
   class Post
+    include Helper
 
     attr_accessor :id, :title, :published, :date, :tags, :content
 
     end
 
     def tags_to_arr
-      @tags.split(",").map(&:strip) if @tags
+      if @tags
+        @tags.split(",").map(&:strip)
+      else
+        []
+      end
     end
 
     def self.id_from_filename(filename)
       end
     end
 
-    # http://stackoverflow.com/a/9420531
-    def non_ascii_encoding_options
-      @naeo ||= {
-        :invalid           => :replace,  # Replace invalid byte sequences
-        :undef             => :replace,  # Replace anything not defined in ASCII
-        :replace           => '',        # Use a blank for those replacements
-        :universal_newline => true       # Always break lines with \n
-      }
-    end
-
-    def remove_non_ascii(str)
-      str.encode Encoding.find('ASCII'), non_ascii_encoding_options
-    end
-
-    # https://github.com/rails/rails/blob/a4278766068ee89fc910e113ef93d82071757965/activesupport/lib/active_support/inflector/transliterate.rb#L81
-    def parameterize(str, sep="-")
-      parameterized_string = remove_non_ascii(str)
-      # Turn unwanted chars into the separator
-      parameterized_string.gsub!(/[^a-z0-9\-_]+/i, sep)
-      unless sep.nil? || sep.empty?
-        re_sep = Regexp.escape(sep)
-        # No more than one of the separator in a row.
-        parameterized_string.gsub!(/#{re_sep}{2,}/, sep)
-        # Remove leading/trailing separator.
-        parameterized_string.gsub!(/^#{re_sep}|#{re_sep}$/i, '')
-      end
-      parameterized_string.downcase
-    end
   end
 end

File amanda/store.rb

View file
  • Ignore whitespace
 module Amanda
 
   class Store
+    include Helper
 
     POST_KEY_PREFIX = "post:"
     POSTS_KEY = "posts"
     POSTS_LAST_KEY = "posts:last"
     POSTS_RANDOM = "posts:random"
     DROPBOX = "dropbox"
+    TAGS_KEY = "tags"
+    TAG_KEY_PREFIX = "tag:"
 
     attr_accessor :config_file
 
       redis.keys(pattern)
     end
 
+    def tags
+      redis.smembers TAGS_KEY
+    end
+
+    def posts_for_tag(tag)
+      redis_tag_id = "#{TAG_KEY_PREFIX}#{tag}" unless redis_tag_id =~ /^#{TAG_KEY_PREFIX}/
+      redis.zrange(redis_tag_id, 0, -1).map{|p| post(p)}
+    end
+
     def post(redis_post_id)
       redis_post_id = "#{POST_KEY_PREFIX}#{redis_post_id}" unless redis_post_id =~ /^#{POST_KEY_PREFIX}/
       Post.from_json(redis.get(redis_post_id))
           redis.zadd POSTS_KEY, post.id, "#{POST_KEY_PREFIX}#{post.id}"
           redis.sadd POSTS_RANDOM, "#{POST_KEY_PREFIX}#{post.id}"
           redis.set POSTS_LAST_KEY, "#{POST_KEY_PREFIX}#{post.id}"
+          post.tags_to_arr.each do |tag|
+            redis.sadd TAGS_KEY, tag
+            redis.zadd "#{TAG_KEY_PREFIX}#{parameterize(tag)}", post.id, "#{POST_KEY_PREFIX}#{post.id}"
+          end
         end
       end
     end