Class: Neurogami::QFT

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

Overview

QFT (quick Firefox tabs) allows you to launch Firefox from the command line, opening up multiple sites as a set of tabbed pages.

The available tab sets are those defined in your FIrefox bookmarks. Firefox allows you to group bookmarks under a folder name. QFT uses this name to know what tab set to open

Because the names you pick for folders are not always what you want to have to type at the command line, you can define aliases.

QFT requires you to create a .qft file in your home directory (where “home” is defined as what ENV returns). This is a YAML file that defines the path to your mozilla profile directory, the command used to run Firefox, and any aliases. Like this:

:profile_dir: /home/james/.mozilla/firefox/8ho2f2g3.default/
:firefox_cmd: firefox
:alias:
  web: "Web Dev Reference"
  ai:  "ActionItem"
  color: "ColorTools"

The bookmark tab-set name or alias is passed to the ‘run’ method; QFT looks through the bookmarks.html

file to extact the URL set and kicks off Firefox.

And that's about it. 

Currently not tried utside of a Kubuntu machine.  Should work as well on win32 so long as you correctly
define the path to Firefox. 

Enjoy!

James Britt
[email protected]

Constant Summary collapse

DD_START_RE =
/<DT><H3/
DD_END_RE =
/<\/DL><p>/

Instance Method Summary collapse

Constructor Details

#initializeQFT

Returns a new instance of QFT.



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/qft/qft.rb', line 45

def initialize
  cfg_file = "#{ENV['HOME']}/.qft" 
  if  File.exist?(cfg_file)
    @cfg = YAML.load( IO.read(cfg_file ) )
    @alias = @cfg[:alias]
    
  else
    help
    exit
  end
end

Instance Method Details

#adjust_url(u) ⇒ Object



63
64
65
66
67
# File 'lib/qft/qft.rb', line 63

def adjust_url u
  proto, path = u.split( '://')
  path << '/' unless path =~ /\//
  proto + '://' + path
end

#aliasesObject



112
113
114
115
116
117
# File 'lib/qft/qft.rb', line 112

def aliases
  puts "\nDefined aliases:"
  @alias.each do |k,v|
    puts "#{k.to_s}: #{v}"
  end
end

#bookmarks_htmlObject



104
105
106
107
108
109
110
# File 'lib/qft/qft.rb', line 104

def bookmarks_html
  unless @bookmarks_html
    @bookmarks_html = IO.readlines("#{@cfg[:profile_dir ]}/bookmarks.html")
  end
  @bookmarks_html

end

#clean(urls) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/qft/qft.rb', line 85

def clean urls
  urls.map! { |l|
    if l =~ /^<DT><A/
      doc = REXML::Document.new("#{l}</DT>")
      adjust_url( doc.root.elements.to_a( "//A").first.attributes["HREF"].strip )
    else
      nil
    end
  }
  urls.compact!
end

#helpObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/qft/qft.rb', line 69

def help
  print <<-HELP
  Usage:  qft <tab-set-name>
  You must also have a '.qft' file in your home directory with configuration settings:

:profile_dir: /path/to/your/firefox/profile/dir
:firefox_cmd: cmd_to_launch_firefox
:alias:
  web: "Web Dev Reference"
  books:  "Online Book Stores"
  color: "Color Tools"


  HELP
end

#is_alias?(tab) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/qft/qft.rb', line 57

def is_alias?(tab)
  @alias.keys.map{|a| a.to_s.strip  }.include?(tab.to_s.strip ) 
end

#listObject



119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/qft/qft.rb', line 119

def list
  tabs = []
  bookmarks_html.each do |line|
    line.strip!
    tabs << line if line =~ DD_START_RE 
  end

  tabs.map! { |l|
      doc = REXML::Document.new("#{l}</DT>")
       doc.root.elements.to_a( "//H3").first.text.strip 
  }
  puts(  tabs.sort.join( "\n" ) )
end

#run(tab_name) ⇒ Object



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/qft/qft.rb', line 133

def run tab_name
  (help
   exit) if tab_name.strip.empty? 
   set_alias tab_name
   target = />#{Regexp.escape( tab_name )}<\/H3>/i
   %w{
     list
     aliases
   }.each do |opt|
     return( send(opt) ) if tab_name.to_s == opt
   end

   urls = []
   
   adding  = false
   
   bookmarks_html.each do |line|
     line.strip!
     if line =~ DD_START_RE  && line =~ target
       adding = true
     elsif adding &&  line =~  DD_END_RE 
       break
     else
       urls << line if adding
     end
   end
   clean urls

   ( 
    warn "No tab set matched '#{tab_name}'"
    exit 
   ) if urls.empty?

   IO.popen( "#{@cfg[:firefox_cmd]}  '#{urls.join( '|' )}'"   )
end

#set_alias(tab) ⇒ Object



97
98
99
# File 'lib/qft/qft.rb', line 97

def set_alias tab
  tab.replace( @alias[tab] ) if is_alias?( tab )
end

#tab_listObject



101
102
# File 'lib/qft/qft.rb', line 101

def tab_list
end