Class: Yuzu::Filters::Filter

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

Overview

Filters are the primary means to derive information from a given source file and place new contents into it before it is placed in a layout.

There are 3 stages of filtering that happen to each file.

  1. prefilter – replacing LINKROOT and CURRENTPATH, so the next phase has the proper paths

  2. filter – transforming contents

  3. postfilter – replacing LINKROOT and CURRENTPATH again

Constant Summary collapse

@@filters =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFilter

Returns a new instance of Filter.



25
26
27
28
# File 'lib/yuzu/filters/base.rb', line 25

def initialize
  @name = :directive
  @directive = "DIRECTIVE"
end

Instance Attribute Details

#directiveObject (readonly)

Returns the value of attribute directive.



23
24
25
# File 'lib/yuzu/filters/base.rb', line 23

def directive
  @directive
end

#nameObject (readonly)

Returns the value of attribute name.



23
24
25
# File 'lib/yuzu/filters/base.rb', line 23

def name
  @name
end

Class Method Details

.filtersObject



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

def self.filters
  @@filters
end

.registryObject



16
17
18
# File 'lib/yuzu/filters/base.rb', line 16

def self.registry
  :filters
end

Instance Method Details

#default(website_file = nil) ⇒ Object



34
35
36
# File 'lib/yuzu/filters/base.rb', line 34

def default(website_file=nil)
  nil
end

#filter_typeObject



30
31
32
# File 'lib/yuzu/filters/base.rb', line 30

def filter_type
  [:filter]
end

#get_match(contents) ⇒ Object



50
51
52
53
# File 'lib/yuzu/filters/base.rb', line 50

def get_match(contents)
  m = contents.match(regex)
  m.nil? ? nil : m[1]
end

#get_value(website_file) ⇒ Object



42
43
44
# File 'lib/yuzu/filters/base.rb', line 42

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

#match(contents) ⇒ Object



46
47
48
# File 'lib/yuzu/filters/base.rb', line 46

def match(contents)
  get_match(contents)
end

#process(website_file, processing_contents) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/yuzu/filters/base.rb', line 69

def process(website_file, processing_contents)
  m = processing_contents.match(regex)

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

    # Remove the next match.
    processing_contents = processing_contents.sub(regex, repl.to_s)

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

  processing_contents
end

#regexObject



65
66
67
# File 'lib/yuzu/filters/base.rb', line 65

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

#replacement(website_file, processing_contents = nil) ⇒ String

Returns the contents to replace the directive with as written.

Parameters:

  • website_file (WebsiteFile)

    The page in which the directive appears

  • processing_contents (String, nil) (defaults to: nil)

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

Returns:

  • (String)

    What to replace the directive with.



61
62
63
# File 'lib/yuzu/filters/base.rb', line 61

def replacement(website_file, processing_contents=nil)
  ""
end

#value(website_file) ⇒ Object



38
39
40
# File 'lib/yuzu/filters/base.rb', line 38

def value(website_file)
  get_value(website_file) || default(website_file)
end