Class: Few

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

Defined Under Namespace

Modules: Util Classes: Config, RemoteHelper

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(o = {}) ⇒ Few

}}}



152
153
154
155
156
157
158
159
160
161
162
# File 'lib/few.rb', line 152

def initialize(o={})
  @opt = {:filetype => :text, :tee => false, :server => false}.merge(o)
  @config = Few::Config.new(
    :remote => false, :private_key => fewdir('key'),
    :public_key => fewdir('key.pub'), :remote_path => 'http://priv2.soralabo.net/few_server.rb')
  load_config
  @remote = Few::RemoteHelper.new(
    :remote_path => @config.remote_path,
    :public_key  => @config.public_key.nil?  ? nil : (File.exist?(@config.public_key ) ? File.read(@config.public_key ) : nil),
    :private_key => @config.private_key.nil? ? nil : (File.exist?(@config.private_key) ? File.read(@config.private_key) : nil))
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



345
346
347
# File 'lib/few.rb', line 345

def config
  @config
end

Instance Method Details

#fewdir(path, runtime = false) ⇒ Object



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/few.rb', line 207

def fewdir(path,runtime=false)
  if $few_speccing || runtime
    return File.dirname(__FILE__) + '/../fewfiles/' + path
  else
    config_dirs = %w[.few fewfiles]
    config_dirs.delete_if{|x| !File.exist?(File.expand_path("~") + '/' + x)}
    if config_dirs.length > 0
      dir = File.expand_path("~") + '/' + config_dirs[0]
      dir+'/'+path
    else
      if /mswin(?!ce)|mingw|bccwin/ == RUBY_PLATFORM
        Dir.mkdir(File.expand_path('~') + '/fewfiles')
        return File.expand_path('~') + '/fewfiles/'+path
      else
        Dir.mkdir(File.expand_path('~') + '/.few')
        return File.expand_path('~') + '/.few/'+path
      end
    end
  end
end

#fewdirs(path, runtime = false) ⇒ Object



228
229
230
231
232
233
# File 'lib/few.rb', line 228

def fewdirs(path,runtime=false)
  if File.exist?(fewdir(path,runtime)) && FileTest.directory?(fewdir(path,runtime))
    Dir.entries(fewdir(path,runtime))
  else; []
  end
end

#generate_remote_key_pairObject



235
236
237
238
239
240
# File 'lib/few.rb', line 235

def generate_remote_key_pair
  @remote.generate_key_pair
  open(@config.public_key ,'w') { |f| f.print @remote.public_key  }
  open(@config.private_key,'w') { |f| f.print @remote.private_key }
  self
end

#init_ftdetectsObject



203
204
205
# File 'lib/few.rb', line 203

def init_ftdetects
  fewdirs('ftdetect') + fewdirs('ftdetect',true)
end

#load_configObject



191
192
193
194
195
196
197
198
199
200
201
# File 'lib/few.rb', line 191

def load_config
  return if $few_speccing
  config_files =
    %w[_fewrc .fewrc .few.conf few.conf .fewrc.rb _fewrc.rb fewrc.rb]
  config_files.delete_if {|x| !File.exist?(File.expand_path("~") + '/' + x) }
  if config_files.length > 0
    config_file = config_files[0]
    eval File.read(File.expand_path('~') + '/' + config_file)
  end
  self
end

#run(i = nil) ⇒ Object



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
# File 'lib/few.rb', line 242

def run(i = nil)
  if @config.remote && i.nil?
    if @opt[:tee]
      b = ''
      ARGF.each do |l|
        print l
        b += l
      end
      a = b
    else
      a = ARGF.read.toutf8
    end
    unless @remote.public_key
      abort "ERROR: public_key is not found. If you don't have keys, try to generate one with this command on host: few --gen-keys\n" +
        "  If you have keys, just move them to ~/.few"
    end
    unless (r = @remote.send(a)) == true
      abort "ERROR: #{r.inspect}"
    end
  else
    t = Tempfile.new('few')

    File.open(t.path, 'w') do |io|
      if i.nil?
        if @opt[:tee]
          b = ''
          ARGF.each do |l|
            print l
            b += l
          end
          a = CGI.escapeHTML b
        else
          a = CGI.escapeHTML ARGF.read.toutf8
        end
      else
        a = CGI.escapeHTML i
      end
      r = a
      a = a.gsub(/\r?\n/, "<br />\n")

      a = a.gsub(/.\x08/, '')
      a = a.gsub(/\x1b\[([0-9]*)m/) do
        case $1
        when "","39"
          '</font>'
        when "30"
          '<font color="black">'
        when "31"
          '<font color="red">'
        when "32"
          '<font color="green">'
        when "33"
          '<font color="yellow">'
        when "34"
          '<font color="blue">'
        when "35"
          '<font color="magenta">'
        when "36"
          '<font color="cyan">'
        when "37"
          '<font color="white">'
        else
          ''
        end
      end

      io.puts <<-EOF
<html>
<head>
  <title>few: #{i.nil? ? ARGF.filename : '-'} (#{@opt[:filetype].to_s})</title>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <style type="text/css">
      body {
        font-family: Georgia, "menlo regular", "monaco", "courier", monospace;
      }
      .few_body {
        font-size: 12pt;
      }

      #{File.exist?(fewdir('style.css')) ? File.read(fewdir('style.css')) : ""}
  </style>
</head>
<body>
  <h1>few: #{i.nil? ? ARGF.filename : '-'} (#{@opt[:filetype].to_s})</h1>
  <div class="few_body">
#{a}
  </div>
  <textarea col="10" row="15">
#{r}
  </textarea>
</body>
</html>
    EOF
    end

    t.close

    File.rename(t.path, html = t.path + '.html')

    open_browser(html)
  end

end

#start(daemonize = false) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/few.rb', line 164

def start(daemonize=false)
  return self unless @opt[:remote_standing]
  abort 'ERROR: public_key or private_key not found. Try generate to this command: few --gen-keys' if @remote.public_key.nil? || @remote.private_key.nil?
  if daemonize
    puts 'Daemoning...'
    if Process.respond_to?(:daemon)
      Process.daemon
    else
      require 'webrick'
      WEBrick::Daemon.start
    end
  end
  puts "Started"
  loop do
    r = @remote.recv
    unless r.nil?
      puts "Received body"
      puts "Running..."
      run(r)
      puts "Opened browser"
      sleep 10
    else
      sleep 6
    end
  end
end