feat: rework post normalization systems
This commit is contained in:
parent
12d3838eae
commit
a65dd96f8f
1 changed files with 55 additions and 42 deletions
|
@ -14,6 +14,51 @@ class PostHandler extends MySQLAdapter {
|
|||
$this->data_directory = 'raw/' . $this->SITE_CONFIG['HTTP_HOST'];
|
||||
}
|
||||
|
||||
function deduce_post_type($post_path) {
|
||||
$ext = pathinfo($post_path, PATHINFO_EXTENSION);
|
||||
|
||||
if(preg_match("/\.(\w+)\.md$/", $post_path, $ext_match)) {
|
||||
$ext = $ext_match[1];
|
||||
}
|
||||
|
||||
$ext_mapping = [
|
||||
'' => 'directory',
|
||||
'md' => 'text/markdown',
|
||||
'png' => 'image',
|
||||
'jpg' => 'image',
|
||||
'jpeg' => 'image'
|
||||
];
|
||||
|
||||
return $ext_mapping[$ext] ?? '?';
|
||||
}
|
||||
|
||||
function fill_in_post_meta($post_path, $meta) {
|
||||
$icon_mapping = [
|
||||
'' => 'question',
|
||||
'text/markdown' => 'markdown',
|
||||
'directory' => 'folder',
|
||||
'gallery' => 'images',
|
||||
'image' => 'image'
|
||||
];
|
||||
|
||||
$meta["title"] ??= basename($post_path);
|
||||
|
||||
if($meta["title"] == "") {
|
||||
$meta["title"] = "root";
|
||||
}
|
||||
|
||||
if(!isset($meta['media_file']) and preg_match("/\.(\w+)\.md$/", $post_path)) {
|
||||
$meta['media_file'] = "https://" . $this->SITE_CONFIG['HTTP_HOST'] . chop($post_path, ".md");
|
||||
}
|
||||
|
||||
$meta['tags'] ??= [];
|
||||
$meta['type'] ??= $this->deduce_post_type($post_path);
|
||||
|
||||
$meta['icon'] ??= $icon_mapping[$meta['type']] ?? 'question';
|
||||
|
||||
return $meta;
|
||||
}
|
||||
|
||||
function _normalize_post_data($post_data) {
|
||||
$post_data = parent::_normalize_post_data($post_data);
|
||||
|
||||
|
@ -24,46 +69,10 @@ class PostHandler extends MySQLAdapter {
|
|||
$post_data["post_basename"] = basename($post_data["post_path"]);
|
||||
|
||||
$post_meta = $post_data['post_metadata'];
|
||||
|
||||
$post_meta["title"] ??= basename($post_data["post_path"]);
|
||||
if($post_meta["title"] == "") {
|
||||
$post_meta["title"] = "root";
|
||||
}
|
||||
|
||||
|
||||
$ext = pathinfo($post_data['post_basename'], PATHINFO_EXTENSION);
|
||||
|
||||
if(preg_match("/\.(\w+)\.md$/", $post_data['post_basename'], $ext_match)) {
|
||||
$ext = $ext_match[1];
|
||||
|
||||
$post_meta['media_file'] ??= "https://" . $this->SITE_CONFIG['HTTP_HOST'] . chop($post_data['post_path'], ".md");
|
||||
}
|
||||
|
||||
$ext_mapping = [
|
||||
'' => 'directory',
|
||||
'md' => 'text/markdown',
|
||||
'png' => 'image',
|
||||
'jpg' => 'image',
|
||||
'jpeg' => 'image'
|
||||
];
|
||||
|
||||
if(isset($ext_mapping[$ext])) {
|
||||
$post_meta['type'] ??= $ext_mapping[$ext];
|
||||
}
|
||||
else {
|
||||
$post_meta['type'] ??= '?';
|
||||
}
|
||||
|
||||
$icon_mapping = [
|
||||
'' => 'question',
|
||||
'text/markdown' => 'markdown',
|
||||
'directory' => 'folder',
|
||||
'image' => 'image'
|
||||
];
|
||||
|
||||
$post_meta['icon'] ??= $icon_mapping[$post_meta['type']] ?? 'question';
|
||||
|
||||
$post_data['post_metadata'] = $post_meta;
|
||||
$post_data['post_metadata'] = $this->fill_in_post_meta(
|
||||
$post_data['post_path'],
|
||||
$post_meta);
|
||||
|
||||
$post_data["post_file_dir"] = '/raw' . $post_data["post_path"];
|
||||
|
||||
|
@ -92,8 +101,15 @@ class PostHandler extends MySQLAdapter {
|
|||
$post_path = $this->_sanitize_path($post_path);
|
||||
|
||||
$post_content = $frontmatter_post->body();
|
||||
|
||||
$post_metadata = $frontmatter_post->matter();
|
||||
|
||||
$post_metadata = $this->fill_in_post_meta(
|
||||
$post_path,
|
||||
$post_metadata);
|
||||
|
||||
$post_metadata['tags'][]= 'type:' . $post_metadata['type'];
|
||||
|
||||
if(basename($post_path) == "README.md") {
|
||||
$readme_metadata = [];
|
||||
if(isset($post_metadata['settings'])) {
|
||||
|
@ -119,9 +135,6 @@ class PostHandler extends MySQLAdapter {
|
|||
|
||||
move_uploaded_file($file_path, $this->data_directory . $post_path);
|
||||
break;
|
||||
case "mddesc":
|
||||
$this->save_markdown_post(chop($post_path, '.mddesc'), file_get_contents($file_path));
|
||||
break;
|
||||
default:
|
||||
$this->save_file($post_path, $file_path);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue