Class: Yuzu::PreProcessors::InsertContentsPreProcessor

Inherits:
PreProcessor show all
Includes:
Helpers
Defined in:
lib/yuzu/preprocessors/insert_contents.rb

Instance Attribute Summary

Attributes inherited from PreProcessor

#name

Instance Method Summary collapse

Methods inherited from PreProcessor

#match, preprocessors, #process, #regex, registry

Constructor Details

#initializeInsertContentsPreProcessor

Returns a new instance of InsertContentsPreProcessor.



8
9
10
11
# File 'lib/yuzu/preprocessors/insert_contents.rb', line 8

def initialize
  @name = :insert_contents
  @directive = "INSERTCONTENTS"
end

Instance Method Details

#default(website_file = nil) ⇒ Object



13
14
15
# File 'lib/yuzu/preprocessors/insert_contents.rb', line 13

def default(website_file=nil)
  nil
end

#insert_file(path) ⇒ String

A raw file insert. Load the file from disk and insert the contents directly.

Parameters:

  • path (Path)

    refers to the file on disk.

Returns:

  • (String)

    The contents of the file.



43
44
45
46
47
48
49
50
51
52
# File 'lib/yuzu/preprocessors/insert_contents.rb', line 43

def insert_file(path)
  if path.exists?
    f = File.open(path.absolute, "r")
    contents = f.read
    f.close
    contents
  else
    ""
  end
end

#replacement(website_file, new_contents = "") ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/yuzu/preprocessors/insert_contents.rb', line 23

def replacement(website_file, new_contents="")
  # Get the next match.
  match_path = match(new_contents)
  raw_path = match_path.nil? ? default(website_file) : match_path
  file_path = Path.new(raw_path)

  siteroot = website_file.root
  file_to_insert = siteroot.find_file_by_path(file_path)

  if not file_to_insert.nil?
    "\n" + file_to_insert.prefiltered_contents
  else
    insert_file(file_path)
  end
end

#value(website_file) ⇒ Object



17
18
19
20
21
# File 'lib/yuzu/preprocessors/insert_contents.rb', line 17

def value(website_file)
  match_path = match(website_file.raw_contents)
  raw_path = match_path.nil? ? default(website_file) : match_path
  Path.new(match_path)
end