Skip to content

Commit e57999c

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent ea236c3 commit e57999c

6 files changed

Lines changed: 53 additions & 38 deletions

File tree

11-opengraph/src/entry.py

Lines changed: 41 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1-
from workers import WorkerEntrypoint, Response, Request, fetch
1+
from workers import WorkerEntrypoint, Request, fetch
22
from js import HTMLRewriter
33
from urllib.parse import urlparse
44
from html import escape
55

66
from pyodide.ffi import to_js
77

8+
89
class MetaTagInjector:
910
"""
1011
Element handler for HTMLRewriter that injects OpenGraph meta tags.
1112
Uses Python's html.escape() for proper HTML escaping.
1213
"""
14+
1315
def __init__(self, og_data: dict):
1416
self.og_data = og_data
1517
self.injected = False
@@ -25,16 +27,22 @@ def _inject_meta_tags(self, head_element):
2527
"""Inject OpenGraph and Twitter Card meta tags."""
2628
# OpenGraph tags
2729
self._create_meta(head_element, "property", "og:title", self.og_data["title"])
28-
self._create_meta(head_element, "property", "og:description", self.og_data["description"])
30+
self._create_meta(
31+
head_element, "property", "og:description", self.og_data["description"]
32+
)
2933
self._create_meta(head_element, "property", "og:image", self.og_data["image"])
3034
self._create_meta(head_element, "property", "og:url", self.og_data["url"])
3135
self._create_meta(head_element, "property", "og:type", self.og_data["type"])
32-
self._create_meta(head_element, "property", "og:site_name", self.og_data["site_name"])
36+
self._create_meta(
37+
head_element, "property", "og:site_name", self.og_data["site_name"]
38+
)
3339

3440
# Twitter Card tags
3541
self._create_meta(head_element, "name", "twitter:card", "summary_large_image")
3642
self._create_meta(head_element, "name", "twitter:title", self.og_data["title"])
37-
self._create_meta(head_element, "name", "twitter:description", self.og_data["description"])
43+
self._create_meta(
44+
head_element, "name", "twitter:description", self.og_data["description"]
45+
)
3846
self._create_meta(head_element, "name", "twitter:image", self.og_data["image"])
3947

4048
def _create_meta(self, head_element, attr_name: str, attr_value: str, content: str):
@@ -45,14 +53,17 @@ def _create_meta(self, head_element, attr_name: str, attr_value: str, content: s
4553
# Use Python's built-in html.escape() which handles all necessary escaping
4654
escaped_attr_value = escape(attr_value, quote=True)
4755
escaped_content = escape(content, quote=True)
48-
meta_html = f'<meta {attr_name}="{escaped_attr_value}" content="{escaped_content}" />'
56+
meta_html = (
57+
f'<meta {attr_name}="{escaped_attr_value}" content="{escaped_content}" />'
58+
)
4959
head_element.prepend(meta_html, html=True)
5060

5161

5262
class ExistingMetaRemover:
5363
"""
5464
Element handler that removes existing OpenGraph and Twitter meta tags.
5565
"""
66+
5667
def element(self, element):
5768
"""Remove the element by calling remove()."""
5869
element.remove()
@@ -103,32 +114,38 @@ def get_opengraph_data(self, path: str) -> dict:
103114
"image": "https://images.unsplash.com/photo-1518770660439-4636190af475",
104115
"url": f"https://yoursite.com{path}",
105116
"type": "website",
106-
"site_name": "Python Workers Demo"
117+
"site_name": "Python Workers Demo",
107118
}
108119

109120
# Customize based on path
110121
if path.startswith("/blog/"):
111122
article_slug = path.replace("/blog/", "").strip("/")
112-
og_data.update({
113-
"title": f"Blog Post: {article_slug.replace('-', ' ').title()}",
114-
"description": f"Read our latest article about {article_slug.replace('-', ' ')}",
115-
"image": "https://images.unsplash.com/photo-1499750310107-5fef28a66643",
116-
"type": "article"
117-
})
123+
og_data.update(
124+
{
125+
"title": f"Blog Post: {article_slug.replace('-', ' ').title()}",
126+
"description": f"Read our latest article about {article_slug.replace('-', ' ')}",
127+
"image": "https://images.unsplash.com/photo-1499750310107-5fef28a66643",
128+
"type": "article",
129+
}
130+
)
118131
elif path.startswith("/products/"):
119132
product_slug = path.replace("/products/", "").strip("/")
120-
og_data.update({
121-
"title": f"Product: {product_slug.replace('-', ' ').title()}",
122-
"description": f"Check out our amazing {product_slug.replace('-', ' ')} product",
123-
"image": "https://images.unsplash.com/photo-1505740420928-5e560c06d30e",
124-
"type": "product"
125-
})
133+
og_data.update(
134+
{
135+
"title": f"Product: {product_slug.replace('-', ' ').title()}",
136+
"description": f"Check out our amazing {product_slug.replace('-', ' ')} product",
137+
"image": "https://images.unsplash.com/photo-1505740420928-5e560c06d30e",
138+
"type": "product",
139+
}
140+
)
126141
elif path == "/about":
127-
og_data.update({
128-
"title": "About Us - Python Workers",
129-
"description": "Learn more about our team and what we do with Python Workers",
130-
"image": "https://images.unsplash.com/photo-1522071820081-009f0129c71c"
131-
})
142+
og_data.update(
143+
{
144+
"title": "About Us - Python Workers",
145+
"description": "Learn more about our team and what we do with Python Workers",
146+
"image": "https://images.unsplash.com/photo-1522071820081-009f0129c71c",
147+
}
148+
)
132149

133150
return og_data
134151

@@ -150,4 +167,4 @@ def inject_opengraph_tags(self, response, og_data: dict):
150167
rewriter.on("head", to_js(meta_injector))
151168

152169
# Transform the response
153-
return rewriter.transform(response.js_object)
170+
return rewriter.transform(response.js_object)

13-js-api-pygments/py/src/entry.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,28 +30,26 @@ async def highlight_code(self, code: str, language: str = None) -> dict:
3030

3131
# Create formatter with line numbers and styling
3232
formatter = HtmlFormatter(
33-
linenos=True,
34-
cssclass="highlight",
35-
style="monokai"
33+
linenos=True, cssclass="highlight", style="monokai"
3634
)
3735

3836
# Generate highlighted HTML
3937
highlighted_html = highlight(code, lexer, formatter)
4038

4139
# Get the CSS for styling
42-
css = formatter.get_style_defs('.highlight')
40+
css = formatter.get_style_defs(".highlight")
4341

4442
return {
4543
"html": highlighted_html,
4644
"css": css,
4745
"language": lexer.name,
48-
"language_alias": lexer.aliases[0] if lexer.aliases else None
46+
"language_alias": lexer.aliases[0] if lexer.aliases else None,
4947
}
5048

5149
except ClassNotFound:
5250
return {
5351
"error": f"Language '{language}' not found",
5452
"html": f"<pre>{code}</pre>",
5553
"css": "",
56-
"language": "unknown"
57-
}
54+
"language": "unknown",
55+
}

13-js-api-pygments/ts/.vscode/settings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
"files.associations": {
33
"wrangler.json": "jsonc"
44
}
5-
}
5+
}

13-js-api-pygments/ts/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@
1515
"vitest": "~3.2.0",
1616
"wrangler": "^4.49.0"
1717
}
18-
}
18+
}

13-js-api-pygments/ts/worker-configuration.d.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable */
22
// Generated by Wrangler by running `wrangler types` (hash: b739a9c19cff1463949c4db47674ed86)
3-
// Runtime types generated with workerd@1.20251113.0 2025-11-19
3+
// Runtime types generated with workerd@1.20251113.0 2025-11-19
44
declare namespace Cloudflare {
55
interface GlobalProps {
66
mainModule: typeof import("./src/index");
@@ -7663,7 +7663,7 @@ interface IncomingRequestCfPropertiesTLSClientAuthPlaceholder {
76637663
certNotAfter: "";
76647664
}
76657665
/** Possible outcomes of TLS verification */
7666-
declare type CertVerificationStatus =
7666+
declare type CertVerificationStatus =
76677667
/** Authentication succeeded */
76687668
"SUCCESS"
76697669
/** No certificate was presented */
@@ -7727,7 +7727,7 @@ interface D1ExecResult {
77277727
count: number;
77287728
duration: number;
77297729
}
7730-
type D1SessionConstraint =
7730+
type D1SessionConstraint =
77317731
// Indicates that the first query should go to the primary, and the rest queries
77327732
// using the same D1DatabaseSession will go to any replica that is consistent with
77337733
// the bookmark maintained by the session (returned by the first query).
@@ -8308,7 +8308,7 @@ declare namespace Rpc {
83088308
// The reason for using a generic type here is to build a serializable subset of structured
83098309
// cloneable composite types. This allows types defined with the "interface" keyword to pass the
83108310
// serializable check as well. Otherwise, only types defined with the "type" keyword would pass.
8311-
type Serializable<T> =
8311+
type Serializable<T> =
83128312
// Structured cloneables
83138313
BaseType
83148314
// Structured cloneable composites

13-js-api-pygments/ts/wrangler.jsonc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@
1212
"service": "py-rpc-server"
1313
}
1414
]
15-
}
15+
}

0 commit comments

Comments
 (0)