1515 from typing import Any
1616
1717 from sphinx .application import Sphinx
18+ from sphinx .config import Config
19+ from sphinx .environment import BuildEnvironment
1820 from sphinx .util .typing import ExtensionMetadata
1921
2022try :
@@ -165,34 +167,16 @@ def get_tags(
165167 and config_social .get ("enable" ) is not False
166168 and create_social_card is not None
167169 ):
168- # Description
169- description_max_length = config_social .get (
170- "description_max_length" , DEFAULT_DESCRIPTION_LENGTH_SOCIAL_CARDS - 3
171- )
172- if len (description ) > description_max_length :
173- description = description [:description_max_length ].strip () + "..."
174-
175- # Page title
176- pagetitle = title
177- if len (pagetitle ) > DEFAULT_PAGE_LENGTH_SOCIAL_CARDS :
178- pagetitle = pagetitle [:DEFAULT_PAGE_LENGTH_SOCIAL_CARDS ] + "..."
179-
180- # Site URL
181- site_url = config_social .get ("site_url" , True )
182- if site_url is True :
183- url_text = app .config .ogp_site_url .split ("://" )[- 1 ]
184- elif isinstance (site_url , str ):
185- url_text = site_url
186-
187- # Plot an image with the given metadata to the output path
188- image_path = create_social_card (
189- app ,
190- config_social ,
191- site_name ,
192- pagetitle ,
193- description ,
194- url_text ,
195- context ["pagename" ],
170+ image_url = social_card_for_page (
171+ config_social = config_social ,
172+ site_name = site_name ,
173+ title = title ,
174+ description = description ,
175+ pagename = context ["pagename" ],
176+ srcdir = app .srcdir ,
177+ outdir = app .outdir ,
178+ config = app .config ,
179+ env = app .env ,
196180 )
197181 ogp_use_first_image = False
198182
@@ -202,12 +186,6 @@ def get_tags(
202186 else :
203187 ogp_image_alt = description
204188
205- # Link the image in our page metadata
206- # We use os.path.sep to standardize behavior acros *nix and Windows
207- url = app .config .ogp_site_url .strip ("/" )
208- image_path = str (image_path ).replace (os .path .sep , "/" ).strip ("/" )
209- image_url = f"{ url } /{ image_path } "
210-
211189 # If the social card objects have been added we add special metadata for them
212190 # These are the dimensions *in pixels* of the card
213191 # They were chosen by looking at the image pixel dimensions on disk
@@ -267,6 +245,59 @@ def get_tags(
267245 )
268246
269247
248+ def social_card_for_page (
249+ config_social : dict [str , bool | str ],
250+ site_name : str ,
251+ title : str ,
252+ description : str ,
253+ pagename : str ,
254+ * ,
255+ srcdir : str | Path ,
256+ outdir : str | Path ,
257+ config : Config ,
258+ env : BuildEnvironment ,
259+ ):
260+ # Description
261+ description_max_length = config_social .get (
262+ "description_max_length" , DEFAULT_DESCRIPTION_LENGTH_SOCIAL_CARDS - 3
263+ )
264+ if len (description ) > description_max_length :
265+ description = description [:description_max_length ].strip () + "..."
266+
267+ # Page title
268+ pagetitle = title
269+ if len (pagetitle ) > DEFAULT_PAGE_LENGTH_SOCIAL_CARDS :
270+ pagetitle = pagetitle [:DEFAULT_PAGE_LENGTH_SOCIAL_CARDS ] + "..."
271+
272+ # Site URL
273+ site_url = config_social .get ("site_url" , True )
274+ if site_url is True :
275+ url_text = config .ogp_site_url .split ("://" )[- 1 ]
276+ elif isinstance (site_url , str ):
277+ url_text = site_url
278+
279+ # Plot an image with the given metadata to the output path
280+ image_path = create_social_card (
281+ config_social ,
282+ site_name ,
283+ pagetitle ,
284+ description ,
285+ url_text ,
286+ pagename ,
287+ srcdir = srcdir ,
288+ outdir = outdir ,
289+ config = config ,
290+ env = env ,
291+ )
292+
293+ # Link the image in our page metadata
294+ # We use os.path.sep to standardize behavior acros *nix and Windows
295+ url = config .ogp_site_url .strip ("/" )
296+ image_path = str (image_path ).replace (os .path .sep , "/" ).strip ("/" )
297+ image_url = f"{ url } /{ image_path } "
298+ return image_url
299+
300+
270301def html_page_context (
271302 app : Sphinx ,
272303 pagename : str ,
0 commit comments