Class Bcat
In: lib/bcat.rb
lib/bcat/ansi.rb
lib/bcat/browser.rb
lib/bcat/html.rb
lib/bcat/reader.rb
lib/bcat/server.rb
Parent: Object

Methods

[]   assemble   browser_command   call   close   command   content_for_head   each   escape_js   foot   new   new   notice   open   serve!   shell_quote   to_app  

Included Modules

Rack::Utils

Classes and Modules

Class Bcat::ANSI
Class Bcat::Browser
Class Bcat::HeadParser
Class Bcat::Reader
Class Bcat::Server
Class Bcat::TeeFilter
Class Bcat::TextFilter

Constants

VERSION = '0.6.1'
COMMANDS = { 'Darwin' => { 'default' => "open", 'safari' => "open -a Safari", 'firefox' => "open -a Firefox", 'chrome' => "open -a Google\\ Chrome", 'chromium' => "open -a Chromium", 'opera' => "open -a Opera", 'curl' => "curl -s"   browser name -> command mappings
ALIASES = { 'google-chrome' => 'chrome', 'google chrome' => 'chrome', 'gnome' => 'epiphany'   alternative names for browsers

Attributes

format  [R] 

Public Class methods

[Source]

    # File lib/bcat.rb, line 14
14:   def initialize(args=[], config={})
15:     @config = {:Host => '127.0.0.1', :Port => 8091}.merge(config)
16:     @reader = Bcat::Reader.new(@config[:command], args)
17:     @format = @config[:format]
18:   end

[Source]

    # File lib/bcat/browser.rb, line 40
40:     def initialize(browser, command=ENV['BCAT_COMMAND'])
41:       @browser = browser
42:       @command = command
43:     end

Public Instance methods

[Source]

    # File lib/bcat.rb, line 20
20:   def [](key)
21:     @config[key]
22:   end

[Source]

    # File lib/bcat.rb, line 41
41:   def assemble
42:     @reader.open
43: 
44:     @format = @reader.sniff if @format.nil?
45: 
46:     @filter = @reader
47:     @filter = TeeFilter.new(@filter) if @config[:tee]
48:     @filter = TextFilter.new(@filter) if @format == 'text'
49:     @filter = ANSI.new(@filter) if @format == 'text' || @config[:ansi]
50:   end

[Source]

    # File lib/bcat/browser.rb, line 57
57:     def browser_command(browser=@browser)
58:       browser ||= 'default'
59:       browser = browser.downcase
60:       browser = ALIASES[browser] || browser
61:       COMMANDS[ENVIRONMENT][browser]
62:     end

[Source]

    # File lib/bcat.rb, line 36
36:   def call(env)
37:     notice "#{env['REQUEST_METHOD']} #{env['PATH_INFO'].inspect}"
38:     [200, {"Content-Type" => "text/html;charset=utf-8"}, self]
39:   end

[Source]

     # File lib/bcat.rb, line 107
107:   def close
108:     unless @config[:persist]
109:       notice "closing with interrupt"
110:       raise Interrupt, "connection closed"
111:     end
112:   end

[Source]

    # File lib/bcat/browser.rb, line 53
53:     def command
54:       @command || browser_command
55:     end

[Source]

    # File lib/bcat.rb, line 83
83:   def content_for_head(inject='')
84:     [
85:       "\n" * 1000,
86:       "<!DOCTYPE html>",
87:       "<html>",
88:       "<head>",
89:       "<!-- bcat was here -->",
90:       inject.to_s,
91:       "<link href=\"data:image/x-icon;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQEAYAAABPYyMiAAAABmJLR0T///////8JWPfcAAAACXBIWXMAAABIAAAASABGyWs+AAAAF0lEQVRIx2NgGAWjYBSMglEwCkbBSAcACBAAAeaR9cIAAAAASUVORK5CYII=\" rel=\"icon\" type=\"image/x-icon\" />",
92:       "<title>#{self[:title] || 'bcat'}</title>",
93:       "</head>"
94:     ].join("\n")
95:   end

[Source]

    # File lib/bcat.rb, line 52
52:   def each
53:     assemble
54: 
55:     head_parser = Bcat::HeadParser.new
56: 
57:     @filter.each do |buf|
58:       if head_parser.nil?
59:         yield buf
60:       elsif head_parser.feed(buf)
61:         yield content_for_head(inject=head_parser.head)
62:         yield "\n"
63:         yield head_parser.body
64:         head_parser = nil
65:       end
66:     end
67: 
68:     if head_parser
69:       yield content_for_head(inject=head_parser.head) +
70:             "\n" +
71:             head_parser.body
72:     end
73: 
74:     yield foot
75:   rescue Errno::EINVAL
76:     # socket was closed
77:     notice "browser client went away"
78:   rescue => boom
79:     notice "boom: #{boom.class}: #{boom.to_s}"
80:     raise
81:   end

[Source]

     # File lib/bcat.rb, line 101
101:   def escape_js(string)
102:     string = string.gsub(/['\\]/) { |char| "\\#{char}" }
103:     string.gsub!(/\n/, '\n')
104:     string
105:   end

[Source]

    # File lib/bcat.rb, line 97
97:   def foot
98:     "</body>\n</html>\n"
99:   end

[Source]

     # File lib/bcat.rb, line 114
114:   def notice(message)
115:     return if !@config[:debug]
116:     warn "#{File.basename($0)}: #{message}"
117:   end

[Source]

    # File lib/bcat/browser.rb, line 45
45:     def open(url)
46:       fork do
47:         $stdin.close
48:         exec "#{command} #{shell_quote(url)}"
49:         exit! 128
50:       end
51:     end

[Source]

    # File lib/bcat.rb, line 32
32:   def serve!(&bk)
33:     Bcat::Server.run to_app, @config, &bk
34:   end

[Source]

    # File lib/bcat/browser.rb, line 64
64:     def shell_quote(argument)
65:       '"' + argument.to_s.gsub(/([\\"`$])/) { "\\" + $1 } + '"'
66:     end

[Source]

    # File lib/bcat.rb, line 24
24:   def to_app
25:     app = self
26:     Rack::Builder.new do
27:       use Rack::Chunked
28:       run app
29:     end
30:   end

[Validate]