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