Module: RackConsole::Formatting

Included in:
AppHelpers
Defined in:
lib/rack_console/formatting.rb

Instance Method Summary collapse

Instance Method Details

#ansi2html(ansi) ⇒ Object



209
210
211
# File 'lib/rack_console/formatting.rb', line 209

def ansi2html ansi
  Ansi2Html.new.convert(ansi, '')
end

#e(text) ⇒ Object



76
77
78
# File 'lib/rack_console/formatting.rb', line 76

def e(text)
  Rack::Utils.escape(text.to_s)
end

#file_line_to_href(name, lineno = nil) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/rack_console/formatting.rb', line 48

def file_line_to_href name, lineno = nil
  link = name.sub(%r{^/}, '-')
  link = link.split('/').map{|s| e s}.join('/')
  link = url_root("/file/#{link}")
  link << ":#{lineno}\##{lineno - 2}" if lineno
  link
end

#file_name_tag(str) ⇒ Object



129
130
131
# File 'lib/rack_console/formatting.rb', line 129

def file_name_tag str
  %Q{<span class="file_name">#{str}</span>}
end

#format_as_terminal(str) ⇒ Object



200
201
202
203
204
205
206
207
# File 'lib/rack_console/formatting.rb', line 200

def format_as_terminal str
  str &&= str.to_s.force_encoding('UTF-8')
  if str.blank?
    %Q{<span class="none">NONE</span>}
  else
    ansi2html(str)
  end
end

#format_backtrace(line) ⇒ Object



145
146
147
148
149
150
151
152
153
154
# File 'lib/rack_console/formatting.rb', line 145

def format_backtrace line
  line = line.to_s
  html =
    if line =~ /^(.*):(\d+):(in .*)$/ && File.exist?($1)
      "#{format_source_location([$1, $2.to_i])}:#{h $3}"
    else
      file_name_tag(h line)
    end
  %Q{<span class="backtrace">#{html}</span>}
end

#format_method(m, kind, owner = nil) ⇒ Object



103
104
105
106
107
108
109
# File 'lib/rack_console/formatting.rb', line 103

def format_method m, kind, owner = nil
  owner ||= m.owner
  source_location = m.source_location
  source_location &&= source_location * ":"
  href = method_href(m, kind, owner)
  "<a href='#{href}' title='#{source_location}' class='method_name'>#{method_name_tag(h(m.name))}</a>"
end

#format_methods(obj) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/rack_console/formatting.rb', line 116

def format_methods obj
  case obj
  when nil
    return nil
  when ::Method, ::UnboundMethod, MockMethod
    name = obj.name or return nil
  else
    name = obj.to_s
  end
  href = url_root("/methods/*/*/#{e name}")
  "<a href='#{href}' title='Other methods named #{h name.inspect}' class='method_name'>#{method_name_tag(h(name))}</a>"
end

#format_module(obj) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/rack_console/formatting.rb', line 88

def format_module obj
  return module_name_tag(h(obj.inspect)) unless obj && obj.name
  path = obj.name.to_s.split('::')
  result = ''
  name = ''; pre = ''
  path.each do | n |
    name << n
    href = url_root("/module/#{name}")
    result << "<a href='#{href}' class='module_name'>#{module_name_tag("#{pre}#{h n}")}</a>"
    name << '::'
    pre = '::'
  end
  module_name_tag(result)
end

#format_object(obj, inline = false) ⇒ Object



9
10
11
12
13
14
15
16
# File 'lib/rack_console/formatting.rb', line 9

def format_object obj, inline = false
  case obj
  when Module
    format_module obj
  else
    format_other obj, inline
  end
end

#format_other(obj, inline = false) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/rack_console/formatting.rb', line 18

def format_other obj, inline = false
  if inline
    literal_tag(h(limit_string(safe_format(obj).strip, 80)))
  else
    safe_format_structured(obj)
  end
end

#format_source_location(source_location, meth = nil, kind = nil, owner = nil) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/rack_console/formatting.rb', line 26

def format_source_location source_location, meth = nil, kind = nil, owner = nil
  file, line = source_location
  if file
    "<a href='#{file_line_to_href file, line}' class='file_name'>#{file_name_tag("#{file}:#{line}")}</a>"
  else
    if meth
      # Assume meth is Ruby Core and link to rdocs on ruby-doc.org.
      name = meth.name
      owner ||= meth.owner
      owner_name = (owner.name || '').gsub('::', '/')
      kind ||= (owner.instance_method(name) rescue nil) ? :i : :c
      a_name = name.to_s.gsub(/([^a-z0-9_])/i){|m| "-%X" % [ m.ord ]}
      a_name.sub!(/^-/, '')
      a_name = "method-#{kind}-#{a_name}"
      ruby_core_link = "http://www.ruby-doc.org/core-#{RUBY_VERSION}/#{owner_name}.html\##{a_name}"
      "<a href='#{ruby_core_link}' class='ruby_core_doc'>#{h ruby_core_link}</a>"
    else
      "NONE"
    end
  end
end

#h(text) ⇒ Object



72
73
74
# File 'lib/rack_console/formatting.rb', line 72

def h(text)
  Rack::Utils.escape_html(text.to_s)
end

#href_to_file_line(path) ⇒ Object



56
57
58
59
60
61
# File 'lib/rack_console/formatting.rb', line 56

def href_to_file_line path
  path.to_s =~ /^([^:]+)(:([^:]+))?/
  file, line = $1, $3
  file.sub!(/^-/, '/')
  [ file, line && line.to_i ]
end

#limit_string(text, len) ⇒ Object



80
81
82
83
84
85
86
# File 'lib/rack_console/formatting.rb', line 80

def limit_string(text, len)
  text = text.to_s
  if text.size > len
    text = text[0 .. len] + ' ...'
  end
  text
end

#literal_tag(str) ⇒ Object



141
142
143
# File 'lib/rack_console/formatting.rb', line 141

def literal_tag str
  %Q{<span class="literal">#{str}</span>}
end

#method_href(m, kind, owner = nil) ⇒ Object



111
112
113
114
# File 'lib/rack_console/formatting.rb', line 111

def method_href m, kind, owner = nil
  owner ||= m.owner
  href = url_root("/method/#{owner.name}/#{e kind.to_s}/#{e m.name}")
end

#method_name_tag(str) ⇒ Object



137
138
139
# File 'lib/rack_console/formatting.rb', line 137

def method_name_tag str
  %Q{<span class="method_name">#{str}</span>}
end

#module_name_tag(str) ⇒ Object



133
134
135
# File 'lib/rack_console/formatting.rb', line 133

def module_name_tag str
  %Q{<span class="module_name">#{str}</span>}
end

#safe_format(obj) ⇒ Object



189
190
191
# File 'lib/rack_console/formatting.rb', line 189

def safe_format obj
  safe_pp(obj)
end

#safe_format_structured(obj) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/rack_console/formatting.rb', line 175

def safe_format_structured obj
  begin
    if config[:awesome_print] && defined?(::AwesomePrint)
      ansi = obj.ai(indent: 2, html: false, index: false)
      ansi2html(ansi)
    else
      '<pre>' << wrap_lines(safe_pp(obj)) << '</pre>'
    end
  rescue
    STDERR.puts "  #{$!.inspect}: falling back to #inspect for #{obj.class}\n  #{$!.backtrace * "\n  "}"
    '<pre>' << wrap_lines(obj.inspect) << '</pre>'
  end
end

#safe_pp(obj) ⇒ Object



193
194
195
196
197
198
# File 'lib/rack_console/formatting.rb', line 193

def safe_pp obj
  ::PP.pp(obj, '')
rescue
  STDERR.puts "  #{$!.inspect}: falling back to #inspect for #{obj.class}\n  #{$!.backtrace * "\n  "}"
  obj.inspect
end

#source_file(source_location) ⇒ Object



68
69
70
# File 'lib/rack_console/formatting.rb', line 68

def source_file source_location
  source_location && SourceFile.new(source_location).load!
end

#source_file_methods_href(file) ⇒ Object



63
64
65
66
# File 'lib/rack_console/formatting.rb', line 63

def source_file_methods_href file
  link = file.sub(%r{^/}, '-')
  link = url_root("/methods/file/#{link}")
end

#wrap_line(str, width = nil) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/rack_console/formatting.rb', line 162

def wrap_line str, width = nil
  width ||= config[:wrap_width] || 80
  str = str.to_s
  out = ''
  pos = 0
  while pos < str.size
    out << h(str[pos, width])
    pos += width
    out << "&nbsp;\u21B5\n" if pos < str.size
  end
  out
end

#wrap_lines(str, width = nil) ⇒ Object



156
157
158
159
160
# File 'lib/rack_console/formatting.rb', line 156

def wrap_lines str, width = nil
  str.to_s.split("\n").map do | line |
    wrap_line line, width
  end * "\n"
end