|
| 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>", [] |
0 commit comments