Module: SnippetsConverter::ClassMethods
- Includes:
- REXML
- Defined in:
- lib/snippets_converter.rb
Instance Method Summary collapse
- #convert(file) ⇒ Object
- #editor_bottom ⇒ Object
-
#editor_conversion(trigger, description, code) ⇒ Object
Dummy methods, the current Editor module must override them.
- #editor_header(language = nil) ⇒ Object
- #editor_target_file(language = nil) ⇒ Object
- #parse_tm_snippet(file) ⇒ Object
-
#transform_snippet(code) ⇒ Object
Nested tab stops and place holders remover, e.g ‘$${3:cool $2 }’ become ‘ $3:cool $2 ’.
Instance Method Details
#convert(file) ⇒ Object
83 84 85 86 87 88 89 |
# File 'lib/snippets_converter.rb', line 83 def convert(file) tm_snippet = parse_tm_snippet(file) if !tm_snippet.blank? transform_snippet(tm_snippet[:code]) return editor_conversion(tm_snippet[:trigger], tm_snippet[:description], tm_snippet[:code]) end end |
#editor_bottom ⇒ Object
178 179 180 |
# File 'lib/snippets_converter.rb', line 178 def editor_bottom raise Exception.new("The current Editor module must implement this method") end |
#editor_conversion(trigger, description, code) ⇒ Object
Dummy methods, the current Editor module must override them
170 171 172 |
# File 'lib/snippets_converter.rb', line 170 def editor_conversion(trigger, description, code) raise Exception.new("The current Editor module must implement this method") end |
#editor_header(language = nil) ⇒ Object
174 175 176 |
# File 'lib/snippets_converter.rb', line 174 def editor_header(language = nil) raise Exception.new("The current Editor module must implement this method") end |
#editor_target_file(language = nil) ⇒ Object
182 183 184 |
# File 'lib/snippets_converter.rb', line 182 def editor_target_file(language = nil) raise Exception.new("The current Editor module must implement this method") end |
#parse_tm_snippet(file) ⇒ Object
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 79 80 81 |
# File 'lib/snippets_converter.rb', line 53 def parse_tm_snippet(file) doc = Document.new( file = File.read(file) ) i = 0 j = 0 arrKey = [] arrString = [] # Transform the key and string node into arrays XPath.each( doc, '//key|//string') do |key| if key.name == "key" arrKey[i] = key.text i+=1 else arrString[j] = key.text j+=1 end end tm_snippet = {} # The array, that contains the snippet info, must have all the keys below if (['tabTrigger', 'content', 'name', 'scope'] - arrKey.uniq ).empty? tm_snippet[:trigger] = arrString[arrKey.index('tabTrigger')] tm_snippet[:code] = arrString[arrKey.index('content')] tm_snippet[:description] = arrString[arrKey.index('name')] tm_snippet[:language] = arrString[arrKey.index('scope')] end tm_snippet end |
#transform_snippet(code) ⇒ Object
Nested tab stops and place holders remover, e.g ‘$${3:cool $2 }’ become ‘ $3:cool $2 ’
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 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/snippets_converter.rb', line 92 def transform_snippet(code) # Transform TextMate snippets syntax into snippets converter internal codes, e.g. '$' become ':dollar:' # Nested tab stops, e.g '${1: $2 }' nested_tab_stop = /((\$\{[0-9]{1,5}:[^${}]*)\$([0-9]{1,5})([^${}]*\}))+/m code.gsub!(nested_tab_stop, '\2:nested_tab_stop\3:\4') code.gsub!(/\$([0-9]{1,5})/, ':tab_stop\1:') # Tab Stop, e.g '$0' # Unescape "$", "`" and "}" if there are in plain text or in the value of a variable # More info at http://manual.macromates.com/en/snippets#plain_text and http://manual.macromates.com/en/snippets#variables code.gsub!(/\\`/, ':escape_backtick:') code.gsub!(/\\\$/, ':escape_dollar:') i = 0 loop do i += 1 break if !code.gsub!(/\{([^{}]*|[^{}]*\{[^{}]*\}[^{}]*)\\\}/, ':escape_bracket:\1:escape_close_bracket:') || i > 20 end code.gsub!(/\$([^{][^0-9]+[^:])/, ':dollar:\1') # Dollar, e.g. '$titi' # Place holders, e.g. '${1: cool }' place_holders = /\$\{((?>[^${}]+)|(\1))+\}/m place_holders_matches = code.scan(place_holders) place_holders_matches.flatten.each do |place_holder| if place_holder idx = place_holder.gsub(/([0-9]{1,5}):.+/,'\1') code.gsub!(/\$\{#{place_holder}\}/m, ":place_holders#{idx}:") end end #TODO Check if the regular expression in variables are correctly converted/supported # More info at http://manual.macromates.com/en/regular_expressions.html # Removed Nested place holders, e.g. '${1: ${3:cool} }' become ' ${3:cool} ' nested_place_holders = /(\$\{[0-9]{1,5}:(([^${}]*(\$\{[0-9]{1,5}:|\{)[^${}]+\}[^${}]*)|[^${}]+)\})/m i = 0 loop do i += 1 break if !code.gsub!(nested_place_holders, '\2') || i > 20 end # Transform snippets converter internal codes into characters, e.g. ':dollar:' become '$' place_holders_matches.flatten.each do |place_holder| if place_holder idx = place_holder.gsub(/([0-9]{1,5}):.+/,'\1') code.gsub!(/:place_holders#{idx}:/m, "\$\{#{place_holder}\}") end end # Nested tab stops code.gsub!(/:nested_tab_stop([0-9]{1,5}):/, '$\1') nested_tab_stop = /(\$\{[0-9]{1,5}:([^${}]*\$[0-9]{1,5}[^${}]*)\})+/m i = 0 loop do i += 1 break if !code.gsub!(nested_tab_stop, '\2') || i > 20 end code.gsub!(/:tab_stop([0-9]{1,5}):/, '$\1') code.gsub!(/:escape_backtick:/, '`') code.gsub!(/:escape_dollar:/, '$') i = 0 loop do i += 1 break if !code.gsub!(/:escape_bracket:(.*):escape_close_bracket:/, '{\1}') || i > 20 end code.gsub!(/:dollar:/, '$') end |