Class: IframeParser

Inherits:
Parser show all
Defined in:
lib/Parsers/IframeParser.rb

Constant Summary collapse

GIST_HOST_REGEX =
/^https:\/\/gist\.github\.com/.freeze
EMBEDLY_HOST_REGEX =
/cdn\.embedly\.com/.freeze
TWITTER_URL_REGEX =

Twitter rebranded to X — match both, plus the mobile subdomain. Capture group 1 is the tweet ID (used by parseTwitterEmbed).

/^https:\/\/(?:(?:mobile\.)?twitter|x)\.com\/[^\/]+\/status\/(\d+)/.freeze
YOUTUBE_HOST_REGEX =

Match every YouTube URL form Medium might hand us:

www.youtube.com/watch?v=ID    youtu.be/ID    youtube.com/shorts/ID    m.youtube.com/...
/(?:www\.|m\.)?youtube\.com|youtu\.be/.freeze
VIMEO_URL_REGEX =
/^https?:\/\/(?:www\.|player\.)?vimeo\.com\/(?:video\/)?(\d+)/.freeze
SOUNDCLOUD_URL_REGEX =
/^https?:\/\/(?:www\.)?soundcloud\.com\/[^\/]+\/[^?\/]+/.freeze
SPOTIFY_URL_REGEX =
/^https?:\/\/open\.spotify\.com\/(track|album|episode|playlist|show)\/([A-Za-z0-9]+)/.freeze
WIDGETIC_URL_REGEX =
/^https:\/\/app\.widgetic\.com/.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Parser

#setNext

Constructor Details

#initialize(isForJekyll, skipImages: false) ⇒ IframeParser

When ‘skipImages: true`, renderThumbnailLink emits the remote thumbnail URL directly without calling ImageDownloader.download or touching pathPolicy. Used by –stdout / –list rendering paths.



34
35
36
37
# File 'lib/Parsers/IframeParser.rb', line 34

def initialize(isForJekyll, skipImages: false)
    @isForJekyll = isForJekyll
    @skipImages = skipImages
end

Instance Attribute Details

#isForJekyllObject

Returns the value of attribute isForJekyll.



13
14
15
# File 'lib/Parsers/IframeParser.rb', line 13

def isForJekyll
  @isForJekyll
end

#nextParserObject

Returns the value of attribute nextParser.



13
14
15
# File 'lib/Parsers/IframeParser.rb', line 13

def nextParser
  @nextParser
end

#pathPolicyObject

Returns the value of attribute pathPolicy.



13
14
15
# File 'lib/Parsers/IframeParser.rb', line 13

def pathPolicy
  @pathPolicy
end

Instance Method Details

#parse(paragraph) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/Parsers/IframeParser.rb', line 39

def parse(paragraph)
    return forwardToNext(paragraph) unless paragraph.type == 'IFRAME'
    return unless paragraph.iframe

    url = if paragraph.iframe.src.nil? || paragraph.iframe.src == ""
              "https://medium.com/media/#{paragraph.iframe.id}"
          else
              paragraph.iframe.src
          end

    return parseYoutube(paragraph, url) if url.match?(YOUTUBE_HOST_REGEX)

    # Resolve embedly wrappers up front so we can dispatch on the inner
    # URL without doing an unnecessary HTTP round-trip for hosts we
    # already know how to handle.
    innerURL = unwrapEmbedly(url)

    return parseTwitterEmbed(paragraph, innerURL)         if innerURL.match?(TWITTER_URL_REGEX)
    return nil                                            if innerURL.match?(WIDGETIC_URL_REGEX)
    return parseVimeo(paragraph, url, innerURL)           if innerURL.match?(VIMEO_URL_REGEX)
    return parseSoundCloud(paragraph, innerURL)           if innerURL.match?(SOUNDCLOUD_URL_REGEX)
    return parseSpotify(paragraph, innerURL)              if innerURL.match?(SPOTIFY_URL_REGEX)

    html = Request.html(Request.URL(url))
    return "" unless html

    srcEl = html.search('script').first
    gistSrc = srcEl ? srcEl.attribute('src').to_s : ""

    if gistSrc.match?(GIST_HOST_REGEX)
        parseGist(gistSrc)
    else
        parseOgImageEmbed(paragraph, innerURL)
    end
end