Spaces:
Runtime error
Runtime error
Download code_builder.py from Jellyfish042/ROSA-Visualizer: direct link, hf CLI and curl.
- Browser
- Download file 1.69 kB
-
https://huggingface.co/spaces/Jellyfish042/ROSA-Visualizer/resolve/refs%2Fpr%2F1/code_builder.py
- Command line
-
hf download hf://spaces/Jellyfish042/ROSA-Visualizer@refs/pr/1/code_builder.py
-
curl -L -o code_builder.py https://huggingface.co/spaces/Jellyfish042/ROSA-Visualizer/resolve/refs%2Fpr%2F1/code_builder.py
1.69 kB
| """ | |
| 代码 HTML 构建模块 - 生成带有行号和高亮的代码 HTML | |
| """ | |
| import html | |
| from code_highlighting import highlight_python_line | |
| def build_code_html(code: str, line_numbers: dict) -> str: | |
| """ | |
| 构建带有交互式标记的代码 HTML | |
| 用于在演示中突出显示关键代码行和标记 | |
| """ | |
| lines = code.splitlines() | |
| rendered = ['<div id="rosa-code" class="rosa-code">'] | |
| marker_t = "__TOK_T__" | |
| marker_k = "__TOK_K__" | |
| marker_v = "__TOK_V__" | |
| for index, line in enumerate(lines, start=1): | |
| line_with_markers = line | |
| if index == line_numbers["LINE_TRY"]: | |
| line_with_markers = line_with_markers.replace("kkk[j:j+w]", marker_k, 1) | |
| line_with_markers = line_with_markers.replace("t", marker_t, 1) | |
| if index == line_numbers["LINE_ASSIGN"]: | |
| line_with_markers = line_with_markers.replace("vvv[j+w]", marker_v, 1) | |
| highlighted = highlight_python_line(line_with_markers) | |
| highlighted = highlighted.replace( | |
| marker_t, '<span class="code-token" data-token="t">t</span>' | |
| ) | |
| highlighted = highlighted.replace( | |
| marker_k, '<span class="code-token" data-token="k">kkk[j:j+w]</span>' | |
| ) | |
| highlighted = highlighted.replace( | |
| marker_v, '<span class="code-token" data-token="v">vvv[j+w]</span>' | |
| ) | |
| rendered.append( | |
| '<div class="code-line" data-line="{line}">' | |
| '<span class="line-no">{line}</span>' | |
| '<span class="line-text">{text}</span>' | |
| "</div>".format(line=index, text=highlighted) | |
| ) | |
| rendered.append("</div>") | |
| return "\n".join(rendered) | |