Class: ArticleJSON::Import::GoogleDoc::HTML::EmbeddedParser

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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Shared::Caption

#caption

Constructor Details

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

Returns a new instance of EmbeddedParser.

Parameters:



11
12
13
14
15
# File 'lib/article_json/import/google_doc/html/embedded_parser.rb', line 11

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

Class Method Details

.build(node:, caption_node:, css_analyzer:) ⇒ ArticleJSON::Elements::Embed

Build a embedded element based on the node’s content

Parameters:

Returns:



69
70
71
72
73
74
75
76
77
# File 'lib/article_json/import/google_doc/html/embedded_parser.rb', line 69

def build(node:, caption_node:, css_analyzer:)
  find_parser(node.inner_text)
    &.new(
      node: node,
      caption_node: caption_node,
      css_analyzer: css_analyzer
    )
    &.element
end

.matches?(text) ⇒ Boolean

Check if a given string is a Youtube embedding

Parameters:

  • text (String)

Returns:

  • (Boolean)


53
54
55
# File 'lib/article_json/import/google_doc/html/embedded_parser.rb', line 53

def matches?(text)
  !!(url_regexp =~ text)
end

.supported?(node) ⇒ Boolean

Check if a node contains a supported embedded element

Parameters:

  • node (Nokogiri::HTML::Node)

Returns:

  • (Boolean)


82
83
84
# File 'lib/article_json/import/google_doc/html/embedded_parser.rb', line 82

def supported?(node)
  !find_parser(node.inner_text).nil?
end

.url_regexpRegexp

Regular expression to check if node content is embeddable element Is also used to extract the ID from the URL.

Returns:

  • (Regexp)

Raises:

  • (NotImplementedError)


60
61
62
# File 'lib/article_json/import/google_doc/html/embedded_parser.rb', line 60

def url_regexp
  raise NotImplementedError
end

Instance Method Details

#elementArticleJSON::Elements::Embed

The embedded element



40
41
42
43
44
45
46
47
# File 'lib/article_json/import/google_doc/html/embedded_parser.rb', line 40

def element
  ArticleJSON::Elements::Embed.new(
    embed_type: embed_type,
    embed_id: embed_id,
    tags: tags,
    caption: caption
  )
end

#embed_idString

Extract the video ID from an URL

Returns:

  • (String)


19
20
21
22
# File 'lib/article_json/import/google_doc/html/embedded_parser.rb', line 19

def embed_id
  match = @node.inner_text.strip.match(self.class.url_regexp)
  match[:id] if match
end

#embed_typeSymbol

The type of this embedded element To be implemented by sub classes!

Returns:

  • (Symbol)

Raises:

  • (NotImplementedError)


27
28
29
# File 'lib/article_json/import/google_doc/html/embedded_parser.rb', line 27

def embed_type
  raise NotImplementedError
end

#tagsArray[Symbol]

Extract any potential tags, specified in brackets after the URL

Returns:

  • (Array[Symbol])


33
34
35
36
# File 'lib/article_json/import/google_doc/html/embedded_parser.rb', line 33

def tags
  match = /(.*?)[\s\u00A0]+\[(?<tags>.*)\]/.match(@node.inner_text)
  (match ? match[:tags] : '').split(' ')
end