Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.79 KB | None | 0 0
  1. require 'builder'
  2. puts "start"
  3.  
  4. lambda { |root, result_path, nesting|
  5.     @xml = Builder::XmlMarkup.new(:target => result, :indent => 2)
  6.     @xml.instruct!
  7.     @template_tree = {}
  8.     @current_nesting
  9.  
  10.     def parse_directory(path)
  11.         Dir.foreach path do |d|
  12.             if File.file?(f = path + d)
  13.                 if d =~ /.\.asp$/
  14.                     @template_tree[f] ||= []
  15.                     file = File.open(f, "r")       
  16.                     while line = file.gets             
  17.                         if line =~ /<!--.*#include.*-->/
  18.                             file_name = Regexp.last_match(0)[/".*"/].slice(1..-2)
  19.                             (@template_tree[f] << path + file_name.gsub("/","")) unless file_name.empty?
  20.                         end
  21.                     end
  22.                     file.close
  23.                 end
  24.             else
  25.                 parse_directory "#{path}#{d}/" if (d != '.' && d != '..')
  26.             end
  27.         end
  28.     end
  29.    
  30.     handle_templates = lambda do|outer_template|
  31.         return if (@current_nesting += 1) > nesting
  32.         @xml.template(path: outer_template) do
  33.          @template_tree[outer_template].each do |inner_template|
  34.                 @xml.template(path: inner_template) do
  35.                     @template_tree[inner_template].each do |more_inner_template|
  36.                         handle_templates.call more_inner_template
  37.                     end if @template_tree[inner_template] && @template_tree[inner_template].length > 0
  38.                 end
  39.          end if @template_tree[outer_template] && @template_tree[outer_template].length > 0
  40.         end
  41.     end
  42.  
  43.     generate_xml = lambda do @xml.templates do
  44.         result = File.open(result_path, 'w')
  45.             @template_tree.each do |template,children|
  46.                 @xml.template(path: template) do
  47.                     children.each do |inner_template|
  48.                         @current_nesting = 0
  49.                         handle_templates.call inner_template
  50.                     end if children && children.length > 0
  51.                 end
  52.             end
  53.         end
  54.         result.close
  55.     end
  56.    
  57.     puts "parse files..."
  58.     parse_directory(root)
  59.     puts "generating xml..."
  60.     generate_xml.call
  61.    
  62. }.call 'F:/rb_incom-realty.ru/', 'F:/tree.xml', 4
  63.  
  64. puts "finish"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement