Class: GNote::Tags

Inherits:
Object
  • Object
show all
Defined in:
lib/gnote/tags.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.tagsObject



11
12
13
# File 'lib/gnote/tags.rb', line 11

def tags
  Tags.new.tags
end

.watchObject



7
8
9
# File 'lib/gnote/tags.rb', line 7

def watch
  Tags.new.watch
end

Instance Method Details

#concat_tagsObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/gnote/tags.rb', line 29

def concat_tags
  content = ""
  Pa.each_r(Rc.p.tagsdir) {|p|
    content << File.read(p.p)
  }

  content = sort(content)

  content = <<-EOF
!_TAG_FILE_SORTED\t1

#{content}
  EOF

  File.write "#{Rc.p.note}/tags", content
end

#generate_tags(file) ⇒ Object

Examples:


generate_tags(Pa("hello.gnote"))


19
20
21
22
23
24
25
26
27
# File 'lib/gnote/tags.rb', line 19

def generate_tags(file)
  content = ""

  File.read("#{Rc.p.note}/#{file}").scan(/∗([^\n ]+)∗/) {|(id)|
    content <<  "#{id}\t#{file}\t/∗#{id}∗\n"
  }

  File.write "#{Rc.p.tagsdir}/#{file}", content, :mkdir => true
end

#sort(content) ⇒ Object



46
47
48
49
50
# File 'lib/gnote/tags.rb', line 46

def sort(content)
  content = content.split(/\n/)
  content = content.sort_by{|v| v.split(/\t/)[0]}
  content.join("\n") + "\n"
end

#tagsObject

task



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/gnote/tags.rb', line 53

def tags
  Pa.empty_dir Rc.p.tagsdir

  Pa.each_r(Rc.p.note) {|p|
    next if skip_file(p.rel)

    generate_tags(p.rel)
  }

  concat_tags
end

#watchObject

task



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/gnote/tags.rb', line 66

def watch
  notifier = INotify::Notifier.new

  notifier.watch(Rc.p.note.p, :modify, :delete, :recursive) { |e|
    file = Pa(e.name) # "ds/pa.gnote"
    if skip_file(file)
      # skip
    else
      if e.flags.include?(:modify)  
        GNote.ui.debug "MODIFY #{file}"
        generate_tags(file)
      elsif e.flags.include?(:delete)
        GNote.ui.debug "DELETE #{file}"
        Pa.rm "#{Rc.p.tagsdir}/#{file}"
      end

      concat_tags
    end
  }

  GNote.ui.say ">> BEGIN WATCH AT #{Rc.p.note}"
  notifier.run
end