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
# 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
                end
            end
        end
    end

    markdownString
end