ZMediumToMarkdown 1.9.0 → 1.9.3

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: bc5867264a6441b236d54340f34e507ef4308f99b6908f21fd975a9061c81483
4
- data.tar.gz: 00d61d0b23a23afe0e445f5a6d98533dd41a02cc41e6ea55168a12620860203f
3
+ metadata.gz: 7f0fc52b06f84a2ed70165d88f47432ec7e7eb3a139f3adb91ce1405b5cf69bc
4
+ data.tar.gz: c57e9ba90accc6f4654176a67851d1eae97e87109e66cf14862f7b3c84f636a3
5
5
  SHA512:
6
- metadata.gz: 23e6f4244d5c291872bcfcedfe961b30e377638e812dade41a9ade68c9e83d66dfe7e1061e2131d106b2605f61014cacc632e4a6984c09b1089e458d0d88cfda
7
- data.tar.gz: b5577f2dd3cec3ce63f15e531b0b9a0574ff74fef8981d21d32badac236a562d1a4ad3cf14757aee8073da9877119b7f3fe64b5ef4a37abf94cbcbcdc1f409f1
6
+ metadata.gz: 300208c7b673ccdb46c27b1871ad15b4cf14baa1a5d33260ff779932c4684b7887e03bb4c95c7b623cca302caab85d81b199b7de04e40408f3989d6cc3e86df8
7
+ data.tar.gz: 6c2bec3f95a1da0e82a86ef917e62fcdc5e880a979821dc2c13dfbe8fae5bc329e38fe8566bf456be9e839ab48904d28ca3b2f939a2057ebcff16979ca5a937d
@@ -32,12 +32,12 @@ class IMGParser < Parser
32
32
  if ImageDownloader.download(absolutePath, imageURL)
33
33
  relativePath = "#{pathPolicy.getRelativePath(nil)}/#{imagePathPolicy.getRelativePath(fileName)}"
34
34
  if isForJekyll
35
- result = "\r\n![#{paragraph.orgTextWithEscape}](/#{relativePath}#{alt})\r\n"
35
+ result = "\r\n\r\n![#{paragraph.orgTextWithEscape}](/#{relativePath}#{alt})\r\n\r\n"
36
36
  else
37
- result = "\r\n![#{paragraph.orgTextWithEscape}](#{relativePath}#{alt})\r\n"
37
+ result = "\r\n\r\n![#{paragraph.orgTextWithEscape}](#{relativePath}#{alt})\r\n\r\n"
38
38
  end
39
39
  else
40
- result = "\r\n![#{paragraph.orgTextWithEscape}](#{imageURL}#{alt})\r\n"
40
+ result = "\r\n\r\n![#{paragraph.orgTextWithEscape}](#{imageURL}#{alt})\r\n\r\n"
41
41
  end
42
42
 
43
43
  if paragraph.text != ""
@@ -6,7 +6,7 @@ require "Request"
6
6
  require "Parsers/Parser"
7
7
  require 'Models/Paragraph'
8
8
  require 'nokogiri'
9
-
9
+ require 'Helper'
10
10
  require 'ImageDownloader'
11
11
  require 'PathPolicy'
12
12
 
@@ -19,12 +19,15 @@ class IframeParser < Parser
19
19
 
20
20
  def parse(paragraph)
21
21
  if paragraph.type == 'IFRAME'
22
+
22
23
  if !paragraph.iframe.src.nil? && paragraph.iframe.src != ""
23
24
  url = paragraph.iframe.src
24
25
  else
25
26
  url = "https://medium.com/media/#{paragraph.iframe.id}"
26
27
  end
27
28
 
29
+ result = "[#{paragraph.iframe.title}](#{url})"
30
+
28
31
  if !url[/(www\.youtube\.com)/].nil?
29
32
  # is youtube
30
33
  youtubeURL = URI(URI.decode(url)).query
@@ -38,12 +41,16 @@ class IframeParser < Parser
38
41
  imagePathPolicy = PathPolicy.new(pathPolicy.getAbsolutePath(nil), paragraph.postID)
39
42
  absolutePath = imagePathPolicy.getAbsolutePath(fileName)
40
43
  title = paragraph.iframe.title
44
+ if title.nil? or title == ""
45
+ title = "Youtube"
46
+ end
47
+
41
48
  if ImageDownloader.download(absolutePath, imageURL)
42
49
  relativePath = "#{pathPolicy.getRelativePath(nil)}/#{imagePathPolicy.getRelativePath(fileName)}"
43
50
  if isForJekyll
44
- result = "\r\n[![#{title}](/#{relativePath} \"#{title}\")](#{params["url"]})\r\n"
51
+ result = "\r\n\r\n[![#{title}](/#{relativePath} \"#{title}\")](#{params["url"]})\r\n\r\n"
45
52
  else
46
- result = "\r\n[![#{title}](#{relativePath} \"#{title}\")](#{params["url"]})\r\n"
53
+ result = "\r\n\r\n[![#{title}](#{relativePath} \"#{title}\")](#{params["url"]})\r\n\r\n"
47
54
  end
48
55
  else
49
56
  result = "\r\n[#{title}](#{params["url"]})\r\n"
@@ -71,18 +78,29 @@ class IframeParser < Parser
71
78
  end
72
79
  end
73
80
  else
74
- ogImageURL = Helper.fetchOGImage(url)
81
+ ogURL = url
82
+ if !url[/(cdn\.embedly\.com)/].nil?
83
+ params = URI::decode_www_form(URI(URI.decode(url)).query).to_h
84
+ if !params["url"].nil?
85
+ ogURL = params["url"]
86
+ end
87
+ end
88
+ ogImageURL = Helper.fetchOGImage(ogURL)
89
+
90
+ title = paragraph.iframe.title
91
+ if title.nil? or title == ""
92
+ title = Helper.escapeMarkdown(ogURL)
93
+ end
94
+
75
95
  if !ogImageURL.nil?
76
- result = "\r\n[![#{paragraph.iframe.title}](#{ogImageURL} \"#{paragraph.iframe.title}\")](#{url})\r\n"
96
+ result = "\r\n\r\n[![#{title}](#{ogImageURL} \"#{title}\")](#{ogURL})\r\n\r\n"
97
+ else
98
+ result = "[#{title}](#{ogURL})"
77
99
  end
78
100
  end
79
101
  end
80
102
 
81
- if result.nil?
82
- "[#{paragraph.iframe.title}](#{url})"
83
- else
84
- result
85
- end
103
+ result
86
104
  else
87
105
  if !nextParser.nil?
88
106
  nextParser.parse(paragraph)
@@ -11,7 +11,7 @@ class MIXTAPEEMBEDParser < Parser
11
11
  if !paragraph.mixtapeMetadata.nil? && !paragraph.mixtapeMetadata.href.nil?
12
12
  ogImageURL = Helper.fetchOGImage(paragraph.mixtapeMetadata.href)
13
13
  if !ogImageURL.nil?
14
- "\r\n[![#{paragraph.orgTextWithEscape}](#{ogImageURL} \"#{paragraph.orgTextWithEscape}\")](#{paragraph.mixtapeMetadata.href})\r\n"
14
+ "\r\n\r\n[![#{paragraph.orgTextWithEscape}](#{ogImageURL} \"#{paragraph.orgTextWithEscape}\")](#{paragraph.mixtapeMetadata.href})\r\n\r\n"
15
15
  else
16
16
  "\n[#{paragraph.orgTextWithEscape}](#{paragraph.mixtapeMetadata.href})"
17
17
  end
@@ -16,14 +16,13 @@ class MarkupStyleRender
16
16
  end
17
17
 
18
18
  class TagChar < TextChar
19
- attr_accessor :sort, :startIndex, :endIndex, :startChars, :endChars, :isCodeBlock
20
- def initialize(sort, startIndex, endIndex, startChars, endChars, isCodeBlock = false)
19
+ attr_accessor :sort, :startIndex, :endIndex, :startChars, :endChars
20
+ def initialize(sort, startIndex, endIndex, startChars, endChars)
21
21
  @sort = sort
22
22
  @startIndex = startIndex
23
23
  @endIndex = endIndex - 1
24
24
  @startChars = TextChar.new(startChars.chars, 'TagStart')
25
25
  @endChars = TextChar.new(endChars.chars, 'TagEnd')
26
- @isCodeBlock = isCodeBlock
27
26
  end
28
27
  end
29
28
 
@@ -127,6 +126,37 @@ class MarkupStyleRender
127
126
  end
128
127
  end
129
128
 
129
+ # remove style in codeblock e.g. `hello world **bold**` (markdown not allow style in codeblock)
130
+
131
+ while true
132
+
133
+ hasExcute = false
134
+ inCodeBlock = false
135
+ index = 0
136
+
137
+ chars.each do |char|
138
+ if char.chars.join() == "`"
139
+ if char.type == "TagStart"
140
+ inCodeBlock = true
141
+ elsif char.type == "TagEnd"
142
+ inCodeBlock = false
143
+ end
144
+ elsif inCodeBlock
145
+ if char.type == "TagStart" or char.type == "TagEnd"
146
+ chars.delete_at(index)
147
+ hasExcute = true
148
+ break
149
+ end
150
+ end
151
+
152
+ index += 1
153
+ end
154
+
155
+ if !hasExcute
156
+ break
157
+ end
158
+ end
159
+
130
160
  chars
131
161
  end
132
162
 
@@ -141,7 +171,7 @@ class MarkupStyleRender
141
171
  if markup.type == "EM"
142
172
  tag = TagChar.new(2, markup.start, markup.end, "_", "_")
143
173
  elsif markup.type == "CODE"
144
- tag = TagChar.new(3, markup.start, markup.end, "`", "`", true)
174
+ tag = TagChar.new(3, markup.start, markup.end, "`", "`")
145
175
  elsif markup.type == "STRONG"
146
176
  tag = TagChar.new(2, markup.start, markup.end, "**", "**")
147
177
  elsif markup.type == "A"
@@ -167,9 +197,6 @@ class MarkupStyleRender
167
197
  response = []
168
198
  stack = []
169
199
 
170
- # markdown unsupoort style in code block
171
- inCodeBlock = false
172
-
173
200
  chars.each do |index, char|
174
201
 
175
202
  if char.chars.join() == "\n"
@@ -189,18 +216,13 @@ class MarkupStyleRender
189
216
  startTags = tags.select { |tag| tag.startIndex == index }.sort_by(&:sort)
190
217
  if !startTags.nil?
191
218
  startTags.each do |tag|
192
- if inCodeBlock == false
193
- response.append(tag.startChars)
194
- stack.append(tag)
195
- end
196
- if tag.isCodeBlock
197
- inCodeBlock = true
198
- end
219
+ response.append(tag.startChars)
220
+ stack.append(tag)
199
221
  end
200
222
  end
201
223
 
202
224
  if char.chars.join() != "\n"
203
- if inCodeBlock
225
+ if !stack.select { |tag| tag.startChars.chars.join() == "`" }.nil?
204
226
  # is in code block
205
227
  response.append(char)
206
228
  else
@@ -223,17 +245,9 @@ class MarkupStyleRender
223
245
  # as expected
224
246
  endTags.delete_at(stackTagInEndTagsIndex)
225
247
  else
226
- if inCodeBlock == false or stackTag.isCodeBlock == true
227
- mismatchTags.append(stackTag)
228
- end
229
- end
230
- if inCodeBlock == false or stackTag.isCodeBlock == true
231
- response.append(stackTag.endChars)
232
- end
233
-
234
- if stackTag.isCodeBlock
235
- inCodeBlock = false
248
+ mismatchTags.append(stackTag)
236
249
  end
250
+ response.append(stackTag.endChars)
237
251
  end
238
252
 
239
253
  while mismatchTags.length > 0
@@ -4,7 +4,7 @@ require "Parsers/Parser"
4
4
  require 'Models/Paragraph'
5
5
 
6
6
  class OLIParser < Parser
7
- attr_accessor :nextParser, :oliIndex
7
+ attr_accessor :nextParser
8
8
 
9
9
  def self.isOLI(paragraph)
10
10
  if paragraph.nil?
@@ -16,7 +16,7 @@ class OLIParser < Parser
16
16
 
17
17
  def parse(paragraph)
18
18
  if OLIParser.isOLI(paragraph)
19
- "#{oliIndex}. #{paragraph.text}"
19
+ "#{paragraph.oliIndex}. #{paragraph.text}"
20
20
  else
21
21
  if !nextParser.nil?
22
22
  nextParser.parse(paragraph)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ZMediumToMarkdown
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.9.0
4
+ version: 1.9.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - ZhgChgLi