Class: Pith::Input
- Inherits:
-
Object
- Object
- Pith::Input
- Includes:
- Observable, Plugins::Publication::TemplateMethods
- Defined in:
- lib/pith/input.rb,
lib/pith/plugins/publication/input.rb
Instance Attribute Summary collapse
-
#output_path ⇒ Object
readonly
Returns the value of attribute output_path.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#pipeline ⇒ Object
readonly
Returns the value of attribute pipeline.
-
#project ⇒ Object
readonly
Returns the value of attribute project.
Instance Method Summary collapse
-
#file ⇒ Object
Public: Get the file-system location of this input.
-
#ignorable? ⇒ Boolean
Consider whether this input can be ignored.
-
#initialize(project, path) ⇒ Input
constructor
A new instance of Input.
-
#meta ⇒ Object
Public: Get YAML metadata declared in the header of of a template.
- #output ⇒ Object
-
#render(context, locals = {}, &block) ⇒ Object
Render this input using Tilt.
-
#resolve_path(ref) ⇒ Object
Public: Resolve a reference relative to this input.
-
#template? ⇒ Boolean
Determine whether this input is a template, requiring evaluation.
-
#title ⇒ Object
Public: Get page title.
- #when_added ⇒ Object
- #when_modified ⇒ Object
- #when_removed ⇒ Object
Methods included from Plugins::Publication::TemplateMethods
#published?, #published_at, #updated_at
Methods included from Observable
#add_observer, #notify_observers, #remove_observer
Constructor Details
#initialize(project, path) ⇒ Input
Returns a new instance of Input.
14 15 16 17 18 |
# File 'lib/pith/input.rb', line 14 def initialize(project, path) @project = project @path = path determine_pipeline end |
Instance Attribute Details
#output_path ⇒ Object (readonly)
Returns the value of attribute output_path.
22 23 24 |
# File 'lib/pith/input.rb', line 22 def output_path @output_path end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
20 21 22 |
# File 'lib/pith/input.rb', line 20 def path @path end |
#pipeline ⇒ Object (readonly)
Returns the value of attribute pipeline.
23 24 25 |
# File 'lib/pith/input.rb', line 23 def pipeline @pipeline end |
#project ⇒ Object (readonly)
Returns the value of attribute project.
20 21 22 |
# File 'lib/pith/input.rb', line 20 def project @project end |
Instance Method Details
#file ⇒ Object
Public: Get the file-system location of this input.
Returns a fully-qualified Pathname.
29 30 31 |
# File 'lib/pith/input.rb', line 29 def file @file ||= project.input_dir + path end |
#ignorable? ⇒ Boolean
Consider whether this input can be ignored.
Returns true if it can.
37 38 39 40 41 42 43 |
# File 'lib/pith/input.rb', line 37 def ignorable? @ignorable ||= path.each_filename do |path_component| project.config.ignore_patterns.each do |pattern| return true if File.fnmatch(pattern, path_component) end end end |
#meta ⇒ Object
Public: Get YAML metadata declared in the header of of a template.
If the first line of the template starts with “—” it is considered to be the start of a YAML ‘document’, which is loaded and returned.
Examples
Given input starting with:
---
published: 2008-09-15
...
OTHER STUFF
input.meta
#=> { "published" => "2008-09-15" }
Returns a Hash.
89 90 91 92 |
# File 'lib/pith/input.rb', line 89 def ensure_loaded @meta end |
#output ⇒ Object
53 54 55 56 57 |
# File 'lib/pith/input.rb', line 53 def output unless ignorable? @output ||= Output.for(self, @output_path) end end |
#render(context, locals = {}, &block) ⇒ Object
Render this input using Tilt
61 62 63 64 65 66 67 68 |
# File 'lib/pith/input.rb', line 61 def render(context, locals = {}, &block) return file.read if !template? ensure_loaded @pipeline.inject(@template_text) do |text, processor| template = processor.new(file.to_s, @template_start_line) { text } template.render(context, locals, &block) end end |
#resolve_path(ref) ⇒ Object
Public: Resolve a reference relative to this input.
ref - a String referencing another asset
A ref starting with “/” is resolved relative to the project root; anything else is resolved relative to this input.
Returns a fully-qualified Pathname of the asset.
119 120 121 122 123 124 125 126 |
# File 'lib/pith/input.rb', line 119 def resolve_path(ref) ref = ref.to_s if ref[0,1] == "/" Pathname(ref[1..-1]) else path.parent + ref end end |
#template? ⇒ Boolean
Determine whether this input is a template, requiring evaluation.
Returns true if it is.
49 50 51 |
# File 'lib/pith/input.rb', line 49 def template? !pipeline.empty? end |
#title ⇒ Object
Public: Get page title.
The default title is based on the input file-name, sans-extension, capitalised, but can be overridden by providing a “title” in the metadata block.
Examples
input.path.to_s
#=> "some_page.html.haml"
input.title
#=> "Some page"
106 107 108 |
# File 'lib/pith/input.rb', line 106 def title ["title"] || default_title end |
#when_added ⇒ Object
128 129 130 |
# File 'lib/pith/input.rb', line 128 def when_added log_lifecycle "+" end |
#when_modified ⇒ Object
132 133 134 135 136 |
# File 'lib/pith/input.rb', line 132 def when_modified log_lifecycle "~" unload if loaded? notify_observers end |
#when_removed ⇒ Object
138 139 140 141 142 |
# File 'lib/pith/input.rb', line 138 def when_removed log_lifecycle "X" output.delete if output notify_observers end |