88
99DEFAULT_DESCRIPTION_LENGTH = 200
1010
11+
1112class HTMLTextParser (HTMLParser ):
1213 """
1314 Parse HTML into text
1415 """
16+
1517 def __init__ (self ):
1618 super ().__init__ ()
1719 # All text found
@@ -22,7 +24,7 @@ def __init__(self):
2224
2325 def handle_starttag (self , tag , attrs ) -> None :
2426 self .level += 1
25-
27+
2628 def handle_endtag (self , tag ) -> None :
2729 self .level -= 1
2830
@@ -31,16 +33,23 @@ def handle_data(self, data) -> None:
3133 if self .level == 0 :
3234 self .text_outside_tags += data
3335
36+
3437class OGMetadataCreatorVisitor (nodes .NodeVisitor ):
3538 """
3639 Finds the title and creates a description from a doctree
3740 """
3841
39- def __init__ (self , desc_len : int , known_titles : Iterable [str ] = None , document : nodes .document = None ):
42+ def __init__ (
43+ self ,
44+ desc_len : int ,
45+ known_titles : Iterable [str ] = None ,
46+ document : nodes .document = None ,
47+ ):
4048
4149 # Hack to prevent requirement for the doctree to be passed in.
42- # It's only used by doctree.walk(...) to print debug messages.
50+ # It's only used by doctree.walk(...) to print debug messages.
4351 if document == None :
52+
4453 class document_cls :
4554 class reporter :
4655 @staticmethod
@@ -137,7 +146,9 @@ def make_tag(property: str, content: str) -> str:
137146 return f'<meta property="{ property } " content="{ content } " />\n '
138147
139148
140- def get_tags (context : Dict [str , Any ], doctree : nodes .document , config : Dict [str , Any ]) -> str :
149+ def get_tags (
150+ context : Dict [str , Any ], doctree : nodes .document , config : Dict [str , Any ]
151+ ) -> str :
141152
142153 # Set length of description
143154 try :
@@ -165,8 +176,7 @@ def get_tags(context: Dict[str, Any], doctree: nodes.document, config: Dict[str,
165176 # url tag
166177 # Get the URL of the specific page
167178 page_url = urljoin (
168- config ["ogp_site_url" ],
169- context ["pagename" ] + context ["file_suffix" ]
179+ config ["ogp_site_url" ], context ["pagename" ] + context ["file_suffix" ]
170180 )
171181 tags += make_tag ("og:url" , page_url )
172182
@@ -194,14 +204,20 @@ def get_tags(context: Dict[str, Any], doctree: nodes.document, config: Dict[str,
194204 tags += make_tag ("og:image:alt" , htp .text )
195205
196206 # custom tags
197- tags += ' \n ' .join (config [' ogp_custom_meta_tags' ])
207+ tags += " \n " .join (config [" ogp_custom_meta_tags" ])
198208
199209 return tags
200210
201211
202- def html_page_context (app : Sphinx , pagename : str , templatename : str , context : Dict [str , Any ], doctree : nodes .document ) -> None :
212+ def html_page_context (
213+ app : Sphinx ,
214+ pagename : str ,
215+ templatename : str ,
216+ context : Dict [str , Any ],
217+ doctree : nodes .document ,
218+ ) -> None :
203219 if doctree :
204- context [' metatags' ] += get_tags (context , doctree , app .config )
220+ context [" metatags" ] += get_tags (context , doctree , app .config )
205221
206222
207223def setup (app : Sphinx ) -> Dict [str , Any ]:
@@ -213,10 +229,9 @@ def setup(app: Sphinx) -> Dict[str, Any]:
213229 app .add_config_value ("ogp_site_name" , None , "html" )
214230 app .add_config_value ("ogp_custom_meta_tags" , [], "html" )
215231
216- app .connect (' html-page-context' , html_page_context )
232+ app .connect (" html-page-context" , html_page_context )
217233
218234 return {
219235 "parallel_read_safe" : True ,
220236 "parallel_write_safe" : True ,
221237 }
222-
0 commit comments