Class: Yuzu::PreProcessors::PreProcessor

Inherits:
Register
  • Object
show all
Defined in:
lib/yuzu/preprocessors/base.rb

Overview

Preprocessors differ from PostProcesors and Filters in that they are allowed to modify the raw_contents of a website_file. e.g. When contents are inserted from other files, they are done so in a manner to create the illusion that those contents were part of the original file.

Direct Known Subclasses

InsertContentsPreProcessor

Constant Summary collapse

@@preprocessors =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePreProcessor

Returns a new instance of PreProcessor.



19
20
21
22
# File 'lib/yuzu/preprocessors/base.rb', line 19

def initialize
  @name = :preprocessor
  @directive = "PREPROCESSOR"
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



17
18
19
# File 'lib/yuzu/preprocessors/base.rb', line 17

def name
  @name
end

Class Method Details

.preprocessorsObject



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

def self.preprocessors
  @@preprocessors
end

.registryObject



10
11
12
# File 'lib/yuzu/preprocessors/base.rb', line 10

def self.registry
  :preprocessors
end

Instance Method Details

#match(contents) ⇒ Object



28
29
30
31
# File 'lib/yuzu/preprocessors/base.rb', line 28

def match(contents)
  m = contents.to_s.match(regex)
  m[1]
end

#process(website_file, new_contents) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/yuzu/preprocessors/base.rb', line 48

def process(website_file, new_contents)
  replaced = false

  m = new_contents.match(regex)

  while not m.nil?
    repl = replacement(website_file, new_contents)

    # Remove the next match.
    new_contents = new_contents.sub(regex, repl.to_s)
    replaced = true

    # Find any others...
    m = new_contents.match(regex)
  end

  if replaced
    website_file.instance_variable_set(:@raw_contents, new_contents)
  end
end

#regexObject



33
34
35
# File 'lib/yuzu/preprocessors/base.rb', line 33

def regex
  Regexp.new('^\s*' + @directive.to_s + '\(([\w\s\.\,\'\"\/\-:]*?)\)')
end

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

Returns the contents to replace the directive with as written. This will modify the raw contents of the website_file.

Parameters:

  • website_file (WebsiteFile)

    The page in which the directive appears

  • new_contents (String, nil) (defaults to: "")

    The contents of the given WebsiteFile as they are being transformed by processing.

Returns:

  • (String)

    What to replace the directive with.



44
45
46
# File 'lib/yuzu/preprocessors/base.rb', line 44

def replacement(website_file, new_contents="")
  website_file.raw_contents
end

#value(website_file) ⇒ Object



24
25
26
# File 'lib/yuzu/preprocessors/base.rb', line 24

def value(website_file)
  match(website_file.raw_contents)
end