public function add() { if ($this->method == 'POST' && Input::has('name')) { $page = new Page(Input::get('name'), 'name'); return $this->redirect(['a' => 'edit', 'p' => $page->getName()]); } }
public function upload() { if (Input::hasFile('files') && $this->method === 'POST') { $files = Input::file('files'); $num = count($files['name']); $saved = 0; for ($i = 0; $i < $num; ++$i) { $file = array_combine(array_keys($files), array_map(function ($value) use($i) { return $value[$i]; }, array_values($files))); if (!is_uploaded_file($file['tmp_name'])) { throw new InternalErrorException(); } $ext = pathinfo($file['name'], PATHINFO_EXTENSION); if ($ext == 'md') { $markdown = file_get_contents($file['tmp_name']); $page_name = basename($file['name'], '.md'); $markdown = preg_replace_callback('/Title: (.*?)(\\r\\n|\\r|\\n)/', function ($matches) use(&$page_name) { $page_name = $matches[1]; return ''; }, $markdown); $page = new Page($page_name, 'name'); $page->update($markdown); ++$saved; } } if ($saved == 1) { return $this->redirect(['p' => $page_name]); } } }
public function testExists() { $page = new Page('PageName', 'name'); $id = $page->getId(); $this->assertFalse((new Page('PageName', 'name'))->exists()); $page->update('Test'); $this->assertTrue((new Page('PageName', 'name'))->exists()); }
public function convert($src, $page = '') { $this->pageStuck[$page] = true; $src = preg_replace_callback('/((.|\\r|\\n)*?)((```(.|\\r|\\n)*?```)|(`(.|\\r|\\n)*?`)|$)/m', function ($matches) use($page) { $src = $matches[1]; // File Transclusion $src = preg_replace_callback('/{{(.*?)}}/', function ($matches) use($page) { $name = $matches[1]; if (!array_key_exists($name, $this->pageStuck)) { $page = new Page($name, 'name'); return $this->convert($page->getHead()->getData(), $name); } else { return $matches[0]; } }, $src); // Abbr link $src = preg_replace('/\\[(.*?)\\](?!\\()/', '[$1]($1)', $src); $src = preg_replace_callback('/(!?)\\[(.*?)]\\((.*?)( .*?)?\\)/', function ($matches) use($page) { if (preg_match('/^(https?)?:\\/\\//', $matches[3])) { return $matches[0]; } else { if (empty($matches[1])) { $encoded = urlencode($matches[3]); if (count($matches) == 5) { return "[{$matches[2]}](?p={$encoded} {$matches[4]})"; } else { return "[{$matches[2]}](?p={$encoded})"; } } else { preg_match('/^((.*)\\/)?([^\\/]+)$/', $matches[3], $path); if (strlen($path[2]) == 0) { $page_name = urlencode($page); } else { $page_name = urlencode($path[2]); } $file_name = urlencode($path[3]); if (count($matches) == 5) { return "![{$matches[2]}](?a=file&p={$page_name}&f={$file_name} {$matches[4]})"; } else { return "![{$matches[2]}](?a=file&p={$page_name}&f={$file_name})"; } } } }, $src); return $src . $matches[3]; }, $src); return MarkdownExtra::defaultTransform($src); }