Class: Awestruct::Extensions::Tagger

Inherits:
Object
  • Object
show all
Defined in:
lib/awestruct/extensions/tagger.rb

Defined Under Namespace

Classes: TagStat

Instance Method Summary collapse

Constructor Details

#initialize(tagged_items_property, input_path, output_path = 'tags', pagination_opts = {}) ⇒ Tagger

Returns a new instance of Tagger.



20
21
22
23
24
25
# File 'lib/awestruct/extensions/tagger.rb', line 20

def initialize(tagged_items_property, input_path, output_path='tags', pagination_opts={})
  @tagged_items_property = tagged_items_property
  @input_path  = input_path
  @output_path = output_path
  @pagination_opts = pagination_opts
end

Instance Method Details

#execute(site) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/awestruct/extensions/tagger.rb', line 27

def execute(site)
  @tags ||= {}
  all = site.send( @tagged_items_property )
  return if ( all.nil? || all.empty? ) 

  all.each do |page|
    tags = page.tags
    if ( tags && ! tags.empty? )
      tags.each do |tag|
        tag = tag.to_s
        @tags[tag] ||= TagStat.new( tag, [] )
        @tags[tag].pages << page
      end
    end
  end

  all.each do |page|
    page.tags = (page.tags||[]).collect{|t| @tags[t]}
  end

  ordered_tags = @tags.values
  ordered_tags.sort!{|l,r| -(l.pages.size <=> r.pages.size)}
  #ordered_tags = ordered_tags[0,100]
  ordered_tags.sort!{|l,r| l.to_s <=> r.to_s}

  min = 9999
  max = 0

  ordered_tags.each do |tag|
    min = tag.pages.size if ( tag.pages.size < min )
    max = tag.pages.size if ( tag.pages.size > max )
  end

  span = max - min
  
  if span > 0
    slice = span / 6
    ordered_tags.each do |tag|
      adjusted_size = tag.pages.size - min
      scaled_size = adjusted_size / slice
      tag.group = ( tag.pages.size - min ) / slice
    end
  else
    ordered_tags.each do |tag|
      tag.group = 0
    end
  end

  @tags.values.each do |tag|
    paginator = Awestruct::Extensions::Paginator.new( @tagged_items_property, @input_path, { :remove_input=>false, :output_prefix=>File.join( @output_path, tag.to_s), :collection=>tag.pages }.merge( @pagination_opts ) )
    primary_page = paginator.execute( site )
    tag.primary_page = primary_page
  end

  site.send( "#{@tagged_items_property}_tags=", ordered_tags )
end