Class: MarkupStyleRender
- Inherits:
-
Object
- Object
- MarkupStyleRender
- Defined in:
- lib/Parsers/MarkupStyleRender.rb
Defined Under Namespace
Instance Attribute Summary collapse
-
#chars ⇒ Object
Returns the value of attribute chars.
-
#encodeType ⇒ Object
Returns the value of attribute encodeType.
-
#paragraph ⇒ Object
Returns the value of attribute paragraph.
Instance Method Summary collapse
-
#initialize(paragraph) ⇒ MarkupStyleRender
constructor
A new instance of MarkupStyleRender.
- #optimize(chars) ⇒ Object
- #parse ⇒ Object
Constructor Details
#initialize(paragraph) ⇒ MarkupStyleRender
Returns a new instance of MarkupStyleRender.
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/Parsers/MarkupStyleRender.rb', line 29 def initialize(paragraph) @paragraph = paragraph 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
#chars ⇒ Object
Returns the value of attribute chars.
7 8 9 |
# File 'lib/Parsers/MarkupStyleRender.rb', line 7 def chars @chars end |
#encodeType ⇒ Object
Returns the value of attribute encodeType.
7 8 9 |
# File 'lib/Parsers/MarkupStyleRender.rb', line 7 def encodeType @encodeType end |
#paragraph ⇒ Object
Returns the value of attribute paragraph.
7 8 9 |
# File 'lib/Parsers/MarkupStyleRender.rb', line 7 def paragraph @paragraph end |
Instance Method Details
#optimize(chars) ⇒ Object
48 49 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 |
# File 'lib/Parsers/MarkupStyleRender.rb', line 48 def optimize(chars) 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 |
#parse ⇒ Object
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 176 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 |
# File 'lib/Parsers/MarkupStyleRender.rb', line 130 def parse() result = paragraph.text if !paragraph.markups.nil? && paragraph.markups.length > 0 = [] 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(3, markup.start, markup.end, "`", "`") elsif markup.type == "STRONG" tag = TagChar.new(2, 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 tag = TagChar.new(1, markup.start, markup.end, "[", "](#{url})") else Helper.makeWarningText("Undefined Markup Type: #{markup.type}.") end if !tag.nil? .append(tag) end end .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 = .select { |tag| tag.startIndex == index }.sort_by(&:sort) if !startTags.nil? startTags.each do |tag| response.append(tag.startChars) stack.append(tag) end end if char.chars.join() != "\n" response.append(TextChar.new(char.chars, 'Text')) end endTags = .select { |tag| tag.endIndex == index } if !endTags.nil? && 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() end result end |