Create a gist now

Instantly share code, notes, and snippets.

@ongspxm /_cat_gen.rb
Last active Aug 19, 2016

What would you like to do?
Tag and Category plugin for Jekyll
module Jekyll
class CatIndex < Page
def initialize(site, base, dir, cat)
@site = site
@base = base
@dir = dir
@name = 'index.html'
self.process(@name)
self.read_yaml(File.join(base, '_layouts'), 'cat_index.html')
self.data['cat'] = cat
end
end
class CatGenerator < Generator
safe true
def generate(site)
if site.layouts.key? 'cat_index'
dir = 'blog/categories'
site.categories.keys.each do |cat|
write_cat_index(site, File.join(dir, cat), cat)
end
end
end
def write_cat_index(site, dir, cat)
index = CatIndex.new(site, site.source, dir, cat)
index.render(site.layouts, site.site_payload)
index.write(site.dest)
site.pages << index
end
end
end
module Jekyll
class TagIndex < 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['tag'] = tag
end
end
class TagGenerator < Generator
safe true
def generate(site)
if site.layouts.key? 'tag_index'
dir = 'blog/tags'
site.tags.keys.each do |tag|
write_tag_index(site, File.join(dir, tag), tag)
end
end
end
def write_tag_index(site, dir, tag)
index = TagIndex.new(site, site.source, dir, tag)
index.render(site.layouts, site.site_payload)
index.write(site.dest)
site.pages << index
end
end
end
---
layout: default
---
<style>
.tag .tag__title{
margin-top:1rem;
}
.tag .tag__content a{
display:inline-block;
line-height:1.25em;
}
</style>
<div class="tag">
<a class="tag__back" href="/">&lt;-- back</a>
<h1 class="tag__title">All {{ page.cat }}</h1>
<div class="tag__content">
{% if site.categories[page.cat] %}
<ul>
{% for post in site.categories[page.cat] %}
<li>
<span class="date">{{ post.date | date: '%Y %b %d' }}</span> - <a href="{{ post.url }}">{{ post.title }}</a>
</li>
{% endfor %}
</ul>
{% else %}
<p>There is no posts for this category.</p>
{% endif %}
</div>
</div>
{% if page.tags %}
<span> | </span>
{% for tag in page.tags %}
<span>
<a href='/blog/tags/{{tag}}'>{{ tag }}</a>{% if forloop.last==false %}, {% endif %}
</span>
{% endfor %}
{% endif %}
---
layout: default
---
<style>
.tag .tag__title{
margin-top:1rem;
}
.tag .tag__content a{
display:inline-block;
line-height:1.25em;
}
</style>
<div class="tag">
<a class="tag__back" href="/">&lt;-- back</a>
<h1 class="tag__title">Posts tagged "{{ page.tag }}"</h1>
<div class="tag__content">
{% if site.tags[page.tag] %}
<ul>
{% for post in site.tags[page.tag] %}
<li>
<span class="date">{{ post.date | date: '%Y %b %d' }}</span> - <a href="{{ post.url }}">{{ post.title }}</a>
</li>
{% endfor %}
</ul>
{% else %}
<p>There is no posts for this tag.</p>
{% endif %}
</div>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment