Completed
Pull Request — master (#1522)
by Abdeali
02:07
created

bears.natural_language.MarkdownBear   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 95
Duplicated Lines 0 %
Metric Value
dl 0
loc 95
rs 10
wmc 1

1 Method

Rating   Name   Duplication   Size   Complexity  
B run() 0 88 1
1
import json
2
import re
3
4
from coalib.bearlib.abstractions.Lint import Lint
5
from coalib.bears.LocalBear import LocalBear
6
7
8
class MarkdownBear(Lint, LocalBear):
9
    executable = 'remark'
10
    diff_message = "The text does not comply to the set style."
11
    arguments = '--no-color --quiet'
12
    gives_corrected = True
13
    use_stdin = True
14
15
    def run(self, filename, file,
16
            markdown_bullets: str="-",
17
            markdown_closed_headings: bool=False,
18
            markdown_setext_headings: bool=False,
19
            markdown_emphasis: str="*",
20
            markdown_strong: str="*",
21
            markdown_encode_entities: bool=False,
22
            markdown_codefence: str="`",
23
            markdown_fences: bool=True,
24
            markdown_list_indent: str="1",
25
            markdown_loose_tables: bool=False,
26
            markdown_spaced_tables: bool=True,
27
            markdown_list_increment: bool=True,
28
            markdown_horizontal_rule: str='*',
29
            markdown_horizontal_rule_spaces: bool=False,
30
            markdown_horizontal_rule_repeat: int=3):
31
        """
32
        Raises issues against style violations on markdown files.
33
34
        :param markdown_bullets:                Character to use for bullets
35
                                                in lists. Can be "-", "*"
36
                                                or "+".
37
        :param markdown_closed_headings:        Whether to close Atx headings
38
                                                or not. if true, extra # marks
39
                                                will be required after the
40
                                                heading. eg: `## Heading ##`.
41
        :param markdown_setext_headings:        Whether to use setext headings.
42
                                                A setext heading uses
43
                                                underlines instead of # marks.
44
        :param markdown_emphasis:               Character to wrap strong
45
                                                emphasis by. Can be "_" or "*".
46
        :param markdown_strong:                 Character to wrap slight
47
                                                emphasis by. Can be "_" or "*".
48
        :param markdown_encode_entities:        Whether to encode symbols that
49
                                                are not ASCII into special
50
                                                HTML characters.
51
        :param markdown_codefence:              Used to find which characters
52
                                                to use for code fences.
53
                                                Can be "`" or "~".
54
        :param markdown_fences:                 Use fences for code blocks.
55
        :param markdown_list_indent:            Used to find spacing after
56
                                                bullet in lists.
57
                                                Can be "1", "tab" or "mixed"
58
                                                 - "1" - 1 space after bullet.
59
                                                 - "tab" - Use tab stops to
60
                                                   begin a sentence after the
61
                                                   bullet.
62
                                                 - "mixed" - Use "1" when the
63
                                                   list item is only 1 line,
64
                                                   "tab" if it spans multiple.
65
        :param markdown_loose_tables:           Whether to use pipes for the
66
                                                outermost borders in a table.
67
        :param markdown_spaced_tables:          Whether to add space between
68
                                                pipes in a table.
69
        :param markdown_list_increment:         Whether an ordered lists
70
                                                numbers should be incremented.
71
        :param markdown_horizontal_rule:        The horizontal rule character.
72
                                                Can be '*', '_' or '-'.
73
        :param markdown_horizontal_rule_spaces: Whether spaces should be added
74
                                                between horizontal rule
75
                                                characters.
76
        :param markdown_horizontal_rule_repeat: The number of times the
77
                                                horizontal rule character will
78
                                                be repeated.
79
        """
80
        self.remark_configs = {
81
            "bullet": markdown_bullets,                         # - or *
82
            "closeAtx": markdown_closed_headings,               # Bool
83
            "setext": markdown_setext_headings,                 # Bool
84
            "emphasis": markdown_emphasis,                      # char (_ or *)
85
            "strong": markdown_strong,                          # char (_ or *)
86
            "entities": markdown_encode_entities,               # Bool
87
            "fence": markdown_codefence,                        # char (~ or `)
88
            "fences": markdown_fences,                          # Bool
89
            "listItemIndent": markdown_list_indent,             # int or "tab"
90
                                                                # or "mixed"
91
            "looseTable": markdown_loose_tables,                # Bool
92
            "spacedTable": markdown_spaced_tables,              # Bool
93
            "incrementListMarker": markdown_list_increment,     # Bool
94
            "rule": markdown_horizontal_rule,                   # - or * or _
95
            "ruleSpaces": markdown_horizontal_rule_spaces,      # Bool
96
            "ruleRepetition": markdown_horizontal_rule_repeat,  # int
97
        }
98
        config_json = json.dumps(self.remark_configs)
99
        # Remove { and } (remark adds it on its own) and escape it
100
        settings = re.escape(config_json[1:-1])
101
        self.arguments += " --setting " + settings
102
        return self.lint(filename, file)
103