Article
Jekyll按照tag分页面
# 单页实现
从jekll-bootstrap检出的代码中,tags.html实现了标签的显示。但是所有的标签和日志列表都码在一个文件里面,总感觉有点太拥挤。
<h1>{{ page.title }} {% if page.tagline %} <small>{{ page.tagline }}</small>{% endif %}</h1>
{% assign tags_list = site.tags %}
{% if tags_list.first[0] == null %}
{% for tag in tags_list %}
<li><a href="#{{ tag }}-ref">{{ tag }} <span>{{ site.tags[tag].size }}</span></a></li>
{% endfor %}
{% else %}
{% for tag in tags_list %}
<li><a href="#{{ tag[0] }}-ref">{{ tag[0] }} <span>{{ tag[1].size }}</span></a></li>
{% endfor %}
{% endif %}
{% assign tags_list = nil %}
{% for tag in site.tags %}
<h2 id="{{ tag[0] }}-ref">{{ tag[0] }}</h2>
{% assign pages_list = tag[1] %}
{% if site.JB.pages_list.provider == "custom" %}
{% include custom/pages_list %}
{% else %}
{% for node in pages_list %}
{% if node.title != null %}
{% if group == null or group == node.group %}
{% if page.url == node.url %}
<a href="{{ BASE_PATH }}{{ node.url }}" class="active">{{ node.title | xml_escape }}</a>
<span><time datetime="{{ node.date | date: "%Y-%m-%d" }}">{{ node.date | date: "%Y/%m/%d" }}</time></span>
{% else %}
<a href="{{ BASE_PATH }}{{ node.url }}" class="active">{{ node.title | xml_escape }}</a>
<span><time datetime="{{ node.date | date: "%Y-%m-%d" }}">{{ node.date | date: "%Y/%m/%d" }}</time></span>
{% endif %}
{% endif %}
{% endif %}
{% endfor %}
{% endif %}
{% assign pages_list = nil %}
{% assign group = nil %}
{% endfor %}
# 插件实现分页面
找了一些资料,使用plugin的方式可以生成文件,以及页面的自定义标签。在_plugins目录下新增jekyll-tag-page.rb :
module Jekyll
class TagPage < Page
def initialize(site, base, dir, tag)
@site = site
@base = base
@dir = dir
@name = 'index.html'
self.process(@name)
self.read_yaml(File.join(base, '_layouts'), 'tag_index.html')
self.data['tags'] = tag
end
end
class TagPageGenerator < Generator
safe true
def generate(site)
if site.layouts.key? 'tag_index'
dir = site.config['tag_dir'] || 'tags'
site.tags.keys.each do |tag|
site.pages << TagPage.new(site, site.source, File.join(dir, tag), tag)
end
end
end
end
end
生成插件为每个TAG生成了一个页面,_layout模板设置为tag_index.html,在模板中可以根据当前页面的tags过滤并只显示该tag的日志列表。文件默认保存到tags/TAG目录下。
{% for tag in site.tags %}
{% if page.tags == tag[0] %}
<h2>{{ tag[0] }}</h2>
{% assign pages_list = tag[1] %}
{% for node in pages_list %}
{% if node.title != null %}
<a href="{{ BASE_PATH }}{{ node.url }}">{{ node.title | xml_escape }}</a>
<span><time datetime="{{ node.date | date: "%Y-%m-%d" }}">{{ node.date | date: "%Y/%m/%d" }}</time></span>
{% endif %}
{% endfor %}
{% assign pages_list = nil %}
{% endif %}
{% endfor %}
# 使用脚本生成目录和md文件来实现
但是由于github不支持自定义插件功能,也就是说,就算我提交了_plugin的代码也是无效的。最终最后的实现,使用Shell脚本在tags目录下生成文件夹和内容:
cd $tools/../tags
find * -type d -exec rm -rf {} +
for tag in `cat tags`; do
mkdir $tag
cat > $tag/index.md <<EOF
---
layout: tag
categories: [$tag]
---
EOF
done;
脚本列表tags文件内容生成目录和index.md文件。
layout模板tag.html页面代码如下:
<h3>Tag: {{ page.categories[-1] }}</h3>
{% for tag in site.tags %}
{% if page.categories[-1] == tag[0] %}
{% assign pages_list = tag[1] %}
{% for node in pages_list %}
{% if node.title != null %}
<time datetime="{{ node.date | date: "%Y-%m-%d" }}">
{{ node.date | date: "%Y/%m/%d" }}
<a href="{{ BASE_PATH }}{{ node.url }}" class="archive-link">{{ node.title | xml_escape }}</a>
{% endif %}
{% endfor %}
{% assign pages_list = nil %}
{% endif %}
{% endfor %}
–END
# 参考
–END
Related
Related posts
-
有一种自由叫做程序员的自由:把公众号文章镜像回自己的博客
2026-06-02
-
微信对接OpenAI
2023-03-18
-
octopress生成TOC
2021-12-08
-
logstash采集网站的访问日志
2018-01-20