`
clark1231
  • 浏览: 247477 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
社区版块
存档分类
最新评论

像hackers一样写博客(三):幫你的Octopress增加文章分類

    博客分类:
阅读更多

发现在octopress似乎没有现成可以放在sidebar的分类,于是自己动手弄一个,首先你可以增加个category_list.rb防止在Octoprss的plugins文件夹下

# encoding: utf-8
module Jekyll

  class Site

    def create_category_list
      write_to_tag_cloud if @config['category_tag_cloud']
      write_to_sidebar if @config['category_sidebar']
    end

    private
    # generate category tag list and write to 'source/_includes/asides/categories_tag.html'
    def write_to_tag_cloud
      puts ' => Creating Categories Tag Cloud'
      lists = {}
      max, min = 1, 1
      @categories.keys.sort_by{ |str| str.downcase }.each do |category|
        count = @categories[category].count
        lists[category] = count
        max = count if count > max
      end

      html = ''
      lists.each do | category, counter |
        url = get_category_url category
        style = "font-size: #{100 + (60 * Float(counter)/max)}%"
        if @config['category_counter']
          html << " <a href='#{url}' style='#{style}'>#{category.capitalize}(#{@categories[category].count})</a> "
        else
          html << " <a href='#{url}' style='#{style}'>#{category.capitalize}</a> "
        end
      end

      File.open(File.join(@source, '_includes/asides/categories_tag.html'), 'w') do |file|
        file << """{% if site.category_tag_cloud %}
<section>
<h1>#{@config['category_title'] || 'Categories'}</h1>
<span class='categories_tag'>#{html}</span>
</section>
{% endif %}
"""
      end
    end

    # generate category lists and write to 'source/_includes/asides/categories_sidebar.html'
    def write_to_sidebar
      puts ' => Creating Categories Sidebar'
      html = "<ul>\n"
      # case insensitive sorting
      @categories.keys.sort_by{ |str| str.downcase }.each do |category|
        url = get_category_url category
        if @config['category_counter']
          html << "  <li><a href='#{url}'>#{category.capitalize} (#{@categories[category].count})</a></li>\n"
        else
          html << "  <li><a href='#{url}'>#{category.capitalize}</a></li>\n"
        end
      end
      html << "</ul>"
      File.open(File.join(@source, '_includes/asides/categories_sidebar.html'), 'w') do |file|
        file << """{% if site.category_sidebar %}
<section>
<h1>#{@config['category_title'] || 'Categories'}</h1>
#{html}
</section>
{% endif %}
"""
      end
    end

    def get_category_url(category)
      dir = @config['category_dir'] || 'categories'
      File.join @config['root'], dir, category.gsub(/_|\P{Word}/, '-').gsub(/-{2,}/, '-').downcase
    end
  end

  class CategoryList < Generator
    safe true
    priority :low

    def generate(site)
      if site.config['category_list']
        puts "## Generating Categories.."
        site.create_category_list
      end
    end
  end

end

打开根目录的_config.yml,加上一些设定

# ----------------------- #
#    Categories           #
# ----------------------- #
# create categories page
category_list: true
# use counter after categories
category_counter: true
# category title
category_title: 分類
# create an include categories list in @source/_includes/asides/categories_sidebar.html
# and don't forget to add 'asides/categories_sidebar.html' into @default_asides if you want to enable it.
category_sidebar: true
# create an include categories tag cloud page in @source/_includes/asides/categories_tag.html
# and don't forget to add 'asides/categories_tag.html' to @default_asides if you want to enable it.
category_tag_cloud: true

其中:

category_list 用来启用是否产生侧边栏分类

category_counter 用来决定是否要显示计数器

category_title 那么区块的标题

category_sidebar 效果维一个个排下来,如果文章分类很多可能会让页面拉得很长,如不喜欢可改用category_tag_cloud的效果

 

此外在_config.yml中要加一下你要include的区块

default_asides: [asides/about.html, asides/recent_posts.html, asides/categories_tag.html]
分享到:
评论
2 楼 clark1231 2013-03-06  
boiaprogramfan0420 写道
求教一个问题  octopress的read on功能怎么实现的?我用<!-more->不行啊



<!-- more -->

详情参考下:http://octopress.org/docs/blogging/
1 楼 boiaprogramfan0420 2013-03-03  
求教一个问题  octopress的read on功能怎么实现的?我用<!-more->不行啊

相关推荐

Global site tag (gtag.js) - Google Analytics