Commit 4b969391 authored by Laurent Peuch's avatar Laurent Peuch
Browse files

feat(deprecated): generate a page per file of the warning stack to be browsable

parent f81c7d527c3f
......@@ -13,7 +13,11 @@ from pygments.lexers import get_lexer_by_name
from pygments.formatters.html import HtmlFormatter
def highlight_code(file_content, lineno):
def highlight_code(file_content, lineno, full_file=False):
if full_file:
return highlight(file_content, get_lexer_by_name("python", stripnl=False),
HtmlFormatter(wrapcode=True, linenos=True, hl_lines=[lineno]))
file_content = file_content.split("\n")[:lineno + 4]
# si on est à la ligne 3
......@@ -112,18 +116,34 @@ def get_all_cubes():
yield cnx.projects.get(id=project_id)
def render_traceback(traceback_id, traceback, warning_name):
def render_traceback(traceback_id, traceback, warning_name, project_id):
os.makedirs("public/traceback", exist_ok=True)
rendered_template = jinja2.Template(open("template/traceback.html",
"r").read()).render(traceback=reversed(traceback),
files_to_url=files_to_url_cache,
highlight_code=highlight_code,
render_source_code_file=render_source_code_file,
warning_name=warning_name,
str=str,
project_id=project_id,
pygment_css=HtmlFormatter().get_style_defs())
open(f"public/traceback/{traceback_id}.html", "w").write(rendered_template)
def render_source_code_file(file_content, path, file_name, lineno):
os.makedirs("public/file", exist_ok=True)
rendered_template = jinja2.Template(open("template/file.html",
"r").read()).render(file_content=file_content,
files_to_url=files_to_url_cache,
highlight_code=highlight_code,
file_name=file_name,
lineno=lineno,
pygment_css=HtmlFormatter().get_style_defs())
open(f"public/file/{path}.html", "w").write(rendered_template)
download_files_to_url_cache()
files_to_url_cache = json.load(open("files_to_url_cache.json"))
......@@ -208,5 +228,6 @@ rendered_template = jinja2.Template(open("template/deprecated-warnings.html",
highlight_code=highlight_code,
str=str,
render_traceback=render_traceback,
render_source_code_file=render_source_code_file,
pygment_css=HtmlFormatter().get_style_defs())
open("public/deprecated-warnings.html", "w").write(rendered_template)
......@@ -51,9 +51,19 @@
{% for (path, lineno), warning in project_data["warnings"].items() %}
<p>
{% with traceback_id=str(warning_id) + "_" + str(project.id) + "_" + path.replace("/", "_") + "_" + str(lineno) %}
{% with %}
{% set traceback_id = str(warning_id) + "_" + str(project.id) + "_" + path.replace("/", "_") + "_" + str(lineno) %}
{{- render_traceback(traceback_id, warning["traceback"], warning_name, project.id) or "" -}}
{% if "file_content" in warning %}
{% set file_uri_path = str(project.id) + "_" + path.replace("/", "_") %}
{{- render_source_code_file(warning["file_content"], file_uri_path, path, warning["lineno"]) or "" -}}
In <a href="file/{{ file_uri_path }}.html#{{ warning["lineno"] }}">{{ path }}</a> line {{ lineno }} (<a href="traceback/{{ traceback_id }}.html">full traceback</a>)
{% else %}
In <u>{{ path }}</u> line {{ lineno }} (<a href="traceback/{{ traceback_id }}.html">full traceback</a>)
{{- render_traceback(traceback_id, warning["traceback"], warning_name) or "" -}}
{% endif %}
{% endwith %}
{% if path in files_to_url %}(<a href="{{ files_to_url[path] }}#L{{ lineno }}" target="_blank">file on branch/default</a>){% endif %}
......
<!doctype html>
<html>
<head>
<title>Content of {{ file_name }}</title>
<link href="../css/style.css" rel="stylesheet" type="text/css">
<style>
{{ pygment_css }}
</style>
</head>
<body>
<h2>{{ file_name }}</h2>
{{ highlight_code(file_content, lineno, full_file=True) }}
</body>
</html>
......@@ -12,9 +12,14 @@
<p><i>The stack frame is in reversed order for practical reason, the upper one first</i></p>
{% for frame in traceback %}
<div class="stack-frame">
<span><u>{{ frame["filename"] }}</u> line {{ frame["lineno"] }}<span>
{% if frame["file_content"] %}
{% set file_uri_path = str(project_id) + "_" + frame["filename"].replace("/", "_") %}
{{- render_source_code_file(frame["file_content"], file_uri_path, frame["filename"], frame["lineno"]) or "" -}}
<span><a href="../file/{{ file_uri_path }}.html#{{ frame["lineno"] }}">{{ frame["filename"] }}</a> line {{ frame["lineno"] }}<span>
{{ highlight_code(frame["file_content"], frame["lineno"]) }}
{% else %}
<span><u>{{ frame["filename"] }}</u> line {{ frame["lineno"] }}<span>
{% endif %}
</div>
{% endfor %}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment