Example #1
0
 /**
  * Converts links from absolute '/' or relative (../..) to a grav friendly format
  *
  * @param         $page         the current page to use as reference
  * @param  string $markdown_url the URL as it was written in the markdown
  *
  * @return string the more friendly formatted url
  */
 public static function convertUrl(Page $page, $markdown_url)
 {
     $grav = Grav::instance();
     $pages_dir = $grav['locator']->findResource('page://');
     $base_url = rtrim($grav['base_url'] . $grav['pages']->base(), '/');
     // if absolute and starts with a base_url move on
     if (pathinfo($markdown_url, PATHINFO_DIRNAME) == '.' && $page->url() == '/') {
         return '/' . $markdown_url;
         // no path to convert
     } elseif ($base_url != '' && Utils::startsWith($markdown_url, $base_url)) {
         return $markdown_url;
         // if contains only a fragment
     } elseif (Utils::startsWith($markdown_url, '#')) {
         return $markdown_url;
     } else {
         $target = null;
         // see if page is relative to this or absolute
         if (Utils::startsWith($markdown_url, '/')) {
             $normalized_url = Utils::normalizePath($base_url . $markdown_url);
             $normalized_path = Utils::normalizePath($pages_dir . $markdown_url);
         } else {
             $normalized_url = $base_url . Utils::normalizePath($page->route() . '/' . $markdown_url);
             $normalized_path = Utils::normalizePath($page->path() . '/' . $markdown_url);
         }
         // special check to see if path checking is required.
         $just_path = str_replace($normalized_url, '', $normalized_path);
         if ($just_path == $page->path()) {
             return $normalized_url;
         }
         $url_bits = parse_url($normalized_path);
         $full_path = $url_bits['path'];
         if (file_exists($full_path)) {
             // do nothing
         } elseif (file_exists(urldecode($full_path))) {
             $full_path = urldecode($full_path);
         } else {
             return $normalized_url;
         }
         $path_info = pathinfo($full_path);
         $page_path = $path_info['dirname'];
         $filename = '';
         if ($markdown_url == '..') {
             $page_path = $full_path;
         } else {
             // save the filename if a file is part of the path
             if (is_file($full_path)) {
                 if ($path_info['extension'] != 'md') {
                     $filename = '/' . $path_info['basename'];
                 }
             } else {
                 $page_path = $full_path;
             }
         }
         // get page instances and try to find one that fits
         $instances = $grav['pages']->instances();
         if (isset($instances[$page_path])) {
             $target = $instances[$page_path];
             $url_bits['path'] = $base_url . $target->route() . $filename;
             return Uri::buildUrl($url_bits);
         }
         return $normalized_url;
     }
 }
Example #2
0
 /**
  * Finds relative CSS urls() and rewrites the URL with an absolute one
  *
  * @param $file                 the css source file
  * @param $relative_path        relative path to the css file
  *
  * @return mixed
  */
 protected function cssRewrite($file, $relative_path)
 {
     // Strip any sourcemap comments
     $file = preg_replace(self::CSS_SOURCEMAP_REGEX, '', $file);
     // Find any css url() elements, grab the URLs and calculate an absolute path
     // Then replace the old url with the new one
     $file = preg_replace_callback(self::CSS_URL_REGEX, function ($matches) use($relative_path) {
         $old_url = $matches[1];
         // ensure this is not a data url
         if (strpos($old_url, 'data:') === 0) {
             return $matches[0];
         }
         $new_url = $this->base_url . ltrim(Utils::normalizePath($relative_path . '/' . $old_url), '/');
         return str_replace($old_url, $new_url, $matches[0]);
     }, $file);
     return $file;
 }
Example #3
0
 public function testNormalizePath()
 {
     $this->assertEquals('/test', Utils::normalizePath('/test'));
     $this->assertEquals('test', Utils::normalizePath('test'));
     $this->assertEquals('test', Utils::normalizePath('../test'));
     $this->assertEquals('/test', Utils::normalizePath('/../test'));
     $this->assertEquals('/test2', Utils::normalizePath('/test/../test2'));
     $this->assertEquals('/test/test2', Utils::normalizePath('/test/./test2'));
 }