netwjx

混乱与有序

Jekyll插件:分类云

| 评论

Octopress默认有存档页, 但是没有能列出所有分类的页面, 我试图找这方面的插件, 但是尝试使用的一些都不是很理想, 所以尝试自己写了一个这样的插件, 效果就如导航栏链接分类 哪样.

以下是代码, 多数代码都是从Jeykll插件示例中抄的, 第一次写实用的ruby程序, 也没多想注释什么的.

plugins/category_cloud.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
module Jekyll
  class CategoryCloudPage < Page
    def initialize(site, base, dir, cloud)
      @site = site
      @base = base
      @dir = dir
      @name = 'index.html'

      self.process(@name)
      self.read_yaml(File.join(base, '_layouts'), 'category_cloud.html')

      self.data['cloud'] = cloud
    end
  end

  class CategoryCloudGenerator < Generator
    safe true

    def generate(site)
      if site.layouts.key? 'category_cloud'
        dir = site.config['category_dir'] || 'categories'

        count = site.categories.map do |item|
          [item[0], item[1].length]
        end

        maxsize = 5
        minsize = 1
        min, max = count.map(&:last).minmax

        cloud = site.categories.map do |name, posts|
          {
            "title" => name,
            "rank"  => maxsize + minsize - ((posts.length - min) * (maxsize - minsize) / (max - min) + minsize),
            "link"  => "/#{dir}/#{name.gsub(/_|\P{Word}/, '-').gsub(/-{2,}/, '-').downcase}/"
          }
        end

        index = CategoryCloudPage.new(site, site.source, dir, cloud)
        index.render(site.layouts, site.site_payload)
        index.write(site.dest)
        site.pages << index
      end
    end
  end

end
sass/partials/_category_cloud.scss
1
2
3
4
5
6
7
.category-cloud {
    @for $i from 1 through 5 {
        .rank-#{$i} { font-size: nth(230% 210% 180% 140% 90%, $i); }
    }
    .rank-1, .rank-2, .rank-3, .rank-4 { font-weight:bold; }
    span { line-height: 1.25em; padding:0 5px; }
}

需要修改sass/_partials.scss, 结尾加入一行

sass/_partials.scss
1
@import "partials/category_cloud"
source/_layouts/category_cloud.html
1
2
3
4
5
6
7
8
9
10
11
---
layout: page
title: 分类
footer: false
---

<div class="category-cloud">
    {% for item in page.cloud %}
        <span class="rank-{{ item.rank }}"><a href="{{ item.link }}">{{ item.title }}</a></span>
    {% endfor %}
</div>

目前只做了生成分类页面, 侧边栏还没有做, 以后有做的冲动了再说吧.

参考资料

评论

Fork me on GitHub