Skip to content

Commit

Permalink
Fix sorting; new options
Browse files Browse the repository at this point in the history
  • Loading branch information
liang2kl committed Sep 8, 2021
1 parent 850d223 commit 88306ab
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 13 deletions.
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,13 @@ And it's done! You can open the page where you insert `{{ blog_content }}` and s
Optionally, you can customize some behaviours of the plugin:

```yml
size: 5 # Number of articles in one page, default: 10
locale: en # The locale for time localizations, default: system's locale
size: 5 # Number of articles in one page, default: 10
locale: en # The locale for time localizations, default: system's locale
sort:
from: new # Sort from new to old, default
# or old Sort from old to new
paging: false # Disable paging
from: new # Sort from new to old, default
# or old # Sort from old to new
by: creation # Sort by the first commit time, default
# or revision # Sort by the latest commit time
paging: false # Disable paging
show_total: false # Remove 'total pages' label
```
25 changes: 19 additions & 6 deletions mkdocs_blogging_plugin/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ class BloggingPlugin(BasePlugin):
config_scheme = (
("dirs", config_options.Type(list, default=[])),
("size", config_options.Type(int, default=10)),
("sort", config_options.Type(dict, default={"from": "old"})),
("sort", config_options.Type(dict, default={"from": "new", "by": "creation"})),
("locale", config_options.Type(str, default=None)),
("paging", config_options.Type(bool, default=True)),
("show_total", config_options.Type(bool, default=True)),
)

blog_pages = []
Expand All @@ -29,9 +30,10 @@ class BloggingPlugin(BasePlugin):
size = 0
additional_html = None
docs_dirs = []
sort = ""
sort = {}
locale = None
paging = True
show_total = True

util = Util()

Expand All @@ -44,8 +46,14 @@ class BloggingPlugin(BasePlugin):
def on_config(self, config):
self.size = self.config.get("size")
self.docs_dirs = self.config.get("dirs")
self.sort = self.config.get("sort")
self.paging = self.config.get("paging")
self.sort = self.config.get("sort")
self.show_total = self.config.get("show_total")

if "from" not in self.sort:
self.sort["from"] = "new"
if "by" not in self.sort:
self.sort["by"] = "creation"

# Abort with error with 'navigation.instant' feature on
# because paging won't work with it.
Expand Down Expand Up @@ -80,7 +88,7 @@ def on_page_content(self, html, page, config, files):
for dir in self.docs_dirs:
if page.file.src_path[:len(dir)] == dir \
and (not "exclude_from_blog" in page.meta or not page.meta["exclude_from_blog"]):
timestamp = self.util.get_git_commit_timestamp(page.file.abs_src_path)
timestamp = self.util.get_git_commit_timestamp(page.file.abs_src_path, is_first_commit=self.sort["by"] != "revision")
page.meta["git-timestamp"] = timestamp
page.meta["localized-time"] = self.util.get_localized_date(timestamp, False, self.locale)
self.blog_pages.append(page)
Expand All @@ -94,9 +102,14 @@ def on_post_page(self, output, page, config):
if not re.findall(pattern, output, flags=re.IGNORECASE):
return output
if not self.additional_html:
self.blog_pages = sorted(self.blog_pages, key=lambda page: page.meta["git-timestamp"])
self.blog_pages = sorted(self.blog_pages,
key=lambda page: page.meta["git-timestamp"],
reverse=self.sort["from"] == "new")
self.additional_html = self.template.render(
pages=self.blog_pages, page_size=self.size, sort=self.sort, paging=self.paging)
pages=self.blog_pages, page_size=self.size, sort=self.sort,
paging=self.paging, is_revision=self.sort["by"] == "revision",
show_total=self.show_total
)

output = re.sub(
pattern,
Expand Down
4 changes: 3 additions & 1 deletion mkdocs_blogging_plugin/templates/blog.html
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ <h3 class="post-title">
<span class="post-timestamp">
{% if pg.meta and pg.meta["localized-time"] %}
<span class="post-timestamp-update">
Updated at: {{ pg.meta["localized-time"] -}}
{{"Updated" if is_revision else "Published"}} at: {{ pg.meta["localized-time"] -}}
</span>
{% endif %}
</span>
Expand All @@ -101,5 +101,7 @@ <h3 class="post-title">
href="#page{{ num + 1 }}">{{ num + 1 }}</a>
{% endfor %}
</div>
{% if show_total %}
<div>Total <b>{{ pages|count }}</b> posts.</div>
{% endif %}
</div>
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

setup(
name="mkdocs-blogging-plugin",
version="0.1",
version="0.1.1",
description="Mkdocs plugin that generates a blog index page sorted by creation date.",
long_description=LONG_DESCRIPTION,
long_description_content_type="text/markdown",
Expand All @@ -24,6 +24,7 @@
license="MIT",
python_requires=">=3.6",
classifiers=[
'Development Status :: 4 - Beta',
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.6",
"Programming Language :: Python :: 3.7",
Expand Down

0 comments on commit 88306ab

Please sign in to comment.