Class: Nanoc::Core::Executor
- Inherits:
-
Object
- Object
- Nanoc::Core::Executor
- Defined in:
- lib/nanoc/core/executor.rb
Instance Method Summary collapse
- #assigns ⇒ Object
- #filter(filter_name, filter_args = {}) ⇒ Object
- #filter_for_filtering(filter_name) ⇒ Object
- #find_layout(arg) ⇒ Object
-
#initialize(rep, compilation_context, dependency_tracker) ⇒ Executor
constructor
A new instance of Executor.
- #layout(layout_identifier, extra_filter_args = nil) ⇒ Object
- #layouts ⇒ Object
- #snapshot(snapshot_name) ⇒ Object
- #use_globs? ⇒ Boolean
- #view_context ⇒ Object
Constructor Details
#initialize(rep, compilation_context, dependency_tracker) ⇒ Executor
Returns a new instance of Executor.
6 7 8 9 10 |
# File 'lib/nanoc/core/executor.rb', line 6 def initialize(rep, compilation_context, dependency_tracker) @rep = rep @compilation_context = compilation_context @dependency_tracker = dependency_tracker end |
Instance Method Details
#assigns ⇒ Object
100 101 102 |
# File 'lib/nanoc/core/executor.rb', line 100 def assigns view_context.assigns_for(@rep, site: @compilation_context.site) end |
#filter(filter_name, filter_args = {}) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/nanoc/core/executor.rb', line 12 def filter(filter_name, filter_args = {}) filter = filter_for_filtering(filter_name) begin Nanoc::Core::NotificationCenter.post( :filtering_started, @rep, filter_name ) # Run filter last = @compilation_context.compiled_content_repo.get_current(@rep) source = last.binary? ? last.filename : last.string filter_args.freeze result = filter.setup_and_run(source, filter_args) last = if filter.class.to_binary? Nanoc::Core::BinaryContent.new(filter.output_filename).tap(&:freeze) else Nanoc::Core::TextualContent.new(result).tap(&:freeze) end # Store @compilation_context.compiled_content_repo.set_current(@rep, last) ensure Nanoc::Core::NotificationCenter.post( :filtering_ended, @rep, filter_name ) end end |
#filter_for_filtering(filter_name) ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/nanoc/core/executor.rb', line 123 def filter_for_filtering(filter_name) klass = Nanoc::Core::Filter.named!(filter_name) last = @compilation_context.compiled_content_repo.get_current(@rep) if klass.from_binary? && !last.binary? raise Nanoc::Core::Errors::CannotUseBinaryFilter.new(@rep, klass) elsif !klass.from_binary? && last.binary? raise Nanoc::Core::Errors::CannotUseTextualFilter.new(@rep, klass) end klass.new(assigns) end |
#find_layout(arg) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/nanoc/core/executor.rb', line 108 def find_layout(arg) req_id = arg.__nanoc_cleaned_identifier layout = layouts.object_with_identifier(req_id) return layout if layout if use_globs? # TODO: use object_matching_glob instead pat = Nanoc::Core::Pattern.from(arg) layout = layouts.find { |l| pat.match?(l.identifier) } return layout if layout end raise Nanoc::Core::Errors::UnknownLayout.new(arg) end |
#layout(layout_identifier, extra_filter_args = nil) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/nanoc/core/executor.rb', line 41 def layout(layout_identifier, extra_filter_args = nil) layout = find_layout(layout_identifier) filter_name_and_args = @compilation_context.filter_name_and_args_for_layout(layout) filter_name = filter_name_and_args.name if filter_name.nil? raise Nanoc::Core::Errors::CannotDetermineFilter .new(layout_identifier) end filter_args = filter_name_and_args.args filter_args = filter_args.merge(extra_filter_args || {}) filter_args.freeze # Check whether item can be laid out last = @compilation_context.compiled_content_repo.get_current(@rep) if last.binary? raise Nanoc::Core::Errors::CannotLayoutBinaryItem.new(@rep) end # Create filter klass = Nanoc::Core::Filter.named!(filter_name) layout_view = Nanoc::Core::LayoutView.new(layout, view_context) filter = klass.new(assigns.merge(layout: layout_view)) # Visit @dependency_tracker.bounce(layout, raw_content: true) begin Nanoc::Core::NotificationCenter.post( :filtering_started, @rep, filter_name ) # Layout content = layout.content arg = content.binary? ? content.filename : content.string res = filter.setup_and_run(arg, filter_args) # Store last = Nanoc::Core::TextualContent.new(res).tap(&:freeze) @compilation_context.compiled_content_repo.set_current(@rep, last) ensure Nanoc::Core::NotificationCenter.post( :filtering_ended, @rep, filter_name ) end end |
#layouts ⇒ Object
104 105 106 |
# File 'lib/nanoc/core/executor.rb', line 104 def layouts @compilation_context.site.layouts end |
#snapshot(snapshot_name) ⇒ Object
89 90 91 92 93 94 95 96 97 98 |
# File 'lib/nanoc/core/executor.rb', line 89 def snapshot(snapshot_name) last = @compilation_context.compiled_content_repo.get_current(@rep) @compilation_context.compiled_content_repo.set( @rep, snapshot_name, last ) Nanoc::Core::NotificationCenter.post( :snapshot_created, @rep, snapshot_name ) end |
#use_globs? ⇒ Boolean
136 137 138 |
# File 'lib/nanoc/core/executor.rb', line 136 def use_globs? @compilation_context.site.config[:string_pattern_type] == 'glob' end |
#view_context ⇒ Object
140 141 142 143 144 145 146 147 148 149 |
# File 'lib/nanoc/core/executor.rb', line 140 def view_context @_view_context ||= Nanoc::Core::ViewContextForCompilation.new( reps: @compilation_context.reps, items: @compilation_context.site.items, dependency_tracker: @dependency_tracker, compilation_context: @compilation_context, compiled_content_repo: @compilation_context.compiled_content_repo, ) end |