mirror of
https://github.com/simonw/datasette.git
synced 2025-12-10 16:51:24 +01:00
truncate_cells_html now affects URLs too, refs #1805
This commit is contained in:
parent
ff9c87197d
commit
d0737e4de5
8 changed files with 68 additions and 14 deletions
|
|
@ -1167,3 +1167,13 @@ def resolve_routes(routes, path):
|
|||
if match is not None:
|
||||
return match, view
|
||||
return None, None
|
||||
|
||||
|
||||
def truncate_url(url, length):
|
||||
if (not length) or (len(url) <= length):
|
||||
return url
|
||||
bits = url.rsplit(".", 1)
|
||||
if len(bits) == 2 and 1 <= len(bits[1]) <= 4 and "/" not in bits[1]:
|
||||
rest, ext = bits
|
||||
return rest[: length - 1 - len(ext)] + "…." + ext
|
||||
return url[: length - 1] + "…"
|
||||
|
|
|
|||
|
|
@ -20,6 +20,7 @@ from datasette.utils import (
|
|||
path_with_format,
|
||||
path_with_removed_args,
|
||||
sqlite3,
|
||||
truncate_url,
|
||||
InvalidSql,
|
||||
)
|
||||
from datasette.utils.asgi import AsgiFileDownload, NotFound, Response, Forbidden
|
||||
|
|
@ -371,6 +372,7 @@ class QueryView(DataView):
|
|||
|
||||
async def extra_template():
|
||||
display_rows = []
|
||||
truncate_cells = self.ds.setting("truncate_cells_html")
|
||||
for row in results.rows if results else []:
|
||||
display_row = []
|
||||
for column, value in zip(results.columns, row):
|
||||
|
|
@ -396,9 +398,12 @@ class QueryView(DataView):
|
|||
if value in ("", None):
|
||||
display_value = Markup(" ")
|
||||
elif is_url(str(display_value).strip()):
|
||||
display_value = Markup(
|
||||
'<a href="{url}">{url}</a>'.format(
|
||||
url=escape(value.strip())
|
||||
display_value = markupsafe.Markup(
|
||||
'<a href="{url}">{truncated_url}</a>'.format(
|
||||
url=markupsafe.escape(value.strip()),
|
||||
truncated_url=markupsafe.escape(
|
||||
truncate_url(value.strip(), truncate_cells)
|
||||
),
|
||||
)
|
||||
)
|
||||
elif isinstance(display_value, bytes):
|
||||
|
|
|
|||
|
|
@ -24,6 +24,7 @@ from datasette.utils import (
|
|||
path_with_removed_args,
|
||||
path_with_replaced_args,
|
||||
to_css_class,
|
||||
truncate_url,
|
||||
urlsafe_components,
|
||||
value_as_boolean,
|
||||
)
|
||||
|
|
@ -966,8 +967,11 @@ async def display_columns_and_rows(
|
|||
display_value = markupsafe.Markup(" ")
|
||||
elif is_url(str(value).strip()):
|
||||
display_value = markupsafe.Markup(
|
||||
'<a href="{url}">{url}</a>'.format(
|
||||
url=markupsafe.escape(value.strip())
|
||||
'<a href="{url}">{truncated_url}</a>'.format(
|
||||
url=markupsafe.escape(value.strip()),
|
||||
truncated_url=markupsafe.escape(
|
||||
truncate_url(value.strip(), truncate_cells)
|
||||
),
|
||||
)
|
||||
)
|
||||
elif column in table_metadata.get("units", {}) and value != "":
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue