| Conditions | 11 | 
| Total Lines | 125 | 
| Code Lines | 65 | 
| 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_latex_absolute_image._main.latex_code() 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 | ||
| 124 | def latex_code(definition: dict[str, Any], keys: dict[str, str]) -> str: | ||
| 125 | """ | ||
| 126 | Get the latex code. | ||
| 127 | |||
| 128 | Parameters | ||
| 129 | ---------- | ||
| 130 | definition | ||
| 131 | The defition | ||
| 132 | keys | ||
| 133 | Key mapping | ||
| 134 | |||
| 135 | Returns | ||
| 136 | ------- | ||
| 137 | str | ||
| 138 | The latex code. | ||
| 139 | """ | ||
| 140 | path = definition.get(keys["image"]) | ||
| 141 | path_odd = definition.get(keys["image-odd"], path) | ||
| 142 | path_even = definition.get(keys["image-even"], path) | ||
| 143 | |||
| 144 | reset = definition.get(keys["reset"]) | ||
| 145 | reset_odd = definition.get(keys["reset-odd"], reset) | ||
| 146 | reset_even = definition.get(keys["reset-even"], reset) | ||
| 147 | |||
| 148 | width = get_latex_size(definition.get(keys["width"])) | ||
| 149 | width_odd = get_latex_size(definition.get(keys["width-odd"]), width) | ||
| 150 | width_even = get_latex_size(definition.get(keys["width-even"]), width) | ||
| 151 | |||
| 152 | height = get_latex_size(definition.get(keys["height"])) | ||
| 153 | height_odd = get_latex_size(definition.get(keys["height-odd"]), height) | ||
| 154 | height_even = get_latex_size(definition.get(keys["height-even"]), height) | ||
| 155 | |||
| 156 | anchor = get_anchor(definition.get(keys["anchor"])) | ||
| 157 | anchor_odd = get_anchor(definition.get(keys["anchor-odd"]), anchor) | ||
| 158 | anchor_even = get_anchor(definition.get(keys["anchor-even"]), anchor) | ||
| 159 | |||
| 160 | opacity = get_opacity(definition.get(keys["opacity"])) | ||
| 161 | opacity_odd = get_opacity(definition.get(keys["opacity-odd"]), opacity) | ||
| 162 | opacity_even = get_opacity(definition.get(keys["opacity-even"]), opacity) | ||
| 163 | |||
| 164 | x_coord = get_latex_size( | ||
| 165 | definition.get(keys["x-coord"], "0cm"), | ||
| 166 | "0cm", | ||
| 167 | ) | ||
| 168 | x_coord_odd = get_latex_size( | ||
| 169 | definition.get(keys["x-coord-odd"], x_coord), | ||
| 170 | x_coord, | ||
| 171 | ) | ||
| 172 | x_coord_even = get_latex_size( | ||
| 173 | definition.get(keys["x-coord-even"], x_coord), x_coord | ||
| 174 | ) | ||
| 175 | |||
| 176 | y_coord = get_latex_size( | ||
| 177 | definition.get(keys["y-coord"], "0cm"), | ||
| 178 | "0cm", | ||
| 179 | ) | ||
| 180 | y_coord_odd = get_latex_size(definition.get(keys["y-coord-odd"], y_coord), y_coord) | ||
| 181 | y_coord_even = get_latex_size( | ||
| 182 | definition.get(keys["y-coord-even"], y_coord), | ||
| 183 | y_coord, | ||
| 184 | ) | ||
| 185 | |||
| 186 | if reset_odd: | ||
| 187 | picture_odd = """ | ||
| 188 | """ | ||
| 189 | else: | ||
| 190 | options = [] | ||
| 191 | if width_odd: | ||
| 192 |             options.append(f"width={width_odd}") | ||
| 193 | if height_odd: | ||
| 194 |             options.append(f"height={height_odd}") | ||
| 195 | options = ",".join(options) | ||
| 196 | |||
| 197 | node_options = [] | ||
| 198 | if anchor_odd: | ||
| 199 |             node_options.append(f"anchor={anchor_odd}") | ||
| 200 | if opacity_odd: | ||
| 201 |             node_options.append(f"opacity={opacity_odd}") | ||
| 202 | node_options = ",".join(node_options) | ||
| 203 | |||
| 204 | picture_odd = f""" | ||
| 205 | \\begin{{tikzpicture}}[ | ||
| 206 | overlay, % Do our drawing on an overlay instead of inline | ||
| 207 | remember picture, % Allow us to share coordinates with other drawings | ||
| 208 | shift=(current page.north west), % Set the top (north) left (west) as the origin | ||
| 209 | yscale=-1, % Switch the y-axis to increase down the page | ||
| 210 | inner sep=0, % Remove inner separator | ||
| 211 | ] | ||
| 212 | \\node[{node_options}] at ({x_coord_odd}, {y_coord_odd}) | ||
| 213 |     {{\\includegraphics[{options}]{{{path_odd}}}}}; | ||
| 214 | \\end{{tikzpicture}} | ||
| 215 | """ | ||
| 216 | |||
| 217 | if reset_even: | ||
| 218 | picture_even = """ | ||
| 219 | """ | ||
| 220 | else: | ||
| 221 | options = [] | ||
| 222 | if width_even: | ||
| 223 |             options.append(f"width={width_even}") | ||
| 224 | if height_odd: | ||
| 225 |             options.append(f"height={height_even}") | ||
| 226 | options = ",".join(options) | ||
| 227 | |||
| 228 | node_options = [] | ||
| 229 | if anchor_even: | ||
| 230 |             node_options.append(f"anchor={anchor_even}") | ||
| 231 | if opacity_even: | ||
| 232 |             node_options.append(f"opacity={opacity_even}") | ||
| 233 | node_options = ",".join(node_options) | ||
| 234 | |||
| 235 | picture_even = f""" | ||
| 236 | \\begin{{tikzpicture}}[ | ||
| 237 | overlay, % Do our drawing on an overlay instead of inline | ||
| 238 | remember picture, % Allow us to share coordinates with other drawings | ||
| 239 | shift=(current page.north west), % Set the top (north) left (west) as the origin | ||
| 240 | yscale=-1, % Switch the y-axis to increase down the page | ||
| 241 | inner sep=0, % Remove inner separator | ||
| 242 | ] | ||
| 243 | \\node[{node_options}] at ({x_coord_even}, {y_coord_even}) | ||
| 244 |   {{\\includegraphics[{options}]{{{path_even}}}}}; | ||
| 245 | \\end{{tikzpicture}} | ||
| 246 | """ | ||
| 247 | |||
| 248 | return f""" | ||
| 249 | \\renewcommand\\PandocLaTeXAbsoluteImage{{% | ||
| 473 |