15
15
from typing import Any
16
16
17
17
from sphinx .application import Sphinx
18
+ from sphinx .config import Config
19
+ from sphinx .environment import BuildEnvironment
18
20
from sphinx .util .typing import ExtensionMetadata
19
21
20
22
try :
@@ -165,34 +167,16 @@ def get_tags(
165
167
and config_social .get ("enable" ) is not False
166
168
and create_social_card is not None
167
169
):
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 ,
196
180
)
197
181
ogp_use_first_image = False
198
182
@@ -202,12 +186,6 @@ def get_tags(
202
186
else :
203
187
ogp_image_alt = description
204
188
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
-
211
189
# If the social card objects have been added we add special metadata for them
212
190
# These are the dimensions *in pixels* of the card
213
191
# They were chosen by looking at the image pixel dimensions on disk
@@ -267,6 +245,59 @@ def get_tags(
267
245
)
268
246
269
247
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
+
270
301
def html_page_context (
271
302
app : Sphinx ,
272
303
pagename : str ,
0 commit comments