-
-
Notifications
You must be signed in to change notification settings - Fork 30
ENH: Add social card previews #88
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Daltz333
merged 14 commits into
sphinx-doc:main
from
choldgraf:feat-social-card-previews
Feb 7, 2023
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
35c138b
First pass at adding social card previews
choldgraf d4e8a7c
Refactor social card generation
choldgraf b4550b2
Fixing tests
choldgraf f6e908d
Document nox
choldgraf 32a9216
Package data
choldgraf 11d4236
Fix plt objects path
choldgraf f6f1e95
Add matplotlib to dev requirements
choldgraf ff69d8e
Run black
choldgraf fa5e49b
Fix path separator
choldgraf 96aadcf
Blackify everything
choldgraf 5029be7
Extra strip
choldgraf fc212b3
Strip image path
choldgraf 9b66501
Oh god please fix it
choldgraf dbd27be
Strips
choldgraf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.