| Conditions | 12 |
| Total Lines | 52 |
| Code Lines | 35 |
| Lines | 20 |
| Ratio | 38.46 % |
| Changes | 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 jrnl.plugins.markdown_exporter.MarkdownExporter.export_entry() 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 | #!/usr/bin/env python |
||
| 20 | @classmethod |
||
| 21 | def export_entry(cls, entry, to_multifile=True): |
||
| 22 | """Returns a markdown representation of a single entry.""" |
||
| 23 | date_str = entry.date.strftime(entry.journal.config["timeformat"]) |
||
| 24 | body_wrapper = "\n" if entry.body else "" |
||
| 25 | body = body_wrapper + entry.body |
||
| 26 | |||
| 27 | if to_multifile is True: |
||
| 28 | heading = "#" |
||
| 29 | else: |
||
| 30 | heading = "###" |
||
| 31 | |||
| 32 | """Increase heading levels in body text""" |
||
| 33 | newbody = "" |
||
| 34 | previous_line = "" |
||
| 35 | warn_on_heading_level = False |
||
| 36 | for line in body.splitlines(True): |
||
| 37 | View Code Duplication | if re.match(r"^#+ ", line): |
|
|
|
|||
| 38 | """ATX style headings""" |
||
| 39 | newbody = newbody + previous_line + heading + line |
||
| 40 | if re.match(r"^#######+ ", heading + line): |
||
| 41 | warn_on_heading_level = True |
||
| 42 | line = "" |
||
| 43 | elif re.match(r"^=+$", line.rstrip()) and not re.match( |
||
| 44 | r"^$", previous_line.strip() |
||
| 45 | ): |
||
| 46 | """Setext style H1""" |
||
| 47 | newbody = newbody + heading + "# " + previous_line |
||
| 48 | line = "" |
||
| 49 | elif re.match(r"^-+$", line.rstrip()) and not re.match( |
||
| 50 | r"^$", previous_line.strip() |
||
| 51 | ): |
||
| 52 | """Setext style H2""" |
||
| 53 | newbody = newbody + heading + "## " + previous_line |
||
| 54 | line = "" |
||
| 55 | else: |
||
| 56 | newbody = newbody + previous_line |
||
| 57 | previous_line = line |
||
| 58 | newbody = newbody + previous_line # add very last line |
||
| 59 | |||
| 60 | # make sure the export ends with a blank line |
||
| 61 | if previous_line not in ["\r", "\n", "\r\n", "\n\r"]: |
||
| 62 | newbody = newbody + os.linesep |
||
| 63 | |||
| 64 | if warn_on_heading_level is True: |
||
| 65 | print( |
||
| 66 | f"{WARNING_COLOR}WARNING{RESET_COLOR}: " |
||
| 67 | f"Headings increased past H6 on export - {date_str} {entry.title}", |
||
| 68 | file=sys.stderr, |
||
| 69 | ) |
||
| 70 | |||
| 71 | return f"{heading} {date_str} {entry.title}\n{newbody} " |
||
| 72 | |||
| 90 |