Class: Docbook::Mirror::Handlers::Media

Inherits:
Object
  • Object
show all
Defined in:
lib/docbook/mirror/handlers/media.rb

Class Method Summary collapse

Class Method Details

.figure(element, context:) ⇒ Object

Figure and InformalFigure



8
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
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/docbook/mirror/handlers/media.rb', line 8

def self.figure(element, context:)
  title_text = element.title&.content&.join
  xml_id = element.xml_id

  # Check for programlisting/screen content first (code figures)
  code_el = nil
  if element.programlisting
    code_el = element.programlisting
  elsif element.screen
    code_el = element.screen
  end

  if code_el
    lang = code_el.language
    code_text = context.extract_text(code_el)
    attrs = { xml_id: xml_id, title: title_text,
              language: lang }.compact
    return Node::CodeBlock.new(
      attrs: attrs,
      content: [Node::Text.new(text: code_text)],
    )
  end

  # Figure has mediaobject (collection), InformalFigure also has mediaobject
  # Both contain imageobject with imagedata.fileref
  media = nil
  if element.mediaobject && !element.mediaobject.empty?
    media = element.mediaobject.first
  elsif element.imageobject && !element.imageobject.empty?
    media = element
  end

  image_obj = media&.imageobject&.first
  href = image_obj&.imagedata&.fileref

  # Emit placeholder with xml_id even without image, for xref anchoring
  unless href
    return nil unless xml_id

    attrs = { xml_id: xml_id, title: title_text }.compact
    return Node::Paragraph.new(
      attrs: attrs,
      content: [Node::Text.new(text: "[#{title_text || xml_id}]")],
    )
  end
  attrs = { xml_id: xml_id, title: title_text, src: href }.compact
  Node::Image.new(attrs: attrs)
end

.inline_image(element, context:) ⇒ Object

Inline image from Inlinemediaobject



58
59
60
61
62
63
# File 'lib/docbook/mirror/handlers/media.rb', line 58

def self.inline_image(element, context:)
  href = element.imageobject&.first&.then do |io|
    io&.imagedata&.fileref
  end
  Node::Image.new(attrs: { src: href }.compact)
end