Class: Processor::Template

Inherits:
Object
  • Object
show all
Includes:
Processor
Defined in:
lib/depengine/processor/template.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Processor

check_filelist, chmod, copy, #load_tags, mkdir, mklink, remove, tgz_file, untgz_file, unzip_file

Instance Attribute Details

#basepathObject

Returns the value of attribute basepath.



4
5
6
# File 'lib/depengine/processor/template.rb', line 4

def basepath
  @basepath
end

#cdb=(value) ⇒ Object (writeonly)

Sets the attribute cdb

Parameters:

  • value

    the value to set the attribute cdb to.



5
6
7
# File 'lib/depengine/processor/template.rb', line 5

def cdb=(value)
  @cdb = value
end

#contentObject

Returns the value of attribute content.



4
5
6
# File 'lib/depengine/processor/template.rb', line 4

def content
  @content
end

#outdirObject

Returns the value of attribute outdir.



4
5
6
# File 'lib/depengine/processor/template.rb', line 4

def outdir
  @outdir
end

#sourceObject

Returns the value of attribute source.



4
5
6
# File 'lib/depengine/processor/template.rb', line 4

def source
  @source
end

Class Method Details

.parse(template, content) ⇒ Object

This function returns the output of a parsed template, ‘template’ can be a filename or a String.



84
85
86
87
88
89
90
# File 'lib/depengine/processor/template.rb', line 84

def self.parse(template, content)
  $log.writer.debug "Parse Template #{template}"
  t = Template.new
  t.source = template
  t.content = content
  t.parse(template)
end

Instance Method Details

#all_from_directoryObject



39
40
41
42
43
44
# File 'lib/depengine/processor/template.rb', line 39

def all_from_directory
  find_templates(@source).each do |t|
    @source = t
    from_file
  end
end

#create_sub_dirs!(target) ⇒ Object



74
75
76
77
78
79
# File 'lib/depengine/processor/template.rb', line 74

def create_sub_dirs!(target)
  if not File.directory?(target)
    FileUtils.mkdir_p(target)
    $log.writer.debug "Create directory #{target}"
  end
end

#excluded?(target) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
66
67
68
69
70
71
72
# File 'lib/depengine/processor/template.rb', line 63

def excluded?(target)
  if @excludes
    @excludes.each do |exclude|
      if target.include? exclude
        return true
      end
    end
  end
  return false
end

#find_templates(path_to_find_in) ⇒ Object



46
47
48
49
50
51
52
53
54
55
# File 'lib/depengine/processor/template.rb', line 46

def find_templates(path_to_find_in)
  templates = []
  Find.find(path_to_find_in) do |path|
    # next if we found ourself
    next if path == path_to_find_in
    next unless File.file?(path) and path.end_with?(".tpl")
    templates << path
  end
  templates
end

#from_fileObject



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/depengine/processor/template.rb', line 26

def from_file
  if excluded? @source
    $log.writer.debug "Skip template #{@source}"
    return
  end
  target_dir = target_path_of(@source)
  create_sub_dirs!(target_dir)
  target_file = File.join(target_dir, File.basename(@source).gsub(".tpl",""))
  File.open(target_file, "w") do |f|
    f.write(parse(@source))
  end
end

#parse(template) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/depengine/processor/template.rb', line 92

def parse(template)
  context = load_tags(@content, @cdb)
  begin
    parser           = Radius::Parser.new(context, :tag_prefix => 't')
    template_content = ""
    result           = ""
    if File.file? template
      $log.writer.debug "template #{template} seems to be a file, reading it..."
      File.open(template, "rb") do |f|
        c = ""
        f.ungetc c unless (c = f.getc)=="\uFEFF" # remove BOM, if present
        while line = f.gets
          template_content << line
        end
      end
    else
      $log.writer.debug "tempalte is not a file, using it as source"
      template_content = template
    end

    result          = parser.parse(template_content)
    nested_result_1 = ""
    nested_result_2 = result
    nested_loop     = 0
    while( nested_result_1 != nested_result_2 )
      if nested_loop >= 100
        $log.writer.error "Template stack overflow, too many nested tags"
        exit 1
      end
      nested_result_1 = parser.parse(nested_result_2)
      nested_result_2 = parser.parse(nested_result_1)

      nested_loop = nested_loop + 1
   end
   result = nested_result_2
  rescue Exception => e
    $log.writer.error "Error in template processing: #{template}"
    $log.writer.error e.message
    exit 1
  end

  result
end

#parse_template(template_source, content, outdir, options = {}) ⇒ Object

Render a template and write the output to a file or directory structure under ‘basepath’



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/depengine/processor/template.rb', line 9

def parse_template(template_source, content, outdir, options={})
   Helper.validates_presence_of basepath, "Basepath not set for template engine"
   # expand basepath
   @basepath = File.expand_path basepath
   @outdir = outdir
   @source = File.join(@basepath, template_source)
   @content = content
   @excludes = options[:excludes]

   if File.file?(File.join(basepath, template_source))
     from_file
   else
     all_from_directory
   end
   return self
end

#target_path_of(path) ⇒ Object



57
58
59
60
61
# File 'lib/depengine/processor/template.rb', line 57

def target_path_of(path)
  path = File.dirname(path) if File.file?(path)
  sub_dir     = File.join(path.split(/\//) - @basepath.split(/\//))
  File.join(@outdir, sub_dir)
end