Class: RegExRX2MD::RegexRX
- Inherits:
-
Object
- Object
- RegExRX2MD::RegexRX
- Defined in:
- lib/regexrx2md/regexrx.rb
Overview
Reads RegExRX document and outputs template
Instance Attribute Summary collapse
-
#flags ⇒ Object
readonly
Returns the value of attribute flags.
-
#replace ⇒ Object
readonly
Returns the value of attribute replace.
-
#search ⇒ Object
readonly
Returns the value of attribute search.
-
#source ⇒ Object
readonly
Returns the value of attribute source.
-
#title ⇒ Object
readonly
Returns the value of attribute title.
Instance Method Summary collapse
- #describe_flags ⇒ Object
- #flags_in_search ⇒ Object
- #grab_opt(name) ⇒ Object
- #grab_pref(name) ⇒ Object
- #grab_string(name) ⇒ Object
-
#initialize(file) ⇒ RegexRX
constructor
A new instance of RegexRX.
- #rx_flags ⇒ Object
- #rx_replace ⇒ Object
- #rx_search ⇒ Object
- #rx_source ⇒ Object
- #to_markdown(template) ⇒ Object
Constructor Details
#initialize(file) ⇒ RegexRX
Returns a new instance of RegexRX.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/regexrx2md/regexrx.rb', line 8 def initialize(file) doc = File.open(file) { |f| Nokogiri::XML(f) } @content = doc.xpath('RegExRX_Document') @title = doc.xpath('//Window').first['Title'].strip @title = File.basename(file, '.regexrx') if @title =~ /^Untitled RegExRX/ @search = rx_search @flags = rx_flags @flags_desc = describe_flags @replace = rx_replace @source = rx_source @search.sub!(/^(\(\?[-misUXxJ]+\))?/, "(?#{@flags})") end |
Instance Attribute Details
#flags ⇒ Object (readonly)
Returns the value of attribute flags.
6 7 8 |
# File 'lib/regexrx2md/regexrx.rb', line 6 def flags @flags end |
#replace ⇒ Object (readonly)
Returns the value of attribute replace.
6 7 8 |
# File 'lib/regexrx2md/regexrx.rb', line 6 def replace @replace end |
#search ⇒ Object (readonly)
Returns the value of attribute search.
6 7 8 |
# File 'lib/regexrx2md/regexrx.rb', line 6 def search @search end |
#source ⇒ Object (readonly)
Returns the value of attribute source.
6 7 8 |
# File 'lib/regexrx2md/regexrx.rb', line 6 def source @source end |
#title ⇒ Object (readonly)
Returns the value of attribute title.
6 7 8 |
# File 'lib/regexrx2md/regexrx.rb', line 6 def title @title end |
Instance Method Details
#describe_flags ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/regexrx2md/regexrx.rb', line 89 def describe_flags @flags ||= rx_flags positives, negatives = @flags.split(/-/) descs = { m: ['Treat target as one line', 'Do not treat target as one line'], i: ['Case insensitive', 'Case sensitive'], s: ['Dot matches newline', 'Dot does not match newline'], U: ['Not greedy', 'Greedy'], x: ['Free spacing', 'No free spacing'], X: ['Character following \ must have special meaning', 'Character following \ treated as literal'], J: ['Allow duplicate group names', 'Disallow duplicate group names'] } ps = positives.nil? ? [] : positives.split('').map(&:to_sym) ns = negatives.nil? ? [] : negatives.split('').map(&:to_sym) desc = [] %i[m i s U J X x].each do |flag| desc << if ps.include?(flag) "#{flag.to_s}: #{descs[flag][0]}" elsif ns.include?(flag) "-#{flag.to_s}: #{descs[flag][1]}" end end desc.delete_if(&:nil?).sort.map { |f| "* #{f}" }.join("\n") end |
#flags_in_search ⇒ Object
80 81 82 83 84 85 86 87 |
# File 'lib/regexrx2md/regexrx.rb', line 80 def flags_in_search @search ||= rx_search positives = [] negatives = [] res = @search.match(/\(\?([-msiUxXJ]+)\)/) res ? res[1].split(/-/).map(&:split) : nil end |
#grab_opt(name) ⇒ Object
131 132 133 |
# File 'lib/regexrx2md/regexrx.rb', line 131 def grab_opt(name) @content.xpath("//OptionMenu[@text='#{name}']").first['checked'] == 'true' end |
#grab_pref(name) ⇒ Object
127 128 129 |
# File 'lib/regexrx2md/regexrx.rb', line 127 def grab_pref(name) @content.xpath("//Preference[@name='#{name}']").first['value'] == 'true' end |
#grab_string(name) ⇒ Object
119 120 121 122 123 124 125 |
# File 'lib/regexrx2md/regexrx.rb', line 119 def grab_string(name) out = @content.xpath("//Control[@name=\"#{name}\"]").first .content .strip .force_encoding('utf-8') out.unpack end |
#rx_flags ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/regexrx2md/regexrx.rb', line 42 def rx_flags positives = [] negatives = [] if grab_opt('Treat Target As One Line') positives << 'm' else negatives << 'm' end if grab_opt('Dot Matches Newline') positives << 's' else negatives << 's' end if grab_opt('Case Sensitive') negatives << 'i' else positives << 'i' end negatives << 'U' if grab_opt('Greedy') search_flags = flags_in_search unless search_flags.nil? ps = search_flags[0] || nil ns = search_flags[1] || nil positives.concat(ps.delete_if { |pos| positives.include?(pos) }) unless ps.nil? negatives.concat(ns.delete_if { |neg| negatives.include?(neg) }) unless ns.nil? end flags = positives.sort.join('') flags += "-#{negatives.sort.join('')}" unless negatives.empty? flags.empty? ? false : flags end |
#rx_replace ⇒ Object
33 34 35 |
# File 'lib/regexrx2md/regexrx.rb', line 33 def rx_replace grab_pref('Do Replace') ? grab_string('fldReplace') : false end |
#rx_search ⇒ Object
29 30 31 |
# File 'lib/regexrx2md/regexrx.rb', line 29 def rx_search grab_string('fldSearch') end |
#rx_source ⇒ Object
37 38 39 40 |
# File 'lib/regexrx2md/regexrx.rb', line 37 def rx_source source = grab_string('fldSource') source.empty? ? false : source end |
#to_markdown(template) ⇒ Object
23 24 25 26 27 |
# File 'lib/regexrx2md/regexrx.rb', line 23 def to_markdown(template) out = ERB.new(template).result(binding) out.force_encoding('utf-8') end |