From 6ba14bed75bc8edb26b5b6937698f2204fa5faf3 Mon Sep 17 00:00:00 2001 From: Saophalkun Ponlu Date: Sun, 5 Sep 2010 02:29:07 +0000 Subject: [PATCH] BUGFIX: Resolve navigation sorting using natural sort --- code/DocumentationParser.php | 28 +++++++++++++++++---------- tests/DocumentationParserTest.php | 8 ++++++++ tests/docs/en/sort/1-basic.md | 0 tests/docs/en/sort/10-some-page.md | 0 tests/docs/en/sort/2-intermediate.md | 0 tests/docs/en/sort/21-another-page.md | 0 tests/docs/en/sort/3-advanced.md | 0 7 files changed, 26 insertions(+), 10 deletions(-) create mode 100644 tests/docs/en/sort/1-basic.md create mode 100644 tests/docs/en/sort/10-some-page.md create mode 100644 tests/docs/en/sort/2-intermediate.md create mode 100644 tests/docs/en/sort/21-another-page.md create mode 100644 tests/docs/en/sort/3-advanced.md diff --git a/code/DocumentationParser.php b/code/DocumentationParser.php index 93aa6e6..320107f 100644 --- a/code/DocumentationParser.php +++ b/code/DocumentationParser.php @@ -384,7 +384,8 @@ class DocumentationParser { /** - * Return the children from a given module. Used for building the tree of the page + * Return the children from a given module sorted by Title using natural ordering. + * It is used for building the tree of the page. * * @param String module name * @@ -393,6 +394,7 @@ class DocumentationParser { public static function get_pages_from_folder($folder) { $handle = opendir($folder); $output = new DataObjectSet(); + $files = array(); if($handle) { $extensions = DocumentationService::get_valid_extensions(); @@ -400,17 +402,23 @@ class DocumentationParser { while (false !== ($file = readdir($handle))) { if(!in_array($file, $ignore)) { - $file = strtolower($file); - - $clean = ($pos = strrpos($file, '.')) ? substr($file, 0, $pos) : $file; - - $output->push(new ArrayData(array( - 'Title' => self::clean_page_name($file), - 'Filename' => $clean, - 'Path' => $folder . $file .'/' - ))); + $files[] = $file; } } + + natsort($files); + + foreach($files as $file) { + $file = strtolower($file); + + $clean = ($pos = strrpos($file, '.')) ? substr($file, 0, $pos) : $file; + + $output->push(new ArrayData(array( + 'Title' => self::clean_page_name($file), + 'Filename' => $clean, + 'Path' => $folder . $file .'/' + ))); + } } return $output; diff --git a/tests/DocumentationParserTest.php b/tests/DocumentationParserTest.php index b7c0c3a..fe20290 100644 --- a/tests/DocumentationParserTest.php +++ b/tests/DocumentationParserTest.php @@ -211,6 +211,14 @@ class DocumentationParserTest extends SapphireTest { $this->assertContains('subfolder', $pages->column('Filename'), 'Foldername'); $this->assertContains('test', $pages->column('Filename'), 'Filename'); $this->assertNotContains('_images', $pages->column('Filename'), 'Ignored files'); + + // test the order of pages + $pages = DocumentationParser::get_pages_from_folder(BASE_PATH . '/sapphiredocs/tests/docs/en/sort'); + $this->assertEquals( + array('1 basic', '2 intermediate', '3 advanced', '10 some page', '21 another page'), + $pages->column('Title') + ); + } } \ No newline at end of file diff --git a/tests/docs/en/sort/1-basic.md b/tests/docs/en/sort/1-basic.md new file mode 100644 index 0000000..e69de29 diff --git a/tests/docs/en/sort/10-some-page.md b/tests/docs/en/sort/10-some-page.md new file mode 100644 index 0000000..e69de29 diff --git a/tests/docs/en/sort/2-intermediate.md b/tests/docs/en/sort/2-intermediate.md new file mode 100644 index 0000000..e69de29 diff --git a/tests/docs/en/sort/21-another-page.md b/tests/docs/en/sort/21-another-page.md new file mode 100644 index 0000000..e69de29 diff --git a/tests/docs/en/sort/3-advanced.md b/tests/docs/en/sort/3-advanced.md new file mode 100644 index 0000000..e69de29