summaryrefslogtreecommitdiff
path: root/pyssg.xyz/live/static/fork-awesome/src/doc/_plugins/site.rb
blob: 0784d0dd6198539a544a1add9a428a6019c6ad0d (plain)
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
##
# Provide an icons attribute on the site object

require 'yaml'
require 'forwardable'

module Jekyll

  class Icon

    attr_reader :name, :id, :unicode, :created, :categories

    def initialize(icon_object)
      @icon_object = icon_object

      # Class name used in CSS and HTML
      @icon_object['class'] = icon_object['id']
      # Normalize the aliases
      @icon_object['aliases'] ||= []

      @name = icon_object['name']
      @id = icon_object['id']
      @class = icon_object['class']
      @aliases = icon_object['aliases']
      @unicode = icon_object['unicode']
      @created = icon_object['created']
      @categories = icon_object['categories']
    end

    def to_liquid
      return @icon_object
    end

  end

  class IconList
    ##
    # A list of icons
    #
    include Enumerable
    extend Forwardable

    def_delegators :@icon_array, :each, :<<

    def initialize(icon_array)
      @original_icon_array = icon_array
      @icon_array = []

      icon_array.each { |icon_object|
        @icon_array << Icon.new(icon_object)
      }
    end

    def [](k)
      @icon_array[k]
    end

    def to_liquid
      @original_icon_array
    end

  end

  module IconFilters
    def expand_aliases(icons)
      expanded = []

      icons.each { |icon|
        # Remove the aliases since we are expanding them
        expanded << icon.reject{ |k| k == 'aliases'}

        icon['aliases'].each { |alias_id|
          alias_icon = expanded[-1].dup
          alias_icon['class'] = alias_id
          alias_icon['alias_of'] = icon

          expanded << alias_icon
        }
      }

      return expanded
    end

    def category(icons, cat)
      icons.select { |icon| icon['categories'].include?(cat) }
    end

    def version(icons, version)
      icons.select { |icon| icon['created'] == version }
    end

    def sort_by(icons, sort_key)
      icons.sort_by! { |icon| icon[sort_key] }
    end
  end

  Liquid::Template.register_filter(IconFilters)

  class Site

    attr_reader :icons

    def process
      self.reset_icons
      self.reset
      self.read
      self.generate
      self.render
      self.cleanup
      self.write

      self.build
    end

    ##
    # Reads the YAML file that stores all data about icons
    def reset_icons
      @icons = IconList.new(YAML.load_file(self.config['icon_meta'])['icons'])
    end

    ##
    # After generation, runs a build of Font-Awesome
    def build
      system("make build", :chdir => self.config['destination'], :out => :err)
    end

    def site_payload
      {
        "site"       => self.config.merge({
          "time"       => self.time,
          "posts"      => self.posts.docs.sort { |a, b| b <=> a },
          "pages"      => self.pages,
          "html_pages" => self.pages.reject { |page| !page.html? },
          "categories" => post_attr_hash('categories'),
          "tags"       => post_attr_hash('tags')}),
        "icons"      => @icons,
      }
    end

  end

end