Debian Codegraph @

docs/script/generate_social_card_previews.py

raw
1 """A helper script to test out what social previews look like.
2
3 I should remove this when I'm happy with the result.
4 """
5
6 # %load_ext autoreload
7 # %autoreload 2
8
9 from __future__ import annotations
10
11 import random
12 from pathlib import Path
13
14 from sphinxext.opengraph._social_cards import (
15 MAX_CHAR_DESCRIPTION,
16 MAX_CHAR_PAGE_TITLE,
17 create_social_card_objects,
18 render_social_card,
19 )
20
21 PROJECT_ROOT = Path(__file__).resolve().parent.parent.parent
22
23 # Dummy lorem text
24 lorem = """Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do
25 eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim
26 veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
27 consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
28 cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
29 proident, sunt in culpa qui officia deserunt mollit anim id est laborum""".split() # NoQA: SIM905
30
31 kwargs_fig = {
32 'image': PROJECT_ROOT / 'docs/_static/og-logo.png',
33 'image_mini': PROJECT_ROOT / 'sphinxext/opengraph/_static/sphinx-logo-shadow.png',
34 }
35
36 print('Generating previews of social media cards...')
37 plt_objects = create_social_card_objects(**kwargs_fig)
38 grid_items = []
39 for perm in range(20):
40 # Create dummy text description and pagetitle for this iteration
41 random.shuffle(lorem)
42 title = ' '.join(lorem[:100])
43 title = title[: MAX_CHAR_PAGE_TITLE - 3] + '...'
44
45 random.shuffle(lorem)
46 desc = ' '.join(lorem[:100])
47 desc = desc[: MAX_CHAR_DESCRIPTION - 3] + '...'
48
49 path_tmp = Path(PROJECT_ROOT / 'docs/tmp')
50 path_tmp.mkdir(exist_ok=True)
51 path_out = Path(path_tmp / f'num_{perm}.png')
52
53 plt_objects = render_social_card(
54 path=path_out,
55 site_title='Sphinx Social Card Demo',
56 page_title=title,
57 description=desc,
58 siteurl='sphinxext-opengraph.readthedocs.io',
59 plt_objects=plt_objects,
60 )
61
62 path_examples_page_folder = PROJECT_ROOT / 'docs' / 'tmp'
63 grid_items.append(f"""\
64 .. grid-item::
65
66 .. image:: ./tmp/{path_out.name}
67 """)
68
69 embed_text = '.. grid:: 2\n :gutter: 5\n\n' + '\n'.join(grid_items)
70
71 # Write text that we can use to embed these images in the docs
72 (PROJECT_ROOT / 'docs/tmp/embed.txt').write_text(embed_text)
73
74 print('Done generating previews of social media cards...')