nanoc 3.2.4 → 3.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (230) hide show
  1. data/.gemtest +0 -0
  2. data/ChangeLog +3 -0
  3. data/Gemfile +32 -0
  4. data/LICENSE +19 -0
  5. data/NEWS.md +470 -0
  6. data/README.md +114 -0
  7. data/Rakefile +14 -0
  8. data/bin/nanoc +7 -27
  9. data/bin/nanoc3 +3 -0
  10. data/doc/yardoc_templates/default/layout/html/footer.erb +10 -0
  11. data/lib/nanoc.rb +41 -0
  12. data/lib/nanoc/base.rb +49 -0
  13. data/lib/nanoc/base/compilation/checksum_store.rb +57 -0
  14. data/lib/nanoc/base/compilation/compiled_content_cache.rb +62 -0
  15. data/lib/nanoc/base/compilation/compiler.rb +458 -0
  16. data/lib/nanoc/base/compilation/compiler_dsl.rb +214 -0
  17. data/lib/nanoc/base/compilation/dependency_tracker.rb +200 -0
  18. data/lib/nanoc/base/compilation/filter.rb +165 -0
  19. data/lib/nanoc/base/compilation/item_rep_proxy.rb +103 -0
  20. data/lib/nanoc/base/compilation/item_rep_recorder_proxy.rb +102 -0
  21. data/lib/nanoc/base/compilation/outdatedness_checker.rb +223 -0
  22. data/lib/nanoc/base/compilation/outdatedness_reasons.rb +46 -0
  23. data/lib/nanoc/base/compilation/rule.rb +73 -0
  24. data/lib/nanoc/base/compilation/rule_context.rb +84 -0
  25. data/lib/nanoc/base/compilation/rule_memory_calculator.rb +40 -0
  26. data/lib/nanoc/base/compilation/rule_memory_store.rb +53 -0
  27. data/lib/nanoc/base/compilation/rules_collection.rb +243 -0
  28. data/lib/nanoc/base/context.rb +47 -0
  29. data/lib/nanoc/base/core_ext.rb +6 -0
  30. data/lib/nanoc/base/core_ext/array.rb +62 -0
  31. data/lib/nanoc/base/core_ext/hash.rb +63 -0
  32. data/lib/nanoc/base/core_ext/pathname.rb +26 -0
  33. data/lib/nanoc/base/core_ext/string.rb +46 -0
  34. data/lib/nanoc/base/directed_graph.rb +275 -0
  35. data/lib/nanoc/base/errors.rb +211 -0
  36. data/lib/nanoc/base/memoization.rb +67 -0
  37. data/lib/nanoc/base/notification_center.rb +84 -0
  38. data/lib/nanoc/base/ordered_hash.rb +200 -0
  39. data/lib/nanoc/base/plugin_registry.rb +181 -0
  40. data/lib/nanoc/base/result_data/item_rep.rb +492 -0
  41. data/lib/nanoc/base/source_data/code_snippet.rb +58 -0
  42. data/lib/nanoc/base/source_data/configuration.rb +24 -0
  43. data/lib/nanoc/base/source_data/data_source.rb +234 -0
  44. data/lib/nanoc/base/source_data/item.rb +301 -0
  45. data/lib/nanoc/base/source_data/layout.rb +130 -0
  46. data/lib/nanoc/base/source_data/site.rb +361 -0
  47. data/lib/nanoc/base/store.rb +135 -0
  48. data/lib/nanoc/cli.rb +137 -0
  49. data/lib/nanoc/cli/command_runner.rb +104 -0
  50. data/lib/nanoc/cli/commands/autocompile.rb +58 -0
  51. data/lib/nanoc/cli/commands/compile.rb +297 -0
  52. data/lib/nanoc/cli/commands/create_item.rb +60 -0
  53. data/lib/nanoc/cli/commands/create_layout.rb +73 -0
  54. data/lib/nanoc/cli/commands/create_site.rb +411 -0
  55. data/lib/nanoc/cli/commands/debug.rb +117 -0
  56. data/lib/nanoc/cli/commands/deploy.rb +79 -0
  57. data/lib/nanoc/cli/commands/info.rb +98 -0
  58. data/lib/nanoc/cli/commands/nanoc.rb +38 -0
  59. data/lib/nanoc/cli/commands/prune.rb +50 -0
  60. data/lib/nanoc/cli/commands/update.rb +70 -0
  61. data/lib/nanoc/cli/commands/view.rb +82 -0
  62. data/lib/nanoc/cli/commands/watch.rb +124 -0
  63. data/lib/nanoc/cli/error_handler.rb +199 -0
  64. data/lib/nanoc/cli/logger.rb +92 -0
  65. data/lib/nanoc/data_sources.rb +29 -0
  66. data/lib/nanoc/data_sources/deprecated/delicious.rb +42 -0
  67. data/lib/nanoc/data_sources/deprecated/last_fm.rb +87 -0
  68. data/lib/nanoc/data_sources/deprecated/twitter.rb +38 -0
  69. data/lib/nanoc/data_sources/filesystem.rb +299 -0
  70. data/lib/nanoc/data_sources/filesystem_unified.rb +121 -0
  71. data/lib/nanoc/data_sources/filesystem_verbose.rb +91 -0
  72. data/lib/nanoc/extra.rb +24 -0
  73. data/lib/nanoc/extra/auto_compiler.rb +103 -0
  74. data/lib/nanoc/extra/chick.rb +125 -0
  75. data/lib/nanoc/extra/core_ext.rb +6 -0
  76. data/lib/nanoc/extra/core_ext/enumerable.rb +33 -0
  77. data/lib/nanoc/extra/core_ext/pathname.rb +30 -0
  78. data/lib/nanoc/extra/core_ext/time.rb +19 -0
  79. data/lib/nanoc/extra/deployer.rb +47 -0
  80. data/lib/nanoc/extra/deployers.rb +15 -0
  81. data/lib/nanoc/extra/deployers/fog.rb +98 -0
  82. data/lib/nanoc/extra/deployers/rsync.rb +70 -0
  83. data/lib/nanoc/extra/file_proxy.rb +40 -0
  84. data/lib/nanoc/extra/pruner.rb +86 -0
  85. data/lib/nanoc/extra/validators.rb +12 -0
  86. data/lib/nanoc/extra/validators/links.rb +268 -0
  87. data/lib/nanoc/extra/validators/w3c.rb +95 -0
  88. data/lib/nanoc/extra/vcs.rb +66 -0
  89. data/lib/nanoc/extra/vcses.rb +17 -0
  90. data/lib/nanoc/extra/vcses/bazaar.rb +25 -0
  91. data/lib/nanoc/extra/vcses/dummy.rb +24 -0
  92. data/lib/nanoc/extra/vcses/git.rb +25 -0
  93. data/lib/nanoc/extra/vcses/mercurial.rb +25 -0
  94. data/lib/nanoc/extra/vcses/subversion.rb +25 -0
  95. data/lib/nanoc/filters.rb +59 -0
  96. data/lib/nanoc/filters/asciidoc.rb +38 -0
  97. data/lib/nanoc/filters/bluecloth.rb +19 -0
  98. data/lib/nanoc/filters/coderay.rb +21 -0
  99. data/lib/nanoc/filters/coffeescript.rb +20 -0
  100. data/lib/nanoc/filters/colorize_syntax.rb +298 -0
  101. data/lib/nanoc/filters/erb.rb +38 -0
  102. data/lib/nanoc/filters/erubis.rb +34 -0
  103. data/lib/nanoc/filters/haml.rb +27 -0
  104. data/lib/nanoc/filters/kramdown.rb +20 -0
  105. data/lib/nanoc/filters/less.rb +53 -0
  106. data/lib/nanoc/filters/markaby.rb +20 -0
  107. data/lib/nanoc/filters/maruku.rb +20 -0
  108. data/lib/nanoc/filters/mustache.rb +24 -0
  109. data/lib/nanoc/filters/rainpress.rb +19 -0
  110. data/lib/nanoc/filters/rdiscount.rb +22 -0
  111. data/lib/nanoc/filters/rdoc.rb +33 -0
  112. data/lib/nanoc/filters/redcarpet.rb +62 -0
  113. data/lib/nanoc/filters/redcloth.rb +47 -0
  114. data/lib/nanoc/filters/relativize_paths.rb +94 -0
  115. data/lib/nanoc/filters/rubypants.rb +20 -0
  116. data/lib/nanoc/filters/sass.rb +74 -0
  117. data/lib/nanoc/filters/slim.rb +25 -0
  118. data/lib/nanoc/filters/typogruby.rb +23 -0
  119. data/lib/nanoc/filters/uglify_js.rb +42 -0
  120. data/lib/nanoc/filters/xsl.rb +46 -0
  121. data/lib/nanoc/filters/yui_compressor.rb +23 -0
  122. data/lib/nanoc/helpers.rb +16 -0
  123. data/lib/nanoc/helpers/blogging.rb +319 -0
  124. data/lib/nanoc/helpers/breadcrumbs.rb +40 -0
  125. data/lib/nanoc/helpers/capturing.rb +138 -0
  126. data/lib/nanoc/helpers/filtering.rb +50 -0
  127. data/lib/nanoc/helpers/html_escape.rb +55 -0
  128. data/lib/nanoc/helpers/link_to.rb +151 -0
  129. data/lib/nanoc/helpers/rendering.rb +140 -0
  130. data/lib/nanoc/helpers/tagging.rb +71 -0
  131. data/lib/nanoc/helpers/text.rb +44 -0
  132. data/lib/nanoc/helpers/xml_sitemap.rb +76 -0
  133. data/lib/nanoc/tasks.rb +10 -0
  134. data/lib/nanoc/tasks/clean.rake +16 -0
  135. data/lib/nanoc/tasks/clean.rb +29 -0
  136. data/lib/nanoc/tasks/deploy/rsync.rake +16 -0
  137. data/lib/nanoc/tasks/validate.rake +92 -0
  138. data/nanoc.gemspec +49 -0
  139. data/tasks/doc.rake +16 -0
  140. data/tasks/test.rake +46 -0
  141. data/test/base/core_ext/array_spec.rb +73 -0
  142. data/test/base/core_ext/hash_spec.rb +98 -0
  143. data/test/base/core_ext/pathname_spec.rb +27 -0
  144. data/test/base/core_ext/string_spec.rb +37 -0
  145. data/test/base/test_checksum_store.rb +35 -0
  146. data/test/base/test_code_snippet.rb +31 -0
  147. data/test/base/test_compiler.rb +403 -0
  148. data/test/base/test_compiler_dsl.rb +161 -0
  149. data/test/base/test_context.rb +31 -0
  150. data/test/base/test_data_source.rb +46 -0
  151. data/test/base/test_dependency_tracker.rb +262 -0
  152. data/test/base/test_directed_graph.rb +288 -0
  153. data/test/base/test_filter.rb +83 -0
  154. data/test/base/test_item.rb +179 -0
  155. data/test/base/test_item_rep.rb +579 -0
  156. data/test/base/test_layout.rb +59 -0
  157. data/test/base/test_memoization.rb +90 -0
  158. data/test/base/test_notification_center.rb +34 -0
  159. data/test/base/test_outdatedness_checker.rb +394 -0
  160. data/test/base/test_plugin.rb +30 -0
  161. data/test/base/test_rule.rb +19 -0
  162. data/test/base/test_rule_context.rb +65 -0
  163. data/test/base/test_site.rb +190 -0
  164. data/test/cli/commands/test_compile.rb +33 -0
  165. data/test/cli/commands/test_create_item.rb +14 -0
  166. data/test/cli/commands/test_create_layout.rb +28 -0
  167. data/test/cli/commands/test_create_site.rb +24 -0
  168. data/test/cli/commands/test_deploy.rb +74 -0
  169. data/test/cli/commands/test_help.rb +12 -0
  170. data/test/cli/commands/test_info.rb +11 -0
  171. data/test/cli/commands/test_prune.rb +98 -0
  172. data/test/cli/commands/test_update.rb +10 -0
  173. data/test/cli/test_cli.rb +102 -0
  174. data/test/cli/test_error_handler.rb +29 -0
  175. data/test/cli/test_logger.rb +10 -0
  176. data/test/data_sources/test_filesystem.rb +433 -0
  177. data/test/data_sources/test_filesystem_unified.rb +536 -0
  178. data/test/data_sources/test_filesystem_verbose.rb +357 -0
  179. data/test/extra/core_ext/test_enumerable.rb +30 -0
  180. data/test/extra/core_ext/test_pathname.rb +17 -0
  181. data/test/extra/core_ext/test_time.rb +15 -0
  182. data/test/extra/deployers/test_fog.rb +67 -0
  183. data/test/extra/deployers/test_rsync.rb +100 -0
  184. data/test/extra/test_auto_compiler.rb +417 -0
  185. data/test/extra/test_file_proxy.rb +19 -0
  186. data/test/extra/test_vcs.rb +22 -0
  187. data/test/extra/validators/test_links.rb +62 -0
  188. data/test/extra/validators/test_w3c.rb +47 -0
  189. data/test/filters/test_asciidoc.rb +22 -0
  190. data/test/filters/test_bluecloth.rb +18 -0
  191. data/test/filters/test_coderay.rb +44 -0
  192. data/test/filters/test_coffeescript.rb +18 -0
  193. data/test/filters/test_colorize_syntax.rb +379 -0
  194. data/test/filters/test_erb.rb +105 -0
  195. data/test/filters/test_erubis.rb +78 -0
  196. data/test/filters/test_haml.rb +96 -0
  197. data/test/filters/test_kramdown.rb +18 -0
  198. data/test/filters/test_less.rb +113 -0
  199. data/test/filters/test_markaby.rb +24 -0
  200. data/test/filters/test_maruku.rb +18 -0
  201. data/test/filters/test_mustache.rb +25 -0
  202. data/test/filters/test_rainpress.rb +29 -0
  203. data/test/filters/test_rdiscount.rb +31 -0
  204. data/test/filters/test_rdoc.rb +18 -0
  205. data/test/filters/test_redcarpet.rb +73 -0
  206. data/test/filters/test_redcloth.rb +33 -0
  207. data/test/filters/test_relativize_paths.rb +533 -0
  208. data/test/filters/test_rubypants.rb +18 -0
  209. data/test/filters/test_sass.rb +229 -0
  210. data/test/filters/test_slim.rb +35 -0
  211. data/test/filters/test_typogruby.rb +21 -0
  212. data/test/filters/test_uglify_js.rb +30 -0
  213. data/test/filters/test_xsl.rb +105 -0
  214. data/test/filters/test_yui_compressor.rb +44 -0
  215. data/test/gem_loader.rb +11 -0
  216. data/test/helper.rb +207 -0
  217. data/test/helpers/test_blogging.rb +754 -0
  218. data/test/helpers/test_breadcrumbs.rb +81 -0
  219. data/test/helpers/test_capturing.rb +41 -0
  220. data/test/helpers/test_filtering.rb +106 -0
  221. data/test/helpers/test_html_escape.rb +32 -0
  222. data/test/helpers/test_link_to.rb +249 -0
  223. data/test/helpers/test_rendering.rb +89 -0
  224. data/test/helpers/test_tagging.rb +87 -0
  225. data/test/helpers/test_text.rb +24 -0
  226. data/test/helpers/test_xml_sitemap.rb +103 -0
  227. data/test/tasks/test_clean.rb +67 -0
  228. metadata +327 -15
  229. data/bin/nanoc-select +0 -86
  230. 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