lukasgeiter / mkdocs-awesome-pages-plugin

An MkDocs plugin that simplifies configuring page titles and their order
MIT License
452 stars 35 forks source link

Add natural sorting #65

Closed rkk1995 closed 1 year ago

rkk1995 commented 1 year ago

Natural sorting is important for documents that have file names that are numbered.

Take for example

>>> items
['3000.', '4.']
>>> print(sorted(items))
['3000.', '4.']
>>> print(os_sorted(items, key = lambda i: i,reverse = False))
['4.', '3000.']
>>>
rkk1995 commented 1 year ago

Hm, I’ll update the tests. How do you feel about the idea?

lukasgeiter commented 1 year ago

Hey, thanks for the PR! I do really like the idea, definitely something that's useful 👍 I don't think I would limit it to ascending though. I could also see scenarios where you'd want it to be descending (e.g. version numbers with the latest at the top)

rkk1995 commented 1 year ago

Yeah I’m thinking I’ll make natural a completely different setting than ascending and descending. That way they can be combined.

rkk1995 commented 1 year ago

going to update the tests next

rkk1995 commented 1 year ago

Lmk what you think

rkk1995 commented 1 year ago

I fixed the lint. The test failure isn't due to these changse.

Running poetry run pytest mkdocs_awesome_pages_plugin/tests/e2e/test_mkdocs_nav.py -k test_sections_nested_rest_dict -rx on the tip of master has the same test failures.

lukasgeiter commented 1 year ago

The test is failing due to one of the dependencies you updated. You likely forgot to run poetry install again after switching to master. I suggest you only add natsort and revert all other dependency changes.

rkk1995 commented 1 year ago

I added natsort = ">=8.1.0" to pyproject.toml and then ran poetry update.

This sets mkdocs to 1.3.1 from 1.2.3 and I think the test breaks due to that. I did pip install mkdocs==1.2.3 and ran the test and it worked afterwards. What do you suggest doing here? If I didn't run poetry update the github workflow threw errors I believe.

lukasgeiter commented 1 year ago

This should do the trick:

poetry add "natsort>=8.1.0"
rkk1995 commented 1 year ago

Fixed stuff up. The most notable change was I mistakenly used os_sort_keygen (nice catch). I switched this for natsort_keygen which is way more generic and allows you to further customize how the sorting is done. By default natsort_keygen parses numbers as integers and ignores any signs ("+","-") which is what most people are looking for imo.

Some resources for anyone that wants to add further natsort customization to this plugin. Shouldn't be too difficult. https://natsort.readthedocs.io/en/5.4.1/intro.html#further-customizing-natsort

lukasgeiter commented 1 year ago

Don't worry about the failing windows-latest, pypy tests. I don't think that has anything to do with your changes. I'm guessing it's probably caused by the a new image for windows-latest.

Please do fix the formatting though. After that I'll merge your PR 🙂

lukasgeiter commented 1 year ago

I've just published version 2.8.0 which includes this change. Thanks for your work! ❤️