Class: Coradoc::Html::Converters::Video
- Inherits:
-
Base
- Object
- Base
- Coradoc::Html::Converters::Video
show all
- Defined in:
- lib/coradoc/html/converters/video.rb
Overview
Converter for video elements
Class Method Summary
collapse
Methods inherited from Base
build_class_attribute, build_element, build_html_attributes, convert_content_to_html, convert_element_to_core, convert_node_to_core, escape_attribute, escape_html, extract_model_attributes, extract_node_attributes, extract_text_fallback, find_converter_class_by_name, find_converter_for_model, handle_unknown_content, render_core_abbreviation, render_core_annotation_block, render_core_bibliography, render_core_bibliography_entry, render_core_block, render_core_block_image, render_core_definition_item, render_core_definition_list, render_core_footnote, render_core_footnote_reference, render_core_inline_element, render_core_inline_image, render_core_list_block, render_core_list_item, render_core_span, render_core_structural_element, render_core_table_cell, render_core_table_row, render_core_term, render_core_toc, render_core_toc_entry, resolve_block_semantic_type, resolve_format_specific_semantic, transform_to_coremodel, treat_children
Class Method Details
.build_attributes(video) ⇒ Object
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
|
# File 'lib/coradoc/html/converters/video.rb', line 84
def self.build_attributes(video)
attrs = []
options = video.metadata&.dig(:options) || []
has_controls = !options.include?('nocontrols')
attrs << ' controls' if has_controls
attrs << ' autoplay' if options.include?('autoplay')
attrs << ' loop' if options.include?('loop')
attrs << ' muted' if options.include?('muted')
poster = video.metadata&.dig(:poster)
attrs << %( poster="#{escape_attribute(poster)}") if poster
width = video.metadata&.dig(:width)
height = video.metadata&.dig(:height)
attrs << %( width="#{escape_attribute(width)}") if width
attrs << %( height="#{escape_attribute(height)}") if height
attrs << %( id="#{escape_attribute(video.id)}") if video.id
attrs.join
end
|
136
137
138
139
140
141
142
143
|
# File 'lib/coradoc/html/converters/video.rb', line 136
def self.build_figure_attrs(video)
attrs = []
attrs << %( id="#{escape_attribute(video.id)}-figure") if video.id
attrs.join
end
|
.build_type_attr(src) ⇒ Object
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
# File 'lib/coradoc/html/converters/video.rb', line 121
def self.build_type_attr(src)
ext = File.extname(src).downcase
type = case ext
when '.mp4'
'video/mp4'
when '.webm'
'video/webm'
when '.ogg', '.ogv'
'video/ogg'
end
type ? %( type="#{type}") : ''
end
|
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
|
# File 'lib/coradoc/html/converters/video.rb', line 154
def self.(element)
metadata = {}
options = []
options << 'controls' if element.has_attribute?('controls')
options << 'autoplay' if element.has_attribute?('autoplay')
options << 'loop' if element.has_attribute?('loop')
options << 'muted' if element.has_attribute?('muted')
metadata[:options] = options unless options.empty?
metadata[:poster] = element['poster'] if element['poster']
metadata[:width] = element['width'] if element['width']
metadata[:height] = element['height'] if element['height']
metadata
end
|
145
146
147
148
149
150
151
152
|
# File 'lib/coradoc/html/converters/video.rb', line 145
def self.(element)
source = element.at_css('source')
return source['src'] if source && source['src']
element['src']
end
|
.to_coradoc(element, _options = {}) ⇒ Object
Convert HTML <video> to CoreModel::Block (video)
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
# File 'lib/coradoc/html/converters/video.rb', line 46
def self.to_coradoc(element, _options = {})
video_elem = if element.name == 'figure'
element.at_css('video')
elsif element.name == 'video'
element
else
return nil
end
return nil unless video_elem
src = (video_elem)
return nil unless src
caption = if element.name == 'figure'
figcaption = element.at_css('figcaption')
figcaption&.text&.strip
end
id = video_elem['id'] || element['id']
metadata = (video_elem)
metadata[:src] = src
Coradoc::CoreModel::Block.new(
element_type: 'video',
content: src,
title: caption,
id: id,
metadata: metadata
)
end
|
.to_html(video, _options = {}) ⇒ Object
Convert CoreModel::Block (video) to HTML <video>
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
# File 'lib/coradoc/html/converters/video.rb', line 9
def self.to_html(video, _options = {})
return '' unless video
attrs = build_attributes(video)
src = video.metadata&.dig(:src) || video.content
source_tag = %(<source src="#{escape_attribute(src)}"#{build_type_attr(src)}>)
caption = video.title
if caption
<<~HTML.strip
<figure#{build_figure_attrs(video)}>
<video#{attrs}>
#{source_tag}
Your browser does not support the video tag.
</video>
<figcaption>#{escape_html(caption)}</figcaption>
</figure>
HTML
else
<<~HTML.strip
<video#{attrs}>
#{source_tag}
Your browser does not support the video tag.
</video>
HTML
end
end
|