sphinx_social_cards.plugins.github
¶
This plugin demonstrates how to add custom context and layouts. The purpose here is to get information from GitHub (eg stars, forks, etc) and use it in a new pre-designed layout.
Enabling¶
To enable this plugin, add it to your list of sphinx extensions
in conf.py:
extensions = [
"sphinx_social_cards",
"sphinx_social_cards.plugins.github",
]
Configuration¶
This extension uses html_theme_options
[repo_url
] from
sphinx-immaterial configuration value or its own repo_url
configuration
value to set the repository URL.
-
repo_url : str =
''
¶ repo_url = "https://github.com/2bndy5/sphinx-social-cards"
The repository’s required identifying information (
owner
andrepo
) can also be parsed from thesite_url
if it uses a standard GitHub Pages address (https://<owner>.github.io/<repo>
).
Tip
Information from GitHub is fetched using GitHub’s REST API endpoints. If there is a
need to authenticate HTTP GET requests from the REST API, then set an environment
variable GITHUB_REST_API_TOKEN
with the generated access token. This is mostly
useful when fetching information about private repositories or to avoid the REST API
rate limit.
Dependencies¶
To cache the owner/repository information, the appdirs dependency is needed. This can either be install directly:
pip install appdirs
or using the sphinx-social-cards
package’s optional dependency:
sphinx-social-cards[github]
Implementation details about the cached information
By default, the cache of owner/repository information is updated once a day (unless the cache is purged).
def get_cache_dir() -> str:
time_fmt = "%B %#d %Y" if platform.system().lower() == "windows" else "%B %-d %Y"
today = time.strftime(time_fmt, time.localtime())
cache_dir = Path(
user_cache_dir(
"sphinx_social_cards.plugins.github",
"2bndy5",
version=today, # (1)!
)
)
if cache_dir.parent.exists() and not cache_dir.exists():
shutil.rmtree(cache_dir.parent) # purge the old cache
return str(cache_dir)
Following the appdirs README, this will create a cache according to the OS used:
On Windows:
%LOCALAPPDATA%\2bndy5\sphinx_social_cards.plugins.github\Cache\<month> <day> <year>
On Linux:
/home/$(whoami)/.cache/sphinx_social_cards.plugins.github/<month> <day> <year>
On MacOS:
/Users/$(id -un)/Library/Caches/sphinx_social_cards.plugins.github/<month> <day> <year>
Added Layouts¶
These are examples of the layouts added:
Added Context¶
This plugin adds the following members to the plugin.*
jinja context:
- class Github¶
A
dict
that can be accessed viaplugin.vcs.github
jinja context
repo
Sub-Context¶
- class Repo¶
A
dict
that can be accessed viaplugin.vcs.github.repo
jinja context- languages : Dict[str, float]¶
A
dict
of the used program languages in the repository. Each key is a language’s name (eg. “Python”), and each value is the corresponding language’s percent used (eg “97.8”).
- homepage : str | None¶
The repository’s designated outreach website (with protocols like
https://
stripped).
- tags : List[str]¶
A
list
of the repository’s tags (str
). These values seem to be ordered in recent descending to oldest tagged commits.
- contributors : List[Contributor]¶
A
list
of the repository’scontributor
s.
owner
Sub-Context¶
- class Owner¶
A
dict
that can be accessed viaplugin.vcs.github.owner
jinja context- organizations : List[Organization]¶
A
list
of theOrganization
s to which the account belongs.
User List Sub-Contexts¶
- class Contributor¶
A
dict
that can be accessed via each item listed in theplugin.vcs.github.repo.contributors
jinja context
- class Organization¶
A
dict
that can be accessed via each item listed in theplugin.vcs.github.owner.organizations
jinja context