-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* First pass at adding social card previews * Refactor social card generation * Fixing tests * Document nox * Package data * Fix plt objects path * Add matplotlib to dev requirements * Run black * Fix path separator * Blackify everything * Extra strip * Strip image path * Oh god please fix it * Strips
- Loading branch information
Showing
16 changed files
with
575 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
sphinx | ||
matplotlib | ||
wheel==0.37.1 | ||
pytest==7.1.3 | ||
beautifulsoup4==4.11.1 | ||
setuptools==65.4.1 | ||
setuptools==65.4.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
myst-parser==0.18.1 | ||
furo==2022.9.29 | ||
sphinx==5.2.3 | ||
sphinx-design | ||
./ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
""" | ||
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 pathlib import Path | ||
from textwrap import dedent | ||
from sphinxext.opengraph.socialcards import ( | ||
render_social_card, | ||
MAX_CHAR_PAGE_TITLE, | ||
MAX_CHAR_DESCRIPTION, | ||
) | ||
import random | ||
|
||
here = Path(__file__).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 | ||
|
||
kwargs_fig = dict( | ||
image=here / "../source/_static/og-logo.png", | ||
image_mini=here / "../../sphinxext/opengraph/_static/sphinx-logo-shadow.png", | ||
) | ||
|
||
print("Generating previews of social media cards...") | ||
plt_objects = None | ||
embed_text = [] | ||
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(here / "../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, | ||
kwargs_fig=kwargs_fig, | ||
) | ||
|
||
path_examples_page_folder = here / ".." | ||
embed_text.append( | ||
dedent( | ||
f""" | ||
````{{grid-item}} | ||
```{{image}} ../{path_out.relative_to(path_examples_page_folder)} | ||
``` | ||
```` | ||
""" | ||
) | ||
) | ||
|
||
embed_text = "\n".join(embed_text) | ||
embed_text = f""" | ||
`````{{grid}} 2 | ||
:gutter: 5 | ||
{embed_text} | ||
````` | ||
""" | ||
|
||
# Write markdown text that we can use to embed these images in the docs | ||
(here / "../tmp/embed.txt").write_text(embed_text) | ||
|
||
print("Done generating previews of social media cards...") |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,9 @@ | ||
```{include} ../../README.md | ||
:relative-images: | ||
:relative-docs: docs/source | ||
``` | ||
|
||
```{toctree} | ||
:hidden: | ||
socialcards | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# Social media card images | ||
|
||
This extension will automatically generate a PNG meant for sharing documentation links on social media platforms. | ||
These cards display metadata about the page that you link to, and are meant to catch the attention of readers. | ||
|
||
See [the opengraph.xyz website](https://www.opengraph.xyz/) for a way to preview what your social media cards look like. | ||
Here's an example of what the card for this page looks like: | ||
|
||
% This is auto-generated at build time | ||
```{image} ../tmp//num_0.png | ||
:width: 500 | ||
``` | ||
|
||
## Disable card images | ||
|
||
To disable social media card images, use the following configuration: | ||
|
||
```{code-block} python | ||
:caption: conf.py | ||
ogp_social_cards = { | ||
"enable": False | ||
} | ||
``` | ||
|
||
## Customize the card | ||
|
||
There are several customization options to change the text and look of the social media preview card. | ||
Below is a summary of these options. | ||
|
||
- **`site_url`**: Set a custom site URL. | ||
- **`image`**: Over-ride the top-right image (by default, `html_logo` is used). | ||
- **`line_color`**: Color of the border line at the bottom of the card, in hex format. | ||
% TODO: add an over-ride for each part of the card. | ||
|
||
## Example social cards | ||
|
||
Below are several social cards to give an idea for how this extension behaves with different length and size of text. | ||
|
||
```{include} ../tmp/embed.txt | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
""" | ||
Configuration to automatically run jobs and tests via `nox`. | ||
For example, to build the documentation with a live server: | ||
nox -s docs -- live | ||
List available jobs: | ||
nox -l | ||
ref: https://nox.thea.codes/ | ||
""" | ||
import nox | ||
from shlex import split | ||
|
||
nox.options.reuse_existing_virtualenvs = True | ||
|
||
|
||
@nox.session | ||
def docs(session): | ||
"""Build the documentation. Use `-- live` to build with a live server.""" | ||
session.install("-e", ".") | ||
session.install("-r", "docs/requirements.txt") | ||
if "live" in session.posargs: | ||
session.install("ipython") | ||
session.install("sphinx-autobuild") | ||
session.run(*split("sphinx-autobuild -b html docs/source docs/build/html")) | ||
else: | ||
session.run( | ||
*split("sphinx-build -nW --keep-going -b html docs/source docs/build/html") | ||
) | ||
|
||
|
||
@nox.session | ||
def test(session): | ||
"""Run the test suite.""" | ||
session.install(".") | ||
session.run(*(["pytest"] + session.posargs)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.