Skip to content

Commit

Permalink
Correct mistake
Browse files Browse the repository at this point in the history
  • Loading branch information
RobertoPrevato committed Oct 1, 2022
1 parent b933457 commit 604261b
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ __pycache__
styles/*.css
styles/*.css.map
__*.html
__*.py
__file_out.py
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.0.7] - 2022-10-01
## [0.0.8] - 2022-10-01
- Improves the Gantt extension:
- - now supports multiple periods in the same row
- - now supports activities using the start date from the previous activity (automatic dates)
Expand Down
87 changes: 87 additions & 0 deletions neoteroi/contribs/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
"""
This module provide a plugin that can be used to generate last modified time and
contributors list for each page, assuming that:
- the MkDocs site is built in a Git repository.
- the Git CLI can be used during the build.
"""
import logging
from datetime import datetime
from pathlib import Path
from typing import List

from mkdocs.config import config_options as c
from mkdocs.plugins import BasePlugin
from mkdocs.structure.files import File
from mkdocs.structure.pages import Page

from neoteroi.contribs.domain import ContributionsReader, Contributor
from neoteroi.contribs.git import GitContributionsReader
from neoteroi.contribs.html import ContribsViewOptions, render_contribution_stats

logger = logging.getLogger("MARKDOWN")


class ContribsPlugin(BasePlugin):
_contribs_reader: ContributionsReader
config_scheme = (
("contributors_label", c.Type(str, default="Contributors")),
("last_modified_label", c.Type(str, default="Last modified on")),
("last_modified_time", c.Type(bool, default=True)),
("time_format", c.Type(str, default="%Y-%m-%d %H:%M:%S")),
("contributors", c.Type(list, default=[])),
)

def __init__(self) -> None:
super().__init__()
self._contribs_reader = GitContributionsReader()

def _get_contributors(self, page_file: File) -> List[Contributor]:
contributors = self._contribs_reader.get_contributors(
Path("docs") / page_file.src_path
)
info = self.config.get("contributors")

if not info:
return contributors

for contributor in contributors:
contributor_info = next(
(item for item in info if item.get("email") == contributor.email), None
)
if contributor_info:
contributor.image = contributor_info.get("image")

return contributors

def _get_last_commit_date(self, page_file: File) -> datetime:
return self._contribs_reader.get_last_commit_date(
Path("docs") / page_file.src_path
)

def _set_contributors(self, markdown: str, page: Page) -> str:
page_file = page.file
last_commit_date = self._get_last_commit_date(page_file)
contributors = self._get_contributors(page_file)
return markdown + render_contribution_stats(
contributors,
last_commit_date,
ContribsViewOptions(
self.config["contributors_label"],
self.config["last_modified_label"],
self.config["last_modified_time"],
self.config["time_format"],
),
)

def on_page_markdown(self, markdown, *args, **kwargs):
try:
markdown = self._set_contributors(markdown, kwargs["page"])
except ValueError:
logger.error(
"Failed to display contributors list for page: %s",
kwargs["page"].title,
)
pass
return markdown
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def readme():

setup(
name="neoteroi-mkdocs",
version="0.0.7",
version="0.0.8",
description="Plugins for MkDocs and Python Markdown",
long_description=readme(),
long_description_content_type="text/markdown",
Expand Down

0 comments on commit 604261b

Please sign in to comment.