/** * {@inheritDoc} */ public function build(AbstractSource $source) { $sourceId = $source->sourceId(); $tags = is_array($source->data()->get('tags')) ? $source->data()->get('tags') : array(); $categories = is_array($source->data()->get('categories')) ? $source->data()->get('categories') : null; $record = array('objectID' => md5($sourceId), 'title' => $source->data()->get('title'), 'content' => strip_tags($source->content()), 'link' => $source->permalink()->relativeUrlPath(), 'date' => $source->data()->get('calculated_date')); // add em if we have em if ($categories != NULL) { $record["categories"] = $categories; } if ($tags != NULL) { $record["tags"] = $tags; } return $record; }
/** * @param AbstractSource $source * @return array */ private function parseSource(AbstractSource $source) { $document = array('objectID' => sha1($source->sourceId()), 'title' => $source->data()->get('title'), 'body' => strip_tags($source->content()), 'url' => rtrim($source->permalink()->relativeUrlPath(), '/') . '/', 'date' => $source->data()->get('calculated_date')); $tags = is_array($source->data()->get('tags')) ? $source->data()->get('tags') : array(); if ($tags) { $document['tags'] = $tags; } return $document; }
/** * @param AbstractSource $source * @return bool */ private function shouldBeConverted(AbstractSource $source) { // File based whitelist has precedence if (!empty($this->files)) { foreach ($this->files as $fileName) { if ($source->relativePathname() === $fileName) { return true; } } return false; } foreach ($this->extensions as $extension) { if (fnmatch("*.{$extension}", $source->filename())) { return true; } } return false; }
/** * Initialize source * * @param bool $hasChanged Has the file changed? */ protected function init($hasChanged = null) { parent::init($hasChanged); $originalData = $this->data; if ($this->isRaw) { $this->useFileReference = true; $this->data = new Data(); } else { $internetMediaType = $this->analyzer->detectFromFilename($this->file); if ($internetMediaType && ('text' === $internetMediaType->getType() || $this->applicationXmlType->equals($internetMediaType))) { // Only text files can be processed by Sculpin and since we // have to read them here we are going to ensure that we use // the content we read here instead of having someone else // read the file again later. $this->useFileReference = false; // Additionally, any text file is a candidate for formatting. $this->canBeFormatted = true; $content = file_get_contents($this->file); if (preg_match('/^\\s*(?:---[\\s]*[\\r\\n]+)(.*?)(?:---[\\s]*[\\r\\n]+)(.*?)$/s', $content, $matches)) { $this->content = $matches[2]; if (preg_match('/^(\\s*[-]+\\s*|\\s*)$/', $matches[1])) { // There is nothing useful in the YAML front matter. $this->data = new Data(); } else { // There may be YAML frontmatter try { $builder = new YamlDataBuilder($matches[1]); $this->data = $builder->build(); } catch (\InvalidArgumentException $e) { // Likely not actually YAML front matter available, // treat the entire file as pure content. echo ' ! ' . $this->sourceId() . ' ' . $e->getMessage() . ' !' . PHP_EOL; $this->content = $content; $this->data = new Data(); } } } else { $this->content = $content; $this->data = new Data(); $this->canBeFormatted = false; } } else { $this->useFileReference = true; $this->data = new Data(); } } if ($this->data->get('date')) { if (!is_numeric($this->data->get('date'))) { $this->data->set('date', strtotime($this->data->get('date'))); } $this->data->set('calculated_date', $this->data->get('date')); } if ($originalData) { $this->data->import($originalData, false); } }
/** * @param AbstractSource $source * @return array */ private function parseSource(AbstractSource $source) { $tags = is_array($source->data()->get('tags')) ? $source->data()->get('tags') : array(); $document = array('title' => $source->data()->get('title'), 'body' => strip_tags($source->content()), 'tags' => implode(', ', $tags), 'url' => rtrim($source->permalink()->relativeUrlPath(), '/') . '/'); return $document; }