Skip to content

Commit e339515

Browse files
Florian Haastimvink
authored andcommitted
Optionally hide email addresses in rendered output
Sometimes, it may be prudent/desirable just to render the authors' names, but not their email address. In order to enable that, introduce a new configuration option, show_email_address, which defaults to true. When true, it retains the prior behaviour of rendering authors' names as "mailto:" links. When false, it only renders the authors' names in plain text. Also, add a unit test to check the new option's behaviour, and update the documentation to explain its purpose.
1 parent 452ab06 commit e339515

File tree

6 files changed

+48
-6
lines changed

6 files changed

+48
-6
lines changed

docs/options.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ plugins:
77
- git-authors:
88
show_contribution: true
99
show_line_count: true
10+
show_email_address: true
1011
count_empty_lines: true
1112
fallback_to_empty: false
1213
exclude:
@@ -29,6 +30,12 @@ Example output:
2930

3031
If this option is set to `true` (default: `false`) the number of lines per author is shown.
3132

33+
## `show_email_address`
34+
35+
If this option is set to `true` (default: `true`), then authors' names
36+
are rendered as a `mailto:` link pointing to their email address. If
37+
set to `false`, they are shown in plain text.
38+
3239
## `count_empty_lines`
3340

3441
If this option is set to `true` (default: `false`) empty lines will count towards an authors' contribution.

mkdocs_git_authors_plugin/plugin.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class GitAuthorsPlugin(BasePlugin):
1515
config_scheme = (
1616
("show_contribution", config_options.Type(bool, default=False)),
1717
("show_line_count", config_options.Type(bool, default=False)),
18+
("show_email_address", config_options.Type(bool, default=True)),
1819
("count_empty_lines", config_options.Type(bool, default=True)),
1920
("fallback_to_empty", config_options.Type(bool, default=False)),
2021
("exclude", config_options.Type(list, default=[])),

mkdocs_git_authors_plugin/util.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,20 @@ def page_authors_summary(page, config: dict):
5050

5151
authors = page.get_authors()
5252
authors_summary = []
53+
author_name = ""
54+
5355
for author in authors:
5456
contrib = (
5557
" (%s)" % author.contribution(page.path(), str)
5658
if page.repo().config("show_contribution") and len(page.get_authors()) > 1
5759
else ""
5860
)
59-
authors_summary.append(
60-
"<a href='mailto:%s'>%s</a>%s" % (author.email(), author.name(), contrib)
61-
)
61+
if page.repo().config("show_email_address"):
62+
author_name = "<a href='mailto:%s'>%s</a>" % (author.email(), author.name())
63+
else:
64+
author_name = author.name()
65+
authors_summary.append("%s%s" % (author_name, contrib))
66+
6267
authors_summary = ", ".join(authors_summary)
6368
return "<span class='git-page-authors git-authors'>%s</span>" % authors_summary
6469

@@ -87,6 +92,7 @@ def site_authors_summary(authors, config: dict):
8792
"""
8893
show_contribution = config["show_contribution"]
8994
show_line_count = config["show_line_count"]
95+
show_email_address = config["show_email_address"]
9096

9197
result = """
9298
<span class='git-authors'>
@@ -97,11 +103,15 @@ def site_authors_summary(authors, config: dict):
97103
" (%s)" % author.contribution(None, str) if show_contribution else ""
98104
)
99105
lines = ": %s lines" % author.lines() if show_line_count else ""
106+
author_name = ""
107+
if show_email_address:
108+
author_name = "<a href='mailto:%s'>%s</a>" % (author.email(), author.name())
109+
else:
110+
author_name = author.name()
100111
result += """
101-
<li><a href='mailto:{author_email}'>{author_name}</a>{lines}{contribution}</li>
112+
<li>{author_name}{lines}{contribution}</li>
102113
""".format(
103-
author_email=author.email(),
104-
author_name=author.name(),
114+
author_name=author_name,
105115
lines=lines,
106116
contribution=contribution,
107117
)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
site_name: test gitauthors_plugin
2+
use_directory_urls: true
3+
4+
plugins:
5+
- search
6+
- git-authors:
7+
show_email_address: false

tests/test_basic.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,22 @@ def test_basic_working(tmp_path, mkdocs_file):
7979

8080

8181

82+
def test_no_email(tmp_path):
83+
84+
result = build_docs_setup("tests/basic_setup/mkdocs_no_email.yml", tmp_path)
85+
assert result.exit_code == 0, (
86+
"'mkdocs build' command failed. Error: %s" % result.stdout
87+
)
88+
89+
index_file = tmp_path / "index.html"
90+
assert index_file.exists(), "%s does not exist" % index_file
91+
92+
contents = index_file.read_text()
93+
assert re.search("<span class='git-page-authors", contents)
94+
assert re.search("<li>Tim Vink</li>", contents)
95+
96+
97+
8298
def test_exclude_working(tmp_path):
8399

84100
result = build_docs_setup("tests/basic_setup/mkdocs_exclude.yml", tmp_path)

tests/test_util.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
DEFAULT_CONFIG = {
3030
"show_contribution": False,
3131
"show_line_count": False,
32+
"show_email_address": True,
3233
"count_empty_lines": True,
3334
"sort_authors_by_name": True,
3435
"sort_reverse": False,

0 commit comments

Comments
 (0)