Skip to content

Commit 9637510

Browse files
authored
add a github_widget shortcode (#439)
1 parent 8347612 commit 9637510

9 files changed

+255
-0
lines changed

v8/github_widget/README.md

+135
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
# GitHub Widget Plugin for Nikola
2+
3+
This is a Nikola plugin that allows you to embed a GitHub repository widget in your Nikola-generated site. The widget displays repository details, including the description, languages, stars, forks, watchers, open issues, and latest activity (commits and releases). You can also customize the widget to show the repository owner's avatar or the default GitHub logo.
4+
5+
## Installation
6+
7+
```shell
8+
nikola plugin -i github_widget
9+
```
10+
11+
## Update conf.py (optional)
12+
13+
Optionally you can add an access token to your config. This is optional.
14+
15+
Without a token you can still use the shortcode, but you will get api rate limit errors if you use it too much.
16+
17+
# Add your GitHub API token here
18+
19+
The token should have repository -> Contents -> read-only permissions.
20+
21+
```python
22+
GITHUB_API_TOKEN = 'your_github_api_token_here'
23+
```
24+
25+
## Use the shortcode
26+
27+
here are some examples:
28+
29+
```markdown
30+
// Shortcode Example 1
31+
{{% github_widget %}}dacog/lazy-docker-compose-wordpress-setup{{% /github_widget %}}
32+
33+
// Shortcode Example 2
34+
{{% github_widget avatar=true max_width=400px %}}dacog/lazy-docker-compose-wordpress-setup{{%/github_widget %}}
35+
36+
// Shortcode Example 3
37+
{{% github_widget avatar=true latest_release=true latest_commit=true max_width=400px %}}dacog/textexpander_android{{%/github_widget %}}
38+
```
39+
40+
Which gives these widgets:
41+
42+
**Shortcode Example 1**
43+
44+
![Shortcode Example 1](imgs/example-1.png)
45+
46+
**Shortcode Example 2**
47+
48+
![Shortcode Example 2](imgs/example-2.png)
49+
50+
**Shortcode Example 3**
51+
52+
![Shortcode Example 3](imgs/example-3.png)
53+
54+
## CSS
55+
56+
Here is a sample CSS wich results in the examples above.
57+
58+
```css
59+
/* github shortcode */
60+
.github-widget {
61+
display: flex;
62+
align-items: center;
63+
border: 1px solid #ddd;
64+
padding: 10px;
65+
margin: 10px 0;
66+
border-radius: 5px;
67+
background-color: #f9f9f9;
68+
}
69+
70+
.github-widget-image {
71+
margin-right: 10px;
72+
}
73+
74+
.github-widget img {
75+
border-radius: 50%;
76+
}
77+
78+
.github-widget .repo-info {
79+
display: flex;
80+
flex-direction: column;
81+
}
82+
83+
.github-widget .repo-info h3 {
84+
margin: 0;
85+
font-size: 1.2em;
86+
}
87+
88+
.github-widget .repo-info p {
89+
margin: 5px 0;
90+
font-size: 0.9em;
91+
color: #555;
92+
}
93+
94+
.github-widget .repo-info ul {
95+
list-style: none;
96+
padding: 0;
97+
display: flex;
98+
gap: 10px;
99+
}
100+
101+
.github-widget .repo-info ul li {
102+
font-size: 0.8em;
103+
color: #333;
104+
}
105+
106+
.github-widget h4 {
107+
color: black;
108+
}
109+
```
110+
111+
## License
112+
113+
this widget is under the MIT License
114+
115+
MIT License
116+
117+
Copyright (c) [2024] [Diego Carrasco G.]
118+
119+
Permission is hereby granted, free of charge, to any person obtaining a copy
120+
of this software and associated documentation files (the "Software"), to deal
121+
in the Software without restriction, including without limitation the rights
122+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
123+
copies of the Software, and to permit persons to whom the Software is
124+
furnished to do so, subject to the following conditions:
125+
126+
The above copyright notice and this permission notice shall be included in all
127+
copies or substantial portions of the Software.
128+
129+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
130+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
131+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
132+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
133+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
134+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
135+
SOFTWARE.

v8/github_widget/__init__.py

Whitespace-only changes.

v8/github_widget/conf.py.sample

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
GITHUB_API_TOKEN = 'your_github_api_token_here'

v8/github_widget/github_widget.plugin

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[Core]
2+
Name = github_widget
3+
Module = github_widget
4+
5+
[Documentation]
6+
Author = Diego Carrasco G.
7+
Version = 0.1
8+
Website = https://plugins.getnikola.com/#github_widget
9+
Description = A shortcode to embed GitHub repository widgets.
10+
11+
[Nikola]
12+
MinVersion = 8.0.0 # I haven't tested it with older versions
13+
MaxVersion = 8.3.1
14+
PluginCategory = ShortCode

v8/github_widget/github_widget.py

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# MIT License
2+
#
3+
# Copyright (c) [2024] [Diego Carrasco G.]
4+
#
5+
# Permission is hereby granted, free of charge, to any person obtaining a copy
6+
# of this software and associated documentation files (the "Software"), to deal
7+
# in the Software without restriction, including without limitation the rights
8+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
# copies of the Software, and to permit persons to whom the Software is
10+
# furnished to do so, subject to the following conditions:
11+
#
12+
# The above copyright notice and this permission notice shall be included in all
13+
# copies or substantial portions of the Software.
14+
#
15+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
# SOFTWARE.
22+
23+
from nikola.plugin_categories import ShortcodePlugin
24+
from github import Github
25+
# Authentication is defined via github.Auth
26+
from github import Auth
27+
from github.GithubException import UnknownObjectException
28+
29+
30+
def render_github_widget(repo_data, show_avatar, max_width, latest_release_bool, latest_commit_bool):
31+
image_url = repo_data.owner.avatar_url if show_avatar else "https://github.githubassets.com/images/modules/logos_page/GitHub-Mark.png"
32+
33+
latest_activity_html = ""
34+
35+
if latest_commit_bool:
36+
# Get the latest commit
37+
latest_commit = repo_data.get_commits()[0]
38+
if latest_commit:
39+
latest_activity_html += f"<p><strong>Latest Commit:</strong> {latest_commit.commit.message} ({latest_commit.commit.author.date})</p>"
40+
41+
if latest_release_bool:
42+
try:
43+
# Get the latest release if exists, if not there will be a 404 error
44+
latest_release = repo_data.get_latest_release()
45+
46+
if latest_release:
47+
latest_activity_html += f"<p><strong>Latest Release:</strong> {latest_release.title} - {latest_release.body} ({latest_release.created_at})</p>"
48+
except UnknownObjectException:
49+
pass
50+
51+
widget_html = f"""
52+
<div class="github-widget" style="max-width: {max_width};">
53+
<div class="github-widget-image">
54+
<a href="{repo_data.html_url}" target="_blank">
55+
<img src="{image_url}" alt="{repo_data.owner}" max-width="50" max-height="50">
56+
</a>
57+
</div>
58+
<div class="repo-info">
59+
<a href="{repo_data.html_url}" target="_blank">
60+
<h3>{repo_data.name}</h3>
61+
</a>
62+
<p>{repo_data.description}</p>
63+
<p><strong>Languages:</strong> {repo_data.language}</p>
64+
<ul>
65+
<li>⭐ Stars: {repo_data.stargazers_count}</li>
66+
<li><svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-git-branch UnderlineNav-octicon">
67+
<path d="M9.5 3.25a2.25 2.25 0 1 1 3 2.122V6A2.5 2.5 0 0 1 10 8.5H6a1 1 0 0 0-1 1v1.128a2.251 2.251 0 1 1-1.5 0V5.372a2.25 2.25 0 1 1 1.5 0v1.836A2.493 2.493 0 0 1 6 7h4a1 1 0 0 0 1-1v-.628A2.25 2.25 0 0 1 9.5 3.25Zm-6 0a.75.75 0 1 0 1.5 0 .75.75 0 0 0-1.5 0Zm8.25-.75a.75.75 0 1 0 0 1.5.75.75 0 0 0 0-1.5ZM4.25 12a.75.75 0 1 0 0 1.5.75.75 0 0 0 0-1.5Z"></path>
68+
</svg> Forks: {repo_data.forks_count}</li>
69+
<li>👁 Watchers: {repo_data.subscribers_count}</li>
70+
<li>❗ Open Issues: {repo_data.open_issues_count}</li>
71+
</ul>
72+
{latest_activity_html}
73+
</div>
74+
</div>
75+
"""
76+
return widget_html
77+
78+
79+
class GitHubWidgetPlugin(ShortcodePlugin):
80+
name = "github_widget"
81+
82+
def handler(self, **kwargs):
83+
data = kwargs.get('data', '').strip().split()
84+
repo = data[0]
85+
show_avatar = kwargs.get('avatar', False)
86+
max_width = kwargs.get('max_width', '100%')
87+
latest_release_bool = kwargs.get('latest_release', False)
88+
latest_commit_bool = kwargs.get('latest_commit', False)
89+
90+
token = self.site.config.get('GITHUB_API_TOKEN')
91+
92+
if token:
93+
# using an access token
94+
auth = Auth.Token(token)
95+
g = Github(auth=auth)
96+
else:
97+
g = Github() # without token there will be api rate limits.
98+
99+
repo_data = g.get_repo(repo)
100+
101+
if repo_data:
102+
return render_github_widget(repo_data, show_avatar, max_width, latest_release_bool, latest_commit_bool), []
103+
else:
104+
return f"<p>Repository '{repo}' not found or an error occurred.</p>", []

v8/github_widget/imgs/example-1.png

79 KB
Loading

v8/github_widget/imgs/example-2.png

71.5 KB
Loading

v8/github_widget/imgs/example-3.png

105 KB
Loading

v8/github_widget/requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
PyGithub

0 commit comments

Comments
 (0)