Class: ExpandSync::TextExpander

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initializevoid

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_xmlNokogiri::XML::Document

Stores the initial XML as pulled from Settings.textexpander.

Returns:

  • (Nokogiri::XML::Document)


17
18
19
# File 'lib/expandsync/textexpander.rb', line 17

def base_xml
  @base_xml
end

#output_fileString

Stores the full output path for the produced XML.

Returns:

  • (String)


21
22
23
# File 'lib/expandsync/textexpander.rb', line 21

def output_file
  @output_file
end

#snippet_xmlNokogiri::XML::Document

Stores the final XML to save.

Returns:

  • (Nokogiri::XML::Document)


25
26
27
# File 'lib/expandsync/textexpander.rb', line 25

def snippet_xml
  @snippet_xml
end

#snippetsArray

Stores an array of snippets.

Returns:

  • (Array)


29
30
31
# File 'lib/expandsync/textexpander.rb', line 29

def snippets
  @snippets
end

Instance Method Details

#backupString

Backs up the current TextExpander settings to a timestamped file in the same directory.

Returns:

  • (String)


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.

Parameters:

  • new_snippets (Array)

    The snippet array to use



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

#savevoid

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