(h: str)
| 38 | |
| 39 | |
| 40 | def format_code(h: str) -> str: |
| 41 | a = h.splitlines() |
| 42 | r = [] |
| 43 | i = 0 |
| 44 | while i < len(a): |
| 45 | if a[i].startswith(" ") or a[i].startswith("```"): |
| 46 | indent = a[i].startswith(" ") |
| 47 | language: str = "" |
| 48 | if not indent: |
| 49 | language = a[i][3:] |
| 50 | i += 1 |
| 51 | if language: |
| 52 | r.append(f'<pre><code class="language-{language}">') |
| 53 | else: |
| 54 | r.append("<pre><code>") |
| 55 | while i < len(a) and ( |
| 56 | (indent and a[i].startswith(" ")) or (not indent and not a[i].startswith("```")) |
| 57 | ): |
| 58 | # Undo > and < |
| 59 | line = a[i].replace(">", ">").replace("<", "<") |
| 60 | if indent: |
| 61 | # Undo this extra level of indentation so it looks nice with |
| 62 | # syntax highlighting CSS. |
| 63 | line = line[4:] |
| 64 | r.append(html.escape(line)) |
| 65 | i += 1 |
| 66 | r.append("</code></pre>") |
| 67 | if not indent and a[i].startswith("```"): |
| 68 | i += 1 |
| 69 | else: |
| 70 | r.append(a[i]) |
| 71 | i += 1 |
| 72 | formatted = "\n".join(r) |
| 73 | # remove empty first line for code blocks |
| 74 | return re.sub(r"<code([^\>]*)>\n", r"<code\1>", formatted) |
| 75 | |
| 76 | |
| 77 | def convert(src: str) -> str: |
no test coverage detected
searching dependent graphs…