Class: Pith::Input

Inherits:
Object
  • Object
show all
Includes:
Observable, Plugins::Publication::TemplateMethods
Defined in:
lib/pith/input.rb,
lib/pith/plugins/publication/input.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_pathObject (readonly)

Returns the value of attribute output_path.



22
23
24
# File 'lib/pith/input.rb', line 22

def output_path
  @output_path
end

#pathObject (readonly)

Returns the value of attribute path.



20
21
22
# File 'lib/pith/input.rb', line 20

def path
  @path
end

#pipelineObject (readonly)

Returns the value of attribute pipeline.



23
24
25
# File 'lib/pith/input.rb', line 23

def pipeline
  @pipeline
end

#projectObject (readonly)

Returns the value of attribute project.



20
21
22
# File 'lib/pith/input.rb', line 20

def project
  @project
end

Instance Method Details

#fileObject

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.

Returns:

  • (Boolean)


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

#metaObject

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 meta
  ensure_loaded
  @meta
end

#outputObject



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.

Returns:

  • (Boolean)


49
50
51
# File 'lib/pith/input.rb', line 49

def template?
  !pipeline.empty?
end

#titleObject

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
  meta["title"] || default_title
end

#when_addedObject



128
129
130
# File 'lib/pith/input.rb', line 128

def when_added
  log_lifecycle "+"
end

#when_modifiedObject



132
133
134
135
136
# File 'lib/pith/input.rb', line 132

def when_modified
  log_lifecycle "~"
  unload if loaded?
  notify_observers
end

#when_removedObject



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