PageRenderTime 47ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/tag_generator.rb

https://gitlab.com/mcorp/mcorp-blog
Ruby | 169 lines | 84 code | 25 blank | 60 comment | 6 complexity | ae591fa781b8bf24af0a75be079da5ae MD5 | raw file
  1. # encoding: utf-8
  2. #
  3. # Octopress tag page generator
  4. #
  5. # Version: 0.3
  6. #
  7. # Copyright (c) 2012 Robby Edwards, http://robbyedwards.com/
  8. # Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php)
  9. #
  10. # Octopress plugin to generate tag pages.
  11. # Based upon the jekyll category page generator plugin by Dave Perrett.
  12. #
  13. #
  14. # A generator that creates tag pages for jekyll sites.
  15. #
  16. # See README for installation and usage instructions.
  17. module Jekyll
  18. # The TagIndex class creates a single tag page for the specified tag.
  19. class TagIndex < Page
  20. # Initializes a new TagIndex.
  21. #
  22. # +base+ is the String path to the <source>.
  23. # +tag_dir+ is the String path between <source> and the tag folder.
  24. # +tag+ is the tag currently being processed.
  25. def initialize(site, base, tag_dir, tag)
  26. @site = site
  27. @base = base
  28. @dir = tag_dir
  29. @name = 'index.html'
  30. self.process(@name)
  31. # Read the YAML data from the layout page.
  32. self.read_yaml(File.join(base, '_layouts'), 'tag_index.html')
  33. self.data['tag'] = tag
  34. # Set the title for this page.
  35. title_prefix = site.config['tag_title_prefix'] || 'Tag: '
  36. self.data['title'] = "#{title_prefix}#{tag}"
  37. # Set the meta-description for this page.
  38. meta_description_prefix = site.config['tag_meta_description_prefix'] || 'Tag: '
  39. self.data['description'] = "#{meta_description_prefix}#{tag}"
  40. end
  41. end
  42. # The TagFeed class creates an Atom feed for the specified tag.
  43. class TagFeed < Page
  44. # Initializes a new TagFeed.
  45. #
  46. # +base+ is the String path to the <source>.
  47. # +tag_dir+ is the String path between <source> and the tag folder.
  48. # +tag+ is the tag currently being processed.
  49. def initialize(site, base, tag_dir, tag)
  50. @site = site
  51. @base = base
  52. @dir = tag_dir
  53. @name = 'atom.xml'
  54. self.process(@name)
  55. # Read the YAML data from the layout page.
  56. self.read_yaml(File.join(base, '_includes/custom'), 'tag_feed.xml')
  57. self.data['tag'] = tag
  58. # Set the title for this page.
  59. title_prefix = site.config['tag_title_prefix'] || 'Tag: '
  60. self.data['title'] = "#{title_prefix}#{tag}"
  61. # Set the meta-description for this page.
  62. meta_description_prefix = site.config['tag_meta_description_prefix'] || 'Tag: '
  63. self.data['description'] = "#{meta_description_prefix}#{tag}"
  64. # Set the correct feed URL.
  65. self.data['feed_url'] = "#{tag_dir}/#{name}"
  66. end
  67. end
  68. # The Site class is a built-in Jekyll class with access to global site config information.
  69. class Site
  70. # Creates an instance of TagIndex for each tag page, renders it, and
  71. # writes the output to a file.
  72. #
  73. # +tag_dir+ is the String path to the tag folder.
  74. # +tag+ is the tag currently being processed.
  75. def write_tag_index(tag_dir, tag)
  76. index = TagIndex.new(self, self.source, tag_dir, tag)
  77. index.render(self.layouts, site_payload)
  78. index.write(self.dest)
  79. # Record the fact that this page has been added, otherwise Site::cleanup will remove it.
  80. self.pages << index
  81. # Create an Atom-feed for each index.
  82. feed = TagFeed.new(self, self.source, tag_dir, tag)
  83. feed.render(self.layouts, site_payload)
  84. feed.write(self.dest)
  85. # Record the fact that this page has been added, otherwise Site::cleanup will remove it.
  86. self.pages << feed
  87. end
  88. # Loops through the list of tag pages and processes each one.
  89. def write_tag_indexes
  90. if self.layouts.key? 'tag_index'
  91. dir = self.config['tag_dir'] || 'tags'
  92. self.tags.keys.each do |tag|
  93. self.write_tag_index(File.join(dir, tag.to_s.gsub(/_|\P{Word}/, '-').gsub(/-{2,}/, '-').downcase), tag)
  94. end
  95. # Throw an exception if the layout couldn't be found.
  96. else
  97. throw "No 'tag_index' layout found."
  98. end
  99. end
  100. end
  101. # Jekyll hook - the generate method is called by jekyll, and generates all of the tag pages.
  102. class GenerateTags < Generator
  103. safe true
  104. priority :low
  105. def generate(site)
  106. site.write_tag_indexes
  107. end
  108. end
  109. # Adds some extra filters used during the tag creation process.
  110. module Filters
  111. # Outputs a list of tags as comma-separated <a> links. This is used
  112. # to output the tag list for each post on a tag page.
  113. #
  114. # +tags+ is the list of tags to format.
  115. #
  116. # Returns string
  117. #
  118. def tag_links(tags)
  119. dir = @context.registers[:site].config['tag_dir']
  120. tags = tags.sort!.map do |item|
  121. "<a class='tag' href='/#{dir}/#{item.gsub(/_|\P{Word}/, '-').gsub(/-{2,}/, '-').downcase}/'>#{item}</a>"
  122. end
  123. case tags.length
  124. when 0
  125. ""
  126. when 1
  127. tags[0].to_s
  128. else
  129. "#{tags[0...-1].join(', ')}, #{tags[-1]}"
  130. end
  131. end
  132. # Outputs the post.date as formatted html, with hooks for CSS styling.
  133. #
  134. # +date+ is the date object to format as HTML.
  135. #
  136. # Returns string
  137. def date_to_html_string(date)
  138. result = '<span class="month">' + date.strftime('%b').upcase + '</span> '
  139. result += date.strftime('<span class="day">%d</span> ')
  140. result += date.strftime('<span class="year">%Y</span> ')
  141. result
  142. end
  143. end
  144. end