Class: SyntaxOn::Browser

Inherits:
Object
  • Object
show all
Defined in:
lib/syntax-on/browser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(directory = '.') ⇒ Browser

Returns a new instance of Browser.



5
6
7
# File 'lib/syntax-on/browser.rb', line 5

def initialize directory = '.'
  @directory = directory || '.'
end

Instance Attribute Details

#current_themeObject

Returns the value of attribute current_theme.



3
4
5
# File 'lib/syntax-on/browser.rb', line 3

def current_theme
  @current_theme
end

#requestObject

Returns the value of attribute request.



3
4
5
# File 'lib/syntax-on/browser.rb', line 3

def request
  @request
end

#responseObject

Returns the value of attribute response.



3
4
5
# File 'lib/syntax-on/browser.rb', line 3

def response
  @response
end

Instance Method Details

#call(env) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/syntax-on/browser.rb', line 9

def call env
  @pwd = Dir.pwd
  Dir.chdir @directory
  
  @response = Rack::Response.new
  @request  = Rack::Request.new env

  @current_theme ||= 'remi' # set default theme to 'remi'
  @current_theme = request['theme'] if request['theme'] and SyntaxOn.theme_names.include? request['theme']

  if request.path_info[/^\/styles\/(.*)\.css$/]
    response['Content-Type'] = 'text/css'
    response.body = SyntaxOn::theme request.path_info.match(/^\/styles\/(.*)\.css$/)[1]
  else
    response.body = response_for env
  end

  Dir.chdir @pwd

  response.finish
end

#code_layout(code = '') ⇒ Object



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
92
# File 'lib/syntax-on/browser.rb', line 66

def code_layout code = ''
  <<HTML
<html>
<head>
  <script src="http://jquery.com/src/jquery-latest.js" type="text/javascript"></script>
  <style type="text/css">
  <!--
    select { position: absolute; top: 5px; right: 5px; }
  -->
  </style>
  <link rel="stylesheet" href="/styles/#{ current_theme }.css" type="text/css" />
<head>
<body>
<h1>#{ @request.path_info }</h1>
#{ theme_selector }
#{ line_number_switcher_link }
#{ line_number_remover }
<hr />
<pre>
#{ code }
</pre>
<hr />
#{ file_list }
</body>
</html>
HTML
end

#cssObject

need to fix this … can’t seem to find the theme directory when running in thin …



94
95
96
# File 'lib/syntax-on/browser.rb', line 94

def css # need to fix this ... can't seem to find the theme directory when running in thin ...
  SyntaxOn::theme :murphy
end

#file_listObject



41
42
43
# File 'lib/syntax-on/browser.rb', line 41

def file_list
  '<ul>' + Dir['**/*'].map { |file_or_dir| %{<li><a href="/#{file_or_dir}">#{file_or_dir}</a></li>} }.join + '</ul>'
end

#line_number_removerObject



51
52
53
54
55
# File 'lib/syntax-on/browser.rb', line 51

def line_number_remover
  <<HTML
  <a href="#" onclick="javascript:var num_line_numbers = document.getElementsByClassName('lnr').length; for(var i=0; i<num_line_numbers; i++){ document.getElementsByTagName('pre')[0].removeChild( document.getElementsByClassName('lnr')[0] ); }">remove line numbers (for copying)</a>
HTML
end


45
46
47
48
49
# File 'lib/syntax-on/browser.rb', line 45

def line_number_switcher_link
  <<HTML
  <a href="#" onclick="javascript:for each(var span in document.getElementsByClassName('lnr')){ (span.style.display == 'none') ? span.style.display = 'inline' : span.style.display = 'none'; }">toggle line numbers</a>
HTML
end

#response_for(env) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/syntax-on/browser.rb', line 31

def response_for env
  path = env['PATH_INFO'].sub '/',''

  if File.file? path
    code_layout SyntaxOn.new( nil, :file => path ).to_html
  else
    code_layout
  end
end

#theme_selectorObject



57
58
59
60
61
62
63
64
# File 'lib/syntax-on/browser.rb', line 57

def theme_selector
  <<HTML
  <select id="theme-selector" onchange="javascript:window.location = window.location.toString().replace(/\\?.*/,'') + '?theme=' + document.getElementById('theme-selector').value">
    <option>... select a theme ...</option>
    #{ SyntaxOn::theme_names.map { |theme| "<option>#{theme}</option>" } }
  </select>
HTML
end