public function process(SourceInterface $source) { foreach ($this->defaults as $name => $value) { if (!$source->data()->get($name) && $value) { $source->data()->set($name, $value); } } }
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; }
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; }
/** * Convert Source * * @param SourceInterface $source Source */ public function convertSource(SourceInterface $source) { $converters = $source->data()->get('converters'); if (!$converters || !is_array($converters)) { $converters = array('null'); } foreach ($converters as $converter) { $this->eventDispatcher->dispatch(Sculpin::EVENT_BEFORE_CONVERT, new ConvertEvent($source, $converter, $this->formatterManager->defaultFormatter())); $this->converter($converter)->convert(new SourceConverterContext($source)); $this->eventDispatcher->dispatch(Sculpin::EVENT_AFTER_CONVERT, new ConvertEvent($source, $converter, $this->formatterManager->defaultFormatter())); } }
public function match(SourceInterface $source) { if ($source->data()->get('draft')) { if (!$this->publishDrafts) { // If we are not configured to publish drafts we should // inform the source that it should be skipped. This // will ensure that it won't be touched by any other // part of the system for this run. $source->setShouldBeSkipped(); return false; } } return true; }
public function process(SourceInterface $source) { if ($source->data()->get('draft')) { $tags = $source->data()->get('tags'); if (null === $tags) { $tags = array('drafts'); } else { if (!is_array($tags)) { $tags = array(); if ($tags) { $tags = array($tags); } } if (!in_array('drafts', $tags)) { // only add drafts if it isn't already in tags. $tags[] = 'drafts'; } } $source->data()->set('tags', $tags); } }
/** * Generate * * @param SourceInterface $source Source * @param SourceSet $sourceSet Source set * @throws \InvalidArgumentException * * @return string */ public function generate(SourceInterface $source, SourceSet $sourceSet) { $data = $source->data(); $generators = array(); $isGenerator = $source->isGenerator(); if ($generatorNames = $data->get('generator')) { if (!$isGenerator) { $source->setIsGenerator(); } $generatorNames = (array) $generatorNames; foreach ($generatorNames as $generatorName) { if (!isset($this->generators[$generatorName])) { throw new \InvalidArgumentException("Requested generator '{$generatorName}' could not be found; was it registered?"); } $generators[] = $this->generators[$generatorName]; } } else { if ($isGenerator) { $source->setIsNotGenerator(); } return; } $targetSources = array($source); foreach ($generators as $generator) { $newTargetSources = array(); foreach ($targetSources as $targetSource) { foreach ((array) $generator->generate($targetSource) as $generatedSource) { $generatedSource->setIsGenerated(); $newTargetSources[] = $generatedSource; } } $targetSources = $newTargetSources; } foreach ($targetSources as $generatedSource) { $sourceSet->mergeSource($generatedSource); } }
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); } } } }
/** * {@inheritdoc} */ public function duplicate($newSourceId) { return $this->source->duplicate($newSourceId); }
/** * Test if Source is formatted by requested formatter * * @param string $requestedFormatter * * @return boolean */ public function isFormattedBy($requestedFormatter) { return $requestedFormatter == $this->source->data()->get('formatter') ?: $this->defaultFormatter; }
/** * {@inheritdoc} */ public function setContent($content) { $this->source->setContent($content); }
/** * {@inheritdoc} */ public function formattedContent() { return $this->source->useFileReference() ? null : $this->source->formattedContent(); }
/** * {@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; }
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; } }
/** * {@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; }
/** * Format blocks for a Source * * @param SourceInterface $source Source * * @return array */ public function formatSourceBlocks(SourceInterface $source) { return $this->formatBlocks($source->sourceId(), $source->content(), $source->data()->export()); }
public function match(SourceInterface $source) { return $source->data()->get($this->key) === $this->value; }