jekyll-tagging 0.3.1 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/ChangeLog CHANGED
@@ -1,3 +1,8 @@
1
+ == 0.4.0 [2012-10-22]
2
+
3
+ * Heavy refactoring to better support monkey patching.
4
+ * Fixed issue when a number is used as a tag. (Fabrice Rossi)
5
+
1
6
  == 0.3.1 [2012-05-24]
2
7
 
3
8
  * Added support for Jekyll::Site#add_payload (custom extension).
data/Rakefile CHANGED
@@ -20,6 +20,6 @@ end
20
20
 
21
21
  begin
22
22
  require 'jekyll/testtasks/rake'
23
- rescue LoadError
24
- warn "Please install the `jekyll-testtasks' gem."
23
+ rescue LoadError => err
24
+ warn "Please install the `jekyll-testtasks' gem. (#{err})"
25
25
  end
@@ -7,58 +7,62 @@ module Jekyll
7
7
 
8
8
  safe true
9
9
 
10
- def generate(site)
11
- %w[TAG_PAGE_DIR TAG_FEED_DIR].each do |dir_name|
12
- unless Tagger.const_defined?(dir_name.to_sym)
13
- Tagger.const_set(dir_name, site.config[dir_name.downcase] || 'tag')
14
- end
15
- end
10
+ attr_accessor :site
16
11
 
17
- @tag_page_layout = site.config['tag_page_layout']
18
- @tag_feed_layout = site.config['tag_feed_layout']
12
+ @types = [:page, :feed]
19
13
 
20
- unless Jekyll::Filters.const_defined?(:PRETTY_URL)
21
- Jekyll::Filters.const_set('PRETTY_URL', site.permalink_style == :pretty)
22
- end
14
+ class << self; attr_accessor :types, :site; end
23
15
 
24
- if @tag_page_layout
25
- generate_tag_pages(site)
26
- else
27
- warn 'WARNING: You have to define a tag_page_layout in configuration file.'
28
- end
16
+ def generate(site)
17
+ self.class.site = self.site = site
29
18
 
30
- t = { 'tag_data' => calculate_tag_cloud(site) }
31
- site.respond_to?(:add_payload) ? site.add_payload(t) : site.config.update(t)
19
+ generate_tag_pages
20
+ add_tag_cloud
32
21
  end
33
22
 
23
+ private
24
+
34
25
  # Generates a page per tag and adds them to all the pages of +site+.
35
26
  # A <tt>tag_page_layout</tt> have to be defined in your <tt>_config.yml</tt>
36
27
  # to use this.
37
- def generate_tag_pages(site)
38
- site.tags.each { |tag, posts|
39
- site.pages << new_tag(site, site.source, TAG_PAGE_DIR, tag, posts.sort.reverse, @tag_page_layout)
40
- if @tag_feed_layout
41
- site.pages << new_tag(site, site.source, TAG_FEED_DIR, tag, posts.sort.reverse, @tag_feed_layout)
28
+ def generate_tag_pages
29
+ site.tags.each { |tag, posts| new_tag(tag, posts) }
30
+ end
31
+
32
+ def new_tag(tag, posts)
33
+ self.class.types.each { |type|
34
+ if layout = site.config["tag_#{type}_layout"]
35
+ data = { 'layout' => layout, 'posts' => posts.sort.reverse! }
36
+
37
+ name = yield data if block_given?
38
+
39
+ site.pages << TagPage.new(
40
+ site, site.source, site.config["tag_#{type}_dir"],
41
+ "#{name || tag}#{site.layouts[data['layout']].ext}", data
42
+ )
42
43
  end
43
44
  }
44
45
  end
45
46
 
46
- def new_tag(site, base, dir, tag, posts, layout)
47
- TagPage.new(site, base, dir, "#{tag}#{site.layouts[layout].ext}", {
48
- 'layout' => layout,
49
- 'posts' => posts,
50
- })
47
+ def add_tag_cloud(num = 5, name = 'tag_data')
48
+ s, t = site, { name => calculate_tag_cloud(num) }
49
+ s.respond_to?(:add_payload) ? s.add_payload(t) : s.config.update(t)
51
50
  end
52
51
 
53
52
  # Calculates the css class of every tag for a tag cloud. The possible
54
53
  # classes are: set-1..set-5.
55
54
  #
56
55
  # [[<TAG>, <CLASS>], ...]
57
- def calculate_tag_cloud(site, num = 5)
58
- tags = site.tags.map { |tag, posts| [tag, posts.size] }.sort
59
- range = 1..tags.map { |_, size| size }.max unless tags.empty?
56
+ def calculate_tag_cloud(num = 5)
57
+ range = 0
58
+
59
+ tags = site.tags.map { |tag, posts|
60
+ [tag.to_s, range < (size = posts.size) ? range = size : size]
61
+ }
62
+
63
+ range = 1..range
60
64
 
61
- tags.map { |tag, size| [tag, range.quantile(size, num)] }
65
+ tags.sort!.map! { |tag, size| [tag, range.quantile(size, num)] }
62
66
  end
63
67
 
64
68
  end
@@ -69,14 +73,12 @@ module Jekyll
69
73
  self.content = data.delete('content') || ''
70
74
  self.data = data
71
75
 
72
- dir = dir[-1, 1] == '/' ? dir : '/' + dir
73
-
74
- super(site, base, dir, name)
76
+ super(site, base, dir[-1, 1] == '/' ? dir : '/' + dir, name)
75
77
 
76
- self.data['tag'] = basename
78
+ data['tag'] ||= basename
77
79
  end
78
80
 
79
- def read_yaml(_, __)
81
+ def read_yaml(*)
80
82
  # Do nothing
81
83
  end
82
84
 
@@ -86,24 +88,25 @@ module Jekyll
86
88
 
87
89
  def tag_cloud(site)
88
90
  site['tag_data'].map { |tag, set|
89
- tag_link(tag, tag_url(tag), { :class => "set-#{set}" })
91
+ tag_link(tag, tag_url(tag), :class => "set-#{set}")
90
92
  }.join(' ')
91
93
  end
92
94
 
93
95
  def tag_link(tag, url = tag_url(tag), html_opts = nil)
94
- unless html_opts.nil?
95
- html_opts = ' ' + html_opts.map { |k, v| %Q{#{k}="#{v}"} }.join(' ')
96
- end
96
+ html_opts &&= ' ' << html_opts.map { |k, v| %Q{#{k}="#{v}"} }.join(' ')
97
97
  %Q{<a href="#{url}"#{html_opts}>#{tag}</a>}
98
98
  end
99
99
 
100
- def tag_url(tag)
101
- "/#{Tagger::TAG_PAGE_DIR}/#{ERB::Util.u(tag)}#{'.html' unless PRETTY_URL}"
100
+ def tag_url(tag, type = :page, site = Tagger.site)
101
+ url = File.join('', site.config["tag_#{type}_dir"], ERB::Util.u(tag))
102
+ site.permalink_style == :pretty ? url : url << '.html'
102
103
  end
103
104
 
104
105
  def tags(obj)
105
- tags = obj['tags'][0].is_a?(Array) ? obj['tags'].map{ |t| t[0] } : obj['tags']
106
- tags.map { |t| tag_link(t, tag_url(t)) if t.is_a?(String) }.compact.join(', ')
106
+ tags = obj['tags'].dup
107
+ tags.map! { |t| t.first } if tags.first.is_a?(Array)
108
+ tags.map! { |t| tag_link(t, tag_url(t)) if t.is_a?(String) }.compact!
109
+ tags.join(', ')
107
110
  end
108
111
 
109
112
  end
@@ -5,8 +5,8 @@ module Jekyll
5
5
  module Version
6
6
 
7
7
  MAJOR = 0
8
- MINOR = 3
9
- TINY = 1
8
+ MINOR = 4
9
+ TINY = 0
10
10
 
11
11
  class << self
12
12
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jekyll-tagging
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2012-05-24 00:00:00.000000000 Z
13
+ date: 2012-10-22 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: ruby-nuggets
@@ -51,7 +51,7 @@ rdoc_options:
51
51
  - --line-numbers
52
52
  - --all
53
53
  - --title
54
- - jekyll-tagging Application documentation (v0.3.1)
54
+ - jekyll-tagging Application documentation (v0.4.0)
55
55
  - --main
56
56
  - ChangeLog
57
57
  require_paths: