41 lines
1.1 KiB
PHP
41 lines
1.1 KiB
PHP
<?php
|
|
|
|
function createCategories($textsPage) {
|
|
$categories = array();
|
|
foreach ($textsPage->categories()->split() as $category) {
|
|
$categories[$category] = array(
|
|
'title' => $category,
|
|
'texts' => array()
|
|
);
|
|
}
|
|
return $categories;
|
|
}
|
|
|
|
function fillCategoriesWithTexts($emptyCategories, $texts) {
|
|
$filledCategories = $emptyCategories;
|
|
foreach ($texts as $text) {
|
|
try {
|
|
$textCategory = $text->category()->value();
|
|
$filledCategories[$textCategory]['texts'][] = $text;
|
|
} catch (\Throwable $th) {
|
|
throw new Exception(json_encode($th->getFile() . ' : ' . $th->getMessage()));
|
|
}
|
|
}
|
|
|
|
// exclude empty categories
|
|
return array_filter($filledCategories, function($category) {
|
|
return count($category['texts']) > 0;
|
|
});
|
|
}
|
|
|
|
return function ($site) {
|
|
$textsPage = $site->find('texts');
|
|
$years = $textsPage->children();
|
|
$texts = $years->children()->index();
|
|
|
|
$emptyCategories = createCategories($textsPage);
|
|
|
|
$filledCategories = fillCategoriesWithTexts($emptyCategories, $texts);
|
|
|
|
return $filledCategories;
|
|
};
|