MarkdownExporter.export_entry()   D
last analyzed

Complexity

Conditions 12

Size

Total Lines 52
Code Lines 35

Duplication

Lines 20
Ratio 38.46 %

Importance

Changes 0
Metric Value
cc 12
eloc 35
nop 3
dl 20
loc 52
rs 4.8
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

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:

Complexity

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
2
# encoding: utf-8
3
4
import os
5
import re
6
import sys
7
8
from jrnl.color import RESET_COLOR
9
from jrnl.color import WARNING_COLOR
10
11
from .text_exporter import TextExporter
12
13
14
class MarkdownExporter(TextExporter):
15
    """This Exporter can convert entries and journals into Markdown."""
16
17
    names = ["md", "markdown"]
18
    extension = "md"
19
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):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
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
73
    @classmethod
74
    def export_journal(cls, journal):
75
        """Returns a Markdown representation of an entire journal."""
76
        out = []
77
        year, month = -1, -1
78
        for e in journal.entries:
79
            if not e.date.year == year:
80
                year = e.date.year
81
                out.append("# " + str(year))
82
                out.append("")
83
            if not e.date.month == month:
84
                month = e.date.month
85
                out.append("## " + e.date.strftime("%B"))
86
                out.append("")
87
            out.append(cls.export_entry(e, False))
88
        result = "\n".join(out)
89
        return result
90