nanoc3 3.0.9 → 3.1.0a1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (104) hide show
  1. data/LICENSE +1 -1
  2. data/NEWS.md +360 -0
  3. data/README.md +85 -0
  4. data/Rakefile +2 -2
  5. data/bin/nanoc3 +0 -4
  6. data/lib/nanoc3/base/code_snippet.rb +14 -6
  7. data/lib/nanoc3/base/compiler.rb +68 -49
  8. data/lib/nanoc3/base/compiler_dsl.rb +70 -29
  9. data/lib/nanoc3/base/context.rb +47 -0
  10. data/lib/nanoc3/base/core_ext/array.rb +4 -0
  11. data/lib/nanoc3/base/core_ext/hash.rb +5 -1
  12. data/lib/nanoc3/base/core_ext/string.rb +2 -0
  13. data/lib/nanoc3/base/data_source.rb +132 -96
  14. data/lib/nanoc3/base/dependency_tracker.rb +160 -185
  15. data/lib/nanoc3/base/directed_graph.rb +252 -0
  16. data/lib/nanoc3/base/errors.rb +52 -4
  17. data/lib/nanoc3/base/filter.rb +43 -28
  18. data/lib/nanoc3/base/item.rb +93 -25
  19. data/lib/nanoc3/base/item_rep.rb +166 -55
  20. data/lib/nanoc3/base/layout.rb +16 -13
  21. data/lib/nanoc3/base/notification_center.rb +28 -12
  22. data/lib/nanoc3/base/plugin_registry.rb +158 -0
  23. data/lib/nanoc3/base/rule.rb +27 -8
  24. data/lib/nanoc3/base/rule_context.rb +59 -46
  25. data/lib/nanoc3/base/site.rb +124 -77
  26. data/lib/nanoc3/base.rb +7 -2
  27. data/lib/nanoc3/cli/base.rb +4 -1
  28. data/lib/nanoc3/cli/commands/autocompile.rb +5 -4
  29. data/lib/nanoc3/cli/commands/compile.rb +28 -7
  30. data/lib/nanoc3/cli/commands/create_item.rb +1 -1
  31. data/lib/nanoc3/cli/commands/create_layout.rb +1 -1
  32. data/lib/nanoc3/cli/commands/create_site.rb +46 -22
  33. data/lib/nanoc3/cli/commands/debug.rb +100 -0
  34. data/lib/nanoc3/cli/commands/help.rb +1 -1
  35. data/lib/nanoc3/cli/commands/info.rb +1 -1
  36. data/lib/nanoc3/cli/commands/view.rb +85 -0
  37. data/lib/nanoc3/cli/commands.rb +2 -0
  38. data/lib/nanoc3/cli/logger.rb +7 -0
  39. data/lib/nanoc3/cli.rb +0 -3
  40. data/lib/nanoc3/data_sources/{delicious.rb → deprecated/delicious.rb} +1 -24
  41. data/lib/nanoc3/data_sources/{last_fm.rb → deprecated/last_fm.rb} +1 -27
  42. data/lib/nanoc3/data_sources/{twitter.rb → deprecated/twitter.rb} +1 -14
  43. data/lib/nanoc3/data_sources/filesystem.rb +188 -176
  44. data/lib/nanoc3/data_sources/filesystem_unified.rb +107 -0
  45. data/lib/nanoc3/data_sources/filesystem_verbose.rb +80 -0
  46. data/lib/nanoc3/data_sources.rb +18 -9
  47. data/lib/nanoc3/extra/core_ext/enumerable.rb +39 -0
  48. data/lib/nanoc3/extra/core_ext/time.rb +2 -2
  49. data/lib/nanoc3/extra/core_ext.rb +1 -0
  50. data/lib/nanoc3/extra/deployers/rsync.rb +49 -3
  51. data/lib/nanoc3/extra/file_proxy.rb +7 -0
  52. data/lib/nanoc3/extra/vcs.rb +25 -24
  53. data/lib/nanoc3/extra/vcses/bazaar.rb +4 -0
  54. data/lib/nanoc3/extra/vcses/dummy.rb +4 -0
  55. data/lib/nanoc3/extra/vcses/git.rb +4 -0
  56. data/lib/nanoc3/extra/vcses/mercurial.rb +4 -0
  57. data/lib/nanoc3/extra/vcses/subversion.rb +4 -0
  58. data/lib/nanoc3/extra.rb +4 -1
  59. data/lib/nanoc3/filters/erb.rb +1 -1
  60. data/lib/nanoc3/filters/erubis.rb +1 -1
  61. data/lib/nanoc3/filters/haml.rb +1 -1
  62. data/lib/nanoc3/filters/kramdown.rb +14 -0
  63. data/lib/nanoc3/filters/maruku.rb +1 -1
  64. data/lib/nanoc3/filters/rainpress.rb +1 -1
  65. data/lib/nanoc3/filters/rdiscount.rb +3 -1
  66. data/lib/nanoc3/filters.rb +2 -0
  67. data/lib/nanoc3/helpers/blogging.rb +91 -75
  68. data/lib/nanoc3/helpers/breadcrumbs.rb +18 -10
  69. data/lib/nanoc3/helpers/capturing.rb +24 -29
  70. data/lib/nanoc3/helpers/filtering.rb +20 -17
  71. data/lib/nanoc3/helpers/html_escape.rb +7 -4
  72. data/lib/nanoc3/helpers/link_to.rb +51 -41
  73. data/lib/nanoc3/helpers/rendering.rb +15 -8
  74. data/lib/nanoc3/helpers/tagging.rb +27 -21
  75. data/lib/nanoc3/helpers/text.rb +12 -8
  76. data/lib/nanoc3/helpers/xml_sitemap.rb +13 -15
  77. data/lib/nanoc3/tasks/deploy/rsync.rake +4 -1
  78. data/lib/nanoc3/tasks.rb +2 -1
  79. data/lib/nanoc3.rb +24 -1
  80. metadata +43 -87
  81. data/NEWS.rdoc +0 -328
  82. data/README.rdoc +0 -83
  83. data/lib/nanoc3/base/plugin.rb +0 -88
  84. data/lib/nanoc3/base/preprocessor_context.rb +0 -37
  85. data/lib/nanoc3/data_sources/filesystem_combined.rb +0 -214
  86. data/lib/nanoc3/data_sources/filesystem_common.rb +0 -22
  87. data/lib/nanoc3/data_sources/filesystem_compact.rb +0 -256
  88. data/lib/nanoc3/extra/context.rb +0 -24
  89. data/lib/nanoc3/package.rb +0 -107
  90. data/vendor/cri/ChangeLog +0 -0
  91. data/vendor/cri/LICENSE +0 -19
  92. data/vendor/cri/NEWS +0 -0
  93. data/vendor/cri/README +0 -4
  94. data/vendor/cri/Rakefile +0 -25
  95. data/vendor/cri/lib/cri/base.rb +0 -153
  96. data/vendor/cri/lib/cri/command.rb +0 -105
  97. data/vendor/cri/lib/cri/core_ext/string.rb +0 -41
  98. data/vendor/cri/lib/cri/core_ext.rb +0 -8
  99. data/vendor/cri/lib/cri/option_parser.rb +0 -186
  100. data/vendor/cri/lib/cri.rb +0 -12
  101. data/vendor/cri/test/test_base.rb +0 -6
  102. data/vendor/cri/test/test_command.rb +0 -6
  103. data/vendor/cri/test/test_core_ext.rb +0 -21
  104. data/vendor/cri/test/test_option_parser.rb +0 -279
@@ -2,76 +2,59 @@
2
2
 
3
3
  module Nanoc3::Helpers
4
4
 
5
- # Nanoc3::Helpers::Blogging provides some functionality for building blogs,
6
- # such as finding articles and constructing feeds.
5
+ # Provides functionality for building blogs, such as finding articles and
6
+ # constructing feeds.
7
7
  #
8
8
  # This helper has a few requirements. First, all blog articles should have
9
9
  # the following attributes:
10
10
  #
11
- # * 'kind', set to 'article'.
11
+ # * `kind` Set to `"article"`
12
12
  #
13
- # * 'created_at', set to the creation timestamp.
13
+ # * `created_at` The article’s publication timestamp. This timestamp can
14
+ # be in any format parseable by `Time.parse`.
14
15
  #
15
- # Some functions in this blogging helper, such as the +atom_feed+ function,
16
+ # Some functions in this blogging helper, such as the {#atom_feed} function,
16
17
  # require additional attributes to be set; these attributes are described in
17
18
  # the documentation for these functions.
18
19
  #
19
- # The two main functions are sorted_articles and atom_feed.
20
- #
21
- # To activate this helper, +include+ it, like this:
22
- #
23
- # include Nanoc3::Helpers::Blogging
20
+ # The two main functions are {#sorted_articles} and {#atom_feed}.
24
21
  module Blogging
25
22
 
26
- # Returns an unsorted list of articles.
23
+ # Returns an unsorted list of articles, i.e. items where the `kind`
24
+ # attribute is set to `"article"`.
25
+ #
26
+ # @return [Array] An array containing all articles
27
27
  def articles
28
28
  @items.select { |item| item[:kind] == 'article' }
29
29
  end
30
30
 
31
- # Returns a list of articles, sorted by descending creation date (so newer
32
- # articles appear first).
31
+ # Returns a sorted list of articles, i.e. items where the `kind`
32
+ # attribute is set to `"article"`. Articles are sorted by descending
33
+ # creation date, so newer articles appear before older articles.
34
+ #
35
+ # @return [Array] A sorted array containing all articles
33
36
  def sorted_articles
34
37
  require 'time'
35
- articles.sort_by do |a|
36
- time = a[:created_at]
37
- time.is_a?(String) ? Time.parse(time) : time
38
- end.reverse
38
+ articles.sort_by { |a| t = a[:created_at] ; t.is_a?(String) ? Time.parse(t) : t }.reverse
39
39
  end
40
40
 
41
41
  # Returns a string representing the atom feed containing recent articles,
42
- # sorted by descending creation date. +params+ is a hash where the
43
- # following keys can be set:
44
- #
45
- # +limit+:: The maximum number of articles to show. Defaults to 5.
46
- #
47
- # +articles+:: A list of articles to include in the feed. Defaults to the
48
- # list of articles returned by the articles function.
49
- #
50
- # +content_proc+:: A proc that returns the content of the given article,
51
- # passed as a parameter. By default, given the argument
52
- # +article+, this proc will return
53
- # +article.reps[0].content+. This function may not return
54
- # nil.
55
- #
56
- # +excerpt_proc+:: A proc that returns the excerpt of the given article,
57
- # passed as a parameter. By default, given the argument
58
- # +article+, this proc will return +article.excerpt+.
59
- # This function may return nil.
42
+ # sorted by descending creation date.
60
43
  #
61
44
  # The following attributes must be set on blog articles:
62
45
  #
63
- # * 'title', containing the title of the blog post.
46
+ # * `title` The title of the blog post
64
47
  #
65
- # * all other attributes mentioned above.
48
+ # * `kind` and `created_at` (described above)
66
49
  #
67
50
  # The following attributes can optionally be set on blog articles to
68
51
  # change the behaviour of the Atom feed:
69
52
  #
70
- # * 'excerpt', containing an excerpt of the article, usually only a few
53
+ # * `excerpt` An excerpt of the article, which is usually only a few
71
54
  # lines long.
72
55
  #
73
- # * 'custom_path_in_feed', containing the path that will be used instead
74
- # of the normal path in the feed. This can be useful when including
56
+ # * `custom_path_in_feed` The path that will be used instead of the
57
+ # normal path in the feed. This can be useful when including
75
58
  # non-outputted items in a feed; such items could have their custom feed
76
59
  # path set to the blog path instead, for example.
77
60
  #
@@ -82,34 +65,63 @@ module Nanoc3::Helpers
82
65
  #
83
66
  # The site configuration will need to have the following attributes:
84
67
  #
85
- # * 'base_url', containing the URL to the site, without trailing slash.
86
- # For example, if the site is at "http://example.com/", the base_url
87
- # would be "http://example.com".
68
+ # * `base_url` The URL to the site, without trailing slash. For
69
+ # example, if the site is at http://example.com/”, the `base_url`
70
+ # would be http://example.com”.
88
71
  #
89
72
  # The feed item will need to have the following attributes:
90
73
  #
91
- # * 'title', containing the title of the feed, which is usually also the
92
- # title of the blog.
74
+ # * `title` — The title of the feed, which is usually also the title of
75
+ # the blog.
93
76
  #
94
- # * 'author_name', containing the name of the item's author. This will
95
- # likely be a global attribute, unless the site is managed by several
96
- # people/
77
+ # * `author_name` The name of the items author.
97
78
  #
98
- # * 'author_uri', containing the URI for the item's author, such as the
99
- # author's web site URL. This will also likely be a global attribute.
79
+ # * `author_uri` The URI for the items author, such as the author’s
80
+ # web site URL.
100
81
  #
101
82
  # The feed item can have the following optional attributes:
102
83
  #
103
- # * 'feed_url', containing the custom URL of the feed. This can be useful
104
- # when the private feed URL shouldn't be exposed; for example, when
105
- # using FeedBurner this would be set to the public FeedBurner URL.
84
+ # * `feed_url` The custom URL of the feed. This can be useful when the
85
+ # private feed URL shouldnt be exposed; for example, when using
86
+ # FeedBurner this would be set to the public FeedBurner URL.
87
+ #
88
+ # To construct a feed, create a new item and make sure that it is
89
+ # filtered with `:erb` or `:erubis`; it should not be laid out. Ensure
90
+ # that it is routed to the proper path, e.g. `/blog.xml`. It may also be
91
+ # useful to set the `is_hidden` attribute to true, so that helpers such
92
+ # as the sitemap helper will ignore the item. The content of the feed
93
+ # item should be `<%= atom_feed %>`.
94
+ #
95
+ # @example Defining compilation and routing rules for a feed item
96
+ #
97
+ # compile '/blog/feed/' do
98
+ # filter :erb
99
+ # end
100
+ #
101
+ # route '/blog/feed/' do
102
+ # '/blog.xml'
103
+ # end
106
104
  #
107
- # To construct a feed, create a blank item with no layout, only the 'erb'
108
- # (or 'erubis') filter, and an 'xml' extension. It may also be useful to
109
- # set 'is_hidden' to true, so that helpers such as the sitemap helper will
110
- # ignore the item. The content of the feed item should be:
105
+ # @example Limiting the number of items in a feed
111
106
  #
112
- # <%= atom_feed %>
107
+ # <%= atom_feed :limit => 5 %>
108
+ #
109
+ # @option params [Number] :limit (5) The maximum number of articles to
110
+ # show
111
+ #
112
+ # @option params [Array] :articles (sorted_articles) A list of articles to
113
+ # include in the feed
114
+ #
115
+ # @option params [Proc] :content_proc (->{ |article|
116
+ # article.compiled_content(:snapshot => :pre) }) A proc that returns the
117
+ # content of the given article, which is passed as a parameter. This
118
+ # function may not return nil.
119
+ #
120
+ # @option params [proc] :excerpt_proc (->{ |article| article[:excerpt] })
121
+ # A proc that returns the excerpt of the given article, passed as a
122
+ # parameter. This function should return nil if there is no excerpt.
123
+ #
124
+ # @return [String] The generated feed content
113
125
  def atom_feed(params={})
114
126
  require 'builder'
115
127
  require 'time'
@@ -117,7 +129,7 @@ module Nanoc3::Helpers
117
129
  # Extract parameters
118
130
  limit = params[:limit] || 5
119
131
  relevant_articles = params[:articles] || articles || []
120
- content_proc = params[:content_proc] || lambda { |a| a.reps[0].content_at_snapshot(:pre) }
132
+ content_proc = params[:content_proc] || lambda { |a| a.compiled_content(:snapshot => :pre) }
121
133
  excerpt_proc = params[:excerpt_proc] || lambda { |a| a[:excerpt] }
122
134
 
123
135
  # Check config attributes
@@ -145,10 +157,7 @@ module Nanoc3::Helpers
145
157
  end
146
158
 
147
159
  # Get sorted relevant articles
148
- sorted_relevant_articles = relevant_articles.sort_by do |a|
149
- time = a[:created_at]
150
- time.is_a?(String) ? Time.parse(time) : time
151
- end.reverse.first(limit)
160
+ sorted_relevant_articles = relevant_articles.sort_by { |a| Time.parse(a[:created_at]) }.reverse.first(limit)
152
161
 
153
162
  # Get most recent article
154
163
  last_article = sorted_relevant_articles.first
@@ -167,8 +176,7 @@ module Nanoc3::Helpers
167
176
  xml.title @item[:title]
168
177
 
169
178
  # Add date
170
- time = last_article[:created_at]
171
- xml.updated((time.is_a?(String) ? Time.parse(time) : time).to_iso8601_time)
179
+ xml.updated Time.parse(last_article[:created_at]).to_iso8601_time
172
180
 
173
181
  # Add links
174
182
  xml.link(:rel => 'alternate', :href => root_url)
@@ -192,8 +200,7 @@ module Nanoc3::Helpers
192
200
  xml.title a[:title], :type => 'html'
193
201
 
194
202
  # Add dates
195
- time = a[:created_at]
196
- xml.published((time.is_a?(String) ? Time.parse(time) : time).to_iso8601_time)
203
+ xml.published Time.parse(a[:created_at]).to_iso8601_time
197
204
  xml.updated a.mtime.to_iso8601_time
198
205
 
199
206
  # Add link
@@ -212,6 +219,10 @@ module Nanoc3::Helpers
212
219
 
213
220
  # Returns the URL for the given item. It will return the URL containing
214
221
  # the custom path in the feed if possible, otherwise the normal path.
222
+ #
223
+ # @param [Nanoc3::Item] item The item for which to fetch the URL.
224
+ #
225
+ # @return [String] The URL of the given item
215
226
  def url_for(item)
216
227
  # Check attributes
217
228
  if @site.config[:base_url].nil?
@@ -219,7 +230,7 @@ module Nanoc3::Helpers
219
230
  end
220
231
 
221
232
  # Get path
222
- path = item[:custom_path_in_feed] || item.reps[0].path
233
+ path = item[:custom_path_in_feed] || item.path
223
234
  return nil if path.nil?
224
235
 
225
236
  # Build URL
@@ -228,28 +239,33 @@ module Nanoc3::Helpers
228
239
 
229
240
  # Returns the URL of the feed. It will return the custom feed URL if set,
230
241
  # or otherwise the normal feed URL.
242
+ #
243
+ # @return [String] The URL of the feed
231
244
  def feed_url
232
245
  # Check attributes
233
246
  if @site.config[:base_url].nil?
234
247
  raise RuntimeError.new('Cannot build Atom feed: site configuration has no base_url')
235
248
  end
236
249
 
237
- @item[:feed_url] || @site.config[:base_url] + @item.reps[0].path
250
+ @item[:feed_url] || @site.config[:base_url] + @item.path
238
251
  end
239
252
 
240
253
  # Returns an URI containing an unique ID for the given item. This will be
241
254
  # used in the Atom feed to uniquely identify articles. These IDs are
242
- # created using a procedure suggested by Mark Pilgrim in this blog post:
243
- # http://diveintomark.org/archives/2004/05/28/howto-atom-id.
255
+ # created using a procedure suggested by Mark Pilgrim and described in his
256
+ # [“How to make a good ID in Atom” blog post]
257
+ # (http://diveintomark.org/archives/2004/05/28/howto-atom-id).
258
+ #
259
+ # @param [Nanoc3::Item] item The item for which to create an atom tag
260
+ #
261
+ # @return [String] The atom tag for the given item
244
262
  def atom_tag_for(item)
245
263
  require 'time'
246
264
 
247
265
  hostname, base_dir = %r{^.+?://([^/]+)(.*)$}.match(@site.config[:base_url])[1..2]
266
+ formatted_date = Time.parse(item[:created_at]).to_iso8601_date
248
267
 
249
- time = item[:created_at]
250
- formatted_date = (time.is_a?(String) ? Time.parse(time) : time).to_iso8601_date
251
-
252
- 'tag:' + hostname + ',' + formatted_date + ':' + base_dir + (item.reps[0].path || item.identifier)
268
+ 'tag:' + hostname + ',' + formatted_date + ':' + base_dir + (item.path || item.identifier)
253
269
  end
254
270
 
255
271
  end
@@ -2,20 +2,28 @@
2
2
 
3
3
  module Nanoc3::Helpers
4
4
 
5
- # Nanoc3::Helpers::Breadcrumbs provides support for breadcrumbs, which allow
6
- # the user to go up in the page hierarchy.
5
+ # Provides support for breadcrumbs, which allow the user to go up in the
6
+ # page hierarchy.
7
7
  module Breadcrumbs
8
8
 
9
- # Returns the breadcrumbs trail as an array. Higher items (items that are
10
- # closer to the root) come before lower items.
9
+ # Creates a breadcrumb trail leading from the current item to its parent,
10
+ # to its parent’s parent, etc, until the root item is reached. This
11
+ # function does not require that each intermediate item exist; for
12
+ # example, if there is no `/foo/` item, breadcrumbs for a `/foo/bar/` item
13
+ # will contain a `nil` element.
14
+ #
15
+ # @return [Array] The breadcrumbs, starting with the root item and ending
16
+ # with the item itself
11
17
  def breadcrumbs_trail
12
- trail = []
13
- item = @item
18
+ trail = []
19
+ current_identifier = @item.identifier
14
20
 
15
- begin
16
- trail.unshift(item)
17
- item = item.parent
18
- end until item.nil?
21
+ loop do
22
+ item = @items.find { |i| i.identifier == current_identifier }
23
+ trail.unshift(item)
24
+ break if current_identifier == '/'
25
+ current_identifier = current_identifier.sub(/[^\/]+\/$/, '')
26
+ end
19
27
 
20
28
  trail
21
29
  end
@@ -2,10 +2,8 @@
2
2
 
3
3
  module Nanoc3::Helpers
4
4
 
5
- # Nanoc3::Helpers::Capturing provides a content_for method, which allows
6
- # content to be "captured" on one item and reused elsewhere.
7
- #
8
- # = Example
5
+ # Provides functionality for “capturing” content in one place and reusing
6
+ # this content elsewhere.
9
7
  #
10
8
  # For example, suppose you want the sidebar of your site to contain a short
11
9
  # summary of the item. You could put the summary in the meta file, but
@@ -14,47 +12,44 @@ module Nanoc3::Helpers
14
12
  # you write the summary on the item itself, but capture it, and print it in
15
13
  # the sidebar layout.
16
14
  #
17
- # Captured content becomes part of the item. For example, a sidebar layout
18
- # could look like this:
15
+ # @example Capturing content into a `content_for_summary` attribute
16
+ # <% content_for :summary do %>
17
+ # <p>On this item, nanoc is introduced, blah blah.</p>
18
+ # <% end %>
19
19
  #
20
+ # @example Showing captured content in a sidebar
20
21
  # <div id="sidebar">
21
22
  # <h3>Summary</h3>
22
23
  # <%= @item[:content_for_summary] || '(no summary)' %>
23
24
  # </div>
24
- #
25
- # To put something inside that content_for_summary variable, capture it
26
- # using the content_for function. In the about item, for example:
27
- #
28
- # <% content_for :summary do %>
29
- # <p>On this item, nanoc is introduced, blah blah.</p>
30
- # <% end %>
31
- #
32
- # When the site is compiled, the sidebar of the about item will say “On
33
- # this item, the purpose of nanoc is described, blah blah blah,” as
34
- # expected.
35
- #
36
- # This helper has been tested with ERB and Haml.
37
- #
38
- # To activate this helper, +include+ it, like this:
39
- #
40
- # include Nanoc3::Helpers::Capturing
41
25
  module Capturing
42
26
 
43
- # Captures the content inside the block into a item attribute named
44
- # "content_for_" followed by the given name. The content of the block
45
- # itself will not be outputted.
27
+ # Captures the content inside the block and stores it in an item
28
+ # attribute named `content_for_` followed by the given name. The
29
+ # content of the block itself will not be outputted.
30
+ #
31
+ # @param [Symbol, String] The base name of the attribute into which
32
+ # the content should be stored
33
+ #
34
+ # @return [void]
46
35
  def content_for(name, &block)
47
36
  eval("@item[:content_for_#{name.to_s}] = capture(&block)")
48
37
  end
49
38
 
50
- # Evaluates the given block and returns the result. The block is not outputted.
51
- def capture(*args, &block)
39
+ # Evaluates the given block and returns the result. The contents of the
40
+ # block is not outputted.
41
+ #
42
+ # This function has been tested with ERB and Haml. Other filters may not
43
+ # work correctly.
44
+ #
45
+ # @return [String] The captured result
46
+ def capture(&block)
52
47
  # Get erbout so far
53
48
  erbout = eval('_erbout', block.binding)
54
49
  erbout_length = erbout.length
55
50
 
56
51
  # Execute block
57
- block.call(*args)
52
+ block.call
58
53
 
59
54
  # Get new piece of erbout
60
55
  erbout_addition = erbout[erbout_length..-1]
@@ -2,28 +2,31 @@
2
2
 
3
3
  module Nanoc3::Helpers
4
4
 
5
- # Nanoc3::Helpers::Filtering provides a filter method, which allows parts of
6
- # an item to be filtered.
7
- #
8
- # For example, the following piece of code only runs the "rubypants" filter
9
- # on the second paragraph:
10
- #
11
- # <p>Lorem ipsum dolor sit amet...</p>
12
- # <% filter :rubypants do %>
13
- # <p>Consectetur adipisicing elit...</p>
14
- # <% end %>
15
- #
16
- # This helper has been tested with ERB and Haml.
17
- #
18
- # To activate this helper, +include+ it, like this:
19
- #
20
- # include Nanoc3::Helpers::Filtering
5
+ # Provides functionality for filtering parts of an item or a layout.
21
6
  module Filtering
22
7
 
23
8
  require 'nanoc3/helpers/capturing'
24
9
  include Nanoc3::Helpers::Capturing
25
10
 
26
- # Filters the content in the given block and outputs it.
11
+ # Filters the content in the given block and outputs it. This function
12
+ # does not return anything; instead, the filtered contents is directly
13
+ # appended to the output buffer (`_erbout`).
14
+ #
15
+ # This function has been tested with ERB and Haml. Other filters may not
16
+ # work correctly.
17
+ #
18
+ # @example Running a filter on a part of an item or layout
19
+ # <p>Lorem ipsum dolor sit amet...</p>
20
+ # <% filter :rubypants do %>
21
+ # <p>Consectetur adipisicing elit...</p>
22
+ # <% end %>
23
+ #
24
+ # @param [Symbol] filter_name The name of the filter to run on the
25
+ # contents of the block
26
+ #
27
+ # @param [Hash] argument Arguments to pass to the filter
28
+ #
29
+ # @return [void]
27
30
  def filter(filter_name, arguments={}, &block)
28
31
  # Capture block
29
32
  data = capture(&block)
@@ -2,12 +2,15 @@
2
2
 
3
3
  module Nanoc3::Helpers
4
4
 
5
- # Nanoc3::Helpers::HTMLEscape contains functionality for HTML-escaping
6
- # strings.
5
+ # Contains functionality for HTML-escaping strings.
7
6
  module HTMLEscape
8
7
 
9
- # Returns the HTML-escaped representation of the given string. Only &, <,
10
- # > and " are escaped.
8
+ # Returns the HTML-escaped representation of the given string. Only `&`,
9
+ # `<`, `>` and `"` are escaped.
10
+ #
11
+ # @param [String] string The string to escape
12
+ #
13
+ # @return [String] The escaped string
11
14
  def html_escape(string)
12
15
  string.gsub('&', '&amp;').
13
16
  gsub('<', '&lt;').
@@ -2,11 +2,7 @@
2
2
 
3
3
  module Nanoc3::Helpers
4
4
 
5
- # Nanoc3::Helpers::LinkTo contains functions for linking to items.
6
- #
7
- # To activate this helper, +include+ it, like this:
8
- #
9
- # include Nanoc3::Helpers::LinkTo
5
+ # Contains functions for linking to items and item representations.
10
6
  module LinkTo
11
7
 
12
8
  require 'nanoc3/helpers/html_escape'
@@ -15,30 +11,39 @@ module Nanoc3::Helpers
15
11
  # Creates a HTML link to the given path or item representation, and with
16
12
  # the given text. All attributes of the `a` element, including the `href`
17
13
  # attribute, will be HTML-escaped; the contents of the `a` element, which
18
- # can contain markup, will not be HTML-escaped.
14
+ # can contain markup, will not be HTML-escaped. The HTML-escaping is done
15
+ # using {Nanoc3::Helpers::HTMLEscape#html_escape}.
19
16
  #
20
- # +path_or_rep+:: the URL or path (a String) that should be linked to, or
21
- # the item representation that should be linked to.
17
+ # @param [String] text The visible link text
22
18
  #
23
- # +text+:: the visible link text.
19
+ # @param [String, Nanoc3::Item, Nanoc3::ItemRep] target The path/URL,
20
+ # item or item representation that should be linked to
24
21
  #
25
- # +attributes+:: a hash containing HTML attributes that will be added to
26
- # the link.
22
+ # @param [Hash] attributes A hash containing HTML attributes (e.g.
23
+ # `rel`, `title`, …) that will be added to the link.
27
24
  #
28
- # Examples:
25
+ # @return [String] The link text
29
26
  #
27
+ # @example Linking to a path
30
28
  # link_to('Blog', '/blog/')
31
29
  # # => '<a href="/blog/">Blog</a>'
32
30
  #
33
- # item_rep = @items.find { |i| i.item_id == 'special' }.reps[0]
34
- # link_to('Special Item', item_rep)
35
- # # => '<a href="/special_item/">Special Item</a>'
31
+ # @example Linking to an item
32
+ # about = @items.find { |i| i.identifier == '/about/' }
33
+ # link_to('About Me', about)
34
+ # # => '<a href="/about.html">About Me</a>'
36
35
  #
36
+ # @example Linking to an item representation
37
+ # about = @items.find { |i| i.identifier == '/about/' }
38
+ # link_to('My vCard', about.rep(:vcard))
39
+ # # => '<a href="/about.vcf">My vCard</a>'
40
+ #
41
+ # @example Linking with custom attributes
37
42
  # link_to('Blog', '/blog/', :title => 'My super cool blog')
38
- # # => '<a href="/blog/" title="My super cool blog">Blog</a>
39
- def link_to(text, path_or_rep, attributes={})
43
+ # # => '<a title="My super cool blog" href="/blog/">Blog</a>'
44
+ def link_to(text, target, attributes={})
40
45
  # Find path
41
- path = path_or_rep.is_a?(String) ? path_or_rep : path_or_rep.path
46
+ path = target.is_a?(String) ? target : target.path
42
47
 
43
48
  # Join attributes
44
49
  attributes = attributes.inject('') do |memo, (key, value)|
@@ -50,52 +55,57 @@ module Nanoc3::Helpers
50
55
  end
51
56
 
52
57
  # Creates a HTML link using link_to, except when the linked item is the
53
- # current one. In this case, a span element with class "active" and with
54
- # the given text will be returned. The HTML-escaping rules for `link_to`
55
- # apply here as well.
58
+ # current one. In this case, a span element with class active and with
59
+ # the given text will be returned. The HTML-escaping rules for
60
+ # {#link_to} apply here as well.
61
+ #
62
+ # @param [String] text The visible link text
63
+ #
64
+ # @param [String, Nanoc3::Item, Nanoc3::ItemRep] target The path/URL,
65
+ # item or item representation that should be linked to
56
66
  #
57
- # Examples:
67
+ # @param [Hash] attributes A hash containing HTML attributes (e.g.
68
+ # `rel`, `title`, …) that will be added to the link.
58
69
  #
70
+ # @return [String] The link text
71
+ #
72
+ # @example Linking to a different page
59
73
  # link_to_unless_current('Blog', '/blog/')
60
74
  # # => '<a href="/blog/">Blog</a>'
61
75
  #
62
- # link_to_unless_current('This Item', @item_rep)
63
- # # => '<span class="active">This Item</span>'
64
- def link_to_unless_current(text, path_or_rep, attributes={})
76
+ # @example Linking to the same page
77
+ # link_to_unless_current('This Item', @item)
78
+ # # => '<span class="active" title="You\'re here.">This Item</span>'
79
+ def link_to_unless_current(text, target, attributes={})
65
80
  # Find path
66
- path = path_or_rep.is_a?(String) ? path_or_rep : path_or_rep.path
81
+ path = target.is_a?(String) ? target : target.path
67
82
 
68
- if @item_rep and @item_rep.path == path
83
+ if @item_rep && @item_rep.path == path
69
84
  # Create message
70
85
  "<span class=\"active\" title=\"You're here.\">#{text}</span>"
71
86
  else
72
- link_to(text, path_or_rep, attributes)
87
+ link_to(text, target, attributes)
73
88
  end
74
89
  end
75
90
 
76
91
  # Returns the relative path from the current item to the given path or
77
92
  # item representation. The returned path will not be HTML-escaped.
78
93
  #
79
- # +path_or_rep+:: the URL or path (a String) to where the relative should
80
- # point, or the item representation to which the relative
81
- # should point.
94
+ # @param [String, Nanoc3::Item, Nanoc3::ItemRep] target The path/URL,
95
+ # item or item representation to which the relative path should be
96
+ # generated
82
97
  #
83
- # Example:
98
+ # @return [String] The relative path to the target
84
99
  #
100
+ # @example
85
101
  # # if the current item's path is /foo/bar/
86
- # relative_path('/foo/qux/')
102
+ # relative_path_to('/foo/qux/')
87
103
  # # => '../qux/'
88
104
  def relative_path_to(target)
89
105
  require 'pathname'
90
106
 
91
107
  # Find path
92
- if target.is_a?(String)
93
- path = target
94
- elsif target.respond_to?(:reps)
95
- path = target.reps.find { |r| r.name == :default }.path
96
- else
97
- path = target.path
98
- end
108
+ path = target.is_a?(String) ? target : target.path
99
109
 
100
110
  # Get source and destination paths
101
111
  dst_path = Pathname.new(path)
@@ -111,7 +121,7 @@ module Nanoc3::Helpers
111
121
 
112
122
  # Add trailing slash if necessary
113
123
  if dst_path.to_s[-1,1] == '/'
114
- relative_path += '/'
124
+ relative_path << '/'
115
125
  end
116
126
 
117
127
  # Done