Skip to content

Commit

Permalink
Add natural sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
rkk1995 committed Jul 24, 2022
1 parent dab8b01 commit f23fcbd
Show file tree
Hide file tree
Showing 10 changed files with 614 additions and 278 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,16 @@ order: desc

> **Note:** Unlike the default order, this does not distinguish between files and directories. Therefore pages and sections might get mixed.
### Natural Sort Type

Create a file named `.pages` in a directory and set the `sort_type` attribute to `natural` to use [natural sort order](https://en.wikipedia.org/wiki/Natural_sort_order).

This can be combined with `order` above.

```yaml
sort_type: natural
```

### Collapse Single Nested Pages

> **Note:** This feature is disabled by default. More on how to use it below
Expand Down
15 changes: 14 additions & 1 deletion mkdocs_awesome_pages_plugin/meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,11 @@ class Meta:
COLLAPSE_SINGLE_PAGES_ATTRIBUTE = "collapse_single_pages"
HIDE_ATTRIBUTE = "hide"
ORDER_ATTRIBUTE = "order"
SORT_TYPE_ATTRIBUTE = "sort_type"

ORDER_ASC = "asc"
ORDER_DESC = "desc"
SORT_NATURAL = "natural"

def __init__(
self,
Expand All @@ -124,7 +126,8 @@ def __init__(
collapse: bool = None,
collapse_single_pages: bool = None,
hide: bool = None,
order: Optional[str] = None
order: Optional[str] = None,
sort_type: Optional[str] = None,
):

if nav is None and arrange is not None:
Expand All @@ -139,6 +142,7 @@ def __init__(
self.collapse_single_pages = collapse_single_pages
self.hide = hide
self.order = order
self.sort_type = sort_type

@staticmethod
def try_load_from(path: Optional[str]) -> "Meta":
Expand All @@ -160,6 +164,7 @@ def load_from(path: str) -> "Meta":
collapse_single_pages = contents.get(Meta.COLLAPSE_SINGLE_PAGES_ATTRIBUTE)
hide = contents.get(Meta.HIDE_ATTRIBUTE)
order = contents.get(Meta.ORDER_ATTRIBUTE)
sort_type = contents.get(Meta.SORT_TYPE_ATTRIBUTE)

if title is not None:
if not isinstance(title, str):
Expand Down Expand Up @@ -232,6 +237,13 @@ def load_from(path: str) -> "Meta":
attribute=Meta.ORDER_ATTRIBUTE, order=order, context=path
)
)
if sort_type is not None:
if sort_type != Meta.SORT_NATURAL:
raise TypeError(
'Expected "{attribute}" to be "natural" - got "{sort_type}" [{context}]'.format(
attribute=Meta.SORT_TYPE_ATTRIBUTE, sort_type=sort_type, context=path
)
)

return Meta(
title=title,
Expand All @@ -242,4 +254,5 @@ def load_from(path: str) -> "Meta":
collapse_single_pages=collapse_single_pages,
hide=hide,
order=order,
sort_type=sort_type,
)
13 changes: 8 additions & 5 deletions mkdocs_awesome_pages_plugin/navigation.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import warnings
from pathlib import Path
from natsort import natsort_keygen
from typing import List, Optional, Union, Set

from mkdocs.structure.nav import (
Expand Down Expand Up @@ -78,11 +79,13 @@ def _process_children(self, children: List[NavigationItem], collapse: bool, meta
return result

def _order(self, items: List[NavigationItem], meta: Meta):
if meta.order is not None:
items.sort(
key=lambda i: basename(self._get_item_path(i)),
reverse=meta.order == Meta.ORDER_DESC,
)
order, sort_type = meta.order, meta.sort_type
if order is None and sort_type is None:
return
key = lambda i: basename(self._get_item_path(i))
if sort_type == Meta.SORT_NATURAL:
key = natsort_keygen(key)
items.sort(key=key, reverse=order == Meta.ORDER_DESC)

def _nav(self, items: List[NavigationItem], meta: Meta) -> List[NavigationItem]:
if meta.nav is None:
Expand Down
2 changes: 2 additions & 0 deletions mkdocs_awesome_pages_plugin/tests/e2e/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def pagesFile(
collapse_single_pages: bool = None,
hide: bool = None,
order: Optional[str] = None,
sort_type: Optional[str] = None,
) -> Tuple[str, str]:

data = self._removeDictNoneValues(
Expand All @@ -41,6 +42,7 @@ def pagesFile(
"collapse_single_pages": collapse_single_pages,
"hide": hide,
"order": order,
"sort_type": sort_type,
}
)

Expand Down
133 changes: 0 additions & 133 deletions mkdocs_awesome_pages_plugin/tests/e2e/test_order.py

This file was deleted.

0 comments on commit f23fcbd

Please sign in to comment.