Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add default author feature #125

Merged
merged 17 commits into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ share/python-wheels/
.installed.cfg
*.egg
MANIFEST
.idea

# PyInstaller
# Usually these files are written by a python script from a template
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ The plugin supports several configuration arguments to tailor its behavior to yo
- `verbose`: Toggles verbose output. Useful for debugging. (default: `True`)
- `enabled`: Toggles plugin activation. (default: `True`)
- `default_image`: Provides a fallback image URL if none is found in your content. (default: `None`)
- `default_author`: GitHub author email to use if no author is found for pages. (default: `None`)
- `add_desc`: Controls the generation of meta description tags. (default: `True`)
- `add_image`: Manages meta image tag generation. (default: `True`)
- `add_keywords`: Allows meta keyword tag generation. (default: `True`)
Expand All @@ -63,6 +64,7 @@ plugins:
verbose: True
enabled: True
default_image: "https://example.com/default-image.png"
default_author: "you@company.com"
add_desc: True
add_image: True
add_keywords: True
Expand Down
18 changes: 12 additions & 6 deletions plugin/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license

import json
from datetime import datetime, timedelta
from pathlib import Path
from subprocess import check_output

Expand All @@ -14,6 +15,10 @@
get_youtube_video_ids,
)

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


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

# Get the creation date
# Get the creation and last modified dates
args = ["git", "log", "--reverse", "--pretty=format:%ai", file_path]
creation_date = check_output(args).decode("utf-8").split("\n")[0]
git_info = {"creation_date": creation_date}

# Get the last modification date
last_modified_date = check_output(["git", "log", "-1", "--pretty=format:%ai", file_path]).decode("utf-8")
git_info["last_modified_date"] = last_modified_date
git_info = {
"creation_date": creation_date or DEFAULT_CREATION_DATE,
"last_modified_date": last_modified_date or DEFAULT_MODIFIED_DATE,
}

# Get the authors and their contributions count using get_github_usernames_from_file function
if self.config["add_authors"]:
authors_info = get_github_usernames_from_file(file_path)
authors_info = get_github_usernames_from_file(file_path, default_user=self.config["default_author"])
git_info["authors"] = [
(author, info["url"], info["changes"], info["avatar"]) for author, info in authors_info.items()
]
Expand Down
5 changes: 4 additions & 1 deletion plugin/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,13 @@ def get_github_username_from_email(email, cache, file_path="", verbose=True):
return None, None


def get_github_usernames_from_file(file_path):
def get_github_usernames_from_file(file_path, default_user=None):
"""
Fetch GitHub usernames associated with a file using Git Log and Git Blame commands.

Args:
file_path (str): The path to the file for which GitHub usernames are to be retrieved.
default_user (str, optional): Default GitHub user email to use if no authors found. Defaults to None.

Returns:
(dict): A dictionary where keys are GitHub usernames or emails (if username is not found) and values are dictionaries containing:
Expand Down Expand Up @@ -196,6 +197,8 @@ def get_github_usernames_from_file(file_path):

info = {}
for email, changes in emails.items():
if not email and default_user:
email = default_user
username, avatar = get_github_username_from_email(email, cache, file_path)
# If we can't determine the user URL, revert to the GitHub file URL
user_url = f"https://github.com/{username}" if username else github_repo_url
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "mkdocs-ultralytics-plugin"
version = "0.1.15"
version = "0.1.16"
description = "An MkDocs plugin that provides Ultralytics Docs customizations at https://docs.ultralytics.com."
readme = "README.md"
requires-python = ">=3.8"
Expand Down