class ArticleJSON::Import::GoogleDoc::HTML::EmbeddedParser

Public Class Methods

build(node:, caption_node:, css_analyzer:) click to toggle source

Build a embedded element based on the node's content @param [Nokogiri::HTML::Node] node @param [Nokogiri::HTML::Node] caption_node @param [ArticleJSON::Import::GoogleDoc::HTML::CSSAnalyzer] css_analyzer @return [ArticleJSON::Elements::Embed]

# 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) click to toggle source

Check if a given string is a Youtube embedding @param [String] text @return [Boolean]

# File lib/article_json/import/google_doc/html/embedded_parser.rb, line 53
def matches?(text)
  !!(url_regexp =~ text)
end
new(node:, caption_node:, css_analyzer:) click to toggle source

@param [Nokogiri::HTML::Node] node @param [Nokogiri::HTML::Node] caption_node @param [ArticleJSON::Import::GoogleDoc::HTML::CSSAnalyzer] css_analyzer

# 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
supported?(node) click to toggle source

Check if a node contains a supported embedded element @param [Nokogiri::HTML::Node] node @return [Boolean]

# File lib/article_json/import/google_doc/html/embedded_parser.rb, line 82
def supported?(node)
  !find_parser(node.inner_text).nil?
end
url_regexp() click to toggle source

Regular expression to check if node content is embeddable element Is also used to extract the ID from the URL. @return [Regexp]

# File lib/article_json/import/google_doc/html/embedded_parser.rb, line 60
def url_regexp
  raise NotImplementedError
end

Private Class Methods

find_parser(text) click to toggle source

Find the first matching class for a given (URL) string @param [String] text @return [Class]

# File lib/article_json/import/google_doc/html/embedded_parser.rb, line 104
def find_parser(text)
  text = text.strip.downcase
  return nil if text.empty?
  parsers.find { |klass| klass.matches?(text) }
end
parsers() click to toggle source

List of embedded element classes @return [ArticleJSON::Import::GoogleDoc::HTML::EmbeddedParser]

# File lib/article_json/import/google_doc/html/embedded_parser.rb, line 90
def parsers
  [
    EmbeddedFacebookVideoParser,
    EmbeddedVimeoVideoParser,
    EmbeddedYoutubeVideoParser,
    EmbeddedTweetParser,
    EmbeddedSlideshareParser,
    EmbeddedSoundcloudParser,
  ]
end

Public Instance Methods

element() click to toggle source

The embedded element @return [ArticleJSON::Elements::Embed]

# 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_id() click to toggle source

Extract the video ID from an URL @return [String]

# 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_type() click to toggle source

The type of this embedded element To be implemented by sub classes! @return [Symbol]

# File lib/article_json/import/google_doc/html/embedded_parser.rb, line 27
def embed_type
  raise NotImplementedError
end
tags() click to toggle source

Extract any potential tags, specified in brackets after the URL @return [Array]

# 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