Class: ExpandSync::TextExpander
- Inherits:
-
Object
- Object
- ExpandSync::TextExpander
- Defined in:
- lib/expandsync/textexpander.rb
Overview
TextExpander Class
Constant Summary collapse
- OUTPUT_FILENAME =
The output filename
'Settings.textexpander'
- OUTPUT_PATH =
The output filepath
File.join(ENV['HOME'], 'Dropbox', 'TextExpander')
Instance Attribute Summary collapse
-
#base_xml ⇒ Nokogiri::XML::Document
Stores the initial XML as pulled from Settings.textexpander.
-
#output_file ⇒ String
Stores the full output path for the produced XML.
-
#snippet_xml ⇒ Nokogiri::XML::Document
Stores the final XML to save.
-
#snippets ⇒ Array
Stores an array of snippets.
Instance Method Summary collapse
-
#backup ⇒ String
Backs up the current TextExpander settings to a timestamped file in the same directory.
-
#construct_data(new_snippets) ⇒ void
Modifies the currently held XML data with new snippet information.
-
#initialize ⇒ void
constructor
Initialize by loading snippets from TextExpander.
-
#save ⇒ void
Saves the current snippets to Settings.textexpander.
Constructor Details
#initialize ⇒ void
Initialize by loading snippets from TextExpander.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/expandsync/textexpander.rb', line 33 def initialize begin xpath = "/*/*/array[preceding-sibling::key[1] = 'snippetsTE2']/*" @output_file = File.join(OUTPUT_PATH, OUTPUT_FILENAME) @base_xml = Nokogiri::XML(File.open(@output_file)) @snippet_xml = @base_xml arr = [] @base_xml.xpath(xpath).each do |snippet| abbreviation = snippet.xpath("string[preceding-sibling::key[1] = 'abbreviation']").text value = snippet.xpath("string[preceding-sibling::key[1] = 'plainText']").text arr << [abbreviation, value, 'TextExpander'] end @snippets = arr rescue fail "Invalid TextExpander XML file: #{ @output_file }" end end |
Instance Attribute Details
#base_xml ⇒ Nokogiri::XML::Document
Stores the initial XML as pulled from Settings.textexpander.
17 18 19 |
# File 'lib/expandsync/textexpander.rb', line 17 def base_xml @base_xml end |
#output_file ⇒ String
Stores the full output path for the produced XML.
21 22 23 |
# File 'lib/expandsync/textexpander.rb', line 21 def output_file @output_file end |
#snippet_xml ⇒ Nokogiri::XML::Document
Stores the final XML to save.
25 26 27 |
# File 'lib/expandsync/textexpander.rb', line 25 def snippet_xml @snippet_xml end |
#snippets ⇒ Array
Stores an array of snippets.
29 30 31 |
# File 'lib/expandsync/textexpander.rb', line 29 def snippets @snippets end |
Instance Method Details
#backup ⇒ String
Backs up the current TextExpander settings to a timestamped file in the same directory.
55 56 57 |
# File 'lib/expandsync/textexpander.rb', line 55 def backup FileUtils.cp(@output_file, @output_file + "_#{ Time.now.utc.iso8601 }") end |
#construct_data(new_snippets) ⇒ void
This method returns an undefined value.
Modifies the currently held XML data with new snippet information.
63 64 65 66 |
# File 'lib/expandsync/textexpander.rb', line 63 def construct_data(new_snippets) snippet_uuids = _add_snippets_to_base_xml(new_snippets) _add_group_to_base_xml(snippet_uuids) end |
#save ⇒ void
This method returns an undefined value.
Saves the current snippets to Settings.textexpander.
70 71 72 |
# File 'lib/expandsync/textexpander.rb', line 70 def save File.open(@output_file, 'w') {|f| f.write(@snippet_xml) } end |