-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #41 from ru-fu/DOCPR519-contributors
Add contributor listing
- Loading branch information
Showing
6 changed files
with
213 additions
and
6 deletions.
There are no files selected for viewing
This file contains 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
81 changes: 81 additions & 0 deletions
81
canonical-sphinx-extensions/contributor-listing/__init__.py
This file contains 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,81 @@ | ||
from git import Repo, InvalidGitRepositoryError | ||
from sphinx.util import logging | ||
import os | ||
from . import common | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def setup(app): | ||
app.connect("html-page-context", setup_func) | ||
|
||
common.add_css(app, "contributors.css") | ||
common.add_js(app, "contributors.js") | ||
|
||
return {"version": "1.0.0", | ||
"parallel_read_safe": True, | ||
"parallel_write_safe": True} | ||
|
||
|
||
def setup_func(app, pagename, templatename, context, doctree): | ||
def get_contributors_for_file(pagename, page_source_suffix): | ||
|
||
if ( | ||
"display_contributors" not in context | ||
or "github_folder" not in context | ||
or "github_url" not in context | ||
): | ||
return [] | ||
|
||
if context["display_contributors"]: | ||
filename = f"{pagename}{page_source_suffix}" | ||
paths = context["github_folder"][1:] + filename | ||
|
||
try: | ||
repo = Repo(".") | ||
except InvalidGitRepositoryError: | ||
cwd = os.getcwd() | ||
ghfolder = context["github_folder"][:-1] | ||
if ghfolder and cwd.endswith(ghfolder): | ||
repo = Repo(cwd.rpartition(ghfolder)[0]) | ||
else: | ||
logger.warning( | ||
"The local Git repository could not be found." | ||
) | ||
return | ||
|
||
since = None | ||
|
||
if ( | ||
"display_contributors_since" in context | ||
and context["display_contributors_since"] | ||
and context["display_contributors_since"].strip() | ||
): | ||
since = context["display_contributors_since"] | ||
|
||
commits = repo.iter_commits(paths=paths, since=since) | ||
|
||
contributors_dict = {} | ||
for commit in commits: | ||
contributor = commit.author.name | ||
if ( | ||
contributor not in contributors_dict | ||
or commit.committed_date > | ||
contributors_dict[contributor]["date"] | ||
): | ||
contributors_dict[contributor] = { | ||
"date": commit.committed_date, | ||
"sha": commit.hexsha, | ||
} | ||
# github_page contains the link to the contributor's latest commit | ||
contributors_list = [ | ||
(name, f"{context['github_url']}/commit/{data['sha']}") | ||
for name, data in contributors_dict.items() | ||
] | ||
|
||
return sorted(contributors_list) | ||
|
||
else: | ||
return [] | ||
|
||
context["get_contributors_for_file"] = get_contributors_for_file |
47 changes: 47 additions & 0 deletions
47
canonical-sphinx-extensions/contributor-listing/_static/contributors.css
This file contains 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,47 @@ | ||
.display-contributors { | ||
color: var(--color-sidebar-link-text); | ||
cursor: pointer; | ||
} | ||
.all-contributors { | ||
display: none; | ||
z-index: 55; | ||
list-style: none; | ||
position: fixed; | ||
top: 0; | ||
bottom: 0; | ||
left: 0; | ||
right: 0; | ||
width: 200px; | ||
height: 200px; | ||
overflow-y: scroll; | ||
margin: auto; | ||
padding: 0; | ||
background: var(--color-background-primary); | ||
scrollbar-color: var(--color-foreground-border) transparent; | ||
scrollbar-width: thin; | ||
} | ||
|
||
.all-contributors li:hover { | ||
background: var(--color-sidebar-item-background--hover); | ||
width: 100%; | ||
} | ||
|
||
.all-contributors li a{ | ||
color: var(--color-sidebar-link-text); | ||
padding: 1rem; | ||
display: inline-block; | ||
} | ||
|
||
#overlay { | ||
position: fixed; | ||
display: none; | ||
width: 100%; | ||
height: 100%; | ||
top: 0; | ||
left: 0; | ||
right: 0; | ||
bottom: 0; | ||
background-color: rgba(0,0,0,0.5); | ||
z-index: 2; | ||
cursor: pointer; | ||
} |
12 changes: 12 additions & 0 deletions
12
canonical-sphinx-extensions/contributor-listing/_static/contributors.js
This file contains 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,12 @@ | ||
$(document).ready(function() { | ||
$(document).on("click", function () { | ||
$(".all-contributors").hide(); | ||
$("#overlay").hide(); | ||
}); | ||
|
||
$('.display-contributors').click(function(event) { | ||
$('.all-contributors').toggle(); | ||
$("#overlay").toggle(); | ||
event.stopPropagation(); | ||
}); | ||
}) |
This file contains 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 @@ | ||
../common.py |
This file contains 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,6 +1,6 @@ | ||
[metadata] | ||
name = canonical-sphinx-extensions | ||
version = 0.0.22 | ||
version = 0.0.23 | ||
author = Ruth Fuchss | ||
author_email = [email protected] | ||
description = A collection of Sphinx extensions used by Canonical documentation | ||
|
@@ -22,6 +22,7 @@ packages = | |
canonical.terminal-output | ||
filtered-toc | ||
canonical.filtered-toc | ||
canonical.contributor-listing | ||
package_dir = | ||
canonical = canonical-sphinx-extensions | ||
= canonical-sphinx-extensions | ||
|
@@ -31,6 +32,7 @@ install_requires = | |
requests | ||
beautifulsoup4 | ||
docutils | ||
gitpython | ||
|
||
[options.package_data] | ||
* = _static/* |