Skip to content

Commit

Permalink
optimize parser
Browse files Browse the repository at this point in the history
  • Loading branch information
zhgchgli0718 committed Jun 8, 2022
1 parent b6ebc7b commit 95b43a8
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 20 deletions.
2 changes: 1 addition & 1 deletion ZMediumToMarkdown.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Gem::Specification.new do |gem|
gem.files = Dir['lib/**/*.*']
gem.executables = ['ZMediumToMarkdown']
gem.name = 'ZMediumToMarkdown'
gem.version = '1.6.1'
gem.version = '1.6.2'

gem.license = "MIT"

Expand Down
2 changes: 1 addition & 1 deletion ZMediumToMarkdown_Github.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Gem::Specification.new do |gem|
gem.files = Dir['lib/**/*.*']
gem.executables = ['ZMediumToMarkdown']
gem.name = 'zmediumtomarkdown'
gem.version = '1.6.1'
gem.version = '1.6.2'

gem.license = "MIT"

Expand Down
4 changes: 2 additions & 2 deletions lib/Helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,12 @@ def self.createWatermark(postURL)
text += "+-----------------------------------------------------------------------------------+"
text += "\r\n"
text += "\r\n"
text += "| **[View original post on Medium](#{postURL}) - Converted by [ZhgChgLi](https://blog.zhgchg.li)/[ZMediumToMarkdown](https://github.com/ZhgChgLi/ZMediumToMarkdown)** |"
text += "| **[View original post on Medium](#{postURL}) - Converted by [ZhgChgLi](https://zhgchg.li)/[ZMediumToMarkdown](https://github.com/ZhgChgLi/ZMediumToMarkdown)** |"
text += "\r\n"
text += "\r\n"
text += "+-----------------------------------------------------------------------------------+"
text += "\r\n"

text
end
end
end
10 changes: 9 additions & 1 deletion lib/Parsers/CodeBlockParser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,16 @@ def self.getTypeString()
'CODE_BLOCK'
end

def self.isCodeBlock(paragraph)
if paragraph.nil?
false
else
paragraph.type == CodeBlockParser.getTypeString()
end
end

def parse(paragraph)
if paragraph.type == CodeBlockParser.getTypeString()
if CodeBlockParser.isCodeBlock(paragraph)
"```\n#{paragraph.text}\n```"
else
if !nextParser.nil?
Expand Down
14 changes: 1 addition & 13 deletions lib/Parsers/LinkParser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,7 @@ def parse(markdownString, markupLinks)

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
markdownString = markdownString.sub! link, "../#{postPath}"
end
end
end
Expand Down
7 changes: 5 additions & 2 deletions lib/ZMediumFetcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,11 @@ def downloadPost(postURL, pathPolicy)

index = 0
paragraphs.each do |paragraph|
markupParser = MarkupParser.new(paragraph)
paragraph.text = markupParser.parse()
if !(CodeBlockParser.isCodeBlock(paragraph) || PREParser.isPRE(paragraph))
markupParser = MarkupParser.new(paragraph)
paragraph.text = markupParser.parse()
end

result = startParser.parse(paragraph)

if !linkParser.nil?
Expand Down

0 comments on commit 95b43a8

Please sign in to comment.