class Pith::Input
Constants
- YamlParseError
Attributes
Public Class Methods
# File lib/pith/input.rb, line 14 def initialize(project, path) @project = project @path = path determine_pipeline end
Public Instance Methods
Public: Get the file-system location of this input.
Returns a fully-qualified Pathname
.
# File lib/pith/input.rb, line 29 def file @file ||= project.input_dir + path end
Consider whether this input can be ignored.
Returns true if it can.
# 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
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.
# File lib/pith/input.rb, line 89 def meta ensure_loaded @meta end
# File lib/pith/input.rb, line 53 def output unless ignorable? @output ||= Output.for(self, @output_path) end end
Render this input using Tilt
# 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
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.
# 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
Determine whether this input is a template, requiring evaluation.
Returns true if it is.
# File lib/pith/input.rb, line 49 def template? !pipeline.empty? end
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"
# File lib/pith/input.rb, line 106 def title meta["title"] || default_title end
# File lib/pith/input.rb, line 128 def when_added log_lifecycle "+" end
# File lib/pith/input.rb, line 132 def when_modified log_lifecycle "~" unload if loaded? notify_observers end
# File lib/pith/input.rb, line 138 def when_removed log_lifecycle "X" output.delete if output notify_observers end
Private Instance Methods
# File lib/pith/input.rb, line 146 def default_title path.basename.to_s.sub(/\..*/, '').tr('_-', ' ').capitalize end
# File lib/pith/input.rb, line 150 def determine_pipeline @pipeline = [] remaining_path = path.to_s while remaining_path =~ /^(.+)(\..+)$/ extension = $2 break if extension == ".html" # ignore Tilt::PlainTemplate break if extension == ".rb" # ignore Opal::Processor if handler = Tilt[extension] remaining_path = $1 @pipeline << handler else break end end @output_path = Pathname(remaining_path) end
Make sure we've loaded the input file.
# File lib/pith/input.rb, line 173 def ensure_loaded load unless loaded? end
Read input file, extracting YAML meta-data header, and template content.
# File lib/pith/input.rb, line 179 def load @load_time = Time.now @meta = {} if template? logger.debug "loading #{path}" file.open do |io| read_meta(io) @template_start_line = io.lineno + 1 @template_text = io.read end end end
# File lib/pith/input.rb, line 167 def loaded? @load_time end
# File lib/pith/input.rb, line 223 def log_lifecycle(state) logger.info("#{state} #{path}") end
# File lib/pith/input.rb, line 219 def logger project.logger end
# File lib/pith/input.rb, line 194 def read_meta(io) header = io.gets if header =~ /^---/ while line = io.gets break if line =~ /^(---|\.\.\.)/ header << line end begin @meta = YAML.load(header) rescue YamlParseError logger.warn "#{file}:1: badly-formed YAML header" end else io.rewind end end
Note that the input file has changed, so we'll need to re-load it.
# File lib/pith/input.rb, line 213 def unload logger.debug "unloading #{path}" @load_time = nil @meta = nil end