給文章分類(lèi)和添加標(biāo)簽是博客必不可少的功能,方便了信息的快速攫取。
進(jìn)入 Octopress\plugins 目錄,新建 category_list_tag.rb 文件,添加如下代碼
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)
進(jìn)入 Octopress\source_includes\custom\asides 目錄,新建 category_list.html,添加如下代碼
<section>
<h1>Categories</h1>
<ul id="categories">
{% category_list %}
</ul>
</section>
打開(kāi) Octopress/_config.yml,在 default_asides 一欄中添加
default_asides: [..., custom/asides/category_list.html]
rake generate
rake preview
rake deploy
打開(kāi)頁(yè)面 http://geekjacky.github.io/
,如下
http://wiki.jikexueyuan.com/project/github-page/images/blog5.jpg" alt="" />
git add .
git commit -m "增加分類(lèi)邊欄"
git push origin source
還記得博文的開(kāi)頭有個(gè) categories 么?填在這里就行了。
http://wiki.jikexueyuan.com/project/github-page/images/vategories.jpg" alt="" />
標(biāo)簽可以加在邊欄,也可以加在頂部導(dǎo)航,這里兩種方法都介紹一下,我自己是加到導(dǎo)航欄,因?yàn)闃?biāo)簽太多了,放在邊欄不好看。
先克隆這兩個(gè)項(xiàng)目到本地,這里會(huì)使用到項(xiàng)目中相關(guān)的插件。
git clone https://github.com/robbyedwards/octopress-tag-pages.git
git clone https://github.com/robbyedwards/octopress-tag-cloud.git
(1)在 Octopress-tag-pages 中,復(fù)制 plugins/tag_generator.rb 到 Octopress/plugins 目錄,復(fù)制 /source/_layouts/tag_index.html 到 /source/_layouts 目錄,復(fù)制 source/_includes/custom/tag_feed.xml 到 /source/_includes/custom/ 目錄。 (2)在 Octopress-tag-cloud中,復(fù)制 tag_cloud.rb 到 /plugins 目錄。
增加邊欄文件
進(jìn)入 Octopress\source_includes\custom\asides 目錄,創(chuàng)建 tags.html,添加如下代碼
<section>
<h1>Tags</h1>
<ul class="tag-cloud">
{% tag_cloud font-size: 90-210%, limit: 10, style: para %}
</ul>
</section>
添加標(biāo)簽到主頁(yè)面邊欄
打開(kāi) Octopress/_config.yml,在 default_asides 一欄中添加
default_asides: [..., custom/asides/tags.html]
增加新網(wǎng)頁(yè)
運(yùn)行如下命令
rake new_page['tag_cloud']
http://wiki.jikexueyuan.com/project/github-page/images/result.jpg" alt="" />
在導(dǎo)航欄添加新頁(yè)面
進(jìn)入 Octopress\source_includes\custom,打開(kāi) navigation.html,添加一欄
<li><a href="/tag-cloud/">Tags</a></li>
修改標(biāo)簽內(nèi)容
進(jìn)入 Octopress\source\tag-cloud 目錄,修改 index.markdown,添加如下內(nèi)容
<ul class="tag-cloud">{% tag_cloud font-size: 90-210%, limit: 1000, style: para %}</ul>
用 markdownpad 打開(kāi)博文,在 categories 下增加一行
tags: [Octopress]
rake generate
rake preview
rake deploy
打開(kāi) Tags 頁(yè)面 http://geekjacky.github.io/tag-cloud/
,如下
http://wiki.jikexueyuan.com/project/github-page/images/tagcloud.jpg" alt="" />
git add .
git commit -m "添加標(biāo)簽"
git push origin source
不想每次都手動(dòng)添加 tag 這一行,沒(méi)問(wèn)題。打開(kāi) Octopress 目錄下的 Rakefile,加入下面這一行,加在哪?你找得到的。
post.puts "tags: "
如果在使用標(biāo)簽生成靜態(tài)頁(yè)面的時(shí)候,出現(xiàn)如下錯(cuò)誤。
Liquid Exception: comparison of Array with Array failed in page