Class: Raw::StaticIncludeFilter

Inherits:
Object
  • Object
show all
Defined in:
lib/raw/compiler/filter/static_include.rb

Overview

Performs static includes. Typically you should include this compiler as the first stage of the compile pipeline.

This compiler is extremely helpful, typically you would want to use static includes in many many cases.

Instance Method Summary collapse

Instance Method Details

#apply(text) ⇒ Object

Statically include sub-template files. The target file is included at compile time. If the given path is relative, the template_root stack of the controller is traversed. If an absolute path is provided, templates are searched only in Template.root

gmosx: must be xformed before the <?r pi.

Example

<?include href=“root/myfile.sx” ?>



22
23
24
# File 'lib/raw/compiler/filter/static_include.rb', line 22

def apply(text)
  resolve_include(text)    
end

#resolve_include(text) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/raw/compiler/filter/static_include.rb', line 56

def resolve_include(text)
  return text.gsub(/<\?include href=["|'](.*?)["|'](.*)\?>/) do |match|
    filename = resolve_include_filename($1)

    c = Context.current.application.compiler
    c.templates = c.templates.push(filename).uniq       
    itext = File.read(filename)
    
    itext.gsub!(/<\?xml.*\?>/, '')
    
    # Recursively resolve to handle sub-includes.
    
    resolve_include(itext)
  end
end

#resolve_include_filename(href) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/raw/compiler/filter/static_include.rb', line 26

def resolve_include_filename(href)
  found = false

  if href[0] == ?/
    # Absolute path, search only in the application template
    # root.

    href = href[1, 999999] # hack!!
    template_dir_stack = [ Template.root_dir ]
  else
    template_dir_stack = Controller.current.ann(:self, :template_dir_stack)
  end
    
  for dir in template_dir_stack
    if File.exist?(filename = "#{dir}/#{href}")
      found = true
      break
    end

    if File.exist?(filename = "#{dir}/#{href}.inc")
      found = true
      break
    end
  end
  
  raise "Cannot statically include '#{href}'" unless found

  return filename
end