Move galleries to collection, generate map and slideshow on build, stop using loopdir plugin, update templates accordingly

This commit is contained in:
2016-01-22 19:27:34 +00:00
parent 0d0edee3a7
commit ad219ff8cc
18 changed files with 236 additions and 213 deletions

View File

@@ -0,0 +1,96 @@
<?php
require __DIR__ . '/../_php/autoload.php';
use Symfony\Component\Yaml\Yaml;
$sOriginalDir = '_gallery.orig';
$sCollectionDir = '_gallery';
$sGalleryDir = 'gallery';
if (!is_dir($sCollectionDir)) {
mkdir($sCollectionDir);
}
if (!is_dir($sGalleryDir)) {
mkdir($sGalleryDir);
}
foreach (glob($sOriginalDir . '/*/index.html') as $sFile) {
$sBasename = basename($sFile, '.md');
$sGallery = basename(dirname($sFile));
printf("%s..\n", $sGallery);
$sDir = sprintf('%s/%s', $sOriginalDir, $sGallery);
$sMapFile = sprintf('%s/map.html', $sDir);
parseFile($sFile, $aYaml, $sContents);
if (strpos($sContents, 'gallery_list') !== false) {
$sContents = null;
}
$aYaml['date'] = date('Y-m-d', $aYaml['date']);
if (strpos($aYaml['title'], ' ') !== false) {
$aYaml['title'] = sprintf('"%s"', $aYaml['title']);
}
if (isset($aYaml['end_date'])) {
$aYaml['end_date'] = date('Y-m-d', $aYaml['end_date']);
}
unset($aYaml['highlight_photo']);
unset($aYaml['layout']);
unset($aYaml['map']);
if (file_exists($sMapFile)) {
parseFile($sMapFile, $aMapYaml, $sContents);
if (isset($aMapYaml['gallery_map'])) {
$aYaml['map'] = $aMapYaml['gallery_map'];
}
}
$sCopyDir = sprintf('%s/%s', $sGalleryDir, $sGallery);
if (!file_exists($sCopyDir)) {
mkdir($sCopyDir);
}
$aYaml['photos'] = [];
$aPhotos = [];
foreach (glob(sprintf('%s/*.md', $sDir)) as $sFile) {
$sName = basename($sFile, '.md');
$aYaml['photos'][] = $sName;
$sTargetFile = sprintf('%s/%s.md', $sCopyDir, $sName);
parseFile($sFile, $aPhotoYaml, $sPhotoContents);
// unset($aPhotoYaml['name']);
if (isset($aPhotoYaml['exif'])) {
$aPhotoYaml = array_merge($aPhotoYaml, $aPhotoYaml['exif']);
unset($aPhotoYaml['exif']);
}
if (isset($aPhotoYaml['album'])) {
$aPhotoYaml['gallery'] = $aPhotoYaml['album'];
unset($aPhotoYaml['album']);
}
if (isset($aPhotoYaml['date'])) {
$aPhotoYaml['date'] = date('Y-m-d H:i:s', $aPhotoYaml['date']);
$aPhotos[$aPhotoYaml['date']] = $sName;
}
writeFile($sTargetFile, $aPhotoYaml, $sPhotoContents);
}
ksort($aPhotos);
// $aYaml['photos'] = array_values($aPhotos);
unset($aYaml['photos']);
$aYaml['highlight'] = current($aPhotos);
$sIndexFile = sprintf('%s/%s.md', $sCollectionDir, $sGallery);
// writeFile($sIndexFile, $aYaml, $sContents);
}
function parseFile($sFile, &$aYaml, &$sContents = null) {
$sContents = file_get_contents($sFile);
$aContents = explode("\n", $sContents);
$aParts = explode('---', implode("\n", array_slice($aContents, 1)));
$aYaml = Yaml::parse(trim(str_replace(" - \n", null, current($aParts))));
$sContents = trim(next($aParts));
}
function writeFile($sFile, $aYaml, $sContents) {
file_put_contents($sFile, '---' . "\n" . yamlDump($aYaml) . '---' . "\n" . $sContents);
}
function yamlDump($aData) {
return str_replace("'", null, Yaml::dump($aData, 4, 2));
}

View File

@@ -244,7 +244,7 @@ $oConsole
$oEndDate = $i > 0 ? max($oEndDate, $aPhoto['date']) : $aPhoto['date'];
if (isset($aPhoto['exif']['Make'])) {
$aMatter['exif']['make'] = $aPhoto['exif']['Make'];
$aMatter['make'] = $aPhoto['exif']['Make'];
if (isset($aPhoto['exif']['Model'])) {
$aMatter['model'] = $aPhoto['exif']['Model'];
}
@@ -294,42 +294,7 @@ $oConsole
);
$oOutput->writeln(' done');
}
// Write slideshow
$bSlideshow = isset($aGallery['slideshow']) ? $aGallery['slideshow'] : true;
if ($bSlideshow) {
$oOutput->write('<comment>slideshow</comment>');
$aMatter = [
'layout' => 'gallery-slideshow',
'title' => empty($aGallery['title']) ? '' : $aGallery['title']
];
file_put_contents(
$sRenderPath . '/slideshow.html',
'---' . "\n" . yamlDump($aMatter) . '---' . "\n"
);
$oOutput->writeln(' done');
}
// Write map
$bMap = count($aLongitude) > 0 && (isset($aGallery['map']) ? $aGallery['map'] : true);
if ($bMap) {
$oOutput->write('<comment>map</comment>');
$aMatter = [
'layout' => 'gallery-map',
'title' => empty($aGallery['title']) ? '' : $aGallery['title'],
'gallery_map' => [
'latitude' => array_sum($aLatitude) / count($aLatitude),
'longitude' => array_sum($aLongitude) / count($aLongitude),
'zoom' => 8
]
];
file_put_contents(
$sRenderPath . '/map.html',
'---' . "\n" . yamlDump($aMatter) . '---' . "\n"
);
$oOutput->writeln(' done');
}
// Write gallery index
$oOutput->write('<comment>index</comment>');
$aMatter = [
@@ -338,9 +303,11 @@ $oConsole
'highlight_photo' => empty($sHighlight) ? $aPhotos[0]['id'] : $sHighlight,
'date' => $oDate->format('Y-m-d')
];
$bSlideshow = isset($aGallery['slideshow']) ? $aGallery['slideshow'] : true;
if (!$bSlideshow) {
$aMatter['slideshow'] = false;
}
$bMap = count($aLongitude) > 0 && (isset($aGallery['map']) ? $aGallery['map'] : true);
if (!$bMap) {
$aMatter['map'] = false;
}
@@ -359,8 +326,7 @@ $oConsole
$sRenderPath . '/index.html',
'---' . "\n" . yamlDump($aMatter) . '---' . "\n" . $sContents
);
$oOutput->writeln(' done');
$oOutput->writeln(' done');
}
);