ZMediumToMarkdown 1.6.2 → 1.7.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 57ebbd86d072c9c43a5baef02031561323e9e7f6857e639aecc754de5741c543
4
- data.tar.gz: 03bde3f39434b21c7d96380d05dbc9ccc7096f30ab97aade0e6838165e28de3e
3
+ metadata.gz: fa026cbea9d117e46a188297165e98602f40a32862699ff9cfb28da67002bfd1
4
+ data.tar.gz: 06a512791cef282c8fd5ccd1d587dc851e6223f5ca7f9659a846c0800aecc618
5
5
  SHA512:
6
- metadata.gz: d108f648afe9eb0f90231dc771e22fd6f3f15d820f6d19aa941d23b48b2416c672350b6353af85c58dd13ccc1c3faa194b55f622476d09ea8bc84cccff6ba6ac
7
- data.tar.gz: e484a2d51bc9ec006dc5511e2586d29eaab102bf778a030240ee92941b4fdfd9a726fbc5542677af7dbd8bb192e6c5d162b30b8c8b5001d166d3561edabdb9ec
6
+ metadata.gz: 4bb69819a9591b7fa9056cbf12a97b26148e98a5e8e4d3a9155ff6e77f4416e5755326b83ac562d30a3b961b49db6501c9ce729687dec1d6e7d54ab0ccec3b10
7
+ data.tar.gz: 13479a388652bfa137c01a6d02300f30fe0b66402aca6b240b29a4d6bd653349b066b3e478420224ef07bb15eb9052a46a73e76d06d83bc494a6ef49ac8dc020
@@ -14,18 +14,35 @@ class Main
14
14
  ARGV << '-h' if ARGV.empty?
15
15
 
16
16
  filePath = ENV['PWD'] || ::Dir.pwd
17
- outputFilePath = PathPolicy.new(filePath, "Output")
18
17
 
19
18
  OptionParser.new do |opts|
20
19
  opts.banner = "Usage: ZMediumFetcher [options]"
21
20
 
22
21
  opts.on('-uUSERNAME', '--username=USERNAME', 'Downloading all posts from user') do |username|
22
+ outputFilePath = PathPolicy.new(filePath, "Output")
23
23
  fetcher.downloadPostsByUsername(username, outputFilePath)
24
24
 
25
25
  Helper.printNewVersionMessageIfExists()
26
26
  end
27
27
 
28
28
  opts.on('-pPOST_URL', '--postURL=POST_URL', 'Downloading single post') do |postURL|
29
+ outputFilePath = PathPolicy.new(filePath, "Output")
30
+ fetcher.downloadPost(postURL, outputFilePath)
31
+
32
+ Helper.printNewVersionMessageIfExists()
33
+ end
34
+
35
+ opts.on('-jUSERNAME', '--jekyllUsername=USERNAME', 'Downloading all posts from user with Jekyll friendly') do |username|
36
+ outputFilePath = PathPolicy.new(filePath, "/")
37
+ fetcher.isForJekyll = true
38
+ fetcher.downloadPostsByUsername(username, outputFilePath)
39
+
40
+ Helper.printNewVersionMessageIfExists()
41
+ end
42
+
43
+ opts.on('-kpPOST_URL', '--jekyllPostURL=POST_URL', 'Downloading single post with Jekyll friendly') do |postURL|
44
+ outputFilePath = PathPolicy.new(filePath, "/")
45
+ fetcher.isForJekyll = true
29
46
  fetcher.downloadPost(postURL, outputFilePath)
30
47
 
31
48
  Helper.printNewVersionMessageIfExists()
@@ -7,7 +7,12 @@ require 'ImageDownloader'
7
7
  require 'PathPolicy'
8
8
 
9
9
  class IMGParser < Parser
10
- attr_accessor :nextParser, :pathPolicy
10
+ attr_accessor :nextParser, :pathPolicy, :isForJekyll
11
+
12
+ def initialize(isForJekyll)
13
+ @isForJekyll = isForJekyll
14
+ end
15
+
11
16
  def parse(paragraph)
12
17
  if paragraph.type == 'IMG'
13
18
 
@@ -25,9 +30,13 @@ class IMGParser < Parser
25
30
 
26
31
  if ImageDownloader.download(absolutePath, imageURL)
27
32
  relativePath = "#{pathPolicy.getRelativePath(nil)}/#{imagePathPolicy.getRelativePath(fileName)}"
28
- "![#{paragraph.text}](/#{relativePath}#{comment})"
33
+ if isForJekyll
34
+ "\r\n![#{paragraph.text}](/#{relativePath}#{comment})\r\n"
35
+ else
36
+ "\r\n![#{paragraph.text}](#{relativePath}#{comment})\r\n"
37
+ end
29
38
  else
30
- "![#{paragraph.text}](#{imageURL}#{comment})"
39
+ "\r\n![#{paragraph.text}](#{imageURL}#{comment})\r\n"
31
40
  end
32
41
  else
33
42
  if !nextParser.nil?
@@ -11,7 +11,12 @@ require 'ImageDownloader'
11
11
  require 'PathPolicy'
12
12
 
13
13
  class IframeParser < Parser
14
- attr_accessor :nextParser, :pathPolicy
14
+ attr_accessor :nextParser, :pathPolicy, :isForJekyll
15
+
16
+ def initialize(isForJekyll)
17
+ @isForJekyll = isForJekyll
18
+ end
19
+
15
20
  def parse(paragraph)
16
21
  if paragraph.type == 'IFRAME'
17
22
  if !paragraph.iframe.src.nil? && paragraph.iframe.src != ""
@@ -35,9 +40,13 @@ class IframeParser < Parser
35
40
  title = paragraph.iframe.title
36
41
  if ImageDownloader.download(absolutePath, imageURL)
37
42
  relativePath = "#{pathPolicy.getRelativePath(nil)}/#{imagePathPolicy.getRelativePath(fileName)}"
38
- result = "\n[![#{title}](#{relativePath} \"#{title}\")](#{params["url"]})"
43
+ if isForJekyll
44
+ result = "\r\n[![#{title}](/#{relativePath} \"#{title}\")](#{params["url"]})\r\n"
45
+ else
46
+ result = "\r\n[![#{title}](#{relativePath} \"#{title}\")](#{params["url"]})\r\n"
47
+ end
39
48
  else
40
- result = "\n[#{title}](#{params["url"]})"
49
+ result = "\r\n[#{title}](#{params["url"]})\r\n"
41
50
  end
42
51
  end
43
52
  else
@@ -3,10 +3,11 @@ $lib = File.expand_path('../', File.dirname(__FILE__))
3
3
  require 'Models/Paragraph'
4
4
 
5
5
  class LinkParser
6
- attr_accessor :usersPostURLs
6
+ attr_accessor :usersPostURLs, :isForJekyll
7
7
 
8
- def initialize(usersPostURLs)
8
+ def initialize(usersPostURLs, isForJekyll)
9
9
  @usersPostURLs = usersPostURLs
10
+ @isForJekyll = isForJekyll
10
11
  end
11
12
 
12
13
  def parse(markdownString, markupLinks)
@@ -21,9 +22,18 @@ class LinkParser
21
22
  # if have provide user's post urls
22
23
  # find & replace medium url to local post url if matched
23
24
 
24
- postPath = link.split("/").last
25
+ if isForJekyll
26
+ postPath = link.split("/").last.split("-").last
27
+ else
28
+ postPath = link.split("/").last
29
+ end
30
+
25
31
  if !usersPostURLs.find { |usersPostURL| usersPostURL.split("/").last.split("-").last == postPath.split("-").last }.nil?
26
- markdownString = markdownString.sub! link, "../#{postPath}"
32
+ if isForJekyll
33
+ markdownString = markdownString.sub! link, "../#{postPath}"
34
+ else
35
+ markdownString = markdownString.sub! link, "#{postPath}"
36
+ end
27
37
  end
28
38
  end
29
39
  end
@@ -32,12 +32,10 @@ class MarkupStyleRender
32
32
  chars = {}
33
33
  index = 0
34
34
 
35
- emojiRegex = /[\u{203C}\u{2049}\u{20E3}\u{2122}\u{2139}\u{2194}-\u{2199}\u{21A9}-\u{21AA}\u{231A}-\u{231B}\u{23E9}-\u{23EC}\u{23F0}\u{23F3}\u{24C2}\u{25AA}-\u{25AB}\u{25B6}\u{25C0}\u{25FB}-\u{25FE}\u{2600}-\u{2601}\u{260E}\u{2611}\u{2614}-\u{2615}\u{261D}\u{263A}\u{2648}-\u{2653}\u{2660}\u{2663}\u{2665}-\u{2666}\u{2668}\u{267B}\u{267F}\u{2693}\u{26A0}-\u{26A1}\u{26AA}-\u{26AB}\u{26BD}-\u{26BE}\u{26C4}-\u{26C5}\u{26CE}\u{26D4}\u{26EA}\u{26F2}-\u{26F3}\u{26F5}\u{26FA}\u{26FD}\u{2702}\u{2705}\u{2708}-\u{270C}\u{270F}\u{2712}\u{2714}\u{2716}\u{2728}\u{2733}-\u{2734}\u{2744}\u{2747}\u{274C}\u{274E}\u{2753}-\u{2755}\u{2757}\u{2764}\u{2795}-\u{2797}\u{27A1}\u{27B0}\u{2934}-\u{2935}\u{2B05}-\u{2B07}\u{2B1B}-\u{2B1C}\u{2B50}\u{2B55}\u{3030}\u{303D}\u{3297}\u{3299}\u{1F004}\u{1F0CF}\u{1F170}-\u{1F171}\u{1F17E}-\u{1F17F}\u{1F18E}\u{1F191}-\u{1F19A}\u{1F1E7}-\u{1F1EC}\u{1F1EE}-\u{1F1F0}\u{1F1F3}\u{1F1F5}\u{1F1F7}-\u{1F1FA}\u{1F201}-\u{1F202}\u{1F21A}\u{1F22F}\u{1F232}-\u{1F23A}\u{1F250}-\u{1F251}\u{1F300}-\u{1F320}\u{1F330}-\u{1F335}\u{1F337}-\u{1F37C}\u{1F380}-\u{1F393}\u{1F3A0}-\u{1F3C4}\u{1F3C6}-\u{1F3CA}\u{1F3E0}-\u{1F3F0}\u{1F400}-\u{1F43E}\u{1F440}\u{1F442}-\u{1F4F7}\u{1F4F9}-\u{1F4FC}\u{1F500}-\u{1F507}\u{1F509}-\u{1F53D}\u{1F550}-\u{1F567}\u{1F5FB}-\u{1F640}\u{1F645}-\u{1F64F}\u{1F680}-\u{1F68A}]/
36
- excludesEmojis = ["⚠"]
37
35
  paragraph.text.each_char do |char|
38
36
  chars[index] = TextChar.new([char], "Text")
39
37
  index += 1
40
- if char =~ emojiRegex && !excludesEmojis.include?(char)
38
+ if char.bytes.length >= 4
41
39
  # some emoji need more space (in Medium)
42
40
  chars[index] = TextChar.new([], "Text")
43
41
  index += 1
@@ -30,7 +30,7 @@ require 'date'
30
30
 
31
31
  class ZMediumFetcher
32
32
 
33
- attr_accessor :progress, :linkParser
33
+ attr_accessor :progress, :linkParser, :isForJekyll
34
34
 
35
35
  class Progress
36
36
  attr_accessor :username, :postPath, :currentPostIndex, :totalPostsLength, :currentPostParagraphIndex, :totalPostParagraphsLength, :message
@@ -71,7 +71,8 @@ class ZMediumFetcher
71
71
 
72
72
  def initialize
73
73
  @progress = Progress.new()
74
- @linkParser = LinkParser.new(nil)
74
+ @linkParser = LinkParser.new(nil, false)
75
+ @isForJekyll = false
75
76
  end
76
77
 
77
78
  def buildParser(imagePathPolicy)
@@ -92,10 +93,10 @@ class ZMediumFetcher
92
93
  oliParser.setNext(mixtapeembedParser)
93
94
  pqParser = PQParser.new()
94
95
  mixtapeembedParser.setNext(pqParser)
95
- iframeParser = IframeParser.new()
96
+ iframeParser = IframeParser.new(isForJekyll)
96
97
  iframeParser.pathPolicy = imagePathPolicy
97
98
  pqParser.setNext(iframeParser)
98
- imgParser = IMGParser.new()
99
+ imgParser = IMGParser.new(isForJekyll)
99
100
  imgParser.pathPolicy = imagePathPolicy
100
101
  iframeParser.setNext(imgParser)
101
102
  bqParser = BQParser.new()
@@ -113,7 +114,12 @@ class ZMediumFetcher
113
114
 
114
115
  def downloadPost(postURL, pathPolicy)
115
116
  postID = Post.getPostIDFromPostURLString(postURL)
116
- postPath = Post.getPostPathFromPostURLString(postURL)
117
+
118
+ if isForJekyll
119
+ postPath = postID # use only post id is more friendly for url seo
120
+ else
121
+ postPath = Post.getPostPathFromPostURLString(postURL)
122
+ end
117
123
 
118
124
  progress.postPath = postPath
119
125
  progress.message = "Downloading Post..."
@@ -203,9 +209,14 @@ class ZMediumFetcher
203
209
  previousParagraph = paragraph
204
210
  end
205
211
 
206
- postPathPolicy = PathPolicy.new(pathPolicy.getAbsolutePath(nil), "posts")
207
-
208
- imagePathPolicy = PathPolicy.new(postPathPolicy.getAbsolutePath(nil), "assets")
212
+ if isForJekyll
213
+ postPathPolicy = PathPolicy.new(pathPolicy.getAbsolutePath(nil), "_posts")
214
+ imagePathPolicy = PathPolicy.new(pathPolicy.getAbsolutePath(nil), "assets")
215
+ else
216
+ postPathPolicy = PathPolicy.new(pathPolicy.getAbsolutePath(nil), "posts")
217
+ imagePathPolicy = PathPolicy.new(postPathPolicy.getAbsolutePath(nil), "assets")
218
+ end
219
+
209
220
  startParser = buildParser(imagePathPolicy)
210
221
 
211
222
  progress.totalPostParagraphsLength = paragraphs.length
@@ -231,6 +242,7 @@ class ZMediumFetcher
231
242
 
232
243
  index = 0
233
244
  paragraphs.each do |paragraph|
245
+
234
246
  if !(CodeBlockParser.isCodeBlock(paragraph) || PREParser.isPRE(paragraph))
235
247
  markupParser = MarkupParser.new(paragraph)
236
248
  paragraph.text = markupParser.parse()
@@ -284,7 +296,7 @@ class ZMediumFetcher
284
296
  nextID = postPageInfo["nextID"]
285
297
  end while !nextID.nil?
286
298
 
287
- @linkParser = LinkParser.new(postURLS)
299
+ @linkParser = LinkParser.new(postURLS, isForJekyll)
288
300
 
289
301
  progress.totalPostsLength = postURLS.length
290
302
  progress.currentPostIndex = 0
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ZMediumToMarkdown
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.6.2
4
+ version: 1.7.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - ZhgChgLi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-06-08 00:00:00.000000000 Z
11
+ date: 2022-06-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nokogiri