- commit
- 890f92ac8e277c61067149ae4123a19ce6f9e9bf
- parent
- 5d4c305198c8cd803009c1cf23cce9addc38b770
- Author
- Tobias Bengfort <tobias.bengfort@posteo.de>
- Date
- 2018-10-14 09:51
wrapp stuff in a class
Diffstat
| M | index.php | 196 | ++++++++++++++++++++++++++++++++++++------------------------- |
| M | templates/page.html | 4 | ++-- |
2 files changed, 118 insertions, 82 deletions
diff --git a/index.php b/index.php
@@ -1,4 +1,4 @@1 -1 <?php-1 1 <?php declare(strict_types=1); 2 2 3 3 require __DIR__ . '/vendor/autoload.php'; 4 4 use Symfony\Component\Yaml\Yaml; @@ -39,40 +39,6 @@ function validatePath($path) 39 39 } 40 40 } 41 4142 -1 function getData($path, $raise=false)43 -1 {44 -1 $data = array(45 -1 'yml' => '',46 -1 'md' => '',47 -1 );48 -1 if (file_exists("../_content$path/index.yml")) {49 -1 $data['yml'] = file_get_contents("../_content$path/index.yml");50 -1 }51 -1 if (file_exists("../_content$path/index.md")) {52 -1 $data['md'] = file_get_contents("../_content$path/index.md");53 -1 }54 -1 return $data;55 -1 }56 -157 -1 function setData($path, $data)58 -1 {59 -1 mkdirp("../_content$path");60 -1 file_put_contents("../_content$path/index.yml", $data['yml']);61 -1 file_put_contents("../_content$path/index.md", $data['md']);62 -1 }63 -164 -1 function getSubpages($path)65 -1 {66 -1 $p = "../_content$path";67 -1 $subpages = array();68 -1 foreach (scandir($p) as $name) {69 -1 if ($name !== '.' && $name !== '..' && is_dir("$p/$name")) {70 -1 $subpages[] = $name;71 -1 }72 -1 }73 -1 return $subpages;74 -1 }75 -176 42 function getBreadcrumbs($path) 77 43 { 78 44 $breadcrumbs = array('home' => ''); @@ -85,42 +51,117 @@ function getBreadcrumbs($path) 85 51 return $breadcrumbs; 86 52 } 87 5388 -1 function render($path, $verbose=false)-1 54 class Pupupu 89 55 {90 -1 $parsedown = new Parsedown();91 -1 // $parsedown->setMarkupEscaped(true);92 -1 // $parsedown->setSafeMode(true);93 -1 $loader = new Twig_Loader_Filesystem('../_templates');94 -1 $twig = new Twig_Environment($loader);-1 56 public function __construct($srcDir, $targetDir) -1 57 { -1 58 $this->srcDir = $srcDir; -1 59 $this->targetDir = $targetDir; -1 60 -1 61 $this->parsedown = new Parsedown(); -1 62 $loader = new Twig_Loader_Filesystem($targetDir . '/_templates'); -1 63 $this->twig = new Twig_Environment($loader); 95 6496 -1 if ($verbose) {97 -1 echo "rendering $path\n";98 65 } 99 66100 -1 $data = getData($path);101 -1 $template = $data['yml']['template'] ?? 'base.html';102 -1 $html = $twig->render($template, array(103 -1 'page' => Yaml::parse($data['yml']),104 -1 'site' => Yaml::parseFile("../_site/site.yml"),105 -1 'body' => $parsedown->text($data['md']),106 -1 'date' => time(),107 -1 ));-1 67 public function get($path, $name) -1 68 { -1 69 $p = $this->srcDir . '/_content' . $path . '/' . $name; -1 70 if (file_exists($p)) { -1 71 return file_get_contents($p); -1 72 } else { -1 73 return ''; -1 74 } -1 75 } 108 76109 -1 mkdirp("..$path");110 -1 file_put_contents("..$path/index.html", $html);111 -1 }-1 77 public function put($path, $name, $content) -1 78 { -1 79 $p = $this->srcDir . '/_content' . $path . '/' . $name; -1 80 mkdirp(dirname($p)); -1 81 file_put_contents($p, $content); -1 82 } 112 83113 -1 function renderAll($path='', $verbose=false)114 -1 {115 -1 render($path, $verbose);116 -1 $dir = "../_content$path";117 -1 foreach (scandir($dir) as $name) {118 -1 if ($name !== '.' && $name !== '..' && is_dir("$dir/$name")) {119 -1 renderAll("$trimmed/$name", $verbose);-1 84 public function rm($path) -1 85 { -1 86 rrmdir($this->srcDir . '/_content' . $path); -1 87 rrmdir($this->targetDir . $path); -1 88 } -1 89 -1 90 public function getBody($path) -1 91 { -1 92 return $this->parsedown->text($this->get($path, 'index.md')); -1 93 } -1 94 -1 95 public function getPage($path) -1 96 { -1 97 return Yaml::parse($this->get($path, 'index.yml')); -1 98 } -1 99 -1 100 public function getSite() -1 101 { -1 102 return Yaml::parse($this->get('', 'site.yml')); -1 103 } -1 104 -1 105 public function getSubpages($path) -1 106 { -1 107 $p = $this->srcDir . '/_content' . $path; -1 108 return array_filter(scandir($p), function ($file) use ($p) { -1 109 return $file !== '.' && $file !== '..' && is_dir("$p/$file"); -1 110 }); -1 111 } -1 112 -1 113 public function upload($file) -1 114 { -1 115 $p = $this->targetDir . '/uploads'; -1 116 mkdirp($p); -1 117 move_uploaded_file($file['tmp_name'], $p . '/' . $file['name']); -1 118 } -1 119 -1 120 public function getUploads() -1 121 { -1 122 $p = $this->targetDir . '/uploads'; -1 123 return array_filter(scandir($p), function ($file) use ($p) { -1 124 return is_file("$p/$file"); -1 125 }); -1 126 } -1 127 -1 128 public function rmUpload($name) -1 129 { -1 130 unlink($this->targetDir . '/uploads/' . $name); -1 131 } -1 132 -1 133 public function render($path, $verbose=false) -1 134 { -1 135 if ($verbose) { -1 136 echo "rendering $path\n"; -1 137 } -1 138 -1 139 $page = $this->getPage($path); -1 140 $template = $page['template'] ?? 'base.html'; -1 141 $html = $this->twig->render($template, array( -1 142 'page' => $page, -1 143 'site' => $this->getSite(), -1 144 'body' => $this->getBody($path), -1 145 'date' => time(), -1 146 )); -1 147 -1 148 $filename = $page['filename'] ?? 'index.html'; -1 149 $p = $this->targetDir . $path . '/' . $filename; -1 150 mkdirp(dirname($p)); -1 151 file_put_contents($p, $html); -1 152 } -1 153 -1 154 public function renderAll($verbose=false, $path='') -1 155 { -1 156 $this->render($path, $verbose); -1 157 foreach ($this->getSubpages($path) as $page) { -1 158 $this->renderAll($verbose, "$path/$page"); 120 159 } 121 160 } 122 161 } 123 162 -1 163 $pupupu = new Pupupu('..', '..'); -1 164 124 165 if (isset($_SERVER['REQUEST_METHOD'])) { 125 166 $loader = new Twig_Loader_Filesystem('templates'); 126 167 $twig = new Twig_Environment($loader); @@ -132,37 +173,33 @@ if (isset($_SERVER['REQUEST_METHOD'])) { 132 173 } elseif ($_GET['path'] === '_site') { 133 174 if ($_SERVER['REQUEST_METHOD'] == 'GET') { 134 175 echo $twig->render('site.html', array(135 -1 'yml' => file_get_contents("../_site/site.yml"),-1 176 'yml' => $pupupu->get('', 'site.yml'), 136 177 )); 137 178 } else {138 -1 file_put_contents("../_site/site.yml", $_POST['yml']);139 -1 renderAll();-1 179 $pupupu->put('', 'site.yml', $_POST['yml']); -1 180 $pupupu->renderAll(); 140 181 header("Location: ", true, 302); 141 182 } 142 183 } elseif ($_GET['path'] === '_uploads') { 143 184 if ($_SERVER['REQUEST_METHOD'] == 'GET') { 144 185 echo $twig->render('uploads.html', array(145 -1 'files' => array_filter(scandir('../uploads/'), function($file) {146 -1 return is_file("../uploads/$file");147 -1 }),-1 186 'files' => $pupupu->getUploads(), 148 187 )); 149 188 } elseif (isset($_FILES['file'])) {150 -1 mkdirp('../uploads');151 -1 $f = $_FILES['file'];152 -1 move_uploaded_file($f['tmp_name'], '../uploads/' . $f['name']);-1 189 $pupupu->upload($_FILES['file']); 153 190 header("Location: ", true, 302); 154 191 } else {155 -1 unlink('../uploads/' . $_POST['file']);-1 192 $pupupu->rmUpload($_POST['file']); 156 193 header("Location: ", true, 302); 157 194 } 158 195 } else { 159 196 $path = validatePath($_GET['path']); 160 197 161 198 if ($_SERVER['REQUEST_METHOD'] == 'GET') {162 -1 $data = getData($path);163 199 echo $twig->render('page.html', array(164 -1 'data' => $data,165 -1 'subpages' => getSubpages($path),-1 200 'yml' => $pupupu->get($path, 'index.yml'), -1 201 'md' => $pupupu->get($path, 'index.md'), -1 202 'subpages' => $pupupu->getSubpages($path), 166 203 'path' => $path, 167 204 'breadcrumbs' => getBreadcrumbs($path), 168 205 )); @@ -171,17 +208,16 @@ if (isset($_SERVER['REQUEST_METHOD'])) { 171 208 http_response_code(400); 172 209 die(); 173 210 }174 -1 rrmdir("../_content$path");175 -1 rrmdir("..$path");-1 211 $pupupu->rm($path); 176 212 $target = dirname($path); 177 213 header("Location: ?path=$target", true, 302); 178 214 } else {179 -1 // TODO validate form180 -1 setData($path, $_POST);181 -1 render($path);-1 215 $pupupu->put($path, 'index.yml', $_POST['yml']); -1 216 $pupupu->put($path, 'index.md', $_POST['md']); -1 217 $pupupu->render($path); 182 218 header('Location: ', true, 302); 183 219 } 184 220 } 185 221 } else {186 -1 renderAll('/', true);-1 222 $pupupu->renderAll(true); 187 223 }
diff --git a/templates/page.html b/templates/page.html
@@ -20,12 +20,12 @@ 20 20 <form method="POST"> 21 21 <label> 22 22 Body23 -1 <textarea name="md">{{ data.md }}</textarea>-1 23 <textarea name="md">{{ md }}</textarea> 24 24 </label> 25 25 26 26 <label> 27 27 Metadata28 -1 <textarea name="yml">{{ data.yml }}</textarea>-1 28 <textarea name="yml">{{ yml }}</textarea> 29 29 </label> 30 30 31 31 <input type="submit" value="Save">