| Conditions | 12 |
| Total Lines | 77 |
| Code Lines | 48 |
| 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 doorstop.core.publishers.html.HtmlPublisher.lines() 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 | # SPDX-License-Identifier: LGPL-3.0-only |
||
| 185 | def lines(self, obj, **kwargs): |
||
| 186 | """Yield lines for an HTML report. |
||
| 187 | |||
| 188 | :param obj: Item, list of Items, or Document to publish |
||
| 189 | :param linkify: turn links into hyperlinks |
||
| 190 | |||
| 191 | :return: iterator of lines of text |
||
| 192 | |||
| 193 | """ |
||
| 194 | linkify = kwargs.get("linkify", False) |
||
| 195 | toc = kwargs.get("toc", False) |
||
| 196 | |||
| 197 | # Determine if a full HTML document should be generated |
||
| 198 | try: |
||
| 199 | iter(obj) |
||
| 200 | except TypeError: |
||
| 201 | document = False |
||
| 202 | else: |
||
| 203 | document = True |
||
| 204 | |||
| 205 | # Generate HTML |
||
| 206 | text = "\n".join(self._lines_markdown(obj, linkify=linkify, to_html=True)) |
||
| 207 | # We need to handle escaped back-ticks before we pass the text to markdown. |
||
| 208 | text = text.replace("\\`", "##!!TEMPINLINE!!##") |
||
| 209 | body_to_check = markdown.markdown(text, extensions=self.EXTENSIONS).splitlines() |
||
| 210 | block = [] |
||
| 211 | # Check for nested lists since they are not supported by the markdown_sane_lists plugin. |
||
| 212 | for i, line in enumerate(body_to_check): |
||
| 213 | # Replace the temporary inline code blocks with the escaped back-ticks. If there are |
||
| 214 | # multiple back-ticks in a row, we need group them in a single <code> block. |
||
| 215 | line = re.sub( |
||
| 216 | r"(##!!TEMPINLINE!!##)+", |
||
| 217 | lambda m: "<code>" + "`" * int(len(m.group()) / 18) + "</code>", |
||
| 218 | line, |
||
| 219 | ) |
||
| 220 | |||
| 221 | # line = line.replace("##!!TEMPINLINE!!##", "<code>`</code>") |
||
| 222 | # Check if we are at the end of the body. |
||
| 223 | if i == len(body_to_check) - 1: |
||
| 224 | next_line = "" |
||
| 225 | else: |
||
| 226 | next_line = body_to_check[i + 1] |
||
| 227 | (_, processed_block, processed_line) = self.process_lists(line, next_line) |
||
| 228 | if processed_block != "": |
||
| 229 | block.append(processed_block) |
||
| 230 | block.append(processed_line) |
||
| 231 | body = "\n".join(block) |
||
| 232 | |||
| 233 | if toc: |
||
| 234 | toc_md = self.table_of_contents(True, obj) |
||
| 235 | toc_html = markdown.markdown(toc_md, extensions=self.EXTENSIONS) |
||
| 236 | else: |
||
| 237 | toc_html = "" |
||
| 238 | |||
| 239 | if document: |
||
| 240 | if self.template == "": |
||
| 241 | self.template = HTMLTEMPLATE |
||
| 242 | try: |
||
| 243 | bottle.TEMPLATE_PATH.insert( |
||
| 244 | 0, os.path.join(os.path.dirname(__file__), "..", "..", "views") |
||
| 245 | ) |
||
| 246 | if "baseurl" not in bottle.SimpleTemplate.defaults: |
||
| 247 | bottle.SimpleTemplate.defaults["baseurl"] = "" |
||
| 248 | html = bottle_template( |
||
| 249 | self.template, |
||
| 250 | body=body, |
||
| 251 | toc=toc_html, |
||
| 252 | parent=obj.parent, |
||
| 253 | document=obj, |
||
| 254 | ) |
||
| 255 | except Exception: |
||
| 256 | raise DoorstopError( |
||
| 257 | "Problem parsing the template {}".format(self.template) |
||
| 258 | ) |
||
| 259 | yield "\n".join(html.split(os.linesep)) |
||
| 260 | else: |
||
| 261 | yield body |
||
| 262 | |||
| 270 |