nanoc 3.2.4 → 3.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gemtest +0 -0
- data/ChangeLog +3 -0
- data/Gemfile +32 -0
- data/LICENSE +19 -0
- data/NEWS.md +470 -0
- data/README.md +114 -0
- data/Rakefile +14 -0
- data/bin/nanoc +7 -27
- data/bin/nanoc3 +3 -0
- data/doc/yardoc_templates/default/layout/html/footer.erb +10 -0
- data/lib/nanoc.rb +41 -0
- data/lib/nanoc/base.rb +49 -0
- data/lib/nanoc/base/compilation/checksum_store.rb +57 -0
- data/lib/nanoc/base/compilation/compiled_content_cache.rb +62 -0
- data/lib/nanoc/base/compilation/compiler.rb +458 -0
- data/lib/nanoc/base/compilation/compiler_dsl.rb +214 -0
- data/lib/nanoc/base/compilation/dependency_tracker.rb +200 -0
- data/lib/nanoc/base/compilation/filter.rb +165 -0
- data/lib/nanoc/base/compilation/item_rep_proxy.rb +103 -0
- data/lib/nanoc/base/compilation/item_rep_recorder_proxy.rb +102 -0
- data/lib/nanoc/base/compilation/outdatedness_checker.rb +223 -0
- data/lib/nanoc/base/compilation/outdatedness_reasons.rb +46 -0
- data/lib/nanoc/base/compilation/rule.rb +73 -0
- data/lib/nanoc/base/compilation/rule_context.rb +84 -0
- data/lib/nanoc/base/compilation/rule_memory_calculator.rb +40 -0
- data/lib/nanoc/base/compilation/rule_memory_store.rb +53 -0
- data/lib/nanoc/base/compilation/rules_collection.rb +243 -0
- data/lib/nanoc/base/context.rb +47 -0
- data/lib/nanoc/base/core_ext.rb +6 -0
- data/lib/nanoc/base/core_ext/array.rb +62 -0
- data/lib/nanoc/base/core_ext/hash.rb +63 -0
- data/lib/nanoc/base/core_ext/pathname.rb +26 -0
- data/lib/nanoc/base/core_ext/string.rb +46 -0
- data/lib/nanoc/base/directed_graph.rb +275 -0
- data/lib/nanoc/base/errors.rb +211 -0
- data/lib/nanoc/base/memoization.rb +67 -0
- data/lib/nanoc/base/notification_center.rb +84 -0
- data/lib/nanoc/base/ordered_hash.rb +200 -0
- data/lib/nanoc/base/plugin_registry.rb +181 -0
- data/lib/nanoc/base/result_data/item_rep.rb +492 -0
- data/lib/nanoc/base/source_data/code_snippet.rb +58 -0
- data/lib/nanoc/base/source_data/configuration.rb +24 -0
- data/lib/nanoc/base/source_data/data_source.rb +234 -0
- data/lib/nanoc/base/source_data/item.rb +301 -0
- data/lib/nanoc/base/source_data/layout.rb +130 -0
- data/lib/nanoc/base/source_data/site.rb +361 -0
- data/lib/nanoc/base/store.rb +135 -0
- data/lib/nanoc/cli.rb +137 -0
- data/lib/nanoc/cli/command_runner.rb +104 -0
- data/lib/nanoc/cli/commands/autocompile.rb +58 -0
- data/lib/nanoc/cli/commands/compile.rb +297 -0
- data/lib/nanoc/cli/commands/create_item.rb +60 -0
- data/lib/nanoc/cli/commands/create_layout.rb +73 -0
- data/lib/nanoc/cli/commands/create_site.rb +411 -0
- data/lib/nanoc/cli/commands/debug.rb +117 -0
- data/lib/nanoc/cli/commands/deploy.rb +79 -0
- data/lib/nanoc/cli/commands/info.rb +98 -0
- data/lib/nanoc/cli/commands/nanoc.rb +38 -0
- data/lib/nanoc/cli/commands/prune.rb +50 -0
- data/lib/nanoc/cli/commands/update.rb +70 -0
- data/lib/nanoc/cli/commands/view.rb +82 -0
- data/lib/nanoc/cli/commands/watch.rb +124 -0
- data/lib/nanoc/cli/error_handler.rb +199 -0
- data/lib/nanoc/cli/logger.rb +92 -0
- data/lib/nanoc/data_sources.rb +29 -0
- data/lib/nanoc/data_sources/deprecated/delicious.rb +42 -0
- data/lib/nanoc/data_sources/deprecated/last_fm.rb +87 -0
- data/lib/nanoc/data_sources/deprecated/twitter.rb +38 -0
- data/lib/nanoc/data_sources/filesystem.rb +299 -0
- data/lib/nanoc/data_sources/filesystem_unified.rb +121 -0
- data/lib/nanoc/data_sources/filesystem_verbose.rb +91 -0
- data/lib/nanoc/extra.rb +24 -0
- data/lib/nanoc/extra/auto_compiler.rb +103 -0
- data/lib/nanoc/extra/chick.rb +125 -0
- data/lib/nanoc/extra/core_ext.rb +6 -0
- data/lib/nanoc/extra/core_ext/enumerable.rb +33 -0
- data/lib/nanoc/extra/core_ext/pathname.rb +30 -0
- data/lib/nanoc/extra/core_ext/time.rb +19 -0
- data/lib/nanoc/extra/deployer.rb +47 -0
- data/lib/nanoc/extra/deployers.rb +15 -0
- data/lib/nanoc/extra/deployers/fog.rb +98 -0
- data/lib/nanoc/extra/deployers/rsync.rb +70 -0
- data/lib/nanoc/extra/file_proxy.rb +40 -0
- data/lib/nanoc/extra/pruner.rb +86 -0
- data/lib/nanoc/extra/validators.rb +12 -0
- data/lib/nanoc/extra/validators/links.rb +268 -0
- data/lib/nanoc/extra/validators/w3c.rb +95 -0
- data/lib/nanoc/extra/vcs.rb +66 -0
- data/lib/nanoc/extra/vcses.rb +17 -0
- data/lib/nanoc/extra/vcses/bazaar.rb +25 -0
- data/lib/nanoc/extra/vcses/dummy.rb +24 -0
- data/lib/nanoc/extra/vcses/git.rb +25 -0
- data/lib/nanoc/extra/vcses/mercurial.rb +25 -0
- data/lib/nanoc/extra/vcses/subversion.rb +25 -0
- data/lib/nanoc/filters.rb +59 -0
- data/lib/nanoc/filters/asciidoc.rb +38 -0
- data/lib/nanoc/filters/bluecloth.rb +19 -0
- data/lib/nanoc/filters/coderay.rb +21 -0
- data/lib/nanoc/filters/coffeescript.rb +20 -0
- data/lib/nanoc/filters/colorize_syntax.rb +298 -0
- data/lib/nanoc/filters/erb.rb +38 -0
- data/lib/nanoc/filters/erubis.rb +34 -0
- data/lib/nanoc/filters/haml.rb +27 -0
- data/lib/nanoc/filters/kramdown.rb +20 -0
- data/lib/nanoc/filters/less.rb +53 -0
- data/lib/nanoc/filters/markaby.rb +20 -0
- data/lib/nanoc/filters/maruku.rb +20 -0
- data/lib/nanoc/filters/mustache.rb +24 -0
- data/lib/nanoc/filters/rainpress.rb +19 -0
- data/lib/nanoc/filters/rdiscount.rb +22 -0
- data/lib/nanoc/filters/rdoc.rb +33 -0
- data/lib/nanoc/filters/redcarpet.rb +62 -0
- data/lib/nanoc/filters/redcloth.rb +47 -0
- data/lib/nanoc/filters/relativize_paths.rb +94 -0
- data/lib/nanoc/filters/rubypants.rb +20 -0
- data/lib/nanoc/filters/sass.rb +74 -0
- data/lib/nanoc/filters/slim.rb +25 -0
- data/lib/nanoc/filters/typogruby.rb +23 -0
- data/lib/nanoc/filters/uglify_js.rb +42 -0
- data/lib/nanoc/filters/xsl.rb +46 -0
- data/lib/nanoc/filters/yui_compressor.rb +23 -0
- data/lib/nanoc/helpers.rb +16 -0
- data/lib/nanoc/helpers/blogging.rb +319 -0
- data/lib/nanoc/helpers/breadcrumbs.rb +40 -0
- data/lib/nanoc/helpers/capturing.rb +138 -0
- data/lib/nanoc/helpers/filtering.rb +50 -0
- data/lib/nanoc/helpers/html_escape.rb +55 -0
- data/lib/nanoc/helpers/link_to.rb +151 -0
- data/lib/nanoc/helpers/rendering.rb +140 -0
- data/lib/nanoc/helpers/tagging.rb +71 -0
- data/lib/nanoc/helpers/text.rb +44 -0
- data/lib/nanoc/helpers/xml_sitemap.rb +76 -0
- data/lib/nanoc/tasks.rb +10 -0
- data/lib/nanoc/tasks/clean.rake +16 -0
- data/lib/nanoc/tasks/clean.rb +29 -0
- data/lib/nanoc/tasks/deploy/rsync.rake +16 -0
- data/lib/nanoc/tasks/validate.rake +92 -0
- data/nanoc.gemspec +49 -0
- data/tasks/doc.rake +16 -0
- data/tasks/test.rake +46 -0
- data/test/base/core_ext/array_spec.rb +73 -0
- data/test/base/core_ext/hash_spec.rb +98 -0
- data/test/base/core_ext/pathname_spec.rb +27 -0
- data/test/base/core_ext/string_spec.rb +37 -0
- data/test/base/test_checksum_store.rb +35 -0
- data/test/base/test_code_snippet.rb +31 -0
- data/test/base/test_compiler.rb +403 -0
- data/test/base/test_compiler_dsl.rb +161 -0
- data/test/base/test_context.rb +31 -0
- data/test/base/test_data_source.rb +46 -0
- data/test/base/test_dependency_tracker.rb +262 -0
- data/test/base/test_directed_graph.rb +288 -0
- data/test/base/test_filter.rb +83 -0
- data/test/base/test_item.rb +179 -0
- data/test/base/test_item_rep.rb +579 -0
- data/test/base/test_layout.rb +59 -0
- data/test/base/test_memoization.rb +90 -0
- data/test/base/test_notification_center.rb +34 -0
- data/test/base/test_outdatedness_checker.rb +394 -0
- data/test/base/test_plugin.rb +30 -0
- data/test/base/test_rule.rb +19 -0
- data/test/base/test_rule_context.rb +65 -0
- data/test/base/test_site.rb +190 -0
- data/test/cli/commands/test_compile.rb +33 -0
- data/test/cli/commands/test_create_item.rb +14 -0
- data/test/cli/commands/test_create_layout.rb +28 -0
- data/test/cli/commands/test_create_site.rb +24 -0
- data/test/cli/commands/test_deploy.rb +74 -0
- data/test/cli/commands/test_help.rb +12 -0
- data/test/cli/commands/test_info.rb +11 -0
- data/test/cli/commands/test_prune.rb +98 -0
- data/test/cli/commands/test_update.rb +10 -0
- data/test/cli/test_cli.rb +102 -0
- data/test/cli/test_error_handler.rb +29 -0
- data/test/cli/test_logger.rb +10 -0
- data/test/data_sources/test_filesystem.rb +433 -0
- data/test/data_sources/test_filesystem_unified.rb +536 -0
- data/test/data_sources/test_filesystem_verbose.rb +357 -0
- data/test/extra/core_ext/test_enumerable.rb +30 -0
- data/test/extra/core_ext/test_pathname.rb +17 -0
- data/test/extra/core_ext/test_time.rb +15 -0
- data/test/extra/deployers/test_fog.rb +67 -0
- data/test/extra/deployers/test_rsync.rb +100 -0
- data/test/extra/test_auto_compiler.rb +417 -0
- data/test/extra/test_file_proxy.rb +19 -0
- data/test/extra/test_vcs.rb +22 -0
- data/test/extra/validators/test_links.rb +62 -0
- data/test/extra/validators/test_w3c.rb +47 -0
- data/test/filters/test_asciidoc.rb +22 -0
- data/test/filters/test_bluecloth.rb +18 -0
- data/test/filters/test_coderay.rb +44 -0
- data/test/filters/test_coffeescript.rb +18 -0
- data/test/filters/test_colorize_syntax.rb +379 -0
- data/test/filters/test_erb.rb +105 -0
- data/test/filters/test_erubis.rb +78 -0
- data/test/filters/test_haml.rb +96 -0
- data/test/filters/test_kramdown.rb +18 -0
- data/test/filters/test_less.rb +113 -0
- data/test/filters/test_markaby.rb +24 -0
- data/test/filters/test_maruku.rb +18 -0
- data/test/filters/test_mustache.rb +25 -0
- data/test/filters/test_rainpress.rb +29 -0
- data/test/filters/test_rdiscount.rb +31 -0
- data/test/filters/test_rdoc.rb +18 -0
- data/test/filters/test_redcarpet.rb +73 -0
- data/test/filters/test_redcloth.rb +33 -0
- data/test/filters/test_relativize_paths.rb +533 -0
- data/test/filters/test_rubypants.rb +18 -0
- data/test/filters/test_sass.rb +229 -0
- data/test/filters/test_slim.rb +35 -0
- data/test/filters/test_typogruby.rb +21 -0
- data/test/filters/test_uglify_js.rb +30 -0
- data/test/filters/test_xsl.rb +105 -0
- data/test/filters/test_yui_compressor.rb +44 -0
- data/test/gem_loader.rb +11 -0
- data/test/helper.rb +207 -0
- data/test/helpers/test_blogging.rb +754 -0
- data/test/helpers/test_breadcrumbs.rb +81 -0
- data/test/helpers/test_capturing.rb +41 -0
- data/test/helpers/test_filtering.rb +106 -0
- data/test/helpers/test_html_escape.rb +32 -0
- data/test/helpers/test_link_to.rb +249 -0
- data/test/helpers/test_rendering.rb +89 -0
- data/test/helpers/test_tagging.rb +87 -0
- data/test/helpers/test_text.rb +24 -0
- data/test/helpers/test_xml_sitemap.rb +103 -0
- data/test/tasks/test_clean.rb +67 -0
- metadata +327 -15
- data/bin/nanoc-select +0 -86
- data/lib/nanoc-select.rb +0 -11
@@ -0,0 +1,73 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Nanoc
|
4
|
+
|
5
|
+
# Contains the processing information for a item.
|
6
|
+
class Rule
|
7
|
+
|
8
|
+
# @return [Regexp] The regex that determines which items this rule can be
|
9
|
+
# applied to. This rule can be applied to items with a identifier
|
10
|
+
# matching this regex.
|
11
|
+
attr_reader :identifier_regex
|
12
|
+
|
13
|
+
# @return [Symbol] The name of the representation that will be compiled
|
14
|
+
# using this rule
|
15
|
+
attr_reader :rep_name
|
16
|
+
|
17
|
+
# @return [Symbol] The name of the snapshot this rule will apply to.
|
18
|
+
# Ignored for compilation rules, but used for routing rules.
|
19
|
+
#
|
20
|
+
# @since 3.2.0
|
21
|
+
attr_reader :snapshot_name
|
22
|
+
|
23
|
+
# Creates a new item compilation rule with the given identifier regex,
|
24
|
+
# compiler and block. The block will be called during compilation with the
|
25
|
+
# item rep as its argument.
|
26
|
+
#
|
27
|
+
# @param [Regexp] identifier_regex A regular expression that will be used
|
28
|
+
# to determine whether this rule is applicable to certain items.
|
29
|
+
#
|
30
|
+
# @param [String, Symbol] rep_name The name of the item representation
|
31
|
+
# where this rule can be applied to
|
32
|
+
#
|
33
|
+
# @param [Proc] block A block that will be called when matching items are
|
34
|
+
# compiled
|
35
|
+
#
|
36
|
+
# @option params [Symbol, nil] :snapshot (nil) The name of the snapshot
|
37
|
+
# this rule will apply to. Ignored for compilation rules, but used for
|
38
|
+
# routing rules.
|
39
|
+
def initialize(identifier_regex, rep_name, block, params={})
|
40
|
+
@identifier_regex = identifier_regex
|
41
|
+
@rep_name = rep_name.to_sym
|
42
|
+
@snapshot_name = params[:snapshot_name]
|
43
|
+
|
44
|
+
@block = block
|
45
|
+
end
|
46
|
+
|
47
|
+
# @param [Nanoc::Item] item The item to check
|
48
|
+
#
|
49
|
+
# @return [Boolean] true if this rule can be applied to the given item
|
50
|
+
# rep, false otherwise
|
51
|
+
def applicable_to?(item)
|
52
|
+
item.identifier =~ @identifier_regex
|
53
|
+
end
|
54
|
+
|
55
|
+
# Applies this rule to the given item rep.
|
56
|
+
#
|
57
|
+
# @param [Nanoc::ItemRep] rep The item representation where this rule
|
58
|
+
# should be applied to
|
59
|
+
#
|
60
|
+
# @option params [Nanoc::Compiler] :compiler The compiler
|
61
|
+
#
|
62
|
+
# @raise [ArgumentError] if no compiler is passed
|
63
|
+
#
|
64
|
+
# @return [void]
|
65
|
+
def apply_to(rep, params={})
|
66
|
+
compiler = params[:compiler] or raise ArgumentError, "Required :compiler option is missing"
|
67
|
+
rep = Nanoc::ItemRepProxy.new(rep, compiler) unless rep.is_proxy?
|
68
|
+
Nanoc::RuleContext.new(:rep => rep, :compiler => compiler).instance_eval &@block
|
69
|
+
end
|
70
|
+
|
71
|
+
end
|
72
|
+
|
73
|
+
end
|
@@ -0,0 +1,84 @@
|
|
1
|
+
module Nanoc
|
2
|
+
|
3
|
+
# Provides a context in which compilation and routing rules can be executed.
|
4
|
+
# It provides access to the item representation that is being compiled or
|
5
|
+
# routed.
|
6
|
+
#
|
7
|
+
# The following variables will be available in this rules context:
|
8
|
+
#
|
9
|
+
# * `rep` ({Nanoc::ItemRep}) - The current item rep
|
10
|
+
# * `item` ({Nanoc::Item}) - The current item
|
11
|
+
# * `site` ({Nanoc::Site}) - The site
|
12
|
+
# * `config` ({Hash}) - The site configuration
|
13
|
+
# * `items` ({Array}<{Nanoc::Item}>) - A list of all items
|
14
|
+
# * `layouts` ({Array}<{Nanoc::Layout}>) - A list of all layouts
|
15
|
+
#
|
16
|
+
# @api private
|
17
|
+
class RuleContext < Context
|
18
|
+
|
19
|
+
# @option params [Nanoc::ItemRep] :rep The item representation that will
|
20
|
+
# be processed in this rule context
|
21
|
+
#
|
22
|
+
# @option params [Nanoc::Compiler] :compiler The compiler that is being
|
23
|
+
# used to compile the site
|
24
|
+
#
|
25
|
+
# @raise [ArgumentError] if the `:rep` or the `:compiler` option is
|
26
|
+
# missing
|
27
|
+
def initialize(params={})
|
28
|
+
rep = params[:rep] or raise ArgumentError, "Required :rep option is missing"
|
29
|
+
compiler = params[:compiler] or raise ArgumentError, "Required :compiler option is missing"
|
30
|
+
|
31
|
+
super({
|
32
|
+
:rep => rep,
|
33
|
+
:item => rep.item,
|
34
|
+
:site => compiler.site,
|
35
|
+
:config => compiler.site.config,
|
36
|
+
:items => compiler.site.items,
|
37
|
+
:layouts => compiler.site.layouts
|
38
|
+
})
|
39
|
+
end
|
40
|
+
|
41
|
+
# Filters the current representation (calls {Nanoc::ItemRep#filter} with
|
42
|
+
# the given arguments on the rep).
|
43
|
+
#
|
44
|
+
# @see Nanoc::ItemRep#filter
|
45
|
+
#
|
46
|
+
# @param [Symbol] filter_name The name of the filter to run the item
|
47
|
+
# representations' content through
|
48
|
+
#
|
49
|
+
# @param [Hash] filter_args The filter arguments that should be passed to
|
50
|
+
# the filter's #run method
|
51
|
+
#
|
52
|
+
# @return [void]
|
53
|
+
def filter(filter_name, filter_args={})
|
54
|
+
rep.filter(filter_name, filter_args)
|
55
|
+
end
|
56
|
+
|
57
|
+
# Layouts the current representation (calls {Nanoc::ItemRep#layout} with
|
58
|
+
# the given arguments on the rep).
|
59
|
+
#
|
60
|
+
# @see Nanoc::ItemRep#layout
|
61
|
+
#
|
62
|
+
# @param [String] layout_identifier The identifier of the layout the item
|
63
|
+
# should be laid out with
|
64
|
+
#
|
65
|
+
# @return [void]
|
66
|
+
def layout(layout_identifier)
|
67
|
+
rep.layout(layout_identifier)
|
68
|
+
end
|
69
|
+
|
70
|
+
# Creates a snapshot of the current compiled item content. Calls
|
71
|
+
# {Nanoc::ItemRep#snapshot} with the given arguments on the rep.
|
72
|
+
#
|
73
|
+
# @see Nanoc::ItemRep#snapshot
|
74
|
+
#
|
75
|
+
# @param [Symbol] snapshot_name The name of the snapshot to create
|
76
|
+
#
|
77
|
+
# @return [void]
|
78
|
+
def snapshot(snapshot_name)
|
79
|
+
rep.snapshot(snapshot_name)
|
80
|
+
end
|
81
|
+
|
82
|
+
end
|
83
|
+
|
84
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Nanoc
|
4
|
+
|
5
|
+
# Calculates rule memories for objects that can be run through a rule (item
|
6
|
+
# representations and layouts).
|
7
|
+
#
|
8
|
+
# @api private
|
9
|
+
class RuleMemoryCalculator
|
10
|
+
|
11
|
+
extend Nanoc::Memoization
|
12
|
+
|
13
|
+
# @option params [Nanoc::RulesCollection] rules_collection The rules
|
14
|
+
# collection
|
15
|
+
def initialize(params={})
|
16
|
+
@rules_collection = params[:rules_collection] or
|
17
|
+
raise ArgumentError, "Required :rules_collection option is missing"
|
18
|
+
end
|
19
|
+
|
20
|
+
# @param [#reference] obj The object to calculate the rule memory for
|
21
|
+
#
|
22
|
+
# @return [Array] The caluclated rule memory for the given object
|
23
|
+
def [](obj)
|
24
|
+
result = case obj.type
|
25
|
+
when :item_rep
|
26
|
+
@rules_collection.new_rule_memory_for_rep(obj)
|
27
|
+
when :layout
|
28
|
+
@rules_collection.new_rule_memory_for_layout(obj)
|
29
|
+
else
|
30
|
+
raise RuntimeError,
|
31
|
+
"Do not know how to calculate the rule memory for #{obj.inspect}"
|
32
|
+
end
|
33
|
+
|
34
|
+
result
|
35
|
+
end
|
36
|
+
memoize :[]
|
37
|
+
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Nanoc
|
4
|
+
|
5
|
+
# Stores rule memories for objects that can be run through a rule (item
|
6
|
+
# representations and layouts).
|
7
|
+
#
|
8
|
+
# @api private
|
9
|
+
class RuleMemoryStore < ::Nanoc::Store
|
10
|
+
|
11
|
+
# @option params [Nanoc::Site] site The site where this rule memory store
|
12
|
+
# belongs to
|
13
|
+
def initialize(params={})
|
14
|
+
super('tmp/rule_memory', 1)
|
15
|
+
|
16
|
+
@site = params[:site] if params.has_key?(:site)
|
17
|
+
|
18
|
+
@rule_memories = {}
|
19
|
+
end
|
20
|
+
|
21
|
+
# @param [Nanoc::ItemRep, Nanoc::Layout] obj The item representation or
|
22
|
+
# the layout to get the rule memory for
|
23
|
+
#
|
24
|
+
# @return [Array] The rule memory for the given object
|
25
|
+
def [](obj)
|
26
|
+
@rule_memories[obj.reference]
|
27
|
+
end
|
28
|
+
|
29
|
+
# @param [Nanoc::ItemRep, Nanoc::Layout] obj The item representation or
|
30
|
+
# the layout to set the rule memory for
|
31
|
+
#
|
32
|
+
# @param [Array] rule_memory The new rule memory to be stored
|
33
|
+
#
|
34
|
+
# @return [void]
|
35
|
+
def []=(obj, rule_memory)
|
36
|
+
@rule_memories[obj.reference] = rule_memory
|
37
|
+
end
|
38
|
+
|
39
|
+
protected
|
40
|
+
|
41
|
+
# @see Nanoc::Store#data
|
42
|
+
def data
|
43
|
+
@rule_memories
|
44
|
+
end
|
45
|
+
|
46
|
+
# @see Nanoc::Store#data=
|
47
|
+
def data=(new_data)
|
48
|
+
@rule_memories = new_data
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
@@ -0,0 +1,243 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Nanoc
|
4
|
+
|
5
|
+
# Keeps track of the rules in a site.
|
6
|
+
#
|
7
|
+
# @api private
|
8
|
+
class RulesCollection
|
9
|
+
|
10
|
+
extend Nanoc::Memoization
|
11
|
+
|
12
|
+
# @return [Array<Nanoc::Rule>] The list of item compilation rules that
|
13
|
+
# will be used to compile items.
|
14
|
+
attr_reader :item_compilation_rules
|
15
|
+
|
16
|
+
# @return [Array<Nanoc::Rule>] The list of routing rules that will be
|
17
|
+
# used to give all items a path.
|
18
|
+
attr_reader :item_routing_rules
|
19
|
+
|
20
|
+
# The hash containing layout-to-filter mapping rules. This hash is
|
21
|
+
# ordered: iterating over the hash will happen in insertion order.
|
22
|
+
#
|
23
|
+
# @return [Hash] The layout-to-filter mapping rules
|
24
|
+
attr_reader :layout_filter_mapping
|
25
|
+
|
26
|
+
# @return [Proc] The code block that will be executed after all data is
|
27
|
+
# loaded but before the site is compiled
|
28
|
+
attr_accessor :preprocessor
|
29
|
+
|
30
|
+
# @param [Nanoc::Compiler] compiler The site’s compiler
|
31
|
+
def initialize(compiler)
|
32
|
+
@compiler = compiler
|
33
|
+
|
34
|
+
@item_compilation_rules = []
|
35
|
+
@item_routing_rules = []
|
36
|
+
@layout_filter_mapping = OrderedHash.new
|
37
|
+
end
|
38
|
+
|
39
|
+
# Add the given rule to the list of item compilation rules.
|
40
|
+
#
|
41
|
+
# @param [Nanoc::Rule] rule The item compilation rule to add
|
42
|
+
#
|
43
|
+
# @param [:before, :after] position The place where the rule should be
|
44
|
+
# added (either at the beginning or the end of the list of rules)
|
45
|
+
#
|
46
|
+
# @return [void]
|
47
|
+
def add_item_compilation_rule(rule, position=:after)
|
48
|
+
case position
|
49
|
+
when :before
|
50
|
+
@item_compilation_rules.unshift(rule)
|
51
|
+
when :after
|
52
|
+
@item_compilation_rules << rule
|
53
|
+
else
|
54
|
+
raise "#add_item_routing_rule expected position to be :after or :before"
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
# Add the given rule to the list of item routing rules.
|
59
|
+
#
|
60
|
+
# @param [Nanoc::Rule] rule The item routing rule to add
|
61
|
+
#
|
62
|
+
# @param [:before, :after] position The place where the rule should be
|
63
|
+
# added (either at the beginning or the end of the list of rules)
|
64
|
+
#
|
65
|
+
# @return [void]
|
66
|
+
def add_item_routing_rule(rule, position=:after)
|
67
|
+
case position
|
68
|
+
when :before
|
69
|
+
@item_routing_rules.unshift(rule)
|
70
|
+
when :after
|
71
|
+
@item_routing_rules << rule
|
72
|
+
else
|
73
|
+
raise "#add_item_routing_rule expected position to be :after or :before"
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
# @param [Nanoc::Item] item The item for which the compilation rules
|
78
|
+
# should be retrieved
|
79
|
+
#
|
80
|
+
# @return [Array] The list of item compilation rules for the given item
|
81
|
+
def item_compilation_rules_for(item)
|
82
|
+
@item_compilation_rules.select { |r| r.applicable_to?(item) }
|
83
|
+
end
|
84
|
+
|
85
|
+
# Loads this site’s rules.
|
86
|
+
#
|
87
|
+
# @return [void]
|
88
|
+
def load
|
89
|
+
# Find rules file
|
90
|
+
rules_filenames = [ 'Rules', 'rules', 'Rules.rb', 'rules.rb' ]
|
91
|
+
rules_filename = rules_filenames.find { |f| File.file?(f) }
|
92
|
+
raise Nanoc::Errors::NoRulesFileFound.new if rules_filename.nil?
|
93
|
+
|
94
|
+
# Get rule data
|
95
|
+
@data = File.read(rules_filename)
|
96
|
+
|
97
|
+
# Load DSL
|
98
|
+
dsl.instance_eval(@data, "./#{rules_filename}")
|
99
|
+
end
|
100
|
+
|
101
|
+
# Unloads this site’s rules.
|
102
|
+
#
|
103
|
+
# @return [void]
|
104
|
+
def unload
|
105
|
+
@item_compilation_rules = []
|
106
|
+
@item_routing_rules = []
|
107
|
+
@layout_filter_mapping = OrderedHash.new
|
108
|
+
end
|
109
|
+
|
110
|
+
# Finds the first matching compilation rule for the given item
|
111
|
+
# representation.
|
112
|
+
#
|
113
|
+
# @param [Nanoc::ItemRep] rep The item rep for which to fetch the rule
|
114
|
+
#
|
115
|
+
# @return [Nanoc::Rule, nil] The compilation rule for the given item rep,
|
116
|
+
# or nil if no rules have been found
|
117
|
+
def compilation_rule_for(rep)
|
118
|
+
@item_compilation_rules.find do |rule|
|
119
|
+
rule.applicable_to?(rep.item) && rule.rep_name == rep.name
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
# Finds the first matching routing rule for the given item representation.
|
124
|
+
#
|
125
|
+
# @param [Nanoc::ItemRep] rep The item rep for which to fetch the rule
|
126
|
+
#
|
127
|
+
# @return [Nanoc::Rule, nil] The routing rule for the given item rep, or
|
128
|
+
# nil if no rules have been found
|
129
|
+
def routing_rule_for(rep)
|
130
|
+
@item_routing_rules.find do |rule|
|
131
|
+
rule.applicable_to?(rep.item) && rule.rep_name == rep.name
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
# Returns the list of routing rules that can be applied to the given item
|
136
|
+
# representation. For each snapshot, the first matching rule will be
|
137
|
+
# returned. The result is a hash containing the corresponding rule for
|
138
|
+
# each snapshot.
|
139
|
+
#
|
140
|
+
# @param [Nanoc::ItemRep] rep The item rep for which to fetch the rules
|
141
|
+
#
|
142
|
+
# @return [Hash<Symbol, Nanoc::Rule>] The routing rules for the given rep
|
143
|
+
def routing_rules_for(rep)
|
144
|
+
rules = {}
|
145
|
+
@item_routing_rules.each do |rule|
|
146
|
+
next if !rule.applicable_to?(rep.item)
|
147
|
+
next if rule.rep_name != rep.name
|
148
|
+
next if rules.has_key?(rule.snapshot_name)
|
149
|
+
|
150
|
+
rules[rule.snapshot_name] = rule
|
151
|
+
end
|
152
|
+
rules
|
153
|
+
end
|
154
|
+
|
155
|
+
# Finds the filter name and arguments to use for the given layout.
|
156
|
+
#
|
157
|
+
# @param [Nanoc::Layout] layout The layout for which to fetch the filter.
|
158
|
+
#
|
159
|
+
# @return [Array, nil] A tuple containing the filter name and the filter
|
160
|
+
# arguments for the given layout.
|
161
|
+
def filter_for_layout(layout)
|
162
|
+
@layout_filter_mapping.each_pair do |layout_identifier, filter_name_and_args|
|
163
|
+
return filter_name_and_args if layout.identifier =~ layout_identifier
|
164
|
+
end
|
165
|
+
nil
|
166
|
+
end
|
167
|
+
|
168
|
+
# Returns the Nanoc::CompilerDSL that should be used for this site.
|
169
|
+
def dsl
|
170
|
+
Nanoc::CompilerDSL.new(self, @compiler.site.config)
|
171
|
+
end
|
172
|
+
memoize :dsl
|
173
|
+
|
174
|
+
# Returns an object that can be used for uniquely identifying objects.
|
175
|
+
#
|
176
|
+
# @return [Object] An unique reference to this object
|
177
|
+
def reference
|
178
|
+
:rules
|
179
|
+
end
|
180
|
+
|
181
|
+
# @return [String] The checksum for this object. If its contents change,
|
182
|
+
# the checksum will change as well.
|
183
|
+
def checksum
|
184
|
+
@data.checksum
|
185
|
+
end
|
186
|
+
|
187
|
+
def inspect
|
188
|
+
"<#{self.class}>"
|
189
|
+
end
|
190
|
+
|
191
|
+
# @param [Nanoc::ItemRep] rep The item representation to get the rule
|
192
|
+
# memory for
|
193
|
+
#
|
194
|
+
# @return [Array] The rule memory for the given item representation
|
195
|
+
def new_rule_memory_for_rep(rep)
|
196
|
+
recording_proxy = rep.to_recording_proxy
|
197
|
+
compilation_rule_for(rep).apply_to(recording_proxy, :compiler => @compiler)
|
198
|
+
recording_proxy.rule_memory
|
199
|
+
end
|
200
|
+
memoize :new_rule_memory_for_rep
|
201
|
+
|
202
|
+
# @param [Nanoc::Layout] layout The layout to get the rule memory for
|
203
|
+
#
|
204
|
+
# @return [Array] The rule memory for the given layout
|
205
|
+
def new_rule_memory_for_layout(layout)
|
206
|
+
filter_for_layout(layout)
|
207
|
+
end
|
208
|
+
memoize :new_rule_memory_for_layout
|
209
|
+
|
210
|
+
# @param [Nanoc::ItemRep] rep The item representation for which to fetch
|
211
|
+
# the list of snapshots
|
212
|
+
#
|
213
|
+
# @return [Array] A list of snapshots, represented as arrays where the
|
214
|
+
# first element is the snapshot name (a Symbol) and the last element is
|
215
|
+
# a Boolean indicating whether the snapshot is final or not
|
216
|
+
def snapshots_for(rep)
|
217
|
+
new_rule_memory_for_rep(rep).select { |e| e[0] == :snapshot }.map do |e|
|
218
|
+
[ e[1], e[2].fetch(:final) { true } ]
|
219
|
+
end
|
220
|
+
end
|
221
|
+
|
222
|
+
# @param [Nanoc::Item] obj The object for which to check the rule memory
|
223
|
+
#
|
224
|
+
# @return [Boolean] true if the rule memory for the given object has
|
225
|
+
# changed since the last compilation, false otherwise
|
226
|
+
def rule_memory_differs_for(obj)
|
227
|
+
!rule_memory_store[obj].eql?(rule_memory_calculator[obj])
|
228
|
+
end
|
229
|
+
memoize :rule_memory_differs_for
|
230
|
+
|
231
|
+
# @return [Nanoc::RuleMemoryStore] The rule memory store
|
232
|
+
def rule_memory_store
|
233
|
+
@compiler.rule_memory_store
|
234
|
+
end
|
235
|
+
|
236
|
+
# @return [Nanoc::RuleMemoryCalculator] The rule memory calculator
|
237
|
+
def rule_memory_calculator
|
238
|
+
@compiler.rule_memory_calculator
|
239
|
+
end
|
240
|
+
|
241
|
+
end
|
242
|
+
|
243
|
+
end
|