Conditions | 17 |
Total Lines | 115 |
Code Lines | 59 |
Lines | 0 |
Ratio | 0 % |
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 pandoc_glfm._main.alert() 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 |
||
21 | def alert(elem: Element, doc: Doc) -> Element | None: |
||
22 | """ |
||
23 | Transform some blockquote elements to alerts. |
||
24 | |||
25 | Arguments |
||
26 | --------- |
||
27 | elem |
||
28 | The current element |
||
29 | doc |
||
30 | The pandoc document |
||
31 | |||
32 | Returns |
||
33 | ------- |
||
34 | Element | None |
||
35 | The modified element or None |
||
36 | """ |
||
37 | if ( |
||
38 | elem.tag == "BlockQuote" |
||
39 | and elem.content |
||
40 | and elem.content[0].tag == "Para" |
||
41 | and elem.content[0].content |
||
42 | and elem.content[0].content[0].tag == "Str" |
||
43 | ): |
||
44 | |||
45 | def extract_first_lines( |
||
46 | first: list[Element], |
||
47 | rest: list[Element], |
||
48 | ) -> list[Element]: |
||
49 | result = [] |
||
50 | for item in first: |
||
51 | if item.tag == "Str": |
||
52 | result.append(item.text) |
||
53 | elif item.tag == "SoftBreak": |
||
54 | result.append("\n") |
||
55 | elif item.tag == "Space": |
||
56 | result.append(" ") |
||
57 | else: |
||
58 | result.append( |
||
59 | convert_text( |
||
60 | Plain(item), |
||
61 | input_format="panflute", |
||
62 | output_format="markdown", |
||
63 | ), |
||
64 | ) |
||
65 | for item in rest: |
||
66 | result.extend( |
||
67 | [ |
||
68 | "\n", |
||
69 | convert_text( |
||
70 | item, |
||
71 | input_format="panflute", |
||
72 | output_format="markdown", |
||
73 | ), |
||
74 | ] |
||
75 | ) |
||
76 | |||
77 | return convert_text("".join(result)) |
||
78 | |||
79 | text = elem.content[0].content[0].text.lower() |
||
80 | if text in ("[!note]", "[!tip]", "[!important]", "[!caution]", "[!warning]"): |
||
81 | # Case |
||
82 | # |
||
83 | # > [!tip] |
||
84 | # |
||
85 | # and |
||
86 | # |
||
87 | # > [!tip] |
||
88 | # > |
||
89 | # > Rest of text |
||
90 | if len(elem.content[0].content) == 1: |
||
91 | title = Div(Para(Str(text[2:-1].capitalize())), classes=["title"]) |
||
92 | content = [*elem.content[1:]] |
||
93 | # Case |
||
94 | # |
||
95 | # > [!tip] |
||
96 | # > Rest of text |
||
97 | elif elem.content[0].content[1].tag == "SoftBreak": |
||
98 | title = Div(Para(Str(text[2:-1].capitalize())), classes=["title"]) |
||
99 | content = extract_first_lines( |
||
100 | elem.content[0].content[2:], |
||
101 | elem.content[1:], |
||
102 | ) |
||
103 | # Case |
||
104 | # |
||
105 | # > [!tip] title |
||
106 | # > Rest of text |
||
107 | # |
||
108 | # and |
||
109 | # |
||
110 | # > [!tip] title |
||
111 | # > |
||
112 | # > Rest of text |
||
113 | else: |
||
114 | alternate = [] |
||
115 | for index in range(2, len(elem.content[0].content)): |
||
116 | if elem.content[0].content[index].tag == "SoftBreak": |
||
117 | title = Div(Para(*alternate), classes=["title"]) |
||
118 | content = extract_first_lines( |
||
119 | elem.content[0].content[index:], |
||
120 | elem.content[1:], |
||
121 | ) |
||
122 | break |
||
123 | alternate.append(elem.content[0].content[index]) |
||
124 | else: |
||
125 | title = Div(Para(*alternate), classes=["title"]) |
||
126 | content = [*elem.content[1:]] |
||
127 | |||
128 | return convert_text( |
||
129 | convert_text( |
||
130 | Div(title, *content, classes=[text[2:-1]]), |
||
131 | input_format="panflute", |
||
132 | output_format="markdown", |
||
133 | ) |
||
134 | ) |
||
135 | return None |
||
136 | |||
211 |