ZMediumToMarkdown 1.6.3 → 1.7.2

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: a289a9d9178f9001eb97db354501e6112331be901a629719c775a28098268cc9
4
- data.tar.gz: f116778fbd8a33287c289a905eb9026620f6d601953446cf946858ed3d108576
3
+ metadata.gz: 2ad7d4aaa279746048959177dd44d856a0ea2bdd5e7abafe0a17bb91c1fa270c
4
+ data.tar.gz: 190cf803221cc22ce226a0d2ce75209a561defde7223b156a373af1e510b730c
5
5
  SHA512:
6
- metadata.gz: c4efcdb20c64941fc5b800c4029088179a65e0298a78a21ee68d6a967738a487395948cade619dd6420e075c882df5744373e7c661e7d47cc92b5a3ac1e8722b
7
- data.tar.gz: 133737ccac50e0d5cddac1ea10ef10cb6e46ca972a195160b6451a04f38d6a18171fd62849769ad159bbd227614495fe4a6d7c59b5a0d60892cb0373f44b5b16
6
+ metadata.gz: 1c38796a2d4f3a3b3e88af2ee3da95af37828a8cc112f10694ee50cba21ed916aeae5bfcde075714fbc51c9b93cdc3595e7cab2672a21bbb7c58ed1bd2ed7d07
7
+ data.tar.gz: a82fbc482dff1902662076964c2eb76f14b1bb69c694257e80ba64755a99d3af38f6a4ff47fa085c0f71cb3c466e6cfe6389f93334ac9775e1bd0f8d49198356
@@ -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,7 +30,11 @@ class IMGParser < Parser
25
30
 
26
31
  if ImageDownloader.download(absolutePath, imageURL)
27
32
  relativePath = "#{pathPolicy.getRelativePath(nil)}/#{imagePathPolicy.getRelativePath(fileName)}"
28
- "\r\n![#{paragraph.text}](#{relativePath}#{comment})\r\n"
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
39
  "\r\n![#{paragraph.text}](#{imageURL}#{comment})\r\n"
31
40
  end
@@ -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,7 +40,11 @@ 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 = "\r\n[![#{title}](#{relativePath} \"#{title}\")](#{params["url"]})\r\n"
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
49
  result = "\r\n[#{title}](#{params["url"]})\r\n"
41
50
  end
@@ -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
@@ -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,17 +296,22 @@ 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
291
303
  progress.message = "Downloading posts..."
292
304
  progress.printLog()
293
305
 
294
- userPathPolicy = PathPolicy.new(pathPolicy.getAbsolutePath(nil), "users/#{username}")
306
+ if isForJekyll
307
+ downloadPathPolicy = pathPolicy
308
+ else
309
+ downloadPathPolicy = PathPolicy.new(pathPolicy.getAbsolutePath(nil), "users/#{username}")
310
+ end
311
+
295
312
  index = 0
296
313
  postURLS.each do |postURL|
297
- downloadPost(postURL, userPathPolicy)
314
+ downloadPost(postURL, downloadPathPolicy)
298
315
 
299
316
  index += 1
300
317
  progress.currentPostIndex = index
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.3
4
+ version: 1.7.2
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-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nokogiri