Class: Nanoc3::Compiler
- Inherits:
-
Object
- Object
- Nanoc3::Compiler
- Extended by:
- Memoization
- Defined in:
- lib/nanoc3/base/compilation/compiler.rb
Overview
Responsible for compiling a site’s item representations.
The compilation process makes use of notifications (see NotificationCenter) to track dependencies between items, layouts, etc. The following notifications are used:
-
‘compilation_started` — indicates that the compiler has started compiling this item representation. Has one argument: the item representation itself. Only one item can be compiled at a given moment; therefore, it is not possible to get two consecutive `compilation_started` notifications without also getting a `compilation_ended` notification in between them.
-
‘compilation_ended` — indicates that the compiler has finished compiling this item representation (either successfully or with failure). Has one argument: the item representation itself.
-
‘visit_started` — indicates that the compiler requires content or attributes from the item representation that will be visited. Has one argument: the visited item identifier. This notification is used to track dependencies of items on other items; a `visit_started` event followed by another `visit_started` event indicates that the item corresponding to the former event will depend on the item from the latter event.
-
‘visit_ended` — indicates that the compiler has finished visiting the item representation and that the requested attributes or content have been fetched (either successfully or with failure)
-
‘processing_started` — indicates that the compiler has started processing the specified object, which can be an item representation (when it is compiled) or a layout (when it is used to lay out an item representation or when it is used as a partial)
-
‘processing_ended` — indicates that the compiler has finished processing the specified object.
Accessors collapse
-
#site ⇒ Nanoc3::Site
readonly
The site this compiler belongs to.
-
#stack ⇒ Array
readonly
The compilation stack.
Public instance methods collapse
-
#initialize(site) ⇒ Compiler
constructor
Creates a new compiler fo the given site.
-
#run ⇒ void
Compiles the site and writes out the compiled item representations.
Private instance methods collapse
-
#assigns_for(rep) ⇒ Hash
private
The assigns that should be used in the next filter/layout operation.
-
#build_reps ⇒ Object
private
Creates the representations of all items as defined by the compilation rules.
-
#dependency_tracker ⇒ Nanoc3::DependencyTracker
private
Returns the dependency tracker for this site, creating it first if it does not yet exist.
-
#load ⇒ void
private
Load the helper data that is used for compiling the site.
-
#objects ⇒ Object
private
Returns all objects managed by the site (items, layouts, code snippets, site configuration and the rules).
-
#outdatedness_checker ⇒ Nanoc3::OutdatednessChecker
The outdatedness checker.
-
#preprocess ⇒ Object
private
Runs the preprocessor.
-
#route_reps ⇒ Object
private
Determines the paths of all item representations.
-
#rules_collection ⇒ Nanoc3::RulesCollection
The collection of rules to be used for compiling this site.
-
#store ⇒ void
private
Store the modified helper data used for compiling the site.
-
#unload ⇒ void
private
Undoes the effects of #load.
Methods included from Memoization
Constructor Details
#initialize(site) ⇒ Compiler
Creates a new compiler fo the given site
62 63 64 65 66 |
# File 'lib/nanoc3/base/compilation/compiler.rb', line 62 def initialize(site) @site = site @stack = [] end |
Instance Attribute Details
#site ⇒ Nanoc3::Site (readonly)
Returns The site this compiler belongs to.
48 49 50 |
# File 'lib/nanoc3/base/compilation/compiler.rb', line 48 def site @site end |
#stack ⇒ Array (readonly)
The compilation stack. When the compiler begins compiling a rep or a layout, it will be placed on the stack; when it is done compiling the rep or layout, it will be removed from the stack.
55 56 57 |
# File 'lib/nanoc3/base/compilation/compiler.rb', line 55 def stack @stack end |
Instance Method Details
#assigns_for(rep) ⇒ Hash
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns The assigns that should be used in the next filter/layout operation.
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 |
# File 'lib/nanoc3/base/compilation/compiler.rb', line 266 def assigns_for(rep) if rep.binary? content_or_filename_assigns = { :filename => rep.temporary_filenames[:last] } else content_or_filename_assigns = { :content => rep.content[:last] } end content_or_filename_assigns.merge({ :item => rep.item, :item_rep => rep, :items => site.items, :layouts => site.layouts, :config => site.config, :site => site }) end |
#build_reps ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Creates the representations of all items as defined by the compilation rules.
212 213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/nanoc3/base/compilation/compiler.rb', line 212 def build_reps items.each do |item| # Find matching rules matching_rules = rules_collection.item_compilation_rules_for(item) raise Nanoc3::Errors::NoMatchingCompilationRuleFound.new(item) if matching_rules.empty? # Create reps rep_names = matching_rules.map { |r| r.rep_name }.uniq rep_names.each do |rep_name| item.reps << ItemRep.new(item, rep_name) end end end |
#dependency_tracker ⇒ Nanoc3::DependencyTracker
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns the dependency tracker for this site, creating it first if it does not yet exist.
184 185 186 187 188 |
# File 'lib/nanoc3/base/compilation/compiler.rb', line 184 def dependency_tracker dt = Nanoc3::DependencyTracker.new(@site.items + @site.layouts) dt.compiler = self dt end |
#load ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Load the helper data that is used for compiling the site.
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/nanoc3/base/compilation/compiler.rb', line 107 def load return if @loaded || @loading @loading = true # Load site if necessary site.load # Preprocess rules_collection.load preprocess site.setup_child_parent_links build_reps route_reps # Load auxiliary stores stores.each { |s| s.load } # Determine which reps need to be recompiled forget_dependencies_if_outdated(items) @loaded = true rescue => e unload raise e ensure @loading = false end |
#objects ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns all objects managed by the site (items, layouts, code snippets, site configuration and the rules).
203 204 205 206 |
# File 'lib/nanoc3/base/compilation/compiler.rb', line 203 def objects site.items + site.layouts + site.code_snippets + [ site.config, rules_collection ] end |
#outdatedness_checker ⇒ Nanoc3::OutdatednessChecker
Returns The outdatedness checker.
284 285 286 287 288 289 |
# File 'lib/nanoc3/base/compilation/compiler.rb', line 284 def outdatedness_checker Nanoc3::OutdatednessChecker.new( :site => @site, :checksum_store => checksum_store, :dependency_tracker => dependency_tracker) end |
#preprocess ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Runs the preprocessor.
194 195 196 197 |
# File 'lib/nanoc3/base/compilation/compiler.rb', line 194 def preprocess return if rules_collection.preprocessor.nil? preprocessor_context.instance_eval(&rules_collection.preprocessor) end |
#route_reps ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Determines the paths of all item representations.
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 |
# File 'lib/nanoc3/base/compilation/compiler.rb', line 229 def route_reps reps.each do |rep| # Find matching rules rules = rules_collection.routing_rules_for(rep) raise Nanoc3::Errors::NoMatchingRoutingRuleFound.new(rep) if rules[:last].nil? rules.each_pair do |snapshot, rule| # Get basic path by applying matching rule basic_path = rule.apply_to(rep, :compiler => self) next if basic_path.nil? if basic_path !~ %r{^/} raise RuntimeError, "The path returned for the #{rep.inspect} item representation, “#{basic_path}”, does not start with a slash. Please ensure that all routing rules return a path that starts with a slash.".make_compatible_with_env end # Get raw path by prepending output directory rep.raw_paths[snapshot] = @site.config[:output_dir] + basic_path # Get normal path by stripping index filename rep.paths[snapshot] = basic_path @site.config[:index_filenames].each do |index_filename| if rep.paths[snapshot][-index_filename.length..-1] == index_filename # Strip and stop rep.paths[snapshot] = rep.paths[snapshot][0..-index_filename.length-1] break end end end end end |
#rules_collection ⇒ Nanoc3::RulesCollection
Returns The collection of rules to be used for compiling this site.
97 98 99 |
# File 'lib/nanoc3/base/compilation/compiler.rb', line 97 def rules_collection Nanoc3::RulesCollection.new(self) end |
#run ⇒ void
Compiles the site and writes out the compiled item representations.
Previous versions of nanoc (< 3.2) allowed passing items to compile, and had a “force” option to make the compiler recompile all pages, even when not outdated. These arguments and options are, as of nanoc 3.2, no longer used, and will simply be ignored when passed to #run.
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/nanoc3/base/compilation/compiler.rb', line 77 def run(*args) # Create output directory if necessary FileUtils.mkdir_p(@site.config[:output_dir]) # Compile reps load @site.freeze dependency_tracker.start compile_reps(reps) dependency_tracker.stop store ensure # Cleanup FileUtils.rm_rf(Nanoc3::Filter::TMP_BINARY_ITEMS_DIR) end |
#store ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Store the modified helper data used for compiling the site.
163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/nanoc3/base/compilation/compiler.rb', line 163 def store # Calculate rule memory (reps + layouts).each do |obj| rule_memory_store[obj] = rule_memory_calculator[obj] end # Calculate checksums self.objects.each do |obj| checksum_store[obj] = obj.checksum end # Store stores.each { |s| s.store } end |
#unload ⇒ void
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/nanoc3/base/compilation/compiler.rb', line 140 def unload return if @unloading @unloading = true stores.each { |s| s.unload } @stack = [] items.each { |item| item.reps.clear } site.teardown_child_parent_links rules_collection.unload site.unload @loaded = false @unloading = false end |