Conditions | 31 |
Total Lines | 133 |
Lines | 39 |
Ratio | 29.32 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like HtmlBlockPreprocessor.run() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | """ |
||
189 | def run(self, lines): |
||
190 | text = "\n".join(lines) |
||
191 | new_blocks = [] |
||
192 | text = text.rsplit("\n\n") |
||
193 | items = [] |
||
194 | left_tag = '' |
||
195 | right_tag = '' |
||
196 | in_tag = False # flag |
||
197 | |||
198 | while text: |
||
199 | block = text[0] |
||
200 | if block.startswith("\n"): |
||
201 | block = block[1:] |
||
202 | text = text[1:] |
||
203 | |||
204 | if block.startswith("\n"): |
||
205 | block = block[1:] |
||
206 | |||
207 | if not in_tag: |
||
208 | if block.startswith("<") and len(block.strip()) > 1: |
||
209 | |||
210 | if block[1:4] == "!--": |
||
211 | # is a comment block |
||
212 | left_tag, left_index, attrs = "--", 2, {} |
||
213 | else: |
||
214 | left_tag, left_index, attrs = self._get_left_tag(block) |
||
215 | right_tag, data_index = self._get_right_tag(left_tag, |
||
216 | left_index, |
||
217 | block) |
||
218 | # keep checking conditions below and maybe just append |
||
219 | |||
220 | if data_index < len(block) and (util.isBlockLevel(left_tag) or left_tag == '--'): |
||
221 | text.insert(0, block[data_index:]) |
||
222 | block = block[:data_index] |
||
223 | |||
224 | if not (util.isBlockLevel(left_tag) or block[1] in ["!", "?", "@", "%"]): |
||
225 | new_blocks.append(block) |
||
226 | continue |
||
227 | |||
228 | if self._is_oneliner(left_tag): |
||
229 | new_blocks.append(block.strip()) |
||
230 | continue |
||
231 | |||
232 | if block.rstrip().endswith(">") \ |
||
233 | and self._equal_tags(left_tag, right_tag): |
||
234 | if self.markdown_in_raw and 'markdown' in attrs.keys(): |
||
235 | block = block[left_index:-len(right_tag) - 2] |
||
236 | new_blocks.append(self.markdown.htmlStash. |
||
237 | store_tag(left_tag, attrs, 0, 2)) |
||
238 | new_blocks.extend([block]) |
||
239 | else: |
||
240 | new_blocks.append( |
||
241 | self.markdown.htmlStash.store(block.strip())) |
||
242 | continue |
||
243 | else: |
||
244 | # if is block level tag and is not complete |
||
245 | if (not self._equal_tags(left_tag, right_tag)) and \ |
||
246 | (util.isBlockLevel(left_tag) or left_tag == "--"): |
||
247 | items.append(block.strip()) |
||
248 | in_tag = True |
||
249 | else: |
||
250 | new_blocks.append( |
||
251 | self.markdown.htmlStash.store(block.strip()) |
||
252 | ) |
||
253 | continue |
||
254 | |||
255 | else: |
||
256 | new_blocks.append(block) |
||
257 | |||
258 | else: |
||
259 | items.append(block) |
||
260 | |||
261 | # Need to evaluate all items so we can calculate relative to the left index. |
||
262 | right_tag, data_index = self._get_right_tag(left_tag, left_index, ''.join(items)) |
||
263 | # Adjust data_index: relative to items -> relative to last block |
||
264 | prev_block_length = 0 |
||
265 | for item in items[:-1]: |
||
266 | prev_block_length += len(item) |
||
267 | data_index -= prev_block_length |
||
268 | |||
269 | if self._equal_tags(left_tag, right_tag): |
||
270 | # if find closing tag |
||
271 | |||
272 | if data_index < len(block): |
||
273 | # we have more text after right_tag |
||
274 | items[-1] = block[:data_index] |
||
275 | text.insert(0, block[data_index:]) |
||
276 | |||
277 | in_tag = False |
||
278 | View Code Duplication | if self.markdown_in_raw and 'markdown' in attrs.keys(): |
|
|
|||
279 | items[0] = items[0][left_index:] |
||
280 | items[-1] = items[-1][:-len(right_tag) - 2] |
||
281 | if items[len(items) - 1]: # not a newline/empty string |
||
282 | right_index = len(items) + 3 |
||
283 | else: |
||
284 | right_index = len(items) + 2 |
||
285 | new_blocks.append(self.markdown.htmlStash.store_tag( |
||
286 | left_tag, attrs, 0, right_index)) |
||
287 | placeholderslen = len(self.markdown.htmlStash.tag_data) |
||
288 | new_blocks.extend( |
||
289 | self._nested_markdown_in_html(items)) |
||
290 | nests = len(self.markdown.htmlStash.tag_data) - \ |
||
291 | placeholderslen |
||
292 | self.markdown.htmlStash.tag_data[-1 - nests][ |
||
293 | 'right_index'] += nests - 2 |
||
294 | else: |
||
295 | new_blocks.append( |
||
296 | self.markdown.htmlStash.store('\n\n'.join(items))) |
||
297 | items = [] |
||
298 | |||
299 | View Code Duplication | if items: |
|
300 | if self.markdown_in_raw and 'markdown' in attrs.keys(): |
||
301 | items[0] = items[0][left_index:] |
||
302 | items[-1] = items[-1][:-len(right_tag) - 2] |
||
303 | if items[len(items) - 1]: # not a newline/empty string |
||
304 | right_index = len(items) + 3 |
||
305 | else: |
||
306 | right_index = len(items) + 2 |
||
307 | new_blocks.append( |
||
308 | self.markdown.htmlStash.store_tag( |
||
309 | left_tag, attrs, 0, right_index)) |
||
310 | placeholderslen = len(self.markdown.htmlStash.tag_data) |
||
311 | new_blocks.extend(self._nested_markdown_in_html(items)) |
||
312 | nests = len(self.markdown.htmlStash.tag_data) - placeholderslen |
||
313 | self.markdown.htmlStash.tag_data[-1 - nests][ |
||
314 | 'right_index'] += nests - 2 |
||
315 | else: |
||
316 | new_blocks.append( |
||
317 | self.markdown.htmlStash.store('\n\n'.join(items))) |
||
318 | new_blocks.append('\n') |
||
319 | |||
320 | new_text = "\n\n".join(new_blocks) |
||
321 | return new_text.split("\n") |
||
322 | |||
353 |