Class: LinkParser
- Inherits:
-
Object
- Object
- LinkParser
- Defined in:
- lib/Parsers/LinkParser.rb
Instance Attribute Summary collapse
-
#isForJekyll ⇒ Object
Returns the value of attribute isForJekyll.
-
#usersPostURLs ⇒ Object
Returns the value of attribute usersPostURLs.
Instance Method Summary collapse
-
#initialize ⇒ LinkParser
constructor
A new instance of LinkParser.
- #parse(markdownString) ⇒ Object
Constructor Details
#initialize ⇒ LinkParser
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
#isForJekyll ⇒ Object
Returns the value of attribute isForJekyll.
6 7 8 |
# File 'lib/Parsers/LinkParser.rb', line 6 def isForJekyll @isForJekyll end |
#usersPostURLs ⇒ Object
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 |