Advertisement
Guest User

Untitled

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