File: generate_social_card_previews.py

package info (click to toggle)
sphinxext-opengraph 0.12.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,200 kB
  • sloc: python: 1,130; makefile: 11; sh: 8
file content (74 lines) | stat: -rw-r--r-- 2,359 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
"""A helper script to test out what social previews look like.

I should remove this when I'm happy with the result.
"""

# %load_ext autoreload
# %autoreload 2

from __future__ import annotations

import random
from pathlib import Path

from sphinxext.opengraph._social_cards import (
    MAX_CHAR_DESCRIPTION,
    MAX_CHAR_PAGE_TITLE,
    create_social_card_objects,
    render_social_card,
)

PROJECT_ROOT = Path(__file__).resolve().parent.parent.parent

# Dummy lorem text
lorem = """Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim
veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum""".split()  # NoQA: SIM905

kwargs_fig = {
    'image': PROJECT_ROOT / 'docs/_static/og-logo.png',
    'image_mini': PROJECT_ROOT / 'sphinxext/opengraph/_static/sphinx-logo-shadow.png',
}

print('Generating previews of social media cards...')
plt_objects = create_social_card_objects(**kwargs_fig)
grid_items = []
for perm in range(20):
    # Create dummy text description and pagetitle for this iteration
    random.shuffle(lorem)
    title = ' '.join(lorem[:100])
    title = title[: MAX_CHAR_PAGE_TITLE - 3] + '...'

    random.shuffle(lorem)
    desc = ' '.join(lorem[:100])
    desc = desc[: MAX_CHAR_DESCRIPTION - 3] + '...'

    path_tmp = Path(PROJECT_ROOT / 'docs/tmp')
    path_tmp.mkdir(exist_ok=True)
    path_out = Path(path_tmp / f'num_{perm}.png')

    plt_objects = render_social_card(
        path=path_out,
        site_title='Sphinx Social Card Demo',
        page_title=title,
        description=desc,
        siteurl='sphinxext-opengraph.readthedocs.io',
        plt_objects=plt_objects,
    )

    path_examples_page_folder = PROJECT_ROOT / 'docs' / 'tmp'
    grid_items.append(f"""\
   .. grid-item::

      .. image:: ./tmp/{path_out.name}
""")

embed_text = '.. grid:: 2\n   :gutter: 5\n\n' + '\n'.join(grid_items)

# Write text that we can use to embed these images in the docs
(PROJECT_ROOT / 'docs/tmp/embed.txt').write_text(embed_text)

print('Done generating previews of social media cards...')