66require 'cgi'
77
88use_helper Nanoc ::Helpers ::Rendering
9+ use_helper Nanoc ::Helpers ::LinkTo
910
1011def h ( text )
1112 CGI . escapeHTML ( text . nil? ? '' : text )
@@ -28,6 +29,7 @@ def to_json(book, url: :relative)
2829 'favorite' : book [ :favorite ] || false ,
2930 'cover' : "#{ url_prefix } #{ cover_path } " || '' ,
3031 'cover_mini' : "#{ url_prefix } #{ cover_mini_path } " || '' ,
32+ 'tags' : book [ :tags ] || [ ]
3133 }
3234end
3335
@@ -151,4 +153,70 @@ def recent_books()
151153
152154def feed_books ( )
153155 last_readings ( @config [ :site ] [ :feed ] [ :max_entries ] )
156+ end
157+
158+ def all_tags
159+ tags = { }
160+ @items . each do |item |
161+ next if item [ :tags ] . nil? || item [ :tags ] . empty? || !item [ :tag ] . nil?
162+
163+ item [ :tags ] . each do |tag |
164+ tags [ tag ] = [ ] if tags [ tag ] . nil?
165+ tags [ tag ] << item unless tags [ tag ] . include? item
166+ end
167+ end
168+ # tags.each do |tag, _|
169+ # tags[tag] = tags[tag].sort_by { |item| Date.parse(item[:datetime].to_s) }.reverse
170+ # end
171+ tags
172+ end
173+
174+ def tag_cloud
175+ # sort on hash sort by key which is what we want
176+ tags = all_tags . sort
177+ result = [ ]
178+ return result if tags . empty?
179+ # tag_<weight> goes from tag_0 to tag_10, map tag weight value to [0,10]
180+ max = tags . max_by { |_ , v | v . size } [ 1 ] . size
181+ min = tags . min_by { |_ , v | v . size } [ 1 ] . size
182+ tags . each do |tag , posts |
183+ posts_count = posts . size
184+ tag_weight = ( ( ( posts_count - min ) * 10.0 ) / max ) . round
185+ result << [ tag , tag_weight , posts_count ]
186+ end
187+ result
188+ end
189+
190+ def tag_slug ( tag )
191+ I18n . transliterate ( tag . downcase ) . gsub ( /\W / , '-' ) . gsub ( /-+/ , '-' ) . strip
192+ end
193+
194+ def tag_item_identifier ( tag )
195+ "/tag/#{ tag_slug ( tag ) } .*"
196+ end
197+
198+ def link_to_tag ( tag )
199+ tag_item = @items [ tag_item_identifier ( tag ) ]
200+ link_to ( tag_item [ :title ] , tag_item )
201+ end
202+
203+ def generate_tag_items
204+ all_tags . each do |tag , books |
205+ # item's content is used as fallback when no post can be found.
206+ @items . create ( "
207+ Aucun livre n'est étiquetté avec \" <%= @item[:title] %>\" .
208+
209+ Voir la liste des <%= link_to('autres étiquettes', @items['/all-tags.*']) %>.
210+ " , {
211+ title : tag ,
212+ tag : tag_slug ( tag ) ,
213+ layout : 'tags' ,
214+ # can't store item ref, from the preprocess block, the item view isn't suitable for future use
215+ # such as compiled_content, path, reps and so on.
216+ # only store identifier and request item from identifier at call site
217+ books : natural_sort ( books ) . map { |p | p . identifier . to_s } ,
218+ extension : 'md'
219+ } , Nanoc ::Identifier . new ( tag_item_identifier ( tag ) )
220+ )
221+ end
154222end
0 commit comments