public function testConstruction() { $kirby = $this->kirbyInstance(); $site = $this->siteInstance($kirby); $page = new Page($site, '1-a'); $this->assertInstanceOf('Kirby', $page->kirby()); $this->assertEquals($kirby, $page->kirby()); $this->assertInstanceOf('Site', $page->site()); $this->assertEquals($site, $page->site()); $this->assertInstanceOf('Site', $page->parent()); $this->assertEquals($site, $page->parent()); $this->assertEquals('1-a', $page->dirname()); $this->assertEquals(1, $page->depth()); $this->assertEquals($kirby->roots()->content() . DS . '1-a', $page->root()); $this->assertEquals('1', $page->num()); $this->assertEquals('a', $page->uid()); $this->assertEquals('a', $page->id()); $this->assertEquals('1-a', $page->diruri()); $this->assertEquals('/a', $page->url()); $this->assertTrue($page->isCachable()); $this->assertEquals('a', $page->slug()); $this->assertTrue($page->is($page)); $this->assertTrue($page->equals($page)); $this->assertFalse($page->isSite()); $this->assertFalse($page->isActive()); $this->assertFalse($page->isOpen()); $this->assertTrue($page->isVisible()); $this->assertFalse($page->isInvisible()); $this->assertFalse($page->isHomePage()); $this->assertFalse($page->isErrorPage()); $this->assertEquals($page->id(), (string) $page); }
/** * Process contents i.e. apply filer to the content. * * @param string $content The content to render. * @param array $options Options to be passed to the renderer. * @param null|Page $page Null or an instance of \Grav\Common\Page. * * @return string The rendered contents. */ public function render($content, $options = [], $page = null) { // Set unique identifier based on page content $this->id($page->id() ?: time() . md5($content)); // Reset class hashes before processing // $this->reset(); $regex = []; // Wrap any text between $ ... $ or $$ ... $$ in display math tags. $regex['latex-block'] = '~(?<!\\\\)(\\$\\$)(.+?)\\1~msx'; $regex['latex-inline'] = '~(?<!\\\\)(\\$)(.+?)\\1~msx'; // Wrap any text between \[ and \] in display math tags. $regex['block'] = '~ ^\\\\ # line starts with a single backslash (double escaping) \\[ # followed by a square bracket (.+) # then the actual LaTeX code \\\\ # followed by another backslash \\] # and closing bracket \\s*$ # and maybe some whitespace before the end of the line ~msxUX'; // Wrap any text between \( and \) in display math tags. $regex['inline'] = '~ \\\\ # line starts with a single backslash (double escaping) \\( # followed by a left parenthesis (.+) # then the actual LaTeX code \\\\ # followed by another backslash \\) # and a right parenthesis ~msxUX'; // Replace all math formulas by a (unique) hash foreach ($regex as $key => $re) { $content = preg_replace_callback($re, function ($matches) use($key) { return $this->hash(trim($matches[0]), $key); }, $content); } return $content; }
/** * Constructor * * @param Page $parent * @param string $dirname */ public function __construct($parent, $dirname) { $this->parent = $parent; $this->site = $parent->site(); $this->dirname = $dirname; $this->root = $parent->root() . DS . $dirname; $this->depth = $parent->depth() + 1; // extract the uid and num of the directory if (preg_match('/^([0-9]+[\\-]+)(.*)/', $this->dirname, $match)) { $this->uid = $match[2]; $this->num = trim(rtrim($match[1], '-')); } else { $this->num = null; $this->uid = $this->dirname; } // assign the uid $this->id = $this->uri = ltrim($parent->id() . '/' . $this->uid, '/'); }
/** * Alternative for $this->equals() */ public function is(Page $page) { return $this->id() == $page->id(); }
/** * Returns all children for this page * * @return Children */ public function children() { if (isset($this->cache['children'])) { return $this->cache['children']; } $this->cache['children'] = new Children($this); $inventory = $this->inventory(); // with page models if (!empty(static::$models)) { foreach ($inventory['children'] as $dirname) { $child = new Page($this, $dirname); // let's create a model if one is defined if (isset(static::$models[$child->intendedTemplate()])) { $model = static::$models[$child->intendedTemplate()]; $child = new $model($this, $dirname); } $this->cache['children']->data[$child->id()] = $child; } // without page models } else { foreach ($inventory['children'] as $dirname) { $child = new Page($this, $dirname); $this->cache['children']->data[$child->id()] = $child; } } return $this->cache['children']; }
/** * Render shortcodes. * * @param string $content The content to render. * @param array $options Options to be passed to the renderer. * @param null|Page $page Null or an instance of \Grav\Common\Page. * * @return string The modified contents. */ public function render($content, $options = [], $page = null) { // Build an anonymous function to pass to twig `render` method $function = function ($tag, $body, $arguments) use($options, $page) { if (isset($this->shortcodes[$tag])) { $options = isset($options[$tag]) ? $options[$tag] : []; } $event = new Event(['body' => $body, 'options' => new Data(array_replace_recursive($options, $arguments)), 'grav' => self::getGrav(), 'shortcodes' => $this, 'page' => $page, 'tag' => $tag]); return $event; }; // Wrapper for shortcodes filter function $filter_function = function ($name, $content, $context, $env) { return $this->filterShortcode($name, $content, $context, $env); }; // Process in-page shortcodes Twig $name = '@Shortcodes:' . $page->path(); $this->loader->setTemplate($name, $content); $vars = ['__shortcodes' => $function, '__shortcodes_filter' => $filter_function]; try { $page_default = $this->page; $this->page = $page; $output = $this->twig->render($name, $vars); } catch (\Twig_Error_Loader $e) { throw new \RuntimeException($e->getRawMessage(), 404, $e); } $shortcodes = isset($page->header()->shortcodes) ? $page->header()->shortcodes : []; if (isset($shortcodes['extra'])) { /** @var Cache $cache */ $cache = self::getGrav()['cache']; $cache_id = md5('shortcodes' . $page->id() . $cache->getKey()); $cache->save($cache_id, $shortcodes['extra']); } $this->page = $page_default; return $output; }
/** * Creates a new Page object and adds it to the collection */ public function add($dirname) { $page = new Page($this->page, $dirname); $this->data[$page->id()] = $page; return $page; }
public static function findByUri($uri, $class = __CLASS__) { //print "Page::findByUri($uri)"; $uri = trim($uri, '/'); $has_behavior = false; $urls = array_merge(array(''), explode_uri($uri)); $url = ''; $parent = new Page(); $parent->id(0); foreach ($urls as $page_slug) { $url = ltrim($url . '/' . $page_slug, '/'); if ($page = Page::findBySlugAndParentId($page_slug, $parent->id())) { if ($page->behaviorId()) { $has_behavior = true; // add a instance of the behavior with the name of the behavior $params = explode_uri(substr($uri, strlen($url))); print_r($params); $page->behavior(Behavior::load($page->behaviorId(), $page, $params)); return $page; } } else { break; } $parent = $page; } return !$page && $has_behavior ? $parent : $page; }
/** * Returns the HTML for a page with caching enabled * * @return string */ protected static function cache(Page $page, $data = array()) { // TODO: check for site modification date and flush the cache // try to read the cache $id = static::$site->multilang() ? static::$site->language()->code() . '.' . md5($page->id()) : md5($page->id()); $cache = true ? cache::get($id) : null; // fetch fresh content if the cache is empty if (empty($cache)) { $cache = static::template($page, $data); cache::set($page->id(), $cache); } return $cache; }