Advertisement
Guest User

Untitled

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