From d1c7b27563046364294faa8948496745097c5d1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Goetz?= Date: Wed, 8 Nov 2017 21:49:30 +0100 Subject: [PATCH] Add support for + at the beginning, fixes #18 --- docs/01_Features/Navigation_and_Sorting.md | 21 +++++++++++++++++++-- libs/Tree/Builder.php | 2 +- libs/Tree/Directory.php | 15 ++++++++++++++- tests/Tree/BuilderTest.php | 2 ++ tests/Tree/DirectoryTest.php | 1 + 5 files changed, 37 insertions(+), 4 deletions(-) diff --git a/docs/01_Features/Navigation_and_Sorting.md b/docs/01_Features/Navigation_and_Sorting.md index 2752fe3..63b0a69 100644 --- a/docs/01_Features/Navigation_and_Sorting.md +++ b/docs/01_Features/Navigation_and_Sorting.md @@ -10,6 +10,23 @@ if you wish to have one defined all you need to do is add an `index.md` file to For example, `/docs/02_Examples` has a landing page for that section since there exists a `/docs/02_Examples/index.md` file. ## Sorting -To sort your files and folders in a specific way, you can prefix them with a number and underscore, e.g. `/docs/01_Hello_World.md` and `/docs/05_Features.md` This will list *Hello World* before *Features*, overriding the default alpha-numeric sorting. The numbers will be stripped out of the navigation and urls. For the file `6 Ways to Get Rich`, you can use `/docs/_6_Ways_to_Get_Rich.md` -You might also wish to stick certain links to the bottom of a page. You can do so by appending a '-' to the start of the filename, e.g. a new file `/docs/-Contact_Us.md` will always appear at the bottom of the current list. Weights can also be added to further sort the bottom entries. e.g. `/docs/-01_Coming.md` will appear before `/docs/-02_Soon.md` but both will only appear after all positive or non-weighted files. +To sort your files and folders in a specific way, you can prefix them with a number and underscore, e.g. `/docs/01_Hello_World.md` and `/docs/05_Features.md`. +This will list *Hello World* before *Features*, overriding the default alpha-numeric sorting. +The numbers will be stripped out of the navigation and urls. For the file `6 Ways to Get Rich`, you can use `/docs/_6_Ways_to_Get_Rich.md` + +You might also wish to stick certain links to the bottom of a page. +You can do so by prefixing the file name with a '-', e.g. a new file `/docs/-Contact_Us.md` will always appear at the bottom of the current list. +Weights can also be added to further sort the bottom entries. e.g. `/docs/-01_Coming.md` will appear before `/docs/-02_Soon.md` but both will only appear after all positive or non-weighted files. + +It works the same for files prefixed with `+`. + +Page order priorities are like this: + +- `+` in front of the filename and numbers in front +- `+` in front of the filename +- The index page +- Numbers in the front +- Pages without prefix +- `-` in front of the filename and numbers in front +- `-` in front of the filename diff --git a/libs/Tree/Builder.php b/libs/Tree/Builder.php index 60322e3..50b77c2 100644 --- a/libs/Tree/Builder.php +++ b/libs/Tree/Builder.php @@ -136,7 +136,7 @@ class Builder */ public static function removeSortingInformations($filename) { - preg_match('/^-?[0-9]*_?(.*)/', $filename, $matches); + preg_match('/^[-+]?[0-9]*_?(.*)/', $filename, $matches); // Remove the numeric part // of the filename, only if diff --git a/libs/Tree/Directory.php b/libs/Tree/Directory.php index a0edc81..f33e65f 100644 --- a/libs/Tree/Directory.php +++ b/libs/Tree/Directory.php @@ -15,6 +15,8 @@ class Directory extends Entry implements \ArrayAccess, \IteratorAggregate { // Separate the values into buckets to sort them separately $buckets = [ + 'up_numeric' => [], + 'up' => [], 'index' => [], 'numeric' => [], 'normal' => [], @@ -41,6 +43,17 @@ class Directory extends Entry implements \ArrayAccess, \IteratorAggregate continue; } + if ($name[0] == '+') { + if (is_numeric($name[1])) { + $exploded = explode('_', $name); + $buckets['up_numeric'][abs(substr($exploded[0], 1))][$key] = $entry; + continue; + } + + $buckets['up'][$key] = $entry; + continue; + } + if (is_numeric($name[0])) { $exploded = explode('_', $name); $buckets['numeric'][abs($exploded[0])][$key] = $entry; @@ -52,7 +65,7 @@ class Directory extends Entry implements \ArrayAccess, \IteratorAggregate $final = []; foreach ($buckets as $name => $bucket) { - if ($name == 'numeric' || $name == 'down_numeric') { + if (substr($name, -7) == 'numeric') { ksort($bucket); foreach ($bucket as $sub_bucket) { $final = $this->sortBucket($sub_bucket, $final); diff --git a/tests/Tree/BuilderTest.php b/tests/Tree/BuilderTest.php index f94e24e..a646a3f 100644 --- a/tests/Tree/BuilderTest.php +++ b/tests/Tree/BuilderTest.php @@ -10,6 +10,7 @@ class BuilderTest extends \PHPUnit_Framework_TestCase return [ ['01_before', 'before'], ['-Down', 'Down'], + ['+Up', 'Up'], ['01_numeric', 'numeric'], ['01_A_File', 'A_File'], ['A_File', 'A_File'], @@ -20,6 +21,7 @@ class BuilderTest extends \PHPUnit_Framework_TestCase ['API_Calls', 'API_Calls'], ['200_Something_Else-Cool', 'Something_Else-Cool'], ['_5_Ways_to_Be_Happy', '5_Ways_to_Be_Happy'], + ['+02_Soon', 'Soon'], ['Before_but_after', 'Before_but_after'], ['Continuing', 'Continuing'], ['01_GitHub_Flavored_Markdown', 'GitHub_Flavored_Markdown'], diff --git a/tests/Tree/DirectoryTest.php b/tests/Tree/DirectoryTest.php index 9612407..6d43ced 100644 --- a/tests/Tree/DirectoryTest.php +++ b/tests/Tree/DirectoryTest.php @@ -15,6 +15,7 @@ class DirectoryTest extends \PHPUnit_Framework_TestCase [['01_numeric', '01_before'], ['01_before', '01_numeric']], [['A_File', '01_A_File'], ['01_A_File', 'A_File']], [['A_File', '01_Continuing', '-01_Coming', '-02_Soon'], ['01_Continuing', 'A_File', '-01_Coming', '-02_Soon']], + [['+A_File', '01_Continuing', '+01_Coming', '-02_Soon'], ['+01_Coming', '+A_File', '01_Continuing', '-02_Soon']], [['01_Getting_Started', 'API_Calls', '200_Something_Else-Cool', '_5_Ways_to_Be_Happy'], ['01_Getting_Started', '200_Something_Else-Cool', '_5_Ways_to_Be_Happy', 'API_Calls']], [['01_Getting_Started', 'API_Calls', 'index', '200_Something_Else-Cool', '_5_Ways_to_Be_Happy'], ['index', '01_Getting_Started', '200_Something_Else-Cool', '_5_Ways_to_Be_Happy', 'API_Calls']], [['Before_but_after', 'A_File', 'Continuing'], ['A_File', 'Before_but_after', 'Continuing']],