Sha256: 4448f502d54027a6c369de122c119e3fe440c3dea5bbc6af5094997753dcbb41

Contents?: true

Size: 1.96 KB

Versions: 2

Compression:

Stored size: 1.96 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)
    resolve_include(text, compiler.controller)    
  end

  def self.resolve_include(text, 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 = [ Nitro::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, ' ')
      
      # Recursively resolve to handle sub-includes.
      
      resolve_include(itext, controller)
    end
  end
end
  
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
nitro-0.41.0 lib/nitro/compiler/include.rb
nitro-0.40.0 lib/nitro/compiler/include.rb