Skip to content

Commit

Permalink
add a github_widget shortcode (#439)
Browse files Browse the repository at this point in the history
  • Loading branch information
dacog authored Jun 23, 2024
1 parent 8347612 commit 9637510
Show file tree
Hide file tree
Showing 9 changed files with 255 additions and 0 deletions.
135 changes: 135 additions & 0 deletions v8/github_widget/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
# GitHub Widget Plugin for Nikola

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.

## Installation

```shell
nikola plugin -i github_widget
```

## Update conf.py (optional)

Optionally you can add an access token to your config. This is optional.

Without a token you can still use the shortcode, but you will get api rate limit errors if you use it too much.

# Add your GitHub API token here

The token should have repository -> Contents -> read-only permissions.

```python
GITHUB_API_TOKEN = 'your_github_api_token_here'
```

## Use the shortcode

here are some examples:

```markdown
// Shortcode Example 1
{{% github_widget %}}dacog/lazy-docker-compose-wordpress-setup{{% /github_widget %}}

// Shortcode Example 2
{{% github_widget avatar=true max_width=400px %}}dacog/lazy-docker-compose-wordpress-setup{{%/github_widget %}}

// Shortcode Example 3
{{% github_widget avatar=true latest_release=true latest_commit=true max_width=400px %}}dacog/textexpander_android{{%/github_widget %}}
```

Which gives these widgets:

**Shortcode Example 1**

![Shortcode Example 1](imgs/example-1.png)

**Shortcode Example 2**

![Shortcode Example 2](imgs/example-2.png)

**Shortcode Example 3**

![Shortcode Example 3](imgs/example-3.png)

## CSS

Here is a sample CSS wich results in the examples above.

```css
/* github shortcode */
.github-widget {
display: flex;
align-items: center;
border: 1px solid #ddd;
padding: 10px;
margin: 10px 0;
border-radius: 5px;
background-color: #f9f9f9;
}

.github-widget-image {
margin-right: 10px;
}

.github-widget img {
border-radius: 50%;
}

.github-widget .repo-info {
display: flex;
flex-direction: column;
}

.github-widget .repo-info h3 {
margin: 0;
font-size: 1.2em;
}

.github-widget .repo-info p {
margin: 5px 0;
font-size: 0.9em;
color: #555;
}

.github-widget .repo-info ul {
list-style: none;
padding: 0;
display: flex;
gap: 10px;
}

.github-widget .repo-info ul li {
font-size: 0.8em;
color: #333;
}

.github-widget h4 {
color: black;
}
```

## License

this widget is under the MIT License

MIT License

Copyright (c) [2024] [Diego Carrasco G.]

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Empty file added v8/github_widget/__init__.py
Empty file.
1 change: 1 addition & 0 deletions v8/github_widget/conf.py.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
GITHUB_API_TOKEN = 'your_github_api_token_here'
14 changes: 14 additions & 0 deletions v8/github_widget/github_widget.plugin
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[Core]
Name = github_widget
Module = github_widget

[Documentation]
Author = Diego Carrasco G.
Version = 0.1
Website = https://plugins.getnikola.com/#github_widget
Description = A shortcode to embed GitHub repository widgets.

[Nikola]
MinVersion = 8.0.0 # I haven't tested it with older versions
MaxVersion = 8.3.1
PluginCategory = ShortCode
104 changes: 104 additions & 0 deletions v8/github_widget/github_widget.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
# MIT License
#
# Copyright (c) [2024] [Diego Carrasco G.]
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

from nikola.plugin_categories import ShortcodePlugin
from github import Github
# Authentication is defined via github.Auth
from github import Auth
from github.GithubException import UnknownObjectException


def render_github_widget(repo_data, show_avatar, max_width, latest_release_bool, latest_commit_bool):
image_url = repo_data.owner.avatar_url if show_avatar else "https://github.githubassets.com/images/modules/logos_page/GitHub-Mark.png"

latest_activity_html = ""

if latest_commit_bool:
# Get the latest commit
latest_commit = repo_data.get_commits()[0]
if latest_commit:
latest_activity_html += f"<p><strong>Latest Commit:</strong> {latest_commit.commit.message} ({latest_commit.commit.author.date})</p>"

if latest_release_bool:
try:
# Get the latest release if exists, if not there will be a 404 error
latest_release = repo_data.get_latest_release()

if latest_release:
latest_activity_html += f"<p><strong>Latest Release:</strong> {latest_release.title} - {latest_release.body} ({latest_release.created_at})</p>"
except UnknownObjectException:
pass

widget_html = f"""
<div class="github-widget" style="max-width: {max_width};">
<div class="github-widget-image">
<a href="{repo_data.html_url}" target="_blank">
<img src="{image_url}" alt="{repo_data.owner}" max-width="50" max-height="50">
</a>
</div>
<div class="repo-info">
<a href="{repo_data.html_url}" target="_blank">
<h3>{repo_data.name}</h3>
</a>
<p>{repo_data.description}</p>
<p><strong>Languages:</strong> {repo_data.language}</p>
<ul>
<li>⭐ Stars: {repo_data.stargazers_count}</li>
<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">
<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>
</svg> Forks: {repo_data.forks_count}</li>
<li>👁 Watchers: {repo_data.subscribers_count}</li>
<li>❗ Open Issues: {repo_data.open_issues_count}</li>
</ul>
{latest_activity_html}
</div>
</div>
"""
return widget_html


class GitHubWidgetPlugin(ShortcodePlugin):
name = "github_widget"

def handler(self, **kwargs):
data = kwargs.get('data', '').strip().split()
repo = data[0]
show_avatar = kwargs.get('avatar', False)
max_width = kwargs.get('max_width', '100%')
latest_release_bool = kwargs.get('latest_release', False)
latest_commit_bool = kwargs.get('latest_commit', False)

token = self.site.config.get('GITHUB_API_TOKEN')

if token:
# using an access token
auth = Auth.Token(token)
g = Github(auth=auth)
else:
g = Github() # without token there will be api rate limits.

repo_data = g.get_repo(repo)

if repo_data:
return render_github_widget(repo_data, show_avatar, max_width, latest_release_bool, latest_commit_bool), []
else:
return f"<p>Repository '{repo}' not found or an error occurred.</p>", []
Binary file added v8/github_widget/imgs/example-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added v8/github_widget/imgs/example-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added v8/github_widget/imgs/example-3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions v8/github_widget/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PyGithub

0 comments on commit 9637510

Please sign in to comment.