Class: OMF::Web::Widget::Code::CodeWidget

Inherits:
AbstractWidget
  • Object
show all
Defined in:
lib/omf-web/widget/UNUSED/code/code_widget.rb

Overview

Maintains the context for a particular code rendering within a specific session.

Constant Summary collapse

@@codeType2mime =
{
  :ruby => '/text/ruby',
  :xml => '/text/xml'
}

Instance Attribute Summary collapse

Attributes inherited from AbstractWidget

#widget_id, #widget_type

Instance Method Summary collapse

Methods inherited from AbstractWidget

#collect_data_sources, #layout?, #mime_type, #title, #tools_menu, #widget_info

Constructor Details

#initialize(opts) ⇒ CodeWidget

Returns a new instance of CodeWidget.



16
17
18
# File 'lib/omf-web/widget/UNUSED/code/code_widget.rb', line 16

def initialize(opts)
  super opts
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



14
15
16
# File 'lib/omf-web/widget/UNUSED/code/code_widget.rb', line 14

def name
  @name
end

#optsObject (readonly)

Returns the value of attribute opts.



14
15
16
# File 'lib/omf-web/widget/UNUSED/code/code_widget.rb', line 14

def opts
  @opts
end

Instance Method Details

#code_typeObject

Return the language the code is written in



59
60
61
62
63
64
65
66
67
68
# File 'lib/omf-web/widget/UNUSED/code/code_widget.rb', line 59

def code_type()
  path = @opts[:file]
  if path.end_with? '.rb'
    :ruby
  elsif path.end_with? '.xml'
    :xml
  else
    :text
  end
end

#contentObject



20
21
22
23
24
# File 'lib/omf-web/widget/UNUSED/code/code_widget.rb', line 20

def content()
  div :id => @base_id, :class => "oml_code CodeRay" do
    rawtext render_code
  end
end

#file_contentObject



47
48
49
50
51
52
53
54
55
# File 'lib/omf-web/widget/UNUSED/code/code_widget.rb', line 47

def file_content()
  ## HACK!!!!
  path = @opts[:file]
  if File.readable?(path)
    File.new(path).read
  else
    "Can't find #{path}"
  end
end

#render_codeObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/omf-web/widget/UNUSED/code/code_widget.rb', line 32

def render_code()
  content = file_content
  type = code_type
  mimeType = @@codeType2mime[type]
  
  #puts ">>>>RENDER_CODE>> #{content.length}"
  begin
    tokens = CodeRay.scan content, type
    tokens.html :line_numbers => :inline, :tab_width => 2, :wrap => :div
  rescue Exception => ex
    puts ">>>> ERORRO: #{ex} #{ex.backtrace}"
  end
  #puts "<<<<< END OF RENDER CODE #{tokens.inspect}"
end