lift max-width limitation
[mdref/mdref] / mdref / Tree.php
index 8f6c7739f6eaca5fea832fe847ae133ad4657823..11cada07293495da26112af7c8e13b66d421bc04 100644 (file)
@@ -8,19 +8,19 @@ class Tree implements \RecursiveIterator {
         * @var \mdref\Repo
         */
        private $repo;
-       
+
        /**
         * List of first level entries
         * @var array
         */
        private $list = array();
-       
+
        /**
         * The list iterator
         * @var array
         */
        private $iter;
-       
+
        /**
         * @param string $path
         * @param \mdref\Repo $repo
@@ -33,11 +33,11 @@ class Tree implements \RecursiveIterator {
                }
                if ($list) {
                        $this->list = array_filter($list, $this->generateFilter($list));
-                       sort($this->list, SORT_STRING);
+                       usort($this->list, $this->generateSorter());
                }
                $this->repo = $repo;
        }
-       
+
        /**
         * @param array $list
         * @return callable
@@ -50,16 +50,58 @@ class Tree implements \RecursiveIterator {
                        if (false !== array_search("$v.md", $list, true)) {
                                return false;
                        }
-                       
+
                        $pi = pathinfo($v);
                        if (isset($pi["extension"]) && "md" !== $pi["extension"]) {
                                return false;
                        }
-                       
+
                        return true;
                };
        }
 
+       /**
+        * @return callable
+        */
+       private function generateSorter() {
+               return function($a, $b) {
+                       $ab = basename($a, ".md");
+                       $bb = basename($b, ".md");
+
+                       if ($ab{0} === ":" && $bb{0} === ":") {
+                               return strcmp($ab, $bb);
+                       } elseif ($ab{0} === ":") {
+                               return -1;
+                       } elseif ($bb{0} === ":") {
+                               return 1;
+                       }
+
+                       $ad = is_dir(dirname($a)."/$ab");
+                       $bd = is_dir(dirname($b)."/$bb");
+
+                       if ($ad && $bd) {
+                               return strcmp($ab, $bb);
+                       } elseif ($ad) {
+                               return -1;
+                       } elseif ($bd) {
+                               return 1;
+                       }
+
+                       $au = preg_match("/^\p{Lu}/", $ab);
+                       $bu = preg_match("/^\p{Lu}/", $bb);
+
+                       if ($au && $bu) {
+                               return strcmp($ab, $bb);
+                       } elseif ($au) {
+                               return -1;
+                       } elseif ($bu) {
+                               return 1;
+                       }
+
+                       return strcmp($ab, $bb);
+               };
+       }
+
        /**
         * Implements \Iterator
         * @return \mdref\Entry
@@ -67,14 +109,14 @@ class Tree implements \RecursiveIterator {
        public function current() {
                return $this->repo->getEntry($this->repo->hasFile(current($this->iter)));
        }
-       
+
        /**
         * Implements \Iterator
         */
        public function next() {
                next($this->iter);
        }
-       
+
        /**
         * Implements \Iterator
         * @return int
@@ -82,7 +124,7 @@ class Tree implements \RecursiveIterator {
        public function key() {
                return key($this->iter);
        }
-       
+
        /**
         * Implements \Iterator
         */
@@ -90,7 +132,7 @@ class Tree implements \RecursiveIterator {
                $this->iter = $this->list;
                reset($this->iter);
        }
-       
+
        /**
         * Implements \Iterator
         * @return bool
@@ -98,7 +140,7 @@ class Tree implements \RecursiveIterator {
        public function valid() {
                return null !== key($this->iter);
        }
-       
+
        /**
         * Implements \RecursiveIterator
         * @return bool
@@ -106,7 +148,7 @@ class Tree implements \RecursiveIterator {
        public function hasChildren() {
                return $this->current()->hasIterator();
        }
-       
+
        /**
         * Implements \RecursiveIterator
         * @return \mdref\Tree