Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'builder'
- puts "start"
- lambda { |root, result_path, nesting|
- @xml = Builder::XmlMarkup.new(:target => result, :indent => 2)
- @xml.instruct!
- @template_tree = {}
- @current_nesting
- def parse_directory(path)
- Dir.foreach path do |d|
- if File.file?(f = path + d)
- if d =~ /.\.asp$/
- @template_tree[f] ||= []
- file = File.open(f, "r")
- while line = file.gets
- if line =~ /<!--.*#include.*-->/
- file_name = Regexp.last_match(0)[/".*"/].slice(1..-2)
- (@template_tree[f] << path + file_name.gsub("/","")) unless file_name.empty?
- end
- end
- file.close
- end
- else
- parse_directory "#{path}#{d}/" if (d != '.' && d != '..')
- end
- end
- end
- handle_templates = lambda do|outer_template|
- return if (@current_nesting += 1) > nesting
- @xml.template(path: outer_template) do
- @template_tree[outer_template].each do |inner_template|
- @xml.template(path: inner_template) do
- @template_tree[inner_template].each do |more_inner_template|
- handle_templates.call more_inner_template
- end if @template_tree[inner_template] && @template_tree[inner_template].length > 0
- end
- end if @template_tree[outer_template] && @template_tree[outer_template].length > 0
- end
- end
- generate_xml = lambda do @xml.templates do
- result = File.open(result_path, 'w')
- @template_tree.each do |template,children|
- @xml.template(path: template) do
- children.each do |inner_template|
- @current_nesting = 0
- handle_templates.call inner_template
- end if children && children.length > 0
- end
- end
- end
- result.close
- end
- puts "parse files..."
- parse_directory(root)
- puts "generating xml..."
- generate_xml.call
- }.call 'F:/rb_incom-realty.ru/', 'F:/tree.xml', 4
- puts "finish"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement