Class: Coradoc::Html::Converters::Video

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

  # Extract options from metadata
  options = video.&.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 poster if specified
  poster = video.&.dig(:poster)
  attrs << %( poster="#{escape_attribute(poster)}") if poster

  # Add width and height if specified
  width = video.&.dig(:width)
  height = video.&.dig(:height)

  attrs << %( width="#{escape_attribute(width)}") if width

  attrs << %( height="#{escape_attribute(height)}") if height

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

  attrs.join
end

.build_figure_attrs(video) ⇒ Object



136
137
138
139
140
141
142
143
# File 'lib/coradoc/html/converters/video.rb', line 136

def self.build_figure_attrs(video)
  attrs = []

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

.extract_video_metadata(element) ⇒ Object



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)
   = {}
  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?

  # Extract poster
  [:poster] = element['poster'] if element['poster']

  # Extract dimensions
  [:width] = element['width'] if element['width']

  [:height] = element['height'] if element['height']

  
end

.extract_video_src(element) ⇒ Object



145
146
147
148
149
150
151
152
# File 'lib/coradoc/html/converters/video.rb', line 145

def self.extract_video_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 <video>
  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 = {})
  # Handle both <video> and <figure><video> structures
  video_elem = if element.name == 'figure'
                 element.at_css('video')
               elsif element.name == 'video'
                 element
               else
                 return nil
               end

  return nil unless video_elem

  # Extract source from <source> tag or src attribute
  src = extract_video_src(video_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 = video_elem['id'] || element['id']

  # Extract video attributes
   = (video_elem)
  [:src] = src

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

  # Build video attributes
  attrs = build_attributes(video)

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

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

  # Build optional caption/title
  caption = video.title

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