xaviershay / enki-vim

Publish to enki with VIM. That's pretty neat.

This URL has Read+Write access

enki-vim / lib / tag_list.rb
100644 91 lines (77 sloc) 2.27 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
79
80
81
82
83
84
85
86
87
88
89
90
91
class TagList < Array
  def initialize(*args)
    add(*args)
  end
  
  # Add tags to the tag_list. Duplicate or blank tags will be ignored.
  #
  # tag_list.add("Fun", "Happy")
  #
  # Use the <tt>:parse</tt> option to add an unparsed tag string.
  #
  # tag_list.add("Fun, Happy", :parse => true)
  def add(*names)
    extract_and_apply_options!(names)
    concat(names)
    clean!
    self
  end
  
  # Remove specific tags from the tag_list.
  #
  # tag_list.remove("Sad", "Lonely")
  #
  # Like #add, the <tt>:parse</tt> option can be used to remove multiple tags in a string.
  #
  # tag_list.remove("Sad, Lonely", :parse => true)
  def remove(*names)
    extract_and_apply_options!(names)
    delete_if { |name| names.include?(name) }
    self
  end
  
  # Transform the tag_list into a tag string suitable for edting in a form.
  # The tags are joined with <tt>TagList.delimiter</tt> and quoted if necessary.
  #
  # tag_list = TagList.new("Round", "Square,Cube")
  # tag_list.to_s # 'Round, "Square,Cube"'
  def to_s
    clean!
    
    map do |name|
      name.include?(delimiter) ? "\"#{name}\"" : name
    end.join(delimiter.ends_with?(" ") ? delimiter : "#{delimiter} ")
  end
  
 private
  # Remove whitespace, duplicates, and blanks.
  def clean!
    reject!(&:blank?)
    map!(&:strip)
    uniq!
  end
  
  def extract_and_apply_options!(args)
    options = args.last.is_a?(Hash) ? args.pop : {}
    options.assert_valid_keys :parse
    
    if options[:parse]
      args.map! { |a| self.class.from(a) }
    end
    
    args.flatten!
  end
  
  class << self
    # Returns a new TagList using the given tag string.
    #
    # tag_list = TagList.from("One , Two, Three")
    # tag_list # ["One", "Two", "Three"]
    def from(string)
      returning new do |tag_list|
        string = string.to_s.dup
        
        # Parse the quoted tags
        string.gsub!(/"(.*?)"\s*#{delimiter}?\s*/) { tag_list << $1; "" }
        string.gsub!(/'(.*?)'\s*#{delimiter}?\s*/) { tag_list << $1; "" }
        
        tag_list.add(string.split(delimiter))
      end
    end
 
    def delimeter
      ', '
    end
  end
 
  def delimeter
    ', '
  end
end