Class: MarkupStyleRender

Inherits:
Object
  • Object
show all
Defined in:
lib/Parsers/MarkupStyleRender.rb

Defined Under Namespace

Classes: TagChar, TextChar

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(paragraph, isForJekyll) ⇒ MarkupStyleRender

Returns a new instance of MarkupStyleRender.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/Parsers/MarkupStyleRender.rb', line 30

def initialize(paragraph, isForJekyll)
    @paragraph = paragraph
    @isForJekyll = isForJekyll

    chars = {}
    index = 0
    
    paragraph.text.each_char do |char|
        chars[index] = TextChar.new([char], "Text")
        index += 1
        if char.bytes.length >= 4
            # some emoji need more space (in Medium)
            chars[index] = TextChar.new([], "Text")
            index += 1
        end
    end
    
    @chars = chars
end

Instance Attribute Details

#charsObject

Returns the value of attribute chars.



8
9
10
# File 'lib/Parsers/MarkupStyleRender.rb', line 8

def chars
  @chars
end

#encodeTypeObject

Returns the value of attribute encodeType.



8
9
10
# File 'lib/Parsers/MarkupStyleRender.rb', line 8

def encodeType
  @encodeType
end

#isForJekyllObject

Returns the value of attribute isForJekyll.



8
9
10
# File 'lib/Parsers/MarkupStyleRender.rb', line 8

def isForJekyll
  @isForJekyll
end

#paragraphObject

Returns the value of attribute paragraph.



8
9
10
# File 'lib/Parsers/MarkupStyleRender.rb', line 8

def paragraph
  @paragraph
end

#usersPostURLsObject

Returns the value of attribute usersPostURLs.



8
9
10
# File 'lib/Parsers/MarkupStyleRender.rb', line 8

def usersPostURLs
  @usersPostURLs
end

Instance Method Details

#optimize(chars) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/Parsers/MarkupStyleRender.rb', line 50

def optimize(chars) 

    # remove style in codeblock e.g. `hello world **bold**` (markdown not allow style in codeblock)

    while true

        hasExcute = false
        inCodeBlock = false
        index = 0

        chars.each do |char|
            if char.chars.join() == "`"
                if char.type == "TagStart"
                    inCodeBlock = true
                elsif char.type == "TagEnd"
                    inCodeBlock = false
                end
            elsif inCodeBlock
                if char.type == "TagStart" or char.type == "TagEnd"
                    chars.delete_at(index)
                    hasExcute = true
                    break
                end
            end
            
            index += 1
        end

        if !hasExcute
            break
        end
    end

    # treat escape tag as normal text
    index = 0
    chars.each do |char|
        if char.type == "TagEnd" && char.chars.join() == ""
            chars.delete_at(index)
        elsif char.type == "TagStart" && char.chars.join() == "\\"
            chars[index] = TextChar.new("\\".chars, "Text")
        end

        index += 1
    end

    # append space between tag and text
    while true
        hasExcute = false
        
        index = 0
        startTagIndex = nil
        preTag = nil
        preTagIndex = nil
        preTextChar = nil
        preTextIndex = nil
        chars.each do |char|

            if !preTag.nil?
                if preTag.type == "TagStart" && char.type == "TagEnd"
                    chars.delete_at(index)
                    chars.delete_at(preTagIndex)
                    hasExcute = true
                    break
                end
            end
            
            if char.type == "TagStart" && (preTag == nil || preTag.type == "TagEnd" || preTag.type == "Text")
                startTagIndex = index
            elsif (char.type  == "TagEnd" || char.type  == "Text") && startTagIndex != nil
                if preTextChar != nil && preTextChar.chars.join() != "\n"
                    # not first tag & insert blank between start tag and before text
                    if preTextChar.chars.join() != " "
                        chars.insert(startTagIndex, TextChar.new(" ".chars, "Text"))
                        hasExcute = true
                        break
                    end
                end
                startTagIndex = nil
            end

            if !preTag.nil?
                if preTag.type == "TagStart" && char.type  == "Text"
                    # delete blank between start tag and after text
                    if char.chars.join().strip == ""
                        chars.delete_at(index)
                        hasExcute = true
                        break
                    end
                end

                if preTag.type == "Text" && char.type  == "TagEnd"
                    if preTextChar.chars.join().strip == "" && preTextChar.chars.join() != "\n"
                        chars.delete_at(preTextIndex)
                        hasExcute = true
                        break
                    end
                end

                if preTag.type == "TagEnd" && char.type  == "Text"
                    if char.chars.join() != " "
                        chars.insert(index, TextChar.new(" ".chars, "Text"))
                        hasExcute = true
                        break
                    end
                end

            end

            if char.type == "Text"
                preTextChar = char
                preTextIndex = index
            end
            
            preTag = char
            preTagIndex = index

            index += 1
        end
        
        if !hasExcute
            break
        end
    end

    chars
end

#parseObject



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# File 'lib/Parsers/MarkupStyleRender.rb', line 177

def parse()
    result = paragraph.text

    if !paragraph.markups.nil? && paragraph.markups.length > 0
        
        tags = []
        paragraph.markups.each do |markup|
            tag = nil
            if markup.type == "EM"
                tag = TagChar.new(2, markup.start, markup.end, "_", "_")
            elsif markup.type == "CODE"
                tag = TagChar.new(0, markup.start, markup.end, "`", "`")
            elsif markup.type == "STRONG"
                tag = TagChar.new(2, markup.start, markup.end, "**", "**")
            elsif markup.type == "ESCAPE"
                tag = TagChar.new(999, markup.start, markup.end, "\\", "")
            elsif markup.type == "A"
                url = markup.href
                if markup.anchorType == "LINK"
                    url = markup.href
                elsif markup.anchorType == "USER"
                    url = "https://medium.com/u/#{markup.userId}"
                end

                if url =~ /^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,6}(:[0-9]{1,5})?(\/.*)?$/ix
                    lastPath = url.split("/").last
                    lastQuery = nil
                    if !lastPath.nil?
                        lastQuery = lastPath.split("-").last
                    end
                    
                    if !usersPostURLs.nil? && !usersPostURLs.find { |usersPostURL| usersPostURL.split("/").last.split("-").last == lastQuery }.nil?
                        if isForJekyll
                            url = "(../#{lastQuery}/)"
                        else
                            url = "(#{lastPath})"
                        end
                    else
                        if isForJekyll
                            url = "(#{url}){:target=\"_blank\"}"
                        else
                            url = "(#{url})"
                        end
                    end
                    
                    tag = TagChar.new(1, markup.start, markup.end, "[", "]#{url}")
                end
            else
                Helper.makeWarningText("Undefined Markup Type: #{markup.type}.")
            end

            if !tag.nil?
                tags.append(tag)
            end
        end

        tags.sort_by(&:startIndex)

        response = []
        stack = []

        chars.each do |index, char|
            if char.chars.join() == "\n"
                brStack = stack.dup
                while brStack.length > 0
                    tag = brStack.pop
                    response.push(tag.endChars)
                end
                response.append(TextChar.new(char.chars, 'Text'))
                brStack = stack.dup.reverse
                while brStack.length > 0
                    tag = brStack.pop
                    response.push(tag.startChars)
                end
            end

            startTags = tags.select { |tag| tag.startIndex == index }.sort_by(&:sort)
            if !startTags.nil?
                hasCodeBlockTag = false
                startTags.each do |tag|
                    if !hasCodeBlockTag 
                        response.append(tag.startChars)
                    end
                    
                    stack.append(tag)

                    if tag.startChars.chars.join() == "`"
                        hasCodeBlockTag = true
                    end
                end
            end

            if char.chars.join() != "\n"
                if !stack.select { |tag| tag.startChars.chars.join() == "`" }.nil?
                    # is in code block
                    response.append(char)
                else
                    resultChar = Helper.escapeMarkdown(char.chars.join())
                    response.append(TextChar.new(resultChar.chars, "Text"))
                end
            end

            endTags = tags.select { |tag| tag.endIndex == index }
            if endTags.length > 0
                mismatchTags = []
                while endTags.length > 0
                    stackTag = stack.pop
                    stackTagInEndTagsIndex = endTags.find_index(stackTag)
                    if !stackTagInEndTagsIndex.nil?
                        # as expected
                        endTags.delete_at(stackTagInEndTagsIndex)
                    else
                        mismatchTags.append(stackTag)
                    end
                    response.append(stackTag.endChars)
                end

                while mismatchTags.length > 0
                    mismatchTag = mismatchTags.pop
                    response.append(mismatchTag.startChars)
                    stack.append(mismatchTag)
                end
            end
        end

        while stack.length > 0
            tag = stack.pop
            response.push(tag.endChars)
        end

        response = optimize(response)
        result = response.map{ |response| response.chars }.join()

    else
        response = []
        chars.each do |index, char|
            resultChar = char
            response.append(resultChar)
        end

        response = optimize(response)
        result = response.map{ |response| response.chars }.join()
    end

    result
end