Commit b4ca625e authored by Laurent Peuch's avatar Laurent Peuch
Browse files

[debug-toolbar/display_source_code] add function to generate html link to source code

parent 3a38f779bed5
...@@ -36,6 +36,26 @@ DEBUG_DISPLAY_SOURCE_CODE_PATH = '_debug_display_source_code' ...@@ -36,6 +36,26 @@ DEBUG_DISPLAY_SOURCE_CODE_PATH = '_debug_display_source_code'
FILES_WHITE_LIST = set() FILES_WHITE_LIST = set()
def _generate_link_to_source(file_path, start=None, end=None, tag_body="<>"):
if start:
# step back a bit so we have a bit of top padding wen displaying the page
# and the highlighted line isn't glued to top of the browser window
line_anchor = max(0, start - 10)
if end:
return '<a href="../%s?file=%s&line=%s&end=%s#line-%s" target="_blank">%s</a>' % (
DEBUG_DISPLAY_SOURCE_CODE_PATH, file_path, start, end, line_anchor, tag_body
)
else:
return '<a href="../%s?file=%s&line=%s#line-%s" target="_blank">%s</a>' % (
DEBUG_DISPLAY_SOURCE_CODE_PATH, file_path, start, line_anchor, tag_body
)
return '<a href="../%s?file=%s" target="_blank">%s</a>' % (
DEBUG_DISPLAY_SOURCE_CODE_PATH, file_path, tag_body
)
def source_code_url(object_or_class): def source_code_url(object_or_class):
if object_or_class is None: if object_or_class is None:
return "" return ""
...@@ -54,17 +74,9 @@ def source_code_url(object_or_class): ...@@ -54,17 +74,9 @@ def source_code_url(object_or_class):
try: try:
source_code, line = inspect.getsourcelines(object_or_class) source_code, line = inspect.getsourcelines(object_or_class)
except OSError: # when we couldn't read the source code/line except OSError: # when we couldn't read the source code/line
return '<a href="../%s?file=%s" target="_blank">&lt;&gt;</a>' % ( return _generate_link_to_source(file_path)
DEBUG_DISPLAY_SOURCE_CODE_PATH, file_path
)
# step back a bit so we have a bit of top padding wen displaying the page return _generate_link_to_source(file_path, line, line + len(source_code))
# and the highlighted line isn't glued to top of the browser window
line_anchor = max(0, line - 10)
return '<a href="../%s?file=%s&line=%s&end=%s#line-%s" target="_blank">&lt;&gt;</a>' % (
DEBUG_DISPLAY_SOURCE_CODE_PATH, file_path, line, line + len(source_code), line_anchor
)
def debug_display_source_code(request): def debug_display_source_code(request):
......
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