.
*
*/
namespace Vvveb\Plugins\MarkdownEditor\Controller;
use function Vvveb\__;
use Vvveb\Controller\Base;
use Vvveb\Plugins\MarkdownEditor\System\Parsedown as Parsedown;
use Vvveb\Sql\categorySQL;
use Vvveb\Sql\postSQL;
class Settings extends Base {
private $cats = [];
private function markdownHtml($markdown, $dir) {
//escape html tags
$markdown = str_replace(['<', '>'], ['<', '>'], $markdown);
//escape code blocks
$markdown = preg_replace_callback('/```.*?```/ms', function ($matches) {
return str_replace(['<', '>'], ['<', '>'], $matches[0]);
}, $markdown);
/*
$markdown = preg_replace_callback('/```.*?```/ms', function ($matches) {
return htmlentities($matches[0]);
}, $markdown);*/
//echo $markdown;
$html = $text = $this->parsedown->text($markdown);
//process images
$mediaPath = PUBLIC_PATH . 'media/';
mkdir(DIR_ROOT . $mediaPath . 'docs/');
$html = preg_replace_callback('/src=["\'](.+?)["\']/',
function ($match) use (&$mediaPath, $dir) {
$image = $match[1];
//if local image copy to media
if (strpos($image, '//') === false) {
copy($dir . '/' . $image, DIR_ROOT . $mediaPath . 'docs/' . $image);
return 'src="/media/docs/' . $image . '"';
}
return $match[0];
},$html);
return $html;
}
private function traverseDir($dir) {
if (! ($dp = opendir($dir))) {
die("Cannot open $dir.");
}
while ((false !== $file = readdir($dp))) {
if (is_dir($dir . DS . $file)) {
if ($file != '.' && $file != '..') {
//echo "category $dir/$file
";
//check and add dir
$this->addCategories($dir . DS . $file);
$this->traverseDir($dir . DS . $file);
chdir($dir);
}
} else {
if ($file != '.' && $file != '..' && strrpos($file, '.md') != false) {
//add post
$this->addPost($file,$dir . DS . $file, $dir);
}
}
}
closedir($dp);
return true;
}
private function addCategories($file) {
$folder = trim(str_replace($this->docs_folder, '', $file), ' /' . DS);
//echo '
';
$cats = explode(DS, $folder);
$prev_category = 0;
$category_id = 0;
foreach ($cats as $category_slug) {
$cat = $this->categories->getCategoryBySlug(['slug' => $category_slug, 'taxonomy_id' => 1, 'parent_id' => $prev_category] + $this->global);
if ($cat) {
$prev_category = $cat['taxonomy_item_id'];
} else {
$cat = $this->categories->addCategory([
'taxonomy_item' => $this->global + [
'parent_id' => $prev_category, 'taxonomy_id' => 1,
],
'taxonomy_item_content' => $this->global + ['slug'=> $category_slug, 'name' => \Vvveb\humanReadable($category_slug), 'content' => \Vvveb\humanReadable($category_slug)],
] + $this->global);
$category_id = $cat['taxonomy_item'];
}
}
if (! $category_id) {
$category_id = $prev_category;
}
if (! isset($this->cats[$folder])) {
$this->cats[$folder] = $category_id;
}
}
private function addPost($filename, $file, $dir) {
$category = trim(str_replace($this->docs_folder, '', dirname($file)), ' /' . DS);
$slug = \Vvveb\filter('/([^.]+)/', basename($file));
$name = \Vvveb\humanReadable($slug);
$slug = \Vvveb\filter('/([^.]+)/', basename($file));
$name = \Vvveb\humanReadable($slug);
$markdown = file_get_contents($file);
$html = $this->markdownHtml($markdown, $dir);
//get name from heading 1 if available
$html = preg_replace_callback('/