Class: IframeParser

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Parser

#setNext

Constructor Details

#initialize(isForJekyll) ⇒ IframeParser

Returns a new instance of IframeParser.



16
17
18
# File 'lib/Parsers/IframeParser.rb', line 16

def initialize(isForJekyll)
    @isForJekyll = isForJekyll
end

Instance Attribute Details

#isForJekyllObject

Returns the value of attribute isForJekyll.



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

def isForJekyll
  @isForJekyll
end

#nextParserObject

Returns the value of attribute nextParser.



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

def nextParser
  @nextParser
end

#pathPolicyObject

Returns the value of attribute pathPolicy.



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

def pathPolicy
  @pathPolicy
end

Instance Method Details

#parse(paragraph) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/Parsers/IframeParser.rb', line 20

def parse(paragraph)
    if paragraph.type == 'IFRAME'
        if !paragraph.iframe.src.nil? && paragraph.iframe.src != ""
            url = paragraph.iframe.src
        else
            url = "https://medium.com/media/#{paragraph.iframe.id}"
        end

        if !url[/(www\.youtube\.com)/].nil?
            # is youtube
            youtubeURL = URI(URI.decode(url)).query
            params = URI::decode_www_form(youtubeURL).to_h
            
            if !params["image"].nil? && !params["url"].nil?

                fileName = "#{paragraph.name}_#{URI(params["image"]).path.split("/").last}" #21de_default.jpg

                imageURL = params["image"]
                imagePathPolicy = PathPolicy.new(pathPolicy.getAbsolutePath(nil), paragraph.postID)
                absolutePath = imagePathPolicy.getAbsolutePath(fileName)
                title = paragraph.iframe.title
                if  ImageDownloader.download(absolutePath, imageURL)
                    relativePath = "#{pathPolicy.getRelativePath(nil)}/#{imagePathPolicy.getRelativePath(fileName)}"
                    if isForJekyll
                        result = "\r\n[![#{title}](/#{relativePath} \"#{title}\")](#{params["url"]})\r\n"
                    else
                        result = "\r\n[![#{title}](#{relativePath} \"#{title}\")](#{params["url"]})\r\n"
                    end
                else
                    result = "\r\n[#{title}](#{params["url"]})\r\n"
                end
            end
        else
            html = Request.html(Request.URL(url))
            src = html.search('script').first.attribute('src')
            result = nil
            if !src.to_s[/^(https\:\/\/gist\.github\.com)/].nil?
                # is gist
                gist = Request.body(Request.URL(src)).scan(/(document\.write\('){1}(.*)(\)){1}/)[1][1]
                gist.gsub! '\n', ''
                gist.gsub! '\"', '"'
                gist.gsub! '<\/', '</'
                gistHTML = Nokogiri::HTML(gist)
                lang = gistHTML.search('table').first['data-tagsearch-lang'].downcase
                if isForJekyll and lang == "objective-c"
                    lang = "objectivec"
                end
                gistHTML.search('a').each do |a|
                    if a.text == 'view raw'
                        gistRAW = Request.body(Request.URL(a['href']))
                        result = "```#{lang}\n#{gistRAW}\n```"
                    end
                end
            end
        end

        if result.nil?
            "[#{paragraph.iframe.title}](#{url})"
        else
            result
        end
    else
        if !nextParser.nil?
            nextParser.parse(paragraph)
        end
    end
end