Class: OMF::Web::Widget::Text::Maruku::WidgetElement

Inherits:
Base::LObject
  • Object
show all
Defined in:
lib/omf-web/widget/text/maruku.rb

Constant Summary collapse

@@pre_create_handlers =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(wdescr) ⇒ WidgetElement

Returns a new instance of WidgetElement.



82
83
84
85
86
87
# File 'lib/omf-web/widget/text/maruku.rb', line 82

def initialize(wdescr)
  debug  "Embedding widget - #{wdescr} "
  @wdescr = wdescr
  @widget = OMF::Web::Widget.create_widget(wdescr)
  debug "Created widget - #{@widget.class}"
end

Instance Attribute Details

#widgetObject (readonly)

Returns the value of attribute widget.



80
81
82
# File 'lib/omf-web/widget/text/maruku.rb', line 80

def widget
  @widget
end

Class Method Details

.create(wdescr) ⇒ Object



69
70
71
72
73
74
75
76
77
78
# File 'lib/omf-web/widget/text/maruku.rb', line 69

def self.create(wdescr)
  wdescr = @@pre_create_handlers.reduce(wdescr) do |wd, block|
    wd2 = block.call(wd)
    unless wd2.is_a? Hash
      raise "Pre_create handler '#{block}' does not return hash, but '#{wd2}'"
    end
    wd2
  end
  self.new(wdescr)
end

.on_pre_create(&block) ⇒ Object

Register a block which is presented with the widget description (Hash) we are about to create. The block is assumed to return a widget description.



65
66
67
# File 'lib/omf-web/widget/text/maruku.rb', line 65

def self.on_pre_create(&block)
  @@pre_create_handlers << block
end

Instance Method Details

#node_typeObject



107
108
109
# File 'lib/omf-web/widget/text/maruku.rb', line 107

def node_type
  :widget
end

#to_htmlObject



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/omf-web/widget/text/maruku.rb', line 89

def to_html
  content = @widget.content
  h = content.to_html
  klass = ['embedded']
  if caption = @wdescr[:caption] || @widget.title
    if mt = @wdescr[:'mime-type']
      klass << "embedded-#{mt.gsub('/', '-')}"
    end
    if ty = @wdescr[:type]
      klass << "embedded-#{ty.gsub('/', '-')}"
    end
    h += "<div class='caption'><span class='figure'>Figure: </span><span class='text'>#{caption}</span></div>"
  end
  root = ::REXML::Document.new("<div class='#{klass.join(' ')}'>#{h}</div>").root
  #puts "EMBEDDED >>> #{root}"
  root
end