<commit_before><?php namespace TightenCo\Jigsaw\PathResolvers;

class CollectionPathResolver
{
    private $outputPathResolver;
    private $viewRenderer;

    public function __construct($outputPathResolver, $viewRenderer)
    {
        $this->outputPathResolver = $outputPathResolver;
        $this->view = $viewRenderer;
    }

    public function link($permalink, $data)
    {
        return collect($data->extends)->map(function($bladeViewPath, $templateKey) use ($permalink, $data) {
            return $this->cleanOutputPath(
                $this->getPath($permalink, $data, $this->getExtension($bladeViewPath), $templateKey)
            );
        });
    }

    public function getExtension($bladeViewPath)
    {
        $extension = $this->view->getExtension($bladeViewPath);

        return collect(['php', 'html'])->contains($extension) ? '' : '.' . $extension;
    }

    private function getPath($permalink, $data, $extension, $templateKey = null)
    {
        $templateKeySuffix = $templateKey ? '/' . $templateKey : '';

        if ($templateKey && is_array($permalink)) {
            $permalink = array_get($permalink, $templateKey);
            $templateKeySuffix = '';

            if (! $permalink) {
                return;
            }
        }

        if (is_callable($permalink)) {
            $link = $this->cleanInputPath($permalink->__invoke($data));

            return $link ? $this->resolve($link . $templateKeySuffix . $extension) : '';
        }

        if (is_string($permalink) && $permalink) {
            $link =$this->parseShorthand($this->cleanInputPath($permalink), $data);

            return $link ? $this->resolve($link . $templateKeySuffix . $extension) : '';
        }

        return $this->getDefaultPath($data, $templateKey) . $templateKeySuffix . $extension;
    }

    private function getDefaultPath($data)
    {
        return str_slug($data->getCollectionName()) . '/' . str_slug($data->getFilename());
    }

    private function parseShorthand($path, $data)
    {
        preg_match_all('/\{(.*?)\}/', $path, $bracketedParameters);

        if (count($bracketedParameters[0]) == 0) {
            return $path . '/' . str_slug($data->getFilename());
        }

        $bracketedParametersReplaced =
            collect($bracketedParameters[0])->map(function($param) use ($data) {
                return ['token' => $param, 'value' => $this->getParameterValue($param, $data)];
            })->reduce(function ($carry, $param) use ($path) {
                return str_replace($param['token'], $param['value'], $carry);
            }, $path);
<commit_msg>Update CollectionPathResolver for _meta changes, rename permalink to path<commit_after><?php namespace TightenCo\Jigsaw\PathResolvers;

use TightenCo\Jigsaw\IterableObject;

class CollectionPathResolver
{
    private $outputPathResolver;
    private $viewRenderer;

    public function __construct($outputPathResolver, $viewRenderer)
    {
        $this->outputPathResolver = $outputPathResolver;
        $this->view = $viewRenderer;
    }

    public function link($path, $data)
    {
        return collect($data->extends)->map(function($bladeViewPath, $templateKey) use ($path, $data) {
            return $this->cleanOutputPath(
                $this->getPath($path, $data, $this->getExtension($bladeViewPath), $templateKey)
            );
        });
    }

    public function getExtension($bladeViewPath)
    {
        $extension = $this->view->getExtension($bladeViewPath);

        return collect(['php', 'html'])->contains($extension) ? '' : '.' . $extension;
    }

    private function getPath($path, $data, $extension, $templateKey = null)
    {
        $templateKeySuffix = $templateKey ? '/' . $templateKey : '';

        if ($templateKey && $path instanceof IterableObject) {
            $path = $path->get($templateKey);
            $templateKeySuffix = '';

            if (! $path) {
                return;
            }
        }

        if (is_callable($path)) {
            $link = $this->cleanInputPath($path->__invoke($data));

            return $link ? $this->resolve($link . $templateKeySuffix . $extension) : '';
        }

        if (is_string($path) && $path) {
            $link =$this->parseShorthand($this->cleanInputPath($path), $data);

            return $link ? $this->resolve($link . $templateKeySuffix . $extension) : '';
        }

        return $this->getDefaultPath($data, $templateKey) . $templateKeySuffix . $extension;
    }

    private function getDefaultPath($data)
    {
        return str_slug($data->_meta->get('collectionName')) . '/' . str_slug($data->_meta->get('filename'));
    }

    private function parseShorthand($path, $data)
    {
        preg_match_all('/\{(.*?)\}/', $path, $bracketedParameters);

        if (count($bracketedParameters[0]) == 0) {
            return $path . '/' . str_slug($data->_meta->get('collectionName'));
        }

        $bracketedParametersReplaced =
            collect($bracketedParameters[0])->map(function($param) use ($data) {
                return ['token' => $param, 'value' => $this->getParameterValue($param, $data)];
            })->reduce(function ($carry, $param) use ($path) {
                return str_replace($param['token'], $param['value'], $carry);
            }, $path);

{'fraction_non_alphanumeric': 0.16132075471698112, 'fraction_numerical': 0.0011320754716981133, 'mean_word_length': 2.628336755646817, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 57, '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': '4929636', 'n_tokens_mistral': 1563, 'n_tokens_neox': 1519, 'n_words': 263}