public function testDispatch() { $aPage = $this->pages->dispatch('/blog'); $this->assertInstanceOf('Grav\\Common\\Page\\Page', $aPage); $aPage = $this->pages->dispatch('/about'); $this->assertInstanceOf('Grav\\Common\\Page\\Page', $aPage); $aPage = $this->pages->dispatch('/blog/post-one'); $this->assertInstanceOf('Grav\\Common\\Page\\Page', $aPage); //Page not existing $aPage = $this->pages->dispatch('/non-existing'); $this->assertNull($aPage); }
protected function _before() { $grav = Fixtures::get('grav'); $this->grav = $grav(); $this->pages = $this->grav['pages']; $this->config = $this->grav['config']; $this->uri = $this->grav['uri']; $this->language = $this->grav['language']; $this->old_home = $this->config->get('system.home.alias'); $this->config->set('system.home.alias', '/item1'); $this->config->set('system.absolute_urls', false); $this->config->set('system.languages.supported', []); unset($this->grav['language']); $this->grav['language'] = new Language($this->grav); /** @var UniformResourceLocator $locator */ $locator = $this->grav['locator']; $locator->addPath('page', '', 'tests/fake/nested-site/user/pages', false); $this->pages->init(); $defaults = ['extra' => false, 'auto_line_breaks' => false, 'auto_url_links' => false, 'escape_markup' => false, 'special_chars' => ['>' => 'gt', '<' => 'lt']]; $this->page = $this->pages->dispatch('/item2/item2-2'); $this->uri->initializeWithURL('http://testing.dev/item2/item2-2')->init(); }
public function testRootAbsoluteLinks() { $this->uri->initializeWithURL('http://testing.dev/')->init(); $defaults = ['extra' => false, 'auto_line_breaks' => false, 'auto_url_links' => false, 'escape_markup' => false, 'special_chars' => ['>' => 'gt', '<' => 'lt']]; $page = $this->pages->dispatch('/'); $this->parsedown = new Parsedown($page, $defaults); $this->assertSame('<p><a href="/item1/item1-3">Down a Level</a></p>', $this->parsedown->text('[Down a Level](item1-3)')); $this->assertSame('<p><a href="/item2">Peer Page</a></p>', $this->parsedown->text('[Peer Page](../item2)')); $this->assertSame('<p><a href="/?foo=bar">With Query</a></p>', $this->parsedown->text('[With Query](?foo=bar)')); $this->assertSame('<p><a href="/foo:bar">With Param</a></p>', $this->parsedown->text('[With Param](/foo:bar)')); $this->assertSame('<p><a href="#foo">With Anchor</a></p>', $this->parsedown->text('[With Anchor](#foo)')); $this->config->set('system.languages.supported', ['fr', 'en']); unset($this->grav['language']); $this->grav['language'] = new Language($this->grav); $this->uri->initializeWithURL('http://testing.dev/fr/item2/item2-2')->init(); $this->assertSame('<p><a href="/fr/item2">Peer Page</a></p>', $this->parsedown->text('[Peer Page](../item2)')); $this->assertSame('<p><a href="/fr/item1/item1-3">Down a Level</a></p>', $this->parsedown->text('[Down a Level](item1-3)')); $this->assertSame('<p><a href="/fr/?foo=bar">With Query</a></p>', $this->parsedown->text('[With Query](?foo=bar)')); $this->assertSame('<p><a href="/fr/foo:bar">With Param</a></p>', $this->parsedown->text('[With Param](/foo:bar)')); $this->assertSame('<p><a href="#foo">With Anchor</a></p>', $this->parsedown->text('[With Anchor](#foo)')); }
protected function inlineImage($excerpt) { if (preg_match($this->twig_link_regex, $excerpt['text'], $matches)) { $excerpt['text'] = str_replace($matches[1], '/', $excerpt['text']); $excerpt = parent::inlineImage($excerpt); $excerpt['element']['attributes']['src'] = $matches[1]; $excerpt['extent'] = $excerpt['extent'] + strlen($matches[1]) - 1; return $excerpt; } else { $excerpt['type'] = 'image'; $excerpt = parent::inlineImage($excerpt); } // Some stuff we will need $actions = []; $media = null; // if this is an image if (isset($excerpt['element']['attributes']['src'])) { $alt = $excerpt['element']['attributes']['alt'] ?: ''; $title = $excerpt['element']['attributes']['title'] ?: ''; $class = isset($excerpt['element']['attributes']['class']) ? $excerpt['element']['attributes']['class'] : ''; //get the url and parse it $url = parse_url(htmlspecialchars_decode($excerpt['element']['attributes']['src'])); $this_host = isset($url['host']) && $url['host'] == $this->uri->host(); // if there is no host set but there is a path, the file is local if ((!isset($url['host']) || $this_host) && isset($url['path'])) { $path_parts = pathinfo($url['path']); // get the local path to page media if possible if ($path_parts['dirname'] == $this->page->url(false, false, false)) { // get the media objects for this page $media = $this->page->media(); } else { // see if this is an external page to this one $base_url = rtrim(self::getGrav()['base_url_relative'] . self::getGrav()['pages']->base(), '/'); $page_route = '/' . ltrim(str_replace($base_url, '', $path_parts['dirname']), '/'); $ext_page = $this->pages->dispatch($page_route, true); if ($ext_page) { $media = $ext_page->media(); } } // if there is a media file that matches the path referenced.. if ($media && isset($media->all()[$path_parts['basename']])) { // get the medium object $medium = $media->all()[$path_parts['basename']]; // if there is a query, then parse it and build action calls if (isset($url['query'])) { $actions = array_reduce(explode('&', $url['query']), function ($carry, $item) { $parts = explode('=', $item, 2); $value = isset($parts[1]) ? $parts[1] : null; $carry[] = ['method' => $parts[0], 'params' => $value]; return $carry; }, []); } // loop through actions for the image and call them foreach ($actions as $action) { $medium = call_user_func_array([$medium, $action['method']], explode(',', urldecode($action['params']))); } if (isset($url['fragment'])) { $medium->urlHash($url['fragment']); } $excerpt['element'] = $medium->parseDownElement($title, $alt, $class, true); } else { // not a current page media file, see if it needs converting to relative $excerpt['element']['attributes']['src'] = Uri::buildUrl($url); } } } return $excerpt; }