Class: OpenStax::Cnx::V1::Figure

Inherits:
Element
  • Object
show all
Defined in:
lib/openstax/cnx/v1/figure.rb

Constant Summary collapse

MATCH_FIGURE =
"//*[contains(@class, 'os-figure')]"
MATCH_FIGURE_CAPTION =
".//*[contains(@class, 'os-caption')]"
MATCH_FIGURE_ALT_TEXT =
'.//*[@alt]'
MATCH_FIGURE_DATA_ALT_TEXT =
'.//*[@data-alt]'
MATCH_FIGURE_ELEM =
'.//figure'

Instance Attribute Summary

Attributes inherited from Element

#node

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node:) ⇒ Figure

Returns a new instance of Figure.



10
11
12
# File 'lib/openstax/cnx/v1/figure.rb', line 10

def initialize(node:)
  super
end

Class Method Details

.matcherObject



27
28
29
# File 'lib/openstax/cnx/v1/figure.rb', line 27

def self.matcher
  MATCH_FIGURE
end

.matches?(node) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/openstax/cnx/v1/figure.rb', line 31

def self.matches?(node)
  node.matches?(MATCH_FIGURE)
end

Instance Method Details

#alt_textObject



22
23
24
25
# File 'lib/openstax/cnx/v1/figure.rb', line 22

def alt_text
  node.at_xpath(MATCH_FIGURE_ALT_TEXT).try(:attr, 'alt') ||
  node.at_xpath(MATCH_FIGURE_DATA_ALT_TEXT).try(:attr, 'data-alt')
end

#captionObject



14
15
16
# File 'lib/openstax/cnx/v1/figure.rb', line 14

def caption
  node.at_xpath(MATCH_FIGURE_CAPTION).try(:text)
end

#idObject



18
19
20
# File 'lib/openstax/cnx/v1/figure.rb', line 18

def id
  node.at_xpath(MATCH_FIGURE_ELEM).attr('id')
end