Class: ArticleJSON::Import::GoogleDoc::HTML::ImageParser

Inherits:
Object
  • Object
show all
Includes:
Shared::Caption, Shared::Float
Defined in:
lib/article_json/import/google_doc/html/image_parser.rb

Instance Method Summary collapse

Methods included from Shared::Caption

#caption

Constructor Details

#initialize(node:, caption_node:, css_analyzer:) ⇒ ImageParser



12
13
14
15
16
17
18
19
20
# File 'lib/article_json/import/google_doc/html/image_parser.rb', line 12

def initialize(node:, caption_node:, css_analyzer:)
  @node = node
  @caption_node = caption_node
  @href = href
  @css_analyzer = css_analyzer

  # Main node indicates the floating behavior
  @float_node = @node
end

Instance Method Details

#altString

The value of the image’s ‘alt` attribute



24
25
26
# File 'lib/article_json/import/google_doc/html/image_parser.rb', line 24

def alt
  image_node.attribute('alt')&.value || ''
end

#elementArticleJSON::Elements::Image



58
59
60
61
62
63
64
65
66
# File 'lib/article_json/import/google_doc/html/image_parser.rb', line 58

def element
  ArticleJSON::Elements::Image.new(
    source_url: source_url,
    float: float,
    caption: caption,
    href: @href,
    alt: alt
  )
end

#floatSymbol

Check if the image is floating (left, right or not at all)



42
43
44
# File 'lib/article_json/import/google_doc/html/image_parser.rb', line 42

def float
  super if floatable_size?
end

#hrefString

Extracts an href from the tag [image-link-to: url]) if present in the caption node.



49
50
51
52
53
54
55
# File 'lib/article_json/import/google_doc/html/image_parser.rb', line 49

def href
  return if @caption_node.nil?
  match = @caption_node.content.strip.match(href_regexp)
  return if match.nil?
  remove_image_link_tag
  match[:url]
end

#image_nodeNokogiri::HTML::Node

The node of the actual image



36
37
38
# File 'lib/article_json/import/google_doc/html/image_parser.rb', line 36

def image_node
  @node.xpath('.//img').first
end

#source_urlString

The value of the image’s ‘src` attribute



30
31
32
# File 'lib/article_json/import/google_doc/html/image_parser.rb', line 30

def source_url
  image_node.attribute('src').value
end