Advertisement
Guest User

Untitled

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