Class: Coradoc::Html::Converters::Audio
- Inherits:
-
Base
- Object
- Base
- Coradoc::Html::Converters::Audio
show all
- Defined in:
- lib/coradoc/html/converters/audio.rb
Overview
Converter for audio 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(audio) ⇒ 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
|
# File 'lib/coradoc/html/converters/audio.rb', line 84
def self.build_attributes(audio)
attrs = []
options = audio.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')
attrs << %( id="#{escape_attribute(audio.id)}") if audio.id
attrs.join
end
|
130
131
132
133
134
135
136
137
|
# File 'lib/coradoc/html/converters/audio.rb', line 130
def self.build_figure_attrs(audio)
attrs = []
attrs << %( id="#{escape_attribute(audio.id)}-figure") if audio.id
attrs.join
end
|
.build_type_attr(src) ⇒ Object
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
# File 'lib/coradoc/html/converters/audio.rb', line 109
def self.build_type_attr(src)
ext = File.extname(src).downcase
type = case ext
when '.mp3'
'audio/mpeg'
when '.ogg', '.oga'
'audio/ogg'
when '.wav'
'audio/wav'
when '.m4a'
'audio/mp4'
when '.aac'
'audio/aac'
when '.flac'
'audio/flac'
end
type ? %( type="#{type}") : ''
end
|
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
# File 'lib/coradoc/html/converters/audio.rb', line 148
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
end
|
139
140
141
142
143
144
145
146
|
# File 'lib/coradoc/html/converters/audio.rb', line 139
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 <audio> to CoreModel::Block (audio)
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/audio.rb', line 46
def self.to_coradoc(element, _options = {})
audio_elem = if element.name == 'figure'
element.at_css('audio')
elsif element.name == 'audio'
element
else
return nil
end
return nil unless audio_elem
src = (audio_elem)
return nil unless src
caption = if element.name == 'figure'
figcaption = element.at_css('figcaption')
figcaption&.text&.strip
end
id = audio_elem['id'] || element['id']
metadata = (audio_elem)
metadata[:src] = src
Coradoc::CoreModel::Block.new(
element_type: 'audio',
content: src,
title: caption,
id: id,
metadata: metadata
)
end
|
.to_html(audio, _options = {}) ⇒ Object
Convert CoreModel::Block (audio) to HTML <audio>
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/audio.rb', line 9
def self.to_html(audio, _options = {})
return '' unless audio
attrs = build_attributes(audio)
src = audio.metadata&.dig(:src) || audio.content
source_tag = %(<source src="#{escape_attribute(src)}"#{build_type_attr(src)}>)
caption = audio.title
if caption
<<~HTML.strip
<figure#{build_figure_attrs(audio)}>
<audio#{attrs}>
#{source_tag}
Your browser does not support the audio tag.
</audio>
<figcaption>#{escape_html(caption)}</figcaption>
</figure>
HTML
else
<<~HTML.strip
<audio#{attrs}>
#{source_tag}
Your browser does not support the audio tag.
</audio>
HTML
end
end
|