2015-04-22 18:24:10 +02:00
|
|
|
<?php namespace Todaymade\Daux\Tree;
|
|
|
|
|
2015-08-16 21:19:55 +02:00
|
|
|
use RuntimeException;
|
|
|
|
use SplFileInfo;
|
2015-04-22 18:24:10 +02:00
|
|
|
use Todaymade\Daux\Daux;
|
2015-07-17 18:28:54 +02:00
|
|
|
use Todaymade\Daux\DauxHelper;
|
2015-04-22 18:24:10 +02:00
|
|
|
|
2015-04-23 00:32:30 +02:00
|
|
|
class Builder
|
|
|
|
{
|
2015-08-16 21:19:55 +02:00
|
|
|
protected static $IGNORED = [
|
|
|
|
// Popular VCS Systems
|
|
|
|
'.svn', '_svn', 'CVS', '_darcs', '.arch-params', '.monotone', '.bzr', '.git', '.hg',
|
|
|
|
|
|
|
|
// Operating system files
|
|
|
|
'.DS_Store', 'Thumbs.db',
|
|
|
|
];
|
|
|
|
|
2015-08-16 22:26:00 +02:00
|
|
|
protected static function isIgnored(\SplFileInfo $file, $ignore)
|
|
|
|
{
|
2016-07-04 20:33:44 +02:00
|
|
|
$filename = $file->getFilename();
|
|
|
|
|
|
|
|
if (in_array($filename, static::$IGNORED)) {
|
2015-08-16 21:19:55 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-07-04 20:33:44 +02:00
|
|
|
if (array_key_exists('folders', $ignore) && $file->isDir() && in_array($filename, $ignore['folders'])) {
|
2015-08-16 21:19:55 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-07-04 20:33:44 +02:00
|
|
|
if (array_key_exists('files', $ignore) && !$file->isDir() && in_array($filename, $ignore['files'])) {
|
2015-08-16 21:19:55 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get name for a file
|
|
|
|
*
|
|
|
|
* @param string $path
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected static function getName($path)
|
|
|
|
{
|
|
|
|
// ['dir' => 1, 'basename' => 2, 'filename' => 3, 'extension' => 5]
|
|
|
|
preg_match('%^(.*?)[\\\\/]*(([^/\\\\]*?)(\.([^\.\\\\/]+?)|))[\\\\/\.]*$%im', $path, $m);
|
|
|
|
|
|
|
|
if (!isset($m[3])) {
|
2016-07-27 21:32:51 +02:00
|
|
|
throw new RuntimeException('Name not found');
|
2015-08-16 21:19:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $m[3];
|
|
|
|
}
|
|
|
|
|
2015-07-17 23:38:06 +02:00
|
|
|
/**
|
|
|
|
* Build the initial tree
|
|
|
|
*
|
2015-07-18 20:52:14 +02:00
|
|
|
* @param Directory $node
|
2015-07-17 23:38:06 +02:00
|
|
|
* @param array $ignore
|
|
|
|
*/
|
2015-07-18 23:13:02 +02:00
|
|
|
public static function build($node, $ignore)
|
2015-04-23 00:32:30 +02:00
|
|
|
{
|
2015-10-21 21:41:42 +02:00
|
|
|
if (($it = new \FilesystemIterator($node->getPath())) == false) {
|
2015-04-22 18:24:10 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-07-18 20:52:14 +02:00
|
|
|
if ($node instanceof Root) {
|
|
|
|
// Ignore config.json in the root directory
|
|
|
|
$ignore['files'][] = 'config.json';
|
2015-04-22 18:24:10 +02:00
|
|
|
}
|
|
|
|
|
2015-08-16 21:19:55 +02:00
|
|
|
/** @var \SplFileInfo $file */
|
|
|
|
foreach ($it as $file) {
|
|
|
|
if (static::isIgnored($file, $ignore)) {
|
2015-04-22 18:24:10 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-08-16 21:19:55 +02:00
|
|
|
if ($file->isDir()) {
|
|
|
|
$new = new Directory($node, static::removeSortingInformations($file->getFilename()), $file);
|
|
|
|
$new->setName(static::getName($file->getPathName()));
|
2015-10-28 00:01:41 +01:00
|
|
|
$new->setTitle(str_replace('_', ' ', static::removeSortingInformations($new->getName())));
|
2015-07-18 23:13:02 +02:00
|
|
|
static::build($new, $ignore);
|
2015-04-22 18:24:10 +02:00
|
|
|
} else {
|
2015-08-16 21:19:55 +02:00
|
|
|
static::createContent($node, $file);
|
2015-04-22 18:24:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$node->sort();
|
2015-07-18 20:52:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Directory $parent
|
2015-08-16 21:19:55 +02:00
|
|
|
* @param SplFileInfo $file
|
2015-07-18 20:52:14 +02:00
|
|
|
* @return Content|Raw
|
|
|
|
*/
|
2015-08-16 21:19:55 +02:00
|
|
|
public static function createContent(Directory $parent, SplFileInfo $file)
|
2015-07-18 20:52:14 +02:00
|
|
|
{
|
2015-08-16 21:19:55 +02:00
|
|
|
$name = static::getName($file->getPathname());
|
2015-07-18 20:52:14 +02:00
|
|
|
|
2015-07-19 16:36:34 +02:00
|
|
|
$config = $parent->getConfig();
|
|
|
|
|
2015-08-16 21:19:55 +02:00
|
|
|
if (!in_array($file->getExtension(), $config['valid_content_extensions'])) {
|
|
|
|
$uri = static::removeSortingInformations($file->getFilename());
|
2015-08-02 15:42:23 +02:00
|
|
|
|
2015-08-16 21:19:55 +02:00
|
|
|
$entry = new Raw($parent, $uri, $file);
|
2015-10-28 00:01:41 +01:00
|
|
|
$entry->setTitle(str_replace('_', ' ', static::removeSortingInformations($name)));
|
2015-07-18 23:13:02 +02:00
|
|
|
$entry->setName($name);
|
|
|
|
|
|
|
|
return $entry;
|
2015-07-18 20:52:14 +02:00
|
|
|
}
|
|
|
|
|
2015-07-20 15:59:52 +02:00
|
|
|
$uri = static::removeSortingInformations($name);
|
2016-08-16 23:25:06 +02:00
|
|
|
if ($config->isStatic()) {
|
2015-07-18 23:13:02 +02:00
|
|
|
$uri .= '.html';
|
|
|
|
}
|
|
|
|
|
2015-08-16 21:19:55 +02:00
|
|
|
$entry = new Content($parent, $uri, $file);
|
2015-07-18 23:13:02 +02:00
|
|
|
|
|
|
|
if ($entry->getUri() == $config['index_key']) {
|
2015-07-18 20:52:14 +02:00
|
|
|
if ($parent instanceof Root) {
|
2015-07-18 23:13:02 +02:00
|
|
|
$entry->setTitle($config['title']);
|
2015-07-18 20:52:14 +02:00
|
|
|
} else {
|
|
|
|
$entry->setTitle($parent->getTitle());
|
|
|
|
}
|
|
|
|
} else {
|
2015-10-28 00:01:41 +01:00
|
|
|
$entry->setTitle(str_replace('_', ' ', static::removeSortingInformations($name)));
|
2015-07-18 20:52:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$entry->setName($name);
|
|
|
|
|
|
|
|
return $entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $filename
|
|
|
|
* @return string
|
|
|
|
*/
|
2015-10-28 00:01:41 +01:00
|
|
|
public static function removeSortingInformations($filename)
|
2015-07-18 20:52:14 +02:00
|
|
|
{
|
2016-07-27 21:32:51 +02:00
|
|
|
preg_match('/^-?[0-9]*_?(.*)/', $filename, $matches);
|
2015-08-04 22:40:24 +02:00
|
|
|
|
2015-10-28 00:01:41 +01:00
|
|
|
// Remove the numeric part
|
|
|
|
// of the filename, only if
|
|
|
|
// there is something after
|
2016-07-27 21:32:51 +02:00
|
|
|
return empty($matches[1]) ? $matches[0] : $matches[1];
|
2015-04-22 18:24:10 +02:00
|
|
|
}
|
2015-07-17 18:28:54 +02:00
|
|
|
|
2015-07-17 23:38:06 +02:00
|
|
|
/**
|
2015-07-18 14:37:18 +02:00
|
|
|
* @param Directory $parent
|
2016-07-27 21:32:51 +02:00
|
|
|
* @param string $title
|
2015-07-17 23:38:06 +02:00
|
|
|
* @return Directory
|
|
|
|
*/
|
2015-07-18 14:37:18 +02:00
|
|
|
public static function getOrCreateDir(Directory $parent, $title)
|
2015-07-17 23:38:06 +02:00
|
|
|
{
|
2015-07-17 18:28:54 +02:00
|
|
|
$slug = DauxHelper::slug($title);
|
|
|
|
|
2015-07-18 14:37:18 +02:00
|
|
|
if (array_key_exists($slug, $parent->getEntries())) {
|
|
|
|
return $parent->getEntries()[$slug];
|
2015-07-17 18:28:54 +02:00
|
|
|
}
|
|
|
|
|
2015-07-18 20:52:14 +02:00
|
|
|
$dir = new Directory($parent, $slug);
|
2015-07-17 18:28:54 +02:00
|
|
|
$dir->setTitle($title);
|
|
|
|
|
|
|
|
return $dir;
|
|
|
|
}
|
|
|
|
|
2015-07-17 23:38:06 +02:00
|
|
|
/**
|
2015-07-18 20:52:14 +02:00
|
|
|
* @param Directory $parent
|
2015-07-29 22:39:19 +02:00
|
|
|
* @param string $path
|
2016-07-29 22:38:03 +02:00
|
|
|
* @return ContentAbstract
|
2015-07-17 23:38:06 +02:00
|
|
|
*/
|
2015-07-29 22:39:19 +02:00
|
|
|
public static function getOrCreatePage(Directory $parent, $path)
|
2015-07-17 23:38:06 +02:00
|
|
|
{
|
2016-01-31 12:00:29 +01:00
|
|
|
$extension = pathinfo($path, PATHINFO_EXTENSION);
|
2015-07-29 22:39:19 +02:00
|
|
|
// If the file doesn't have an extension, set .md as a default
|
2016-01-31 12:00:29 +01:00
|
|
|
if ($extension == '') {
|
|
|
|
$extension = 'md';
|
2015-07-29 22:39:19 +02:00
|
|
|
$path .= '.md';
|
|
|
|
}
|
|
|
|
|
2016-01-31 12:00:29 +01:00
|
|
|
$raw = !in_array($extension, $parent->getConfig()['valid_content_extensions']);
|
|
|
|
|
|
|
|
$title = $uri = $path;
|
|
|
|
if (!$raw) {
|
|
|
|
$title = static::getName($path);
|
|
|
|
$uri = DauxHelper::slug($title);
|
2016-08-16 23:25:06 +02:00
|
|
|
if ($parent->getConfig()->isStatic()) {
|
2016-07-27 21:32:51 +02:00
|
|
|
$uri .= '.html';
|
2016-01-31 12:00:29 +01:00
|
|
|
}
|
2015-07-21 09:51:55 +02:00
|
|
|
}
|
2015-07-17 18:28:54 +02:00
|
|
|
|
2015-07-18 20:52:14 +02:00
|
|
|
if (array_key_exists($uri, $parent->getEntries())) {
|
|
|
|
return $parent->getEntries()[$uri];
|
2015-07-17 18:28:54 +02:00
|
|
|
}
|
|
|
|
|
2016-07-27 21:32:51 +02:00
|
|
|
$page = $raw ? new ComputedRaw($parent, $uri) : new Content($parent, $uri);
|
|
|
|
$page->setContent('-'); //set an almost empty content to avoid problems
|
2016-01-31 12:00:29 +01:00
|
|
|
$page->setName($path);
|
|
|
|
$page->setTitle($title);
|
2015-07-17 18:28:54 +02:00
|
|
|
|
2016-01-31 12:00:29 +01:00
|
|
|
if ($title == 'index' || $title == '_index') {
|
2015-07-18 20:52:14 +02:00
|
|
|
$page->setTitle($parent->getTitle());
|
2015-07-17 18:28:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $page;
|
|
|
|
}
|
2015-04-22 18:24:10 +02:00
|
|
|
}
|