Sha256: 1ac92498a73c3a95c0f2202778db272b98d5aec4bdb1f784e613f6c48febcc5f

Contents?: true

Size: 1.85 KB

Versions: 3

Compression:

Stored size: 1.85 KB

Contents

module Nitro
  
# 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.
 
class StaticInclude
  # 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" ?>
  
  def self.transform(text, compiler)
    controller = compiler.controller
    
    return text.gsub(/<\?include href=["|'](.*?)["|'](.*)\?>/) do |match|
      found = false

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

        href = $1[1, 999999] # hack!!
        template_stack = [ Glue::Template.root ]
      else
        # Relative path, traverse the template_root stack.
        
        href = $1
        template_stack = controller.instance_variable_get(:@template_root)
      end
        
      for template_root in template_stack
        if File.exist?(filename = "#{template_root}/#{href}")
          found = true
          break
        end

        if File.exist?(filename = "#{template_root}/#{href}.xinc")
          found = true
          break
        end

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

      itext = File.read(filename)
      itext.gsub!(/<\?xml.*\?>/, '')
      itext.gsub!(/<\/?root(.*?)>/m, ' ');
      itext
    end
  end
  
end
  
end

# * George Moschovitis <gm@navel.gr>

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
nitro-0.29.0 lib/nitro/compiler/include.rb
nitro-0.30.0 lib/nitro/compiler/include.rb
nitro-0.31.0 lib/nitro/compiler/include.rb