Class: LinkParser

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLinkParser

Returns a new instance of LinkParser.



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

def initialize()
    @usersPostURLs = nil
    @isForJekyll = false
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) ⇒ 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
45
46
47
48
49
50
51
52
53
# File 'lib/Parsers/LinkParser.rb', line 13

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

        matchLinks.each do |matchLink|
            link = matchLink[0]
            linkMarkdown = "(#{link})"
            newLinkMarkdown = linkMarkdown

            if isForJekyll
                newLinkMarkdown = "(#{link}){:target=\"_blank\"}"
            end
            

            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
                        newLinkMarkdown = "(../#{postPath})"
                    else
                        newLinkMarkdown = "(#{postPath})"
                    end
                end
            end

            if linkMarkdown != newLinkMarkdown
                markdownString = markdownString.sub! linkMarkdown, newLinkMarkdown
            end
        end
    end

    markdownString
end