Module: Plugins::Core::MediaWidgetParser
- Defined in:
- app/widgets/plugins/core/media_widget_parser.rb
Class Method Summary collapse
- .content_item_element(id) ⇒ Object
- .document_for(html) ⇒ Object
- .image?(mime_type) ⇒ Boolean
- .parse(body) ⇒ Object
- .render_widget_inner(widget) ⇒ Object
- .widget_nodes_for(document) ⇒ Object
Class Method Details
.content_item_element(id) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'app/widgets/plugins/core/media_widget_parser.rb', line 33 def self.content_item_element(id) asset_field_item = ContentItem.find(id).field_items.find { |field_item| field_item.field.field_type_instance.is_a?(AssetFieldType) } url = asset_field_item.data['asset']['versions']['original']['url'] if image? asset_field_item.data['asset']['versions']['original']['mime_type'] element = { src: url } tag_type = 'img' else element = { href: url } tag_type = 'a' end [element, tag_type] end |
.document_for(html) ⇒ Object
16 17 18 |
# File 'app/widgets/plugins/core/media_widget_parser.rb', line 16 def self.document_for(html) Nokogiri::HTML::DocumentFragment.parse html end |
.image?(mime_type) ⇒ Boolean
48 49 50 |
# File 'app/widgets/plugins/core/media_widget_parser.rb', line 48 def self.image?(mime_type) MimeMagic.new(mime_type).mediatype == 'image' end |
.parse(body) ⇒ Object
6 7 8 9 10 11 12 13 14 |
# File 'app/widgets/plugins/core/media_widget_parser.rb', line 6 def self.parse(body) body_document = document_for body (body_document).each do || .inner_html = end body_document.to_html end |
.render_widget_inner(widget) ⇒ Object
24 25 26 27 28 29 30 31 |
# File 'app/widgets/plugins/core/media_widget_parser.rb', line 24 def self.() Nokogiri::HTML::Builder.new do |doc| element, tag_type = content_item_element(['id']) element.merge!({ width: ['width'], height: ['height'], alt: ['alt'], style: ['style'], class: ['class'] }) doc.send(tag_type, element) end.doc.root end |
.widget_nodes_for(document) ⇒ Object
20 21 22 |
# File 'app/widgets/plugins/core/media_widget_parser.rb', line 20 def self.(document) document.css 'media' end |