Skip to content

Commit 1289268

Browse files
authored
Merge pull request #32 from timvink/add_markdown_tags
Add markdown tags
2 parents 59f8e17 + 350b47b commit 1289268

File tree

11 files changed

+378
-350
lines changed

11 files changed

+378
-350
lines changed

README.md

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -39,25 +39,29 @@ plugins:
3939

4040
## Usage
4141

42+
### In supported themes
43+
44+
no supported themes *yet*.
45+
4246
### In markdown pages
4347

44-
You can use ``{{ git_authors_summary }}`` to insert a summary of the authors of a page. Authors are sorted by their name and have a `mailto:` link with their email.
48+
You can use the following jinja tags to insert content into your markdown pages:
4549

46-
An example output:
50+
- ``{{ git_page_authors }}`` a summary of the authors of a page. Output wrapped in `<span class='git-page-authors'>`
51+
- ``{{ git_site_authors }}`` a summary of all authors of all pages in your site. Output wrapped in `<span class='git-site-authors'>`
52+
53+
For example, adding ``{{ git_page_authors }}`` will insert:
4754

4855
```html
49-
<span class='git-authors'><a href='mailto:[email protected]'>Jane Doe</a><a href='mailto:[email protected]'>John Doe</a></span>
56+
<span class='git-page-authors'><a href='mailto:[email protected]'>Jane Doe</a><a href='mailto:[email protected]'>John Doe</a></span>
5057
```
5158

5259
Which renders as:
5360

54-
> [Jane Doe](mailto:#), [John Doe](mailto:#)
55-
56-
### In supported themes
61+
> [Jane Doe](mailto:[email protected]), [John Doe](mailto:[email protected])
5762

58-
no supported themes *yet*.
5963

60-
### In theme customization
64+
### In theme customizations
6165

6266
[MkDocs](https://www.mkdocs.org/) offers possibilities to [customize an existing theme](https://www.mkdocs.org/user-guide/styling-your-docs/#customizing-a-theme).
6367

@@ -71,7 +75,7 @@ As an example, if you use [mkdocs-material](https://github.com/squidfunk/mkdocs-
7175
{% block disqus %}
7276
<div class="md-source-date">
7377
<small>
74-
Authors: {{ git_authors_summary }}
78+
Authors: {{ git_page_authors }}
7579
</small>
7680
</div>
7781
{% include "partials/integrations/disqus.html" %}
@@ -88,46 +92,53 @@ theme:
8892

8993
### In theme templates
9094

91-
To add more detailed author information to your theme you can [customize a MkDocs theme](https://www.mkdocs.org/user-guide/styling-your-docs/#customizing-a-theme) or even [develop your own](https://www.mkdocs.org/user-guide/custom-themes/). When enabling this plugin, you will have access to the jinja2 variable `git_authors`, which contains a list of authors dicts, like the following example:
95+
To add more detailed git author information to your theme you can [customize a MkDocs theme](https://www.mkdocs.org/user-guide/styling-your-docs/#customizing-a-theme) or even [develop your own](https://www.mkdocs.org/user-guide/custom-themes/).
96+
97+
When enabling this plugin, you will have access to the jinja2 variable `git_info` which contains as dict with the following structure:
9298

9399
```python
94-
[{
100+
{
101+
'page_authors' : [
102+
{
95103
'name' : 'Jane Doe',
96104
'email' : '[email protected]',
97105
'last_datetime' : datetime.datetime(),
98106
'lines' : 200,
99107
'contribution' : '40.0%'
100-
},
101-
{
108+
},
109+
{
102110
'name' : 'John Doe',
103111
'email' : '[email protected]',
104112
'last_datetime' : datetime.datetime(),
105113
'lines' : 300,
106114
'contribution' : '60.0%'
107-
}]
115+
}
116+
],
117+
'site_authors' : # same structure
118+
}
108119
```
109120

110121
#### Example usage in theme development
111122

112123
An example of how to use in your templates:
113124

114125
```django hljs
115-
{% if git_authors %}
116-
{%- for author in git_authors -%}
126+
{% if git_info %}
127+
{%- for author in git_info.get('page_authors') -%}
117128
<a href="{{ author.email }}" title="{{ author.name }}">
118129
{{ author.name }}
119130
</a>,
120131
{%- endfor -%}
121132
{% endif %}
122133
```
123134

124-
Alternatively, you could use the simple preformatted ``{{ git_authors_summary }}`` to insert a summary of the authors.
135+
Alternatively, you could use the simple pre-formatted `{{ git_page_authors }}` to insert a summary of the authors.
125136

126137
## Options
127138

128139
### `show_contribution`
129140

130-
If this option is set to `true` (default: `false`) The contribution to a page is
141+
If this option is set to `true` (default: `false`) the contribution of a author is
131142
printed as a percentage of (source file) lines per author. The output is
132143
suppressed if there is only *one* author for a page.
133144

@@ -136,6 +147,16 @@ Example output:
136147
* Authors: [John Doe](#) (33.33%), [Jane Doe](#) (66.67%) *(more than one author)*
137148
* Authors: [John Doe](#) *(one author)*
138149

150+
### `show_line_count`
151+
152+
If this option is set to `true` (default: `false`) the number of lines per author is shown.
153+
154+
### `count_empty_lines`
155+
156+
If this option is set to `true` (default: `false`) empty lines will count towards an authors' contribution.
157+
158+
## Tricks
159+
139160
### Aggregating Authors
140161

141162
In some repositories authors may have committed with differing name/email combinations.

mkdocs_git_authors_plugin/git/author.py

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ def add_lines(self, page: Page, commit: Commit, lines: int = 1):
3333
"""
3434
path = page.path()
3535
entry = self.page(path, page)
36-
entry['lines'] += lines
37-
current_dt = entry.get('datetime')
36+
entry["lines"] += lines
37+
current_dt = entry.get("datetime")
3838
commit_dt = commit.datetime()
3939
if not current_dt or commit_dt > current_dt:
40-
entry['datetime'] = commit_dt
41-
entry['datetime_str'] = commit.datetime(str)
40+
entry["datetime"] = commit_dt
41+
entry["datetime_str"] = commit.datetime(str)
4242

4343
def contribution(self, path=None, _type=float):
4444
"""
@@ -59,15 +59,13 @@ def contribution(self, path=None, _type=float):
5959
"""
6060
lines = self.lines(path)
6161
total_lines = (
62-
self.page(path)['page'].total_lines()
63-
if path
64-
else self.repo().total_lines()
62+
self.page(path)["page"].total_lines() if path else self.repo().total_lines()
6563
)
6664
result = lines / total_lines
6765
if _type == float:
6866
return result
6967
else:
70-
return str(round(result * 100, 2)) + '%'
68+
return str(round(result * 100, 2)) + "%"
7169

7270
def datetime(self, path, fmt=str):
7371
"""
@@ -81,7 +79,7 @@ def datetime(self, path, fmt=str):
8179
a formatted string (fmt=str)
8280
or a datetime.datetime object with tzinfo
8381
"""
84-
key = 'datetime_str' if fmt == str else 'datetime'
82+
key = "datetime_str" if fmt == str else "datetime"
8583
return self.page(path).get(key)
8684

8785
def email(self):
@@ -107,11 +105,9 @@ def lines(self, path=None):
107105
or on the given page.
108106
"""
109107
if path:
110-
return self.page(path)['lines']
108+
return self.page(path)["lines"]
111109
else:
112-
return sum([
113-
v['lines'] for v in self._pages.values()
114-
])
110+
return sum([v["lines"] for v in self._pages.values()])
115111

116112
def name(self):
117113
"""
@@ -149,8 +145,8 @@ def page(self, path, page=None):
149145
"""
150146
if not self._pages.get(path):
151147
self._pages[path] = {
152-
'page': page or self.repo().page(path),
153-
'lines': 0
148+
"page": page or self.repo().page(path),
149+
"lines": 0
154150
# datetime and datetime_str will be populated later
155151
}
156152
return self._pages[path]

mkdocs_git_authors_plugin/git/command.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import subprocess
22

3+
34
class GitCommandError(Exception):
45
"""
56
Exception thrown by a GitCommand.
67
"""
8+
79
pass
810

911

@@ -53,30 +55,29 @@ def run(self):
5355
Note: usually the result will be used through the methods.
5456
"""
5557

56-
args = ['git']
58+
args = ["git"]
5759
args.append(self._command)
5860
args.extend(self._args)
5961
p = subprocess.run(
6062
args,
6163
# encoding='utf8', # Uncomment after dropping support for python 3.5
6264
stdout=subprocess.PIPE,
63-
stderr=subprocess.PIPE
65+
stderr=subprocess.PIPE,
6466
)
6567
try:
6668
p.check_returncode()
6769
except subprocess.CalledProcessError:
68-
msg = ['GitCommand error:']
69-
msg.append('Command "%s" failed' % ' '.join(args))
70-
msg.append('Return code: %s' % p.returncode)
71-
msg.append('Output:')
70+
msg = ["GitCommand error:"]
71+
msg.append('Command "%s" failed' % " ".join(args))
72+
msg.append("Return code: %s" % p.returncode)
73+
msg.append("Output:")
7274
msg.append(p.stdout.decode("utf-8"))
73-
msg.append('Error messages:')
75+
msg.append("Error messages:")
7476
msg.append(p.stderr.decode("utf-8"))
75-
raise GitCommandError('\n'.join(msg))
76-
77-
self._stdout = p.stdout.decode("utf-8").strip('\'\n').split('\n')
78-
self._stderr = p.stderr.decode("utf-8").strip('\'\n').split('\n')
79-
77+
raise GitCommandError("\n".join(msg))
78+
79+
self._stdout = p.stdout.decode("utf-8").strip("'\n").split("\n")
80+
self._stderr = p.stderr.decode("utf-8").strip("'\n").split("\n")
8081

8182
self._completed = True
8283
return int(str(p.returncode))
@@ -109,7 +110,7 @@ def stderr(self):
109110
string list
110111
"""
111112
if not self._completed:
112-
raise GitCommandError('Trying to read from uncompleted GitCommand')
113+
raise GitCommandError("Trying to read from uncompleted GitCommand")
113114
return self._stderr
114115

115116
def stdout(self):
@@ -122,5 +123,5 @@ def stdout(self):
122123
string list
123124
"""
124125
if not self._completed:
125-
raise GitCommandError('Trying to read from uncompleted GitCommand')
126+
raise GitCommandError("Trying to read from uncompleted GitCommand")
126127
return self._stdout

mkdocs_git_authors_plugin/git/commit.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from .repo import AbstractRepoObject, Repo
33
from .. import util
44

5+
56
class Commit(AbstractRepoObject):
67
"""
78
Information about a single commit.
@@ -20,7 +21,7 @@ def __init__(
2021
author_email: str,
2122
author_time: str,
2223
author_tz: str,
23-
summary: str
24+
summary: str,
2425
):
2526
"""Initialize a commit from its SHA.
2627
@@ -33,15 +34,12 @@ def __init__(
3334

3435
super().__init__(repo)
3536

36-
# Replace <>
37+
# Replace <>
3738
# from '<[email protected]>'
3839
39-
author_email = re.sub(r"\<|\>","", author_email)
40-
41-
self._author = self.repo().author(
42-
author_name,
43-
author_email
44-
)
40+
author_email = re.sub(r"\<|\>", "", author_email)
41+
42+
self._author = self.repo().author(author_name, author_email)
4543
self._datetime = util.commit_datetime(author_time, author_tz)
4644
self._datetime_string = util.commit_datetime_string(self._datetime)
4745
self._summary = summary

0 commit comments

Comments
 (0)