site.rb 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. ##
  2. # Provide an icons attribute on the site object
  3. require 'yaml'
  4. require 'forwardable'
  5. module Jekyll
  6. class Icon
  7. attr_reader :name, :id, :unicode, :created, :categories
  8. def initialize(icon_object)
  9. @icon_object = icon_object
  10. # Class name used in CSS and HTML
  11. @icon_object['class'] = icon_object['id']
  12. # Normalize the aliases
  13. @icon_object['aliases'] ||= []
  14. @name = icon_object['name']
  15. @id = icon_object['id']
  16. @class = icon_object['class']
  17. @aliases = icon_object['aliases']
  18. @unicode = icon_object['unicode']
  19. @created = icon_object['created']
  20. @categories = icon_object['categories']
  21. end
  22. def to_liquid
  23. return @icon_object
  24. end
  25. end
  26. class IconList
  27. ##
  28. # A list of icons
  29. #
  30. include Enumerable
  31. extend Forwardable
  32. def_delegators :@icon_array, :each, :<<
  33. def initialize(icon_array)
  34. @original_icon_array = icon_array
  35. @icon_array = []
  36. icon_array.each { |icon_object|
  37. @icon_array << Icon.new(icon_object)
  38. }
  39. end
  40. def [](k)
  41. @icon_array[k]
  42. end
  43. def to_liquid
  44. @original_icon_array
  45. end
  46. end
  47. module IconFilters
  48. def expand_aliases(icons)
  49. expanded = []
  50. icons.each { |icon|
  51. # Remove the aliases since we are expanding them
  52. expanded << icon.reject{ |k| k == 'aliases'}
  53. icon['aliases'].each { |alias_id|
  54. alias_icon = expanded[-1].dup
  55. alias_icon['class'] = alias_id
  56. alias_icon['alias_of'] = icon
  57. expanded << alias_icon
  58. }
  59. }
  60. return expanded
  61. end
  62. def category(icons, cat)
  63. icons.select { |icon| icon['categories'].include?(cat) }
  64. end
  65. def version(icons, version)
  66. icons.select { |icon| icon['created'] == version }
  67. end
  68. def sort_by(icons, sort_key)
  69. icons.sort_by! { |icon| icon[sort_key] }
  70. end
  71. end
  72. Liquid::Template.register_filter(IconFilters)
  73. class Site
  74. attr_reader :icons
  75. def process
  76. self.reset_icons
  77. self.reset
  78. self.read
  79. self.generate
  80. self.render
  81. self.cleanup
  82. self.write
  83. self.build
  84. end
  85. ##
  86. # Reads the YAML file that stores all data about icons
  87. def reset_icons
  88. @icons = IconList.new(YAML.load_file(self.config['icon_meta'])['icons'])
  89. end
  90. ##
  91. # After generation, runs a build of Font-Awesome
  92. def build
  93. system("make build", :chdir => self.config['destination'], :out => :err)
  94. end
  95. def site_payload
  96. {
  97. "site" => self.config.merge({
  98. "time" => self.time,
  99. "posts" => self.posts.sort { |a, b| b <=> a },
  100. "pages" => self.pages,
  101. "html_pages" => self.pages.reject { |page| !page.html? },
  102. "categories" => post_attr_hash('categories'),
  103. "tags" => post_attr_hash('tags')}),
  104. "icons" => @icons,
  105. }
  106. end
  107. end
  108. end