ZMediumToMarkdown 1.6.0 → 1.6.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/Helper.rb +8 -3
- data/lib/Parsers/CodeBlockParser.rb +9 -1
- data/lib/Parsers/IMGParser.rb +7 -2
- data/lib/Parsers/IframeParser.rb +3 -3
- data/lib/Parsers/LinkParser.rb +1 -13
- data/lib/Parsers/MarkupStyleRender.rb +1 -3
- data/lib/Post.rb +8 -1
- data/lib/ZMediumFetcher.rb +10 -4
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a289a9d9178f9001eb97db354501e6112331be901a629719c775a28098268cc9
|
4
|
+
data.tar.gz: f116778fbd8a33287c289a905eb9026620f6d601953446cf946858ed3d108576
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c4efcdb20c64941fc5b800c4029088179a65e0298a78a21ee68d6a967738a487395948cade619dd6420e075c882df5744373e7c661e7d47cc92b5a3ac1e8722b
|
7
|
+
data.tar.gz: 133737ccac50e0d5cddac1ea10ef10cb6e46ca972a195160b6451a04f38d6a18171fd62849769ad159bbd227614495fe4a6d7c59b5a0d60892cb0373f44b5b16
|
data/lib/Helper.rb
CHANGED
@@ -77,10 +77,15 @@ class Helper
|
|
77
77
|
end
|
78
78
|
|
79
79
|
def self.createPostInfo(postInfo)
|
80
|
+
|
81
|
+
title = postInfo.title.gsub("[","")
|
82
|
+
title = title.gsub("]","")
|
83
|
+
|
80
84
|
result = "---\n"
|
81
|
-
result += "title: #{
|
85
|
+
result += "title: #{title}\n"
|
82
86
|
result += "author: #{postInfo.creator}\n"
|
83
87
|
result += "date: #{postInfo.firstPublishedAt.strftime('%Y-%m-%dT%H:%M:%S.%LZ')}\n"
|
88
|
+
result += "categories: #{postInfo.collectionName}\n"
|
84
89
|
result += "tags: [#{postInfo.tags.join(",")}]\n"
|
85
90
|
result += "---\n"
|
86
91
|
result += "\r\n"
|
@@ -159,7 +164,7 @@ class Helper
|
|
159
164
|
text += "+-----------------------------------------------------------------------------------+"
|
160
165
|
text += "\r\n"
|
161
166
|
text += "\r\n"
|
162
|
-
text += "| **[View original post on Medium](#{postURL}) - Converted by [ZhgChgLi](https://
|
167
|
+
text += "| **[View original post on Medium](#{postURL}) - Converted by [ZhgChgLi](https://zhgchg.li)/[ZMediumToMarkdown](https://github.com/ZhgChgLi/ZMediumToMarkdown)** |"
|
163
168
|
text += "\r\n"
|
164
169
|
text += "\r\n"
|
165
170
|
text += "+-----------------------------------------------------------------------------------+"
|
@@ -167,4 +172,4 @@ class Helper
|
|
167
172
|
|
168
173
|
text
|
169
174
|
end
|
170
|
-
end
|
175
|
+
end
|
@@ -10,8 +10,16 @@ class CodeBlockParser < Parser
|
|
10
10
|
'CODE_BLOCK'
|
11
11
|
end
|
12
12
|
|
13
|
+
def self.isCodeBlock(paragraph)
|
14
|
+
if paragraph.nil?
|
15
|
+
false
|
16
|
+
else
|
17
|
+
paragraph.type == CodeBlockParser.getTypeString()
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
13
21
|
def parse(paragraph)
|
14
|
-
if
|
22
|
+
if CodeBlockParser.isCodeBlock(paragraph)
|
15
23
|
"```\n#{paragraph.text}\n```"
|
16
24
|
else
|
17
25
|
if !nextParser.nil?
|
data/lib/Parsers/IMGParser.rb
CHANGED
@@ -18,11 +18,16 @@ class IMGParser < Parser
|
|
18
18
|
imagePathPolicy = PathPolicy.new(pathPolicy.getAbsolutePath(nil), paragraph.postID)
|
19
19
|
absolutePath = imagePathPolicy.getAbsolutePath(fileName)
|
20
20
|
|
21
|
+
comment = ""
|
22
|
+
if paragraph.text != ""
|
23
|
+
comment = " \"#{paragraph.text}\""
|
24
|
+
end
|
25
|
+
|
21
26
|
if ImageDownloader.download(absolutePath, imageURL)
|
22
27
|
relativePath = "#{pathPolicy.getRelativePath(nil)}/#{imagePathPolicy.getRelativePath(fileName)}"
|
23
|
-
"![#{paragraph.text}](#{relativePath}
|
28
|
+
"\r\n![#{paragraph.text}](#{relativePath}#{comment})\r\n"
|
24
29
|
else
|
25
|
-
"![#{paragraph.text}](#{imageURL}
|
30
|
+
"\r\n![#{paragraph.text}](#{imageURL}#{comment})\r\n"
|
26
31
|
end
|
27
32
|
else
|
28
33
|
if !nextParser.nil?
|
data/lib/Parsers/IframeParser.rb
CHANGED
@@ -35,9 +35,9 @@ class IframeParser < Parser
|
|
35
35
|
title = paragraph.iframe.title
|
36
36
|
if ImageDownloader.download(absolutePath, imageURL)
|
37
37
|
relativePath = "#{pathPolicy.getRelativePath(nil)}/#{imagePathPolicy.getRelativePath(fileName)}"
|
38
|
-
result = "\n[![#{title}](#{relativePath} \"#{title}\")](#{params["url"]})"
|
38
|
+
result = "\r\n[![#{title}](#{relativePath} \"#{title}\")](#{params["url"]})\r\n"
|
39
39
|
else
|
40
|
-
result = "\n[#{title}](#{params["url"]})"
|
40
|
+
result = "\r\n[#{title}](#{params["url"]})\r\n"
|
41
41
|
end
|
42
42
|
end
|
43
43
|
else
|
@@ -55,7 +55,7 @@ class IframeParser < Parser
|
|
55
55
|
gistHTML.search('a').each do |a|
|
56
56
|
if a.text == 'view raw'
|
57
57
|
gistRAW = Request.body(Request.URL(a['href']))
|
58
|
-
result = "```#{lang}\n#{gistRAW}\n```"
|
58
|
+
result = "```#{lang.downcase}\n#{gistRAW}\n```"
|
59
59
|
end
|
60
60
|
end
|
61
61
|
end
|
data/lib/Parsers/LinkParser.rb
CHANGED
@@ -23,19 +23,7 @@ class LinkParser
|
|
23
23
|
|
24
24
|
postPath = link.split("/").last
|
25
25
|
if !usersPostURLs.find { |usersPostURL| usersPostURL.split("/").last.split("-").last == postPath.split("-").last }.nil?
|
26
|
-
markdownString = markdownString.sub! link, postPath
|
27
|
-
end
|
28
|
-
else
|
29
|
-
if !(link =~ /\A#{URI::regexp(['http', 'https'])}\z/)
|
30
|
-
# medium will give you an relative path if url is medium's post (due to we use html to markdown render)
|
31
|
-
# e.g. /zrealm-ios-dev/visitor-pattern-in-ios-swift-ba5773a7bfea
|
32
|
-
# it's not a vaild url
|
33
|
-
|
34
|
-
# fullfill url from markup attribute
|
35
|
-
match = markupLinks.find{ |markupLink| markupLink.include? link }
|
36
|
-
if !match.nil?
|
37
|
-
markdownString = markdownString.sub! link, match
|
38
|
-
end
|
26
|
+
markdownString = markdownString.sub! link, "#{postPath}"
|
39
27
|
end
|
40
28
|
end
|
41
29
|
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
|
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
|
data/lib/Post.rb
CHANGED
@@ -9,7 +9,7 @@ require 'date'
|
|
9
9
|
class Post
|
10
10
|
|
11
11
|
class PostInfo
|
12
|
-
attr_accessor :title, :tags, :creator, :firstPublishedAt, :latestPublishedAt
|
12
|
+
attr_accessor :title, :tags, :creator, :firstPublishedAt, :latestPublishedAt, :collectionName
|
13
13
|
end
|
14
14
|
|
15
15
|
def self.getPostIDFromPostURLString(postURLString)
|
@@ -68,6 +68,13 @@ class Post
|
|
68
68
|
postInfo.creator = content&.dig(creatorRef, "name")
|
69
69
|
end
|
70
70
|
|
71
|
+
colletionRef = content&.dig("Post:#{postID}", "collection", "__ref")
|
72
|
+
if !colletionRef.nil?
|
73
|
+
postInfo.collectionName = content&.dig(colletionRef, "name")
|
74
|
+
end
|
75
|
+
|
76
|
+
|
77
|
+
|
71
78
|
firstPublishedAt = content&.dig("Post:#{postID}", "firstPublishedAt")
|
72
79
|
if !firstPublishedAt.nil?
|
73
80
|
postInfo.firstPublishedAt = Time.at(0, firstPublishedAt, :millisecond)
|
data/lib/ZMediumFetcher.rb
CHANGED
@@ -26,6 +26,7 @@ require "PathPolicy"
|
|
26
26
|
require "Request"
|
27
27
|
require "Post"
|
28
28
|
require "User"
|
29
|
+
require 'date'
|
29
30
|
|
30
31
|
class ZMediumFetcher
|
31
32
|
|
@@ -204,7 +205,7 @@ class ZMediumFetcher
|
|
204
205
|
|
205
206
|
postPathPolicy = PathPolicy.new(pathPolicy.getAbsolutePath(nil), "posts")
|
206
207
|
|
207
|
-
imagePathPolicy = PathPolicy.new(postPathPolicy.getAbsolutePath(nil), "
|
208
|
+
imagePathPolicy = PathPolicy.new(postPathPolicy.getAbsolutePath(nil), "assets")
|
208
209
|
startParser = buildParser(imagePathPolicy)
|
209
210
|
|
210
211
|
progress.totalPostParagraphsLength = paragraphs.length
|
@@ -212,7 +213,9 @@ class ZMediumFetcher
|
|
212
213
|
progress.message = "Converting Post..."
|
213
214
|
progress.printLog()
|
214
215
|
|
215
|
-
|
216
|
+
postWithDatePath = "#{postInfo.firstPublishedAt.strftime("%Y-%m-%d")}-#{postPath}"
|
217
|
+
|
218
|
+
absolutePath = postPathPolicy.getAbsolutePath("#{postWithDatePath}.md")
|
216
219
|
|
217
220
|
# if markdown file is exists and last modification time is >= latestPublishedAt(last update post time on medium)
|
218
221
|
if File.file?(absolutePath) && File.mtime(absolutePath) >= postInfo.latestPublishedAt
|
@@ -228,8 +231,11 @@ class ZMediumFetcher
|
|
228
231
|
|
229
232
|
index = 0
|
230
233
|
paragraphs.each do |paragraph|
|
231
|
-
|
232
|
-
|
234
|
+
if !(CodeBlockParser.isCodeBlock(paragraph) || PREParser.isPRE(paragraph))
|
235
|
+
markupParser = MarkupParser.new(paragraph)
|
236
|
+
paragraph.text = markupParser.parse()
|
237
|
+
end
|
238
|
+
|
233
239
|
result = startParser.parse(paragraph)
|
234
240
|
|
235
241
|
if !linkParser.nil?
|
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.
|
4
|
+
version: 1.6.3
|
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-
|
11
|
+
date: 2022-06-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: nokogiri
|