Skip to content
Open
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
37 changes: 37 additions & 0 deletions redbot/formatter/html_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from urllib.parse import urljoin, urlparse

import httplint
from httplint.note import levels
from jinja2 import Environment, PackageLoader, select_autoescape
from markupsafe import Markup, escape
from typing_extensions import Unpack
Expand Down Expand Up @@ -106,6 +107,8 @@ def start_output(self) -> None:
if self.kw.get("is_blank", None):
extra_body_class = "blank"

social_meta = self.social_meta(uri)

tpl = self.templates.get_template("response_start.html")
self.output(
tpl.render(
Expand Down Expand Up @@ -145,11 +148,45 @@ def start_output(self) -> None:
"extra_body_class": extra_body_class,
"descend": self.kw.get("descend", False),
"test_id": self.kw.get("test_id", ""),
**social_meta,
},
)
)
)

def social_meta(self, uri: str) -> dict[str, str]:
"""
Build per-result Open Graph / Twitter card metadata for a saved result.

Only saved results carry useful metadata: the full resource (and its
notes) is available when the <head> is rendered, whereas a live test is
still fetching. Returns an empty dict otherwise, so the template falls
back to the static site metadata.
"""
if not self.kw.get("is_saved") or not uri:
return {}
og_url = urljoin(self.template_vars["baseuri"], f"saved/{self.kw.get('test_id', '')}")
if getattr(self.resource, "descend", False):
description = "REDbot HTTP checks for this resource and its embedded resources."
else:
notes = getattr(getattr(self.resource, "response", None), "notes", []) or []
bad = sum(1 for note in notes if note.level == levels.BAD)
warn = sum(1 for note in notes if note.level == levels.WARN)
counts = []
if bad:
counts.append(f"{bad} problem" + ("" if bad == 1 else "s"))
if warn:
counts.append(f"{warn} warning" + ("" if warn == 1 else "s"))
if counts:
description = f"{', '.join(counts)} found by REDbot's HTTP checks."
else:
description = "No problems found by REDbot's HTTP checks."
return {
"og_title": f"REDbot: {uri}",
"og_description": description,
"og_url": og_url,
}

def finish_output(self) -> None:
"""
The bottom bits.
Expand Down
13 changes: 11 additions & 2 deletions redbot/formatter/templates/response_start.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,20 @@
<meta name="application-name" content="REDbot" />
<meta name="ROBOTS" content="INDEX, NOFOLLOW" />
<meta property="og:type" content="website">
<meta property="og:site_name" content="REDbot">
<meta property="og:image" content="https://redbot.org/static/logo/redbot-sq.png">
{% if og_title %}
<meta property="og:title" content="{{ og_title }}">
<meta property="og:description" content="{{ og_description }}">
<meta property="og:url" content="{{ og_url }}">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="{{ og_title }}">
<meta name="twitter:description" content="{{ og_description }}">
{% else %}
<meta property="og:title" content="REDbot">
<meta property="og:description" content="REDbot is lint for your HTTP resources.">
<meta property="og:url" content="https://redbot.org/">
<meta property="og:site_name" content="REDbot">
<meta property="og:image" content="https://redbot.org/static/logo/redbot-sq.png">
{% endif %}
<link rel="stylesheet" type="text/css" href="{{ static }}/style.css?{{ redbot_version }}">
<link rel="apple-touch-icon-precomposed" sizes="144x144" href="{{ static }}/logo/apple-touch-icon-144x144.png" />
<link rel="apple-touch-icon-precomposed" sizes="152x152" href="{{ static }}/logo/apple-touch-icon-152x152.png" />
Expand Down
Loading