我的第一篇博客


几个月前看过Ruby基本语法,也无意中接触了Octopress,便想在GitHub pages上采用Octopress搭建一个属于自己的博客,照着官网提示开始动手实施,不过最后以失败告终。后又忙于其他工作,此事便搁浅。直至最近得空,便又开始着手此事。搭建博客工作再一次展开。 详细搭建过程官方文档写的相当详细,这里不再赘述,请参考: 官网
下面只说我搭建过程中遇到的难题: 我每次都是运行rake deploy时出现问题,也就是博客无法push到github的repository中。 每次都是![rejected] master->master (no-fast-forward) 最后网上找了不少资料,然后自己也细细思考了一番,最后终得以解决。方法如下: 转到deploy目录下,执行$git pull,然后再执行git push origin master即可。

Read more ⟶

配置文章分类插件


Octopress搭建的博客默认没有文章分类,这肯定让人很不爽,但这个号称Hacker的博客肯定有这功能,便网上搜寻了答案,最终得以解决。方法如下:

###1. 添加category_list插件

代码如下:

    module Jekyll
        class CategoryListTag < Liquid::Tag
            def render(context)
                html = ""
                categories = context.registers[:site].categories.keys
                categories.sort.each do |category|
                    posts_in_category = context.registers[:site].categories[category].size
                    category_dir = context.registers[:site].config['category_dir']
                    category_url = File.join(category_dir, category.gsub(/_|\P{Word}/, '-').gsub(/-{2,}/, '-').downcase)
                    html << "<li class='category'><a href='/#{category_url}/'>#{category} (#{posts_in_category})</a></li>\n"
                end
                html
            end
        end
    end

    Liquid::Template.register_tag('category_list', Jekyll::CategoryListTag)

将以上代码保存到plugins/category_list_tag.rb中。

这个插件会向liquid注册一个名为category_list的tag,该tag就是以li的形式将站点所有的category组织起来。如果要将category加入到侧边导航栏,需要增加一个aside

2. 添加category到aside中

在source/_include/custom/asides中新建category_list.html文件,并且将以下代码复制到其中:

    <section>
        <h1>Categories</h1>
        <ul id="categories">
            {\% category_list %}
        </ul>
    </section>

其中\是为了显示才添加上的,不然无法正常显示,复制代码时请去掉%前的\。

3. 配置侧边栏

在_config.yml中配置侧边栏,修改其中的default_asides项:

    default_asides: [custom/asides/category_list.html, ...]
Read more ⟶