public function generate(SourceInterface $source)
 {
     $dataProvider = $this->dataProviderManager->dataProvider($this->dataProviderName);
     $taxons = $dataProvider->provideData();
     $generatedSources = array();
     foreach ($taxons as $taxon => $items) {
         $generatedSource = $source->duplicate($source->sourceId() . ':' . $this->injectedTaxonKey . '=' . $taxon);
         $permalink = $source->data()->get('permalink') ?: $source->relativePathname();
         $basename = basename($permalink);
         $permalink = dirname($permalink);
         $indexType = null;
         if (preg_match('/^(.+?)\\.(.+)$/', $basename, $matches)) {
             $urlTaxon = $this->permalinkStrategyCollection->process($taxon);
             $indexType = $matches[2];
             $suffix = in_array($indexType, array('xml', 'rss', 'json')) ? '.' . $indexType : '/';
             $permalink = $permalink . '/' . $urlTaxon . $suffix;
         } else {
             // not sure what case this is?
         }
         if (0 === strpos($permalink, './')) {
             $permalink = substr($permalink, 2);
         }
         if (0 !== strpos($permalink, '/')) {
             $permalink = '/' . $permalink;
         }
         if ($permalink) {
             // not sure if this is ever going to happen?
             $generatedSource->data()->set('permalink', $permalink);
         }
         $generatedSource->data()->set($this->injectedTaxonKey, $taxon);
         $generatedSource->data()->set($this->injectedTaxonItemsKey, $items);
         if ($indexType) {
             foreach ($items as $item) {
                 $key = $this->injectedTaxonKey . '_' . $indexType . '_index_permalinks';
                 $taxonIndexPermalinks = $item->data()->get($key) ?: array();
                 $taxonIndexPermalinks[$taxon] = $permalink;
                 $item->data()->set($key, $taxonIndexPermalinks);
             }
         }
         //
         // TODO: REMOVE THIS
         //
         // This is needed for BC purposes. This should be removed
         // eventually and existing markup should be updated.
         //
         switch ($this->injectedTaxonItemsKey) {
             case 'tag_posts':
                 $generatedSource->data()->set('tagged_posts', $items);
                 break;
             case 'category_posts':
                 $generatedSource->data()->set('categoried_posts', $items);
                 break;
         }
         $generatedSources[] = $generatedSource;
     }
     return $generatedSources;
 }
Beispiel #2
0
 public function match(SourceInterface $source)
 {
     $normalizedPath = $this->directorySeparatorNormalizer->normalize($source->relativePathname());
     foreach ($this->patterns as $pattern) {
         if ($this->antPathMatcher->match($pattern, $normalizedPath)) {
             return true;
         }
     }
     return false;
 }
 public function process(SourceInterface $source)
 {
     if (!$source->data()->get('calculated_date')) {
         if (preg_match('/(\\d{4})[\\/\\-]*(\\d{2})[\\/\\-]*(\\d{2})[\\/\\-]*(\\d+|)/', $source->relativePathname(), $matches)) {
             list($dummy, $year, $month, $day, $time) = $matches;
             $parts = array(implode('-', array($year, $month, $day)));
             if ($time && false !== strtotime($time)) {
                 $parts[] = $time;
             }
             $source->data()->set('calculated_date', $calculatedDate = strtotime(implode(' ', $parts)));
             if (!$source->data()->get('date')) {
                 $source->data()->set('date', $calculatedDate);
             }
         }
     }
 }
 protected function generatePermalinkPathname(SourceInterface $source)
 {
     $pathname = $source->relativePathname();
     // Make sure that twig files end up as .html files.
     $pathname = preg_replace('/(html\\.)?twig$|twig\\.html$/', 'html', $pathname);
     $date = $source->data()->get('calculated_date');
     $title = $source->data()->get('title');
     $slug = $source->data()->get('slug');
     if (!($permalink = $source->data()->get('permalink'))) {
         $permalink = $this->defaultPermalink;
     }
     switch ($permalink) {
         case 'none':
             return $pathname;
             break;
         case 'pretty':
             if ($response = $this->isDatePath($pathname)) {
                 return implode('/', array_merge($response, array('index.html')));
             } else {
                 $pretty = preg_replace('/(\\.[^\\.\\/]+|\\.[^\\.\\/]+\\.[^\\.\\/]+)$/', '', $pathname);
                 if (basename($pretty) == 'index') {
                     return $pretty . '.html';
                 } else {
                     return $pretty . '/index.html';
                 }
             }
             break;
         case 'date':
             if ($response = $this->isDatePath($pathname)) {
                 return implode('/', $response) . '.html';
             }
             return preg_replace('/(\\.[^\\.]+|\\.[^\\.]+\\.[^\\.]+)$/', '', $pathname) . '.html';
             break;
         default:
             list($year, $yr, $month, $mo, $day, $dy) = explode('-', date('Y-y-m-n-d-j', (int) $date));
             $permalink = preg_replace('/:year/', $year, $permalink);
             $permalink = preg_replace('/:yr/', $yr, $permalink);
             $permalink = preg_replace('/:month/', $month, $permalink);
             $permalink = preg_replace('/:mo/', $mo, $permalink);
             $permalink = preg_replace('/:day/', $day, $permalink);
             $permalink = preg_replace('/:dy/', $dy, $permalink);
             $permalink = preg_replace('/:title/', $this->normalize($title), $permalink);
             $permalink = preg_replace('/:slug_title/', $this->normalize($slug ?: $title), $permalink);
             $filename = $pathname;
             if ($isDatePath = $this->isDatePath($pathname)) {
                 $filename = $isDatePath[3];
             }
             $permalink = preg_replace('/:filename/', $filename, $permalink);
             $permalink = preg_replace('/:slug_filename/', $this->normalize($slug ?: $filename), $permalink);
             if (strrpos($filename, DIRECTORY_SEPARATOR) !== false) {
                 $basename = substr($filename, strrpos($filename, DIRECTORY_SEPARATOR) + 1);
             } else {
                 $basename = $filename;
             }
             $prettyBasename = false !== strrpos($basename, '.') ? substr($basename, 0, strrpos($basename, '.')) : $basename;
             $permalink = preg_replace('/:basename_real/', $basename, $permalink);
             $permalink = preg_replace('/:basename/', $prettyBasename, $permalink);
             if (substr($permalink, -1, 1) == '/') {
                 $permalink .= 'index.html';
             }
             return $permalink;
             break;
     }
 }
Beispiel #5
0
 /**
  * {@inheritdoc}
  */
 public function relativePathname()
 {
     return $this->source->relativePathname();
 }
 /**
  * {@inheritdoc}
  */
 public function generate(SourceInterface $source)
 {
     // select data source
     $data = null;
     $config = $source->data()->get('sculpin_calendarian') ?: array();
     if (!isset($config['provider'])) {
         $config['provider'] = 'data.posts';
     }
     if (preg_match('/^(data|page)\\.(.+)$/', $config['provider'], $matches)) {
         switch ($matches[1]) {
             case 'data':
                 $data = $this->dataProviderManager->dataProvider($matches[2])->provideData();
                 break;
             case 'page':
                 $data = $source->data()->get($matches[2]);
                 break;
         }
     }
     if (null === $data) {
         return;
     }
     // distribute the post by date
     $page_list = array();
     foreach ($data as $id => $d) {
         $year = date('Y****', $d->date());
         $month = date('Ym**', $d->date());
         $day = date('Ymd', $d->date());
         // add to 'year'
         if (!array_key_exists($year, $page_list)) {
             $page_list[$year] = array();
         }
         $page_list[$year][] = $d;
         // add to 'month'
         if (!array_key_exists($month, $page_list)) {
             $page_list[$month] = array();
         }
         $page_list[$month][] = $d;
         // add to 'day'
         if (!array_key_exists($day, $page_list)) {
             $page_list[$day] = array();
         }
         $page_list[$day][] = $d;
     }
     $pages = array();
     foreach ($page_list as $k => $sources) {
         // generate permalink
         $key = str_replace('/**', '', preg_replace('/(....)(..)(..)/', '$1/$2/$3', $k));
         $permalink = $source->data()->get('permalink') ?: $source->relativePathname();
         $basename = basename($permalink);
         if (preg_match('/^(.+?)\\.(.+)$/', $basename, $m)) {
             $permalink = dirname($permalink) . '/' . $key . '/index.' . $m[2];
         } else {
             $permalink = null;
         }
         $page = $source->duplicate($source->sourceId() . ':calendarian=' . $key);
         $keys = explode('/', $key);
         if (0 < count($keys)) {
             $page->data()->set('calendarian.year', (int) $keys[0]);
         }
         if (1 < count($keys)) {
             $page->data()->set('calendarian.month', (int) $keys[1]);
         }
         if (2 < count($keys)) {
             $page->data()->set('calendarian.day', (int) $keys[2]);
         }
         if (null != $permalink) {
             $page->data()->set('permalink', $permalink);
         }
         $page->data()->set('calendarian.items', $sources);
         $pages[] = $page;
     }
     return $pages;
 }
 /**
  * {@inheritdoc}
  */
 public function generate(SourceInterface $source)
 {
     $data = null;
     $config = $source->data()->get('pagination') ?: array();
     if (!isset($config['provider'])) {
         $config['provider'] = 'data.posts';
     }
     if (preg_match('/^(data|page)\\.(.+)$/', $config['provider'], $matches)) {
         switch ($matches[1]) {
             case 'data':
                 $data = $this->dataProviderManager->dataProvider($matches[2])->provideData();
                 break;
             case 'page':
                 $data = $source->data()->get($matches[2]);
                 break;
         }
     }
     if (null === $data) {
         return;
     }
     $maxPerPage = isset($config['max_per_page']) ? $config['max_per_page'] : $this->maxPerPage;
     $slices = array();
     $slice = array();
     $totalItems = 0;
     foreach ($data as $k => $v) {
         if (count($slice) == $maxPerPage) {
             $slices[] = $slice;
             $slice = array();
         }
         $slice[$k] = $v;
         $totalItems++;
     }
     if (count($slice)) {
         $slices[] = $slice;
     }
     $sources = array();
     $pageNumber = 0;
     foreach ($slices as $slice) {
         $pageNumber++;
         $permalink = null;
         if ($pageNumber > 1) {
             $permalink = $source->data()->get('permalink') ?: $source->relativePathname();
             $basename = basename($permalink);
             if (preg_match('/^(.+?)\\.(.+)$/', $basename, $matches)) {
                 if ('index' === $matches[1]) {
                     $paginatedPage = '';
                     $index = '/index';
                 } else {
                     $paginatedPage = $matches[1] . '/';
                     $index = '';
                 }
                 $permalink = dirname($permalink) . '/' . $paginatedPage . 'page/' . $pageNumber . $index . '.' . $matches[2];
             } else {
                 $permalink = dirname($permalink) . '/' . $basename . '/page/' . $pageNumber . '.html';
             }
             if (0 === strpos($permalink, './')) {
                 $permalink = substr($permalink, 2);
             }
         }
         $generatedSource = $source->duplicate($source->sourceId() . ':page=' . $pageNumber);
         if (null !== $permalink) {
             $generatedSource->data()->set('permalink', $permalink);
         }
         $generatedSource->data()->set('pagination.items', $slice);
         $generatedSource->data()->set('pagination.page', $pageNumber);
         $generatedSource->data()->set('pagination.total_pages', count($slices));
         $generatedSource->data()->set('pagination.total_items', $totalItems);
         $sources[] = $generatedSource;
     }
     for ($i = 0; $i < count($sources); $i++) {
         $generatedSource = $sources[$i];
         if (0 === $i) {
             $generatedSource->data()->set('pagination.previous_page', null);
         } else {
             $generatedSource->data()->set('pagination.previous_page', $sources[$i - 1]);
         }
         if ($i + 1 < count($sources)) {
             $generatedSource->data()->set('pagination.next_page', $sources[$i + 1]);
         } else {
             $generatedSource->data()->set('pagination.next_page', null);
         }
     }
     return $sources;
 }