site.rb 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. @name = icon_object['name']
  11. @id = icon_object['id']
  12. @aliases = icon_object['aliases']
  13. @unicode = icon_object['unicode']
  14. @created = icon_object['created']
  15. @categories = icon_object['categories']
  16. end
  17. def to_liquid
  18. return @icon_object
  19. end
  20. end
  21. class IconList
  22. ##
  23. # A list of icons
  24. #
  25. include Enumerable
  26. extend Forwardable
  27. def_delegators :@icon_array, :each, :<<
  28. def initialize(icon_array)
  29. @original_icon_array = icon_array
  30. @icon_array = []
  31. icon_array.each { |icon_object|
  32. @icon_array << Icon.new(icon_object)
  33. }
  34. end
  35. def [](k)
  36. @icon_array[k]
  37. end
  38. def to_liquid
  39. @original_icon_array
  40. end
  41. end
  42. module IconFilters
  43. def category(icons, cat)
  44. icons.select { |icon| icon['categories'].include?(cat) }
  45. end
  46. def version(icons, version)
  47. icons.select { |icon| icon['created'] == version }
  48. end
  49. end
  50. Liquid::Template.register_filter(IconFilters)
  51. class Site
  52. attr_reader :icons
  53. def process
  54. self.reset_icons
  55. self.reset
  56. self.read
  57. self.generate
  58. self.render
  59. self.cleanup
  60. self.write
  61. self.build
  62. end
  63. ##
  64. # Reads the YAML file that stores all data about icons
  65. def reset_icons
  66. @icons = IconList.new(YAML.load_file(self.config['icon_meta'])['icons'])
  67. end
  68. ##
  69. # After generation, runs a build of Font-Awesome
  70. def build
  71. system("make build", :chdir => self.config['destination'], :out => :err)
  72. end
  73. def site_payload
  74. {
  75. "site" => self.config.merge({
  76. "time" => self.time,
  77. "posts" => self.posts.sort { |a, b| b <=> a },
  78. "pages" => self.pages,
  79. "html_pages" => self.pages.reject { |page| !page.html? },
  80. "categories" => post_attr_hash('categories'),
  81. "tags" => post_attr_hash('tags')}),
  82. "icons" => @icons,
  83. }
  84. end
  85. end
  86. end