module Processor class Template include Processor attr_accessor :basepath attr_writer :cdb # Render a template and write the output to a file or directory # structure under 'basepath' def parse_template(template_source, content, outdir, options={}) Helper.validates_presence_of basepath, "Basepath not set for template engine" # expand basepath self.basepath = File.expand_path basepath if File.file?( File.join( basepath, template_source ) ) source_root_path = File.join( basepath, File.dirname(template_source) ) single_file = File.join( basepath, template_source ) else # source is a directory source_root_path = File.join( basepath, template_source) single_file = nil end target_root_path = File.join( basepath, outdir ) target_path = target_root_path Find.find( single_file || source_root_path ) do |path| # next if we found ourself next if path == source_root_path sub_dir = File.join( File.dirname( path ).split(/\//) - \ source_root_path.split(/\//) ) target_path = File.join( target_root_path, sub_dir ) target = File.join( target_path, File.basename( path ) ) if not options[:excludes].nil? options[:excludes].each do |exclude| if target.include? exclude $log.writer.debug "Skip template #{target}" Find.prune next end end end if File.directory? path if not File.directory? target FileUtils.mkdir_p( target ) $log.writer.debug "Create directory #{target}" end else target_file = File.join( target_path, \ File.basename(target, ".tpl")) begin if not File.binary? path and not File.zero? path writer = File.new(target_file, 'w') writer.write(parse(path, content)) writer.close if not writer.nil? writer = nil else FileUtils.cp(path, File.expand_path(target_file)) end rescue Exception => e $log.writer.error "Can not write output from template to #{target_file}" $log.writer.error e.message exit 1 end end end end # This function returns the output of a parsed template, 'template' can be # a filename or a String. def self.parse(template, content) $log.writer.debug "Parse Template #{template}" Template.new.parse(template, content) end def parse(template, content) context = load_tags(content, @cdb) begin parser = Radius::Parser.new(context, :tag_prefix => 't') template_content = "" result = "" if File.file? template read_mode = "" if RUBY_VERSION < '1.9' read_mode = 'r' else read_mode = 'rb' end File.open(template, read_mode) 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 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 end end