site.rb 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. @icons = IconList.new(YAML.load_file(self.config['icon_meta'])['icons'])
  55. self.reset
  56. self.read
  57. self.generate
  58. self.render
  59. self.cleanup
  60. self.write
  61. end
  62. def site_payload
  63. {
  64. "site" => self.config.merge({
  65. "time" => self.time,
  66. "posts" => self.posts.sort { |a, b| b <=> a },
  67. "pages" => self.pages,
  68. "html_pages" => self.pages.reject { |page| !page.html? },
  69. "categories" => post_attr_hash('categories'),
  70. "tags" => post_attr_hash('tags')}),
  71. "icons" => @icons,
  72. }
  73. end
  74. end
  75. end