<commit_before><?php namespace Fungku\Postmark;

use Fungku\Postmark\Contracts\Parseable;
use Illuminate\Filesystem\Filesystem;

class Postmark
{
    /**
     * @var Filesystem
     */
    private $filesystem;
    /**
     * @var Parseable
     */
    private $parser;

    /**
     * TODO: Not liking the filesystem dependency, so look at other options
     *
     * @param Filesystem $filesystem
     * @param Parseable $parser
     */
    public function __construct(Filesystem $filesystem, Parseable $parser)
    {
        $this->filesystem = $filesystem;
        $this->parser = $parser;
    }

    /**
     * @param Parseable $parser
     * @return static
     */
    public static function create(Parseable $parser = null)
    {
        $parser = $parser ?: new Parser(new \Parsedown());

        return new static(new Filesystem(), $parser);
    }

    /**
     * @param string $basePath
     * @param string $post
     * @return mixed
     */
    public static function parse($basePath, $post)
    {
        $parser = static::create();

        return $parser->parse($basePath, $post);
    }

    /**
     * TODO: Refactor
     *
     * @param string $basePath
     * @param string $post
     * @return array
     * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
     */
    public function getContent($basePath, $post)
    {
        $breadcrumbs = explode('/', $post);
        $breadcrumbs = empty($breadcrumbs[0]) ? array() : $breadcrumbs;
        $postPath = $basePath .'/'. $post;
        $isDir = false;
        $index = [];

        if ($this->filesystem->isDirectory($postPath)) {
            $isDir = true;
            $index['subcategories'] = $this->filesystem->directories($postPath);
            $index['files'] = $this->filesystem->files($postPath);
            $postPath .= '/index';
        }

        $file = $postPath .'.md';
        if ($this->filesystem->exists($file)) {
            $post = $this->parser->parse($this->filesystem->get($file));
        } else {
            $post = '';
        }

        if ($isDir) {
            foreach ($index['subcategories'] as $i => $item) {
                $item = str_replace($basePath, '', $item);
                $paths = explode('/', $item);
                $index['subcategories'][$i] = [
                    'href' => $item,
                    'name' => array_pop($paths),
                ];
            }
            foreach ($index['files'] as $i => $item) {
                $item = str_replace($basePath, '', $item);
                $item = str_replace('.md', '', $item);
                $paths = explode('/', $item);
                $index['files'][$i] = [
                    'href' => $item,
                    'name' => array_pop($paths),
                ];
                if ($index['files'][$i]['name'] === 'index') {
                    unset($index['files'][$i]);
                }
            }
        }

        return [
            'breadcrumbs' => $breadcrumbs,
            'index'       => $index,
            'post'        => $post,
        ];
    }
}<commit_msg>Add deslugging method, and extract breadcrumbs method...<commit_after><?php namespace Fungku\Postmark;

use Fungku\Postmark\Contracts\Parseable;
use Illuminate\Filesystem\Filesystem;

class Postmark
{
    /**
     * @var Filesystem
     */
    private $filesystem;
    /**
     * @var Parseable
     */
    private $parser;

    /**
     * TODO: Not liking the filesystem dependency, so look at other options
     *
     * @param Filesystem $filesystem
     * @param Parseable $parser
     */
    public function __construct(Filesystem $filesystem, Parseable $parser)
    {
        $this->filesystem = $filesystem;
        $this->parser = $parser;
    }

    /**
     * @param Parseable $parser
     * @return static
     */
    public static function create(Parseable $parser = null)
    {
        $parser = $parser ?: new Parser(new \Parsedown());

        return new static(new Filesystem(), $parser);
    }

    /**
     * @param string $basePath
     * @param string $post
     * @return mixed
     */
    public static function parse($basePath, $post)
    {
        $parser = static::create();

        return $parser->parse($basePath, $post);
    }

    /**
     * TODO: Refactor
     *
     * @param string $basePath
     * @param string $post
     * @return array
     * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
     */
    public function getContent($basePath, $post)
    {
        $breadcrumbs = $this->makeBreadcrumbsFromPost($post);

        $postPath = $basePath .'/'. $post;
        $isDir = false;
        $index = [];

        if ($this->filesystem->isDirectory($postPath)) {
            $isDir = true;
            $index['subcategories'] = $this->filesystem->directories($postPath);
            $index['files'] = $this->filesystem->files($postPath);
            $postPath .= '/index';
        }

        $file = $postPath .'.md';
        if ($this->filesystem->exists($file)) {
            $post = $this->parser->parse($this->filesystem->get($file));
        } else {
            $post = '';
        }

        if ($isDir) {
            foreach ($index['subcategories'] as $i => $item) {
                $item = str_replace($basePath, '', $item);
                $paths = explode('/', $item);
                $index['subcategories'][$i] = [
                    'href' => $item,
                    'name' => array_pop($paths),
                ];
            }
            foreach ($index['files'] as $i => $item) {
                $item = str_replace($basePath, '', $item);
                $item = str_replace('.md', '', $item);
                $paths = explode('/', $item);
                $index['files'][$i] = [
                    'href' => $item,
                    'name' => array_pop($paths),
                ];
                if ($index['files'][$i]['name'] === 'index') {
                    unset($index['files'][$i]);
                }
            }
        }

        return [
            'breadcrumbs' => $breadcrumbs,
            'index'       => $index,
            'post'        => $post,
        ];
    }

    /**
     * @param string $post
     * @return array
     */
    private function makeBreadcrumbsFromPost($post)
    {
        $breadcrumbs = explode('/', $post);
        $breadcrumbs = empty($breadcrumbs[0]) ? array() : $breadcrumbs;

        return $this->deslug($breadcrumbs);
    }

    /**
     * @param array|string $slugged
     * @return array|string
     */
    private function deslug($slugged)
    {
        if (is_array($slugged)) {
            return array_map($this->deslug, $slugged);
        }

        return str_replace(['-', '_'], ' ', $slugged);
    }
}
{'fraction_non_alphanumeric': 0.16006551518761167, 'fraction_numerical': 0.0002977963073257892, 'mean_word_length': 1.7073760580411124, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 54, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6767194', 'n_tokens_mistral': 1976, 'n_tokens_neox': 1945, 'n_words': 392}