0.1.0 initial commit
This commit is contained in:
commit
96ee2c4b79
8 changed files with 268 additions and 0 deletions
132
src/PageLoader.php
Normal file
132
src/PageLoader.php
Normal file
|
|
@ -0,0 +1,132 @@
|
|||
<?php
|
||||
|
||||
namespace Yusurko\Caluta7;
|
||||
|
||||
use Symfony\Component\Yaml\Yaml;
|
||||
use League\CommonMark\CommonMarkConverter;
|
||||
|
||||
define('PAGES_PATH', dirname(__DIR__) . '/pages');
|
||||
|
||||
|
||||
class PageLoader {
|
||||
protected string $name, $title;
|
||||
protected mixed $error;
|
||||
protected $updatedAt;
|
||||
protected string $raw_content;
|
||||
|
||||
public function __construct() {
|
||||
|
||||
|
||||
}
|
||||
|
||||
public function load(string $name, bool $full = true): bool {
|
||||
if (!$name || strpos($name, '/') !== false || strpos($name, '*') !== false) {
|
||||
$this->error = 'BAD_ARGUMENT';
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->name = $name;
|
||||
|
||||
$filename = PAGES_PATH . "/$name.md";
|
||||
|
||||
if (!file_exists($filename)) {
|
||||
$this->error = "NOT_FOUND";
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->updatedAt = filemtime($filename);
|
||||
|
||||
$fp = fopen($filename, 'r');
|
||||
if (!$fp) {
|
||||
$this->error = "FORBIDDEN";
|
||||
return false;
|
||||
}
|
||||
|
||||
$title_line = false;
|
||||
$raw_meta_lines = array();
|
||||
$into_meta = false;
|
||||
|
||||
while (($line = trim(fgets($fp))) !== false) {
|
||||
if ($title_line === false && $line !== '---') {
|
||||
$title_line = $line;
|
||||
} elseif ($line === '---') {
|
||||
$into_meta = !$into_meta;
|
||||
} elseif ($into_meta) {
|
||||
$raw_meta_lines[] = $line;
|
||||
} else {
|
||||
if (!$title_line) {
|
||||
$title_line = $name;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if ($full) {
|
||||
$raw_content = '';
|
||||
while($chunk = fread($fp, 8192)) {
|
||||
$raw_content .= $chunk;
|
||||
}
|
||||
$this->raw_content = $raw_content;
|
||||
}
|
||||
|
||||
$this->title = $title_line;
|
||||
|
||||
$raw_meta = implode("\n", $raw_meta_lines);
|
||||
$this->meta = @Yaml::parse($raw_meta);
|
||||
|
||||
if ($this->raw_content) {
|
||||
//$this->preprocess($this->raw_content);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function getHtml() {
|
||||
if (!$this->raw_content) return false;
|
||||
$md = new CommonMarkConverter();
|
||||
$content = $md->convert($this->raw_content);
|
||||
return "<div class=\"parser-output\">$content</div>";
|
||||
}
|
||||
|
||||
public function getTitle() {
|
||||
return $this->title;
|
||||
}
|
||||
|
||||
public function getUrl() {
|
||||
if ($this->isIndex()) {
|
||||
return "/";
|
||||
}
|
||||
|
||||
return "/{$this->name}";
|
||||
}
|
||||
|
||||
public function getError() {
|
||||
if (!$this->error) {
|
||||
return false;
|
||||
} elseif ($this->error === 'NOT_FOUND') {
|
||||
return [
|
||||
'status' => 404,
|
||||
'message' => 'Not found'
|
||||
];
|
||||
} elseif ($this->error === 'FORBIDDEN') {
|
||||
return [
|
||||
'status' => 403,
|
||||
'message' => 'Access Denied'
|
||||
];
|
||||
} elseif ($this->error === 'BAD_ARGUMENT') {
|
||||
return [
|
||||
'status' => 400,
|
||||
'message' => 'Bad Request'
|
||||
];
|
||||
} else {
|
||||
return [
|
||||
'status' => 500,
|
||||
'message' => 'Unknown Error'
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
public function isIndex () {
|
||||
return $this->name === 'index';
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue