Skip to content

Commit 8f7d73b

Browse files
Add default author feature (#125)
Co-authored-by: UltralyticsAssistant <web@ultralytics.com>
1 parent 926c105 commit 8f7d73b

File tree

5 files changed

+20
-8
lines changed

5 files changed

+20
-8
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ share/python-wheels/
2525
.installed.cfg
2626
*.egg
2727
MANIFEST
28+
.idea
2829

2930
# PyInstaller
3031
# Usually these files are written by a python script from a template

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ The plugin supports several configuration arguments to tailor its behavior to yo
4545
- `verbose`: Toggles verbose output. Useful for debugging. (default: `True`)
4646
- `enabled`: Toggles plugin activation. (default: `True`)
4747
- `default_image`: Provides a fallback image URL if none is found in your content. (default: `None`)
48+
- `default_author`: GitHub author email to use if no author is found for pages. (default: `None`)
4849
- `add_desc`: Controls the generation of meta description tags. (default: `True`)
4950
- `add_image`: Manages meta image tag generation. (default: `True`)
5051
- `add_keywords`: Allows meta keyword tag generation. (default: `True`)
@@ -63,6 +64,7 @@ plugins:
6364
verbose: True
6465
enabled: True
6566
default_image: "https://example.com/default-image.png"
67+
default_author: "you@company.com"
6668
add_desc: True
6769
add_image: True
6870
add_keywords: True

plugin/main.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license
22

33
import json
4+
from datetime import datetime, timedelta
45
from pathlib import Path
56
from subprocess import check_output
67

@@ -14,6 +15,10 @@
1415
get_youtube_video_ids,
1516
)
1617

18+
today = datetime.now()
19+
DEFAULT_CREATION_DATE = (today - timedelta(days=365)).strftime("%Y-%m-%d %H:%M:%S +0000")
20+
DEFAULT_MODIFIED_DATE = (today - timedelta(days=40)).strftime("%Y-%m-%d %H:%M:%S +0000")
21+
1722

1823
class MetaPlugin(BasePlugin):
1924
"""
@@ -36,6 +41,7 @@ class MetaPlugin(BasePlugin):
3641
("verbose", config_options.Type(bool, default=True)), # Enable verbose output for debugging
3742
("enabled", config_options.Type(bool, default=True)), # Enable or disable the plugin
3843
("default_image", config_options.Type(str, default=None)), # Default image URL if none found in content
44+
("default_author", config_options.Type(str, default=None)), # Default GitHub author email if none found
3945
("add_desc", config_options.Type(bool, default=True)), # Add meta description tags
4046
("add_image", config_options.Type(bool, default=True)), # Add meta image tags
4147
("add_keywords", config_options.Type(bool, default=True)), # Add meta keywords tags
@@ -71,18 +77,18 @@ def get_git_info(self, file_path):
7177
"""
7278
file_path = str(Path(file_path).resolve())
7379

74-
# Get the creation date
80+
# Get the creation and last modified dates
7581
args = ["git", "log", "--reverse", "--pretty=format:%ai", file_path]
7682
creation_date = check_output(args).decode("utf-8").split("\n")[0]
77-
git_info = {"creation_date": creation_date}
78-
79-
# Get the last modification date
8083
last_modified_date = check_output(["git", "log", "-1", "--pretty=format:%ai", file_path]).decode("utf-8")
81-
git_info["last_modified_date"] = last_modified_date
84+
git_info = {
85+
"creation_date": creation_date or DEFAULT_CREATION_DATE,
86+
"last_modified_date": last_modified_date or DEFAULT_MODIFIED_DATE,
87+
}
8288

8389
# Get the authors and their contributions count using get_github_usernames_from_file function
8490
if self.config["add_authors"]:
85-
authors_info = get_github_usernames_from_file(file_path)
91+
authors_info = get_github_usernames_from_file(file_path, default_user=self.config["default_author"])
8692
git_info["authors"] = [
8793
(author, info["url"], info["changes"], info["avatar"]) for author, info in authors_info.items()
8894
]

plugin/utils.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,12 +135,13 @@ def get_github_username_from_email(email, cache, file_path="", verbose=True):
135135
return None, None
136136

137137

138-
def get_github_usernames_from_file(file_path):
138+
def get_github_usernames_from_file(file_path, default_user=None):
139139
"""
140140
Fetch GitHub usernames associated with a file using Git Log and Git Blame commands.
141141
142142
Args:
143143
file_path (str): The path to the file for which GitHub usernames are to be retrieved.
144+
default_user (str, optional): Default GitHub user email to use if no authors found. Defaults to None.
144145
145146
Returns:
146147
(dict): A dictionary where keys are GitHub usernames or emails (if username is not found) and values are dictionaries containing:
@@ -196,6 +197,8 @@ def get_github_usernames_from_file(file_path):
196197

197198
info = {}
198199
for email, changes in emails.items():
200+
if not email and default_user:
201+
email = default_user
199202
username, avatar = get_github_username_from_email(email, cache, file_path)
200203
# If we can't determine the user URL, revert to the GitHub file URL
201204
user_url = f"https://github.com/{username}" if username else github_repo_url

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ build-backend = "setuptools.build_meta"
2424

2525
[project]
2626
name = "mkdocs-ultralytics-plugin"
27-
version = "0.1.15"
27+
version = "0.1.16"
2828
description = "An MkDocs plugin that provides Ultralytics Docs customizations at https://docs.ultralytics.com."
2929
readme = "README.md"
3030
requires-python = ">=3.8"

0 commit comments

Comments
 (0)