`
clark1231
  • 浏览: 252810 次
  • 性别: 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->不行啊

相关推荐

    Memory Barriers: a Hardware View for Software Hackers

    文章深入探讨了内存屏障(Memory Barriers)的概念及其在现代多处理器系统中的作用,并解释了为什么CPU设计者需要将内存屏障强加给SMP(对称多处理器)软件设计师。 #### 并发同步与内存屏障 内存屏障是用于确保...

    Hackers-HomeAuto-Server:Hackers-HomeAuto系统的服务器软件

    3. **API接口**:提供RESTful API接口,允许用户通过手机应用、Web界面或者其他第三方服务与服务器交互,进行设备操作或获取设备状态。 4. **事件驱动**:使用事件驱动架构,当设备状态发生变化或触发预设条件时,...

    Hackers-HomeAuto-Node:用于与Hackers-HomeAuto系统进行节点交互的软件

    黑客家庭自动节点 使设备能够与Hackers-HomeAuto-Server系统连接的节点软件 这是一个仓库,包含许多不同的Node实现,包括Python Flask应用程序,用于mqtt和http coms的Arduino代码等。

    Hackers: Heroes of the Computer Revolution

    - **标题**:“Hackers: Heroes of the Computer Revolution”(黑客:计算机革命的英雄) - “黑客”在这里指的是那些热衷于探索计算机技术极限的人,他们对计算机充满热情,并通过创新和技术突破推动了计算机科学...

    hackers-at-berkeley:H @ B研讨会的示例文件

    伯克利黑客 H @ B研讨会的示例和演示源文件。 目标 使用DCOS和相关的分布式系统部署示例“物联网”摄取管道。 作坊! 中提供了练习。

    Machine Learning for Hackers(中英文)

    Machine Learning for Hackers (中文译名:机器学习-实用案例解析)通过实例讲解机器学习算法,用R实现的,可以一边学习机器学习一边学习R。 是实操型的书,重点放在讲怎么用R做数据挖掘,机器学习的算法更多的是...

    贝叶斯黑客方法:概率编程和贝叶斯推理Bayesian Methods for Hackers: Probabilistic Programming and Bayesian Inference

    使用Python从计算/理解第一,数学第二的角度介绍贝叶斯方法和概率编程。

    Bayesian Methods for Hackers

    《Bayesian Methods for Hackers》是一本面向程序员和数据爱好者的概率编程与贝叶斯方法教程。这本书通过Python编程语言和IPython Notebook(现在称为Jupyter Notebook)来讲解复杂的统计概念,使读者能够轻松地理解...

    Hackers and Painters

    《Hackers and Painters》是Paul Graham所著的一本极具影响力的书籍,主要探讨了计算机科学家(黑客)与艺术家(画家)之间的共性以及创新思维在技术领域的应用。书中的内容涵盖了编程艺术、互联网创业、语言设计等...

    Linux Kernel Hackers' Guide.pdf

    Linux Kernel Hackers' Guide Due to the fact that nearly every post to this site recently has been either by rude cracker- wannabes asking how to break into other people's systems or a request for ...

    Linux Basics for Hackers (AZW3)

    Linux Basics for Hackers: Getting Started with Networking, Scripting, and Security in Kali (AZW3)

    Linux Basics for Hackers 中文版

    Linux Basics for Hackers 中文版

    Hackers delight 2nd edition

    《Hackers Delight 2nd edition》是一本专注于计算机编程的书籍,特别是那些针对底层系统编程和硬件交互方面的内容。作者是Henry S.Warren, Jr.,这本书是该系列的第二版。书籍的标题《Hackers Delight》暗示了它为...

    Linux Basics for Hackers_ Getting Started

    从文件中提供的信息可以了解到,《Linux Basics for Hackers: Getting Started with Networking, Scripting, and Security in Kali》是OccupyTheWeb撰写的一本关于Linux基本知识的书籍,这本书专门针对想要利用Kali ...

Global site tag (gtag.js) - Google Analytics