1- from workers import WorkerEntrypoint , Response , Request , fetch
1+ from workers import WorkerEntrypoint , Request , fetch
22from js import HTMLRewriter
33from urllib .parse import urlparse
44from html import escape
55
66from pyodide .ffi import to_js
77
8+
89class 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
5262class 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 )
0 commit comments