Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions scripts/build_readout.py
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,12 @@ def credibility_word(value: str) -> str:
return "высокая" if v >= 4 else "средняя" if v == 3 else "низкая"


def is_web_url(url: str) -> bool:
"""Source URLs come from fetched pages; any other scheme (javascript:, data:) would run on click."""
parsed = urlparse(url)
return parsed.scheme in {"http", "https"} and bool(parsed.netloc)


def source_index(rows: list[dict], used_by: dict[str, list[int]]) -> str:
live = [r for r in rows if (r.get("used") or "Y") != "N"]
if not live:
Expand Down Expand Up @@ -502,11 +508,8 @@ def source_index(rows: list[dict], used_by: dict[str, list[int]]) -> str:
)
)
url = r.get("url") or ""
parsed = urlparse(url)
host = parsed.netloc.removeprefix("www.")
# URLs come from fetched pages; any other scheme (javascript:, data:) would run on click.
linkable = parsed.scheme in {"http", "https"} and host
link = f' · <a href="{html.escape(url)}">{html.escape(host)}</a>' if linkable else ""
host = urlparse(url).netloc.removeprefix("www.")
link = f' · <a href="{html.escape(url)}">{html.escape(host)}</a>' if is_web_url(url) else ""
num = int(sid[1:]) if re.fullmatch(r"s\d+", sid) else html.escape(sid)
out.append(
f'<dt id="{html.escape(sid)}">{num}</dt><dd><b>{title}</b><br>{" · ".join(bits)}{link}</dd>'
Expand Down
8 changes: 6 additions & 2 deletions scripts/build_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,12 @@ def sources_appendix(rows: list[dict]) -> str:
],
)
)
url = html.escape(r.get("url") or "")
link = f'<div class="meta"><a href="{url}">{url}</a></div>' if url else ""
raw_url = r.get("url") or ""
url = html.escape(raw_url)
if build_readout.is_web_url(raw_url):
link = f'<div class="meta"><a href="{url}">{url}</a></div>'
else:
link = f'<div class="meta">{url}</div>' if url else ""
out.append(f'<dt id="{sid}">[{sid}]</dt>')
out.append(f'<dd>{title}<div class="meta">{meta}</div>{link}</dd>')
out += ["</dl>", "</section>"]
Expand Down
11 changes: 11 additions & 0 deletions tests/test_report_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,17 @@ def test_sources_appendix_lists_every_row():
assert "Источник A" in out and "Primary" in out


def test_sources_appendix_links_only_http_urls():
rows = [
{"id": "s01", "url": "javascript:alert(1)", "title": "x"},
{"id": "s02", "url": "https://ok.example/p", "title": "y"},
]
out = br.sources_appendix(rows)
assert 'href="javascript:' not in out
assert "javascript:alert(1)" in out # адрес виден текстом, но не кликается
assert 'href="https://ok.example/p"' in out


def test_empty_sources_produce_no_appendix():
assert br.sources_appendix([]) == ""

Expand Down
Loading