Class: LinkParser

Inherits:
Object
  • Object
show all
Defined in:
lib/Parsers/LinkParser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(usersPostURLs) ⇒ LinkParser

Returns a new instance of LinkParser.



8
9
10
# File 'lib/Parsers/LinkParser.rb', line 8

def initialize(usersPostURLs)
    @usersPostURLs = usersPostURLs
end

Instance Attribute Details

#usersPostURLsObject

Returns the value of attribute usersPostURLs.



6
7
8
# File 'lib/Parsers/LinkParser.rb', line 6

def usersPostURLs
  @usersPostURLs
end

Instance Method Details

#parse(markdownString, markupLinks) ⇒ Object



12
13
14
15
16
17
18
19
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
# File 'lib/Parsers/LinkParser.rb', line 12

def parse(markdownString, markupLinks)
    if !markupLinks.nil?
        matchLinks = markdownString.scan(/\[[^\]]*\]\(([^\)]*)\)/)
        if !matchLinks.nil?

            matchLinks.each do |matchLink|
                link = matchLink[0]

                if !usersPostURLs.nil?
                    # if have provide user's post urls
                    # find & replace medium url to local post url if matched

                    postPath = link.split("/").last
                    if !usersPostURLs.find { |usersPostURL| usersPostURL.split("/").last.split("-").last == postPath.split("-").last }.nil?
                        markdownString = markdownString.sub! link, postPath
                    end
                else
                    if !(link =~ /\A#{URI::regexp(['http', 'https'])}\z/)
                        # medium will give you an relative path if url is medium's post (due to we use html to markdown render)
                        # e.g. /zrealm-ios-dev/visitor-pattern-in-ios-swift-ba5773a7bfea
                        # it's not a vaild url

                        # fullfill url from markup attribute
                        match = markupLinks.find{ |markupLink| markupLink.include? link }
                        if !match.nil?
                            markdownString = markdownString.sub! link, match
                        end
                    end
                end
            end
        end
    end

    markdownString
end