From 6b6fea89388f806b42cda8ec35c52628c426d144 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Mon, 12 Oct 2015 13:52:58 +0200 Subject: [PATCH 1/1] add categories --- personal/_config.yml | 3 +- personal/_includes/post-feed.html | 12 ++++ personal/_includes/post-header.html | 7 +- personal/_includes/post-list.html | 8 +++ personal/_includes/vars/blog-category.html | 1 + personal/_layouts/category_feed.html | 8 +++ personal/_layouts/category_index.html | 9 +++ personal/_layouts/feed.html | 18 +++++ personal/_layouts/{default.html => page.html} | 5 +- personal/_plugins/categories.rb | 65 +++++++++++++++++++ personal/_plugins/readmes.rb | 2 +- personal/blog/feed.xml | 32 ++------- personal/blog/index.html | 14 +--- 13 files changed, 139 insertions(+), 45 deletions(-) create mode 100644 personal/_includes/post-feed.html create mode 100644 personal/_includes/post-list.html create mode 100644 personal/_includes/vars/blog-category.html create mode 100644 personal/_layouts/category_feed.html create mode 100644 personal/_layouts/category_index.html create mode 100644 personal/_layouts/feed.html rename personal/_layouts/{default.html => page.html} (80%) create mode 100644 personal/_plugins/categories.rb diff --git a/personal/_config.yml b/personal/_config.yml index d96c35d..a19a6d0 100644 --- a/personal/_config.yml +++ b/personal/_config.yml @@ -7,6 +7,7 @@ timezone: "Europe/Berlin" blog: title: "Ralf's Ramblings" + category_dir: "/blog/categories" readmes: src_base: "/home/r/src" @@ -24,7 +25,7 @@ defaults: - scope: path: "" # all files in the project values: - layout: "default" + layout: "page" - scope: path: "" type: "posts" diff --git a/personal/_includes/post-feed.html b/personal/_includes/post-feed.html new file mode 100644 index 0000000..18959ed --- /dev/null +++ b/personal/_includes/post-feed.html @@ -0,0 +1,12 @@ +{% for post in include.posts limit:12 %} + + {{ post.title | xml_escape }} + + {{ post.date | date_to_xmlschema }} + {{ post.url | prepend: site.baseurl | prepend: site.url }} + {% for cat in post.categories %} + + {% endfor %} + {{ post.content | xml_escape }} + +{% endfor %} diff --git a/personal/_includes/post-header.html b/personal/_includes/post-header.html index 6163cf5..435447b 100644 --- a/personal/_includes/post-header.html +++ b/personal/_includes/post-header.html @@ -1 +1,6 @@ -
{{ include.item.date | date: "%b %-d, %Y" }}{% if include.item.author %} • {{ include.item.author }}{% endif %}{% if include.item.meta %} • {{ include.item.meta }}{% endif %} • Permalink
+
{{ include.item.date | date: "%b %-d, %Y" }} + {% if include.item.author %} • {{ include.item.author }}{% endif %} + {% if include.item.meta %} • {{ include.item.meta }}{% endif %} + {% if include.item.categories %} • {{ include.item.categories | category_links }}{% endif %} + • Permalink +
diff --git a/personal/_includes/post-list.html b/personal/_includes/post-list.html new file mode 100644 index 0000000..7b14efc --- /dev/null +++ b/personal/_includes/post-list.html @@ -0,0 +1,8 @@ +{% for post in include.posts %} +
+ {% include post-header.html item=post %} +

{{ post.title }}

+
+ {{ post.excerpt }} +

Read more...

+{% endfor %} diff --git a/personal/_includes/vars/blog-category.html b/personal/_includes/vars/blog-category.html new file mode 100644 index 0000000..a16df5a --- /dev/null +++ b/personal/_includes/vars/blog-category.html @@ -0,0 +1 @@ +{% assign pretitle = site.blog.title %} diff --git a/personal/_layouts/category_feed.html b/personal/_layouts/category_feed.html new file mode 100644 index 0000000..db32965 --- /dev/null +++ b/personal/_layouts/category_feed.html @@ -0,0 +1,8 @@ +--- +layout: feed +hide: true +vars: "blog-category.html" +--- + +{% assign posts = site.categories[page.category] %} +{% include post-feed.html posts=posts %} diff --git a/personal/_layouts/category_index.html b/personal/_layouts/category_index.html new file mode 100644 index 0000000..315dd44 --- /dev/null +++ b/personal/_layouts/category_index.html @@ -0,0 +1,9 @@ +--- +layout: page +hide: true +rss: true +vars: "blog-category.html" +--- + +{% assign posts = site.categories[page.category] %} +{% include post-list.html posts=posts %} diff --git a/personal/_layouts/feed.html b/personal/_layouts/feed.html new file mode 100644 index 0000000..446a38d --- /dev/null +++ b/personal/_layouts/feed.html @@ -0,0 +1,18 @@ + + + {% assign title = page.title %} + {% assign pretitle = page.pretitle %} + {% if page.vars %}{% include {{ page.vars | prepend: "vars/" }} %}{% endif %} + {% if pretitle %}{{ pretitle | xml_escape }} • {% endif %}{{ title | xml_escape }} + + {% if page.category %} + + {% else %} + + {% endif %} + {{ site.time | date_to_xmlschema }} + + Ralf Jung + + {{ content }} + diff --git a/personal/_layouts/default.html b/personal/_layouts/page.html similarity index 80% rename from personal/_layouts/default.html rename to personal/_layouts/page.html index 3ac3dee..a45f67f 100644 --- a/personal/_layouts/default.html +++ b/personal/_layouts/page.html @@ -14,7 +14,10 @@ - {% if page.rss %}{% endif %} + {% if page.rss %} + + {% if page.category %}{% endif %} + {% endif %}
diff --git a/personal/_plugins/categories.rb b/personal/_plugins/categories.rb new file mode 100644 index 0000000..69375da --- /dev/null +++ b/personal/_plugins/categories.rb @@ -0,0 +1,65 @@ +module Jekyll + + class CategoryPage < Page + def initialize(site, base, dir, name, category, layout) + @site = site + @base = base + @dir = dir + @name = name + + self.process(@name) + self.read_yaml(File.join(base, '_layouts'), layout) + self.data['category'] = category + + category_title_prefix = site.config['blog']['category_title_prefix'] || 'Category: ' + self.data['title'] = "#{category_title_prefix}#{category.capitalize}" + end + end + + class CategoryPageGenerator < Generator + safe true + + def generate(site) + dir = site.config['blog']['category_dir'] || 'categories' + if site.layouts.key? 'category_index' + site.categories.each_key do |category| + site.pages << CategoryPage.new(site, site.source, dir, "#{category}.html", category, 'category_index.html') + end + end + if site.layouts.key? 'category_feed' + site.categories.each_key do |category| + site.pages << CategoryPage.new(site, site.source, dir, "#{category}.xml", category, 'category_feed.html') + end + end + end + end + + # Adds some extra filters used during the category creation process. + module Filters + + # Outputs a list of categories as comma-separated links. This is used + # to output the category list for each post on a category page. + # + # +categories+ is the list of categories to format. + # + # Returns string + def category_links(categories) + base_dir = @context.registers[:site].config['blog']['category_dir'] + categories = categories.sort!.map do |category| + category_url = File.join(base_dir, "#{category}.html") + # Make sure the category directory begins with a slash. + category_dir = "/#{category_dir}" unless category_dir =~ /^\// + "#{category.capitalize}" + end + + case categories.length + when 0 + "" + when 1 + categories[0].to_s + else + categories.join(', ') + end + end + end +end diff --git a/personal/_plugins/readmes.rb b/personal/_plugins/readmes.rb index 922d3a8..63df995 100644 --- a/personal/_plugins/readmes.rb +++ b/personal/_plugins/readmes.rb @@ -12,7 +12,7 @@ module Jekyll content = File.read(src, self.merged_file_read_opts({})).each_line.to_a - self.data['layout'] = 'default' + self.data['layout'] = 'page' self.data['title'] = content[0].match(/^#* ?(.*)\n$/)[1] slug = self.data['title'].match(/^([^:]*):.*$/) if slug diff --git a/personal/blog/feed.xml b/personal/blog/feed.xml index 7a7e919..17b789d 100644 --- a/personal/blog/feed.xml +++ b/personal/blog/feed.xml @@ -1,31 +1,7 @@ --- -layout: null +layout: feed slug: Subscribe +vars: blog-index.html --- - - - - {{ site.blog.title | xml_escape }} - {{ site.blog.title | xml_escape }} - {{ site.url }}{{ site.baseurl }}/ - - {{ site.time | date_to_rfc822 }} - {{ site.time | date_to_rfc822 }} - Jekyll v{{ jekyll.version }} - {% for post in site.posts limit:10 %} - - {{ post.title | xml_escape }} - {{ post.content | xml_escape }} - {{ post.date | date_to_rfc822 }} - {{ post.url | prepend: site.baseurl | prepend: site.url }} - {{ post.url | prepend: site.baseurl | prepend: site.url }} - {% for tag in post.tags %} - {{ tag | xml_escape }} - {% endfor %} - {% for cat in post.categories %} - {{ cat | xml_escape }} - {% endfor %} - - {% endfor %} - - + +{% include post-feed.html posts=site.posts %} diff --git a/personal/blog/index.html b/personal/blog/index.html index 380aaaa..f85ed56 100644 --- a/personal/blog/index.html +++ b/personal/blog/index.html @@ -5,16 +5,4 @@ rss: true sort: 10 --- -{% assign num_posts = 5 %} -{% for post in site.posts %} - {% assign num_posts = num_posts | minus: 1 %} - {% if num_posts >= 0 %} -
- {% include post-header.html item=post %} -

{{ post.title }}

-
- {{ post.excerpt }} - -

Read more...

- {% endif %} -{% endfor %} +{% include post-list.html posts=site.posts %} -- 2.30.2