Class: IframeParser
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
-
#isForJekyll ⇒ Object
Returns the value of attribute isForJekyll.
-
#nextParser ⇒ Object
Returns the value of attribute nextParser.
-
#pathPolicy ⇒ Object
Returns the value of attribute pathPolicy.
Instance Method Summary collapse
-
#initialize(isForJekyll) ⇒ IframeParser
constructor
A new instance of IframeParser.
- #parse(paragraph) ⇒ Object
Methods inherited from Parser
Constructor Details
#initialize(isForJekyll) ⇒ IframeParser
Returns a new instance of IframeParser.
31 32 33 |
# File 'lib/Parsers/IframeParser.rb', line 31 def initialize(isForJekyll) @isForJekyll = isForJekyll end |
Instance Attribute Details
#isForJekyll ⇒ Object
Returns the value of attribute isForJekyll.
13 14 15 |
# File 'lib/Parsers/IframeParser.rb', line 13 def isForJekyll @isForJekyll end |
#nextParser ⇒ Object
Returns the value of attribute nextParser.
13 14 15 |
# File 'lib/Parsers/IframeParser.rb', line 13 def nextParser @nextParser end |
#pathPolicy ⇒ Object
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
35 36 37 38 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 |
# File 'lib/Parsers/IframeParser.rb', line 35 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 |