Class: Coradoc::Html::Converters::Audio

Inherits:
Base
  • Object
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 = []

  # Extract options from metadata
  options = audio.&.dig(:options) || []

  # Add controls by default (unless nocontrols option is set)
  has_controls = !options.include?('nocontrols')
  attrs << ' controls' if has_controls

  # Add autoplay if specified in options
  attrs << ' autoplay' if options.include?('autoplay')

  # Add loop if specified in options
  attrs << ' loop' if options.include?('loop')

  # Add muted if specified in options
  attrs << ' muted' if options.include?('muted')

  # Add ID if present
  attrs << %( id="#{escape_attribute(audio.id)}") if audio.id

  attrs.join
end

.build_figure_attrs(audio) ⇒ Object



130
131
132
133
134
135
136
137
# File 'lib/coradoc/html/converters/audio.rb', line 130

def self.build_figure_attrs(audio)
  attrs = []

  # Add ID to figure if present
  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)
  # Determine audio MIME type from extension
  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

.extract_audio_metadata(element) ⇒ Object



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)
   = {}
  options = []

  # Extract boolean attributes
  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')

  [:options] = options unless options.empty?

  
end

.extract_audio_src(element) ⇒ Object



139
140
141
142
143
144
145
146
# File 'lib/coradoc/html/converters/audio.rb', line 139

def self.extract_audio_src(element)
  # Try to get src from <source> tag first
  source = element.at_css('source')
  return source['src'] if source && source['src']

  # Fall back to src attribute on <audio>
  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 = {})
  # Handle both <audio> and <figure><audio> structures
  audio_elem = if element.name == 'figure'
                 element.at_css('audio')
               elsif element.name == 'audio'
                 element
               else
                 return nil
               end

  return nil unless audio_elem

  # Extract source from <source> tag or src attribute
  src = extract_audio_src(audio_elem)
  return nil unless src

  # Extract caption if in figure
  caption = if element.name == 'figure'
              figcaption = element.at_css('figcaption')
              figcaption&.text&.strip
            end

  # Extract ID if present
  id = audio_elem['id'] || element['id']

  # Extract audio attributes
   = (audio_elem)
  [:src] = src

  Coradoc::CoreModel::Block.new(
    element_type: 'audio',
    content: src,
    title: caption,
    id: id,
    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

  # Build audio attributes
  attrs = build_attributes(audio)

  # Get audio source from metadata or content
  src = audio.&.dig(:src) || audio.content

  # Build source element
  source_tag = %(<source src="#{escape_attribute(src)}"#{build_type_attr(src)}>)

  # Build optional caption/title
  caption = audio.title

  # Determine if we need a wrapper (for block audio with caption)
  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