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, isForJekyll) ⇒ LinkParser

Returns a new instance of LinkParser.



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

def initialize(usersPostURLs, isForJekyll)
    @usersPostURLs = usersPostURLs
    @isForJekyll = isForJekyll
end

Instance Attribute Details

#isForJekyllObject

Returns the value of attribute isForJekyll.



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

def isForJekyll
  @isForJekyll
end

#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



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
# File 'lib/Parsers/LinkParser.rb', line 13

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

                    if isForJekyll
                        postPath = link.split("/").last.split("-").last
                    else
                        postPath = link.split("/").last
                    end
                    
                    if !usersPostURLs.find { |usersPostURL| usersPostURL.split("/").last.split("-").last == postPath.split("-").last }.nil?
                        if isForJekyll
                            markdownString = markdownString.sub! link, "../#{postPath}"
                        else
                            markdownString = markdownString.sub! link, "#{postPath}"
                        end
                    end
                end
            end
        end
    end

    markdownString
end