nanoc3 3.0.9 → 3.1.0a1
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +1 -1
- data/NEWS.md +360 -0
- data/README.md +85 -0
- data/Rakefile +2 -2
- data/bin/nanoc3 +0 -4
- data/lib/nanoc3/base/code_snippet.rb +14 -6
- data/lib/nanoc3/base/compiler.rb +68 -49
- data/lib/nanoc3/base/compiler_dsl.rb +70 -29
- data/lib/nanoc3/base/context.rb +47 -0
- data/lib/nanoc3/base/core_ext/array.rb +4 -0
- data/lib/nanoc3/base/core_ext/hash.rb +5 -1
- data/lib/nanoc3/base/core_ext/string.rb +2 -0
- data/lib/nanoc3/base/data_source.rb +132 -96
- data/lib/nanoc3/base/dependency_tracker.rb +160 -185
- data/lib/nanoc3/base/directed_graph.rb +252 -0
- data/lib/nanoc3/base/errors.rb +52 -4
- data/lib/nanoc3/base/filter.rb +43 -28
- data/lib/nanoc3/base/item.rb +93 -25
- data/lib/nanoc3/base/item_rep.rb +166 -55
- data/lib/nanoc3/base/layout.rb +16 -13
- data/lib/nanoc3/base/notification_center.rb +28 -12
- data/lib/nanoc3/base/plugin_registry.rb +158 -0
- data/lib/nanoc3/base/rule.rb +27 -8
- data/lib/nanoc3/base/rule_context.rb +59 -46
- data/lib/nanoc3/base/site.rb +124 -77
- data/lib/nanoc3/base.rb +7 -2
- data/lib/nanoc3/cli/base.rb +4 -1
- data/lib/nanoc3/cli/commands/autocompile.rb +5 -4
- data/lib/nanoc3/cli/commands/compile.rb +28 -7
- data/lib/nanoc3/cli/commands/create_item.rb +1 -1
- data/lib/nanoc3/cli/commands/create_layout.rb +1 -1
- data/lib/nanoc3/cli/commands/create_site.rb +46 -22
- data/lib/nanoc3/cli/commands/debug.rb +100 -0
- data/lib/nanoc3/cli/commands/help.rb +1 -1
- data/lib/nanoc3/cli/commands/info.rb +1 -1
- data/lib/nanoc3/cli/commands/view.rb +85 -0
- data/lib/nanoc3/cli/commands.rb +2 -0
- data/lib/nanoc3/cli/logger.rb +7 -0
- data/lib/nanoc3/cli.rb +0 -3
- data/lib/nanoc3/data_sources/{delicious.rb → deprecated/delicious.rb} +1 -24
- data/lib/nanoc3/data_sources/{last_fm.rb → deprecated/last_fm.rb} +1 -27
- data/lib/nanoc3/data_sources/{twitter.rb → deprecated/twitter.rb} +1 -14
- data/lib/nanoc3/data_sources/filesystem.rb +188 -176
- data/lib/nanoc3/data_sources/filesystem_unified.rb +107 -0
- data/lib/nanoc3/data_sources/filesystem_verbose.rb +80 -0
- data/lib/nanoc3/data_sources.rb +18 -9
- data/lib/nanoc3/extra/core_ext/enumerable.rb +39 -0
- data/lib/nanoc3/extra/core_ext/time.rb +2 -2
- data/lib/nanoc3/extra/core_ext.rb +1 -0
- data/lib/nanoc3/extra/deployers/rsync.rb +49 -3
- data/lib/nanoc3/extra/file_proxy.rb +7 -0
- data/lib/nanoc3/extra/vcs.rb +25 -24
- data/lib/nanoc3/extra/vcses/bazaar.rb +4 -0
- data/lib/nanoc3/extra/vcses/dummy.rb +4 -0
- data/lib/nanoc3/extra/vcses/git.rb +4 -0
- data/lib/nanoc3/extra/vcses/mercurial.rb +4 -0
- data/lib/nanoc3/extra/vcses/subversion.rb +4 -0
- data/lib/nanoc3/extra.rb +4 -1
- data/lib/nanoc3/filters/erb.rb +1 -1
- data/lib/nanoc3/filters/erubis.rb +1 -1
- data/lib/nanoc3/filters/haml.rb +1 -1
- data/lib/nanoc3/filters/kramdown.rb +14 -0
- data/lib/nanoc3/filters/maruku.rb +1 -1
- data/lib/nanoc3/filters/rainpress.rb +1 -1
- data/lib/nanoc3/filters/rdiscount.rb +3 -1
- data/lib/nanoc3/filters.rb +2 -0
- data/lib/nanoc3/helpers/blogging.rb +91 -75
- data/lib/nanoc3/helpers/breadcrumbs.rb +18 -10
- data/lib/nanoc3/helpers/capturing.rb +24 -29
- data/lib/nanoc3/helpers/filtering.rb +20 -17
- data/lib/nanoc3/helpers/html_escape.rb +7 -4
- data/lib/nanoc3/helpers/link_to.rb +51 -41
- data/lib/nanoc3/helpers/rendering.rb +15 -8
- data/lib/nanoc3/helpers/tagging.rb +27 -21
- data/lib/nanoc3/helpers/text.rb +12 -8
- data/lib/nanoc3/helpers/xml_sitemap.rb +13 -15
- data/lib/nanoc3/tasks/deploy/rsync.rake +4 -1
- data/lib/nanoc3/tasks.rb +2 -1
- data/lib/nanoc3.rb +24 -1
- metadata +43 -87
- data/NEWS.rdoc +0 -328
- data/README.rdoc +0 -83
- data/lib/nanoc3/base/plugin.rb +0 -88
- data/lib/nanoc3/base/preprocessor_context.rb +0 -37
- data/lib/nanoc3/data_sources/filesystem_combined.rb +0 -214
- data/lib/nanoc3/data_sources/filesystem_common.rb +0 -22
- data/lib/nanoc3/data_sources/filesystem_compact.rb +0 -256
- data/lib/nanoc3/extra/context.rb +0 -24
- data/lib/nanoc3/package.rb +0 -107
- data/vendor/cri/ChangeLog +0 -0
- data/vendor/cri/LICENSE +0 -19
- data/vendor/cri/NEWS +0 -0
- data/vendor/cri/README +0 -4
- data/vendor/cri/Rakefile +0 -25
- data/vendor/cri/lib/cri/base.rb +0 -153
- data/vendor/cri/lib/cri/command.rb +0 -105
- data/vendor/cri/lib/cri/core_ext/string.rb +0 -41
- data/vendor/cri/lib/cri/core_ext.rb +0 -8
- data/vendor/cri/lib/cri/option_parser.rb +0 -186
- data/vendor/cri/lib/cri.rb +0 -12
- data/vendor/cri/test/test_base.rb +0 -6
- data/vendor/cri/test/test_command.rb +0 -6
- data/vendor/cri/test/test_core_ext.rb +0 -21
- data/vendor/cri/test/test_option_parser.rb +0 -279
@@ -2,76 +2,59 @@
|
|
2
2
|
|
3
3
|
module Nanoc3::Helpers
|
4
4
|
|
5
|
-
#
|
6
|
-
#
|
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
|
-
# *
|
11
|
+
# * `kind` — Set to `"article"`
|
12
12
|
#
|
13
|
-
# *
|
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
|
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,
|
32
|
-
#
|
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
|
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.
|
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
|
-
# *
|
46
|
+
# * `title` — The title of the blog post
|
64
47
|
#
|
65
|
-
# *
|
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
|
-
# *
|
53
|
+
# * `excerpt` — An excerpt of the article, which is usually only a few
|
71
54
|
# lines long.
|
72
55
|
#
|
73
|
-
# *
|
74
|
-
#
|
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
|
-
# *
|
86
|
-
#
|
87
|
-
# would be
|
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
|
-
# *
|
92
|
-
#
|
74
|
+
# * `title` — The title of the feed, which is usually also the title of
|
75
|
+
# the blog.
|
93
76
|
#
|
94
|
-
# *
|
95
|
-
# likely be a global attribute, unless the site is managed by several
|
96
|
-
# people/
|
77
|
+
# * `author_name` — The name of the item’s author.
|
97
78
|
#
|
98
|
-
# *
|
99
|
-
#
|
79
|
+
# * `author_uri` — The URI for the item’s 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
|
-
# *
|
104
|
-
#
|
105
|
-
#
|
84
|
+
# * `feed_url` — The custom URL of the feed. This can be useful when the
|
85
|
+
# private feed URL shouldn’t 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
|
-
#
|
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.
|
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
|
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
|
-
|
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
|
-
|
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.
|
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.
|
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
|
243
|
-
#
|
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
|
-
|
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
|
-
#
|
6
|
-
#
|
5
|
+
# Provides support for breadcrumbs, which allow the user to go up in the
|
6
|
+
# page hierarchy.
|
7
7
|
module Breadcrumbs
|
8
8
|
|
9
|
-
#
|
10
|
-
#
|
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
|
-
|
18
|
+
trail = []
|
19
|
+
current_identifier = @item.identifier
|
14
20
|
|
15
|
-
|
16
|
-
|
17
|
-
item
|
18
|
-
|
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
|
-
#
|
6
|
-
# content
|
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
|
-
#
|
18
|
-
#
|
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
|
44
|
-
#
|
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
|
51
|
-
|
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
|
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
|
-
#
|
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
|
-
#
|
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
|
-
#
|
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('&', '&').
|
13
16
|
gsub('<', '<').
|
@@ -2,11 +2,7 @@
|
|
2
2
|
|
3
3
|
module Nanoc3::Helpers
|
4
4
|
|
5
|
-
#
|
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
|
-
#
|
21
|
-
# the item representation that should be linked to.
|
17
|
+
# @param [String] text The visible link text
|
22
18
|
#
|
23
|
-
#
|
19
|
+
# @param [String, Nanoc3::Item, Nanoc3::ItemRep] target The path/URL,
|
20
|
+
# item or item representation that should be linked to
|
24
21
|
#
|
25
|
-
#
|
26
|
-
#
|
22
|
+
# @param [Hash] attributes A hash containing HTML attributes (e.g.
|
23
|
+
# `rel`, `title`, …) that will be added to the link.
|
27
24
|
#
|
28
|
-
#
|
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
|
-
#
|
34
|
-
#
|
35
|
-
#
|
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
|
39
|
-
def link_to(text,
|
43
|
+
# # => '<a title="My super cool blog" href="/blog/">Blog</a>'
|
44
|
+
def link_to(text, target, attributes={})
|
40
45
|
# Find path
|
41
|
-
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
|
54
|
-
# the given text will be returned. The HTML-escaping rules for
|
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
|
-
#
|
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
|
-
#
|
63
|
-
#
|
64
|
-
|
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 =
|
81
|
+
path = target.is_a?(String) ? target : target.path
|
67
82
|
|
68
|
-
if @item_rep
|
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,
|
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
|
-
#
|
80
|
-
#
|
81
|
-
#
|
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
|
-
#
|
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
|
-
#
|
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
|
-
|
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
|