MCPcopy Index your code
hub / github.com/python/mypy / format_code

Function format_code

misc/gen_blog_post_html.py:40–74  ·  view source on GitHub ↗
(h: str)

Source from the content-addressed store, hash-verified

38
39
40def 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 &gt; and &lt;
59 line = a[i].replace("&gt;", ">").replace("&lt;", "<")
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
77def convert(src: str) -> str:

Callers 1

convertFunction · 0.85

Calls 6

lenFunction · 0.85
splitlinesMethod · 0.80
appendMethod · 0.80
replaceMethod · 0.80
startswithMethod · 0.45
joinMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…