From ca22c7afd881564255cdb69d4267931950b8ae13 Mon Sep 17 00:00:00 2001 From: Anton Vakhrushev Date: Wed, 2 Jan 2019 17:00:35 +0300 Subject: [PATCH] =?UTF-8?q?=D0=92=D0=BE=D0=B7=D0=B2=D1=80=D0=B0=D1=89?= =?UTF-8?q?=D0=B5=D0=BD=20sitemap?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/SculpinKernel.php | 4 +- bundle/HtmlPrettierBundle/HtmlPrettier.php | 8 +- .../DependencyInjection/SiteMapExtension.php | 20 ++++ .../Resources/config/services.xml | 13 +++ bundle/SiteMapBundle/SiteMapBundle.php | 9 ++ bundle/SiteMapBundle/SiteMapGenerator.php | 98 +++++++++++++++++++ source/sitemap.xml | 2 +- 7 files changed, 150 insertions(+), 4 deletions(-) create mode 100644 bundle/SiteMapBundle/DependencyInjection/SiteMapExtension.php create mode 100644 bundle/SiteMapBundle/Resources/config/services.xml create mode 100644 bundle/SiteMapBundle/SiteMapBundle.php create mode 100644 bundle/SiteMapBundle/SiteMapGenerator.php diff --git a/app/SculpinKernel.php b/app/SculpinKernel.php index 6196bef..4755b50 100644 --- a/app/SculpinKernel.php +++ b/app/SculpinKernel.php @@ -1,7 +1,7 @@ allSources() as $source) { $filename = $source->filename(); - if ($this->endsWith($filename, '.md') || $this->endsWith($filename, '.html.twig')) { + + $isSuitable = $filename === 'sitemap.xml' + || $this->endsWith($filename, '.md') + || $this->endsWith($filename, '.html.twig') + ; + + if ($isSuitable) { yield $source; } } diff --git a/bundle/SiteMapBundle/DependencyInjection/SiteMapExtension.php b/bundle/SiteMapBundle/DependencyInjection/SiteMapExtension.php new file mode 100644 index 0000000..cb7eb72 --- /dev/null +++ b/bundle/SiteMapBundle/DependencyInjection/SiteMapExtension.php @@ -0,0 +1,20 @@ +load('services.xml'); + } +} diff --git a/bundle/SiteMapBundle/Resources/config/services.xml b/bundle/SiteMapBundle/Resources/config/services.xml new file mode 100644 index 0000000..3118598 --- /dev/null +++ b/bundle/SiteMapBundle/Resources/config/services.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + diff --git a/bundle/SiteMapBundle/SiteMapBundle.php b/bundle/SiteMapBundle/SiteMapBundle.php new file mode 100644 index 0000000..01186c9 --- /dev/null +++ b/bundle/SiteMapBundle/SiteMapBundle.php @@ -0,0 +1,9 @@ + 'saveSourceSet', + ]; + } + + /** + * Before run. + * + * @param SourceSetEvent $sourceSetEvent Source Set Event + */ + public function saveSourceSet(SourceSetEvent $sourceSetEvent) + { + $this->sources = $sourceSetEvent->sourceSet(); + } + + protected function buildSiteMap() + { + if (!empty($this->siteMap)) { + return $this->siteMap; + } + + $siteMap = []; + + /** @var \Sculpin\Core\Source\FileSource $source */ + foreach ($this->sources->allSources() as $source) { + $data = $source->data()->export(); + + if (empty($data) || $source->useFileReference()) { + continue; + } + + $siteMapData = $data['sitemap'] ?? []; + + if (isset($siteMapData['_exclude'])) { + continue; + } + + $loc = $data['canonical'] ?? $data['url']; + + if (is_callable([$source, 'file'])) { + $lastmod = date(DATE_W3C, $source->file()->getMTime()); + } else { + $lastmod = date(DATE_W3C); + } + + $url = [ + 'loc' => $loc, + 'lastmod' => $lastmod, + ]; + + if (isset($data['sitemap'])) { + $url = array_merge($url, $data['sitemap']); + } + + $siteMap[$url['loc']] = $url; + } + + $this->siteMap = $siteMap; + + return $this->siteMap; + } + + /** + * Provide data. + * + * @return array + */ + public function provideData(): array + { + $this->buildSiteMap(); + + return $this->siteMap; + } +} diff --git a/source/sitemap.xml b/source/sitemap.xml index e0c3512..b587919 100644 --- a/source/sitemap.xml +++ b/source/sitemap.xml @@ -11,7 +11,7 @@ sitemap: {# Last slash for pretty url #} - {{ site.url }}{{ url.loc != '/.' ? (url.loc ~ '/') : '' }} + {{ site.url }}{{ url.loc != '/.' ? (url.loc|trim('/', side='right') ~ '/') : '' }} {{ url.lastmod }}