Guest User

Untitled

a guest
Jun 22nd, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.64 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2. require 'fileutils'
  3. require 'nokogiri'
  4. require 'optparse'
  5. require 'erb'
  6.  
  7. def class_exists?(class_name)
  8. klass = Module.const_get(class_name)
  9. return klass.is_a?(Class)
  10. rescue NameError
  11. return false
  12. end
  13.  
  14. if class_exists? 'Encoding'
  15. Encoding.default_external = Encoding::UTF_8 if Encoding.respond_to?('default_external')
  16. Encoding.default_internal = Encoding::UTF_8 if Encoding.respond_to?('default_internal')
  17. end
  18.  
  19. class String
  20. def unpack
  21. [self].pack('H*')
  22. end
  23.  
  24. def indent
  25. out = ''
  26. self.split("\n").each {|line|
  27. out += " #{line}\n"
  28. }
  29. out
  30. end
  31.  
  32. def to_js
  33. self.gsub(/(?mi)(?<!\\)\//,'\/')
  34. end
  35. end
  36.  
  37. class RegexRX
  38. attr_reader :title, :search, :flags, :replace, :source
  39.  
  40. def initialize(file)
  41. doc = File.open(file) { |f| Nokogiri::XML(f) }
  42. @content = doc.xpath('RegExRX_Document')
  43.  
  44. @title = doc.xpath("//Window").first["Title"].strip
  45.  
  46. @search = grabString('fldSearch')
  47.  
  48. @flags = ''
  49.  
  50. @flags += 's' if grabOpt('Dot Matches Newline')
  51. @flags += 'i' unless grabOpt('Case Sensitive')
  52. @flags += 'm' if grabOpt('Treat Target As One Line')
  53.  
  54. if @flags.length == 0
  55. @flags = false
  56. end
  57.  
  58. # @regex = '/' + @search + '/' + @flags
  59.  
  60. if grabPref('Do Replace')
  61. @replace = grabString('fldReplace')
  62. else
  63. @replace = false
  64. end
  65.  
  66. @source = false
  67. source = grabString('fldSource')
  68. if source.length > 0
  69. @source = source
  70. end
  71. end
  72.  
  73. def to_markdown(template)
  74. out = ERB.new(template).result(binding)
  75.  
  76. out.force_encoding('utf-8')
  77. end
  78.  
  79.  
  80. def grabString(name)
  81. out = @content.xpath("//Control[@name=\"#{name}\"]").first
  82. .content
  83. .strip
  84. .force_encoding('utf-8')
  85. out.unpack
  86. end
  87.  
  88. def grabPref(name)
  89. @content.xpath("//Preference[@name=\"#{name}\"]").first["value"] == "true"
  90. end
  91.  
  92. def grabOpt(name)
  93. @content.xpath("//OptionMenu[@text=\"#{name}\"]").first["checked"] == "true"
  94. end
  95. end
  96.  
  97. options = {}
  98. optparse = OptionParser.new do|opts|
  99. opts.banner = "Usage: #{__FILE__} [OPTIONS]"
  100. options[:prefix] = ''
  101. options[:output] = 'markdown output'
  102. opts.on( '-o', '--output-dir=DIRECTORY', 'Output folder, defaults to "markdown output"') do |output|
  103. options[:output] = output
  104. end
  105. opts.on( '-p','--prefix=PREFIX', 'Prefix added before output filenames' ) do |prefix|
  106. options[:prefix] = prefix.strip + ' '
  107. end
  108. options[:template] = nil
  109. opts.on( '-t','--template=TEMPLATE', 'Use alternate ERB template' ) do |template|
  110. options[:template] = template
  111. end
  112. opts.on( '-h', '--help', 'Display this screen' ) do
  113. puts opts
  114. exit
  115. end
  116. end
  117. optparse.parse!
  118.  
  119. default_template = <<-ENDOFTEMPLATE
  120. # <%= @title %>
  121. <% if @flags %>
  122.  
  123. **Flags:** _<%= @flags %>_
  124. <% end %>
  125.  
  126. **Search:**
  127.  
  128. <%= @search.indent %>
  129. <% if @replace %>
  130.  
  131. **Replace:**
  132.  
  133. <%= @replace.indent %>
  134. <% end %>
  135. <% if @source %>
  136. ---
  137.  
  138. ## Test string:
  139.  
  140. ```text
  141. <%= @source %>
  142. ```
  143. <% end %>
  144.  
  145. ENDOFTEMPLATE
  146.  
  147. # If ERB template is specified, use that instead of the default
  148. if options[:template]
  149. if File.exists?(File.expand_path(options[:template])) && File.basename(options[:template]) =~ /\.erb$/
  150. template = IO.read(File.expand_path(options[:template]))
  151. else
  152. $stderr.puts %Q{Specified template "#{options[:template]}" is not a valid template}
  153. Process.exit 1
  154. end
  155. else
  156. template = default_template
  157. end
  158.  
  159. FileUtils.mkdir_p(options[:output]) unless File.exists?(options[:output])
  160.  
  161. Dir.glob('*.regexrx').each {|file|
  162. # $stderr.puts "Reading #{file}"
  163. rx = RegexRX.new(file)
  164. filename = File.join(options[:output], options[:prefix] + rx.title + '.md')
  165. File.open(filename, 'w') {|f|
  166. f.print(rx.to_markdown(template))
  167. }
  168. $stderr.puts "Regex written to #{filename}"
  169. }
Add Comment
Please, Sign In to add comment