Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move html generation to onPagedRendered. #5

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 21 additions & 16 deletions PicoFotofolder.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ class PicoFotofolder extends AbstractPicoPlugin {
*/
public function onContentLoaded(&$rawContent) {
$rawContent = preg_replace_callback( '/\(%\s+' . $this->p_keyword .'\s*\(\s*(.*?)\s*\)\s+%\)/', function($match) {

// returning the input again unless an error happens.
$out = $match[0];
if ($match[1]) {

//check for GD library see #1
Expand All @@ -38,27 +39,19 @@ public function onContentLoaded(&$rawContent) {
list ($this->image_src['path'],
$this->image_src['sort'],
$this->image_src['order']) = explode(',', str_replace('"', '', $match[1]));

$this->image_src['path'] = trim($this->image_src['path']);
$this->image_src['sort'] = trim($this->image_src['sort']);
$this->image_src['order'] = trim($this->image_src['order']);
if ($this->image_src['sort'] == "") $this->image_src['sort'] = 'name';
if ($this->image_src['order'] == "") $this->image_src['order'] = 'dsc';

// handle image path if %assets_url% is used see #1
// handle image path if %assets_url% is used see #1
$this->image_src['path'] = preg_replace('/%assets_url%/', rtrim($this->getConfig('assets_url'), "/"), $this->image_src['path']);
$repl = '/http[s]?:\/\/' . $_SERVER['SERVER_NAME'] . '/';
$this->image_src['path'] = preg_replace($repl, '', $this->image_src['path']);

$img_metas = $this->readMetaArray();

if (count($img_metas) > 0) {
$out = $this->createOutput($img_metas);
$this->p_count++;
}
else {
$out = "no media found in: {$this->image_src['path']}";
}
$img_metas = $this->readMetaArray();
}
}
return $out;
Expand All @@ -69,9 +62,12 @@ public function onContentLoaded(&$rawContent) {
/**
* Triggered after Pico has rendered the page
*/
public function onPageRendered(&$output ) {
// add required elements in head tag
public function onPageRendered(&$output) {
$img_metas = $this->readMetaArray();
$this->p_count = count($img_metas);
if ($this->p_count > 0) {

// add required elements in head tag
$jsh = ' <!-- Fotofolder Elements -->' . "\n";
$jsh .= ' <link href="' . $this->getConfig('plugins_url') . 'PicoFotofolder/assets/css/fotofolder.css" rel="stylesheet">' . "\n";
$jsh .= ' <script src="' . $this->getConfig('plugins_url') . 'PicoFotofolder/vendor/lazyload/dist/lazyload.min.js"></script>' . "\n";
Expand All @@ -80,6 +76,9 @@ public function onPageRendered(&$output ) {
$jsh .= '</head>' . "\n" . '<body>' . "\n";
$output = preg_replace('/\\<\\/head\\>\s*\n\s*\\<body\\>/', $jsh, $output, 1);

// add photofolder to page
$output = preg_replace('/\(%\s+' . $this->p_keyword .'.*?\s%\)/', $this->createOutput($img_metas), $output, 1);

// Add LazyLoad
$jsh = '<script>' . "\n";
$jsh .= ' var lazyLoadInstance = new LazyLoad({ ' . "\n";
Expand All @@ -89,6 +88,8 @@ public function onPageRendered(&$output ) {
$jsh .= '</script>' . "\n";
$jsh .= '</body>' . "\n" . '</html>' . "\n";
$output = preg_replace('/\\<\\/body\\>\s*\n\s*\\<\/html\\>/', $jsh, $output, 1);
} else {
$output = preg_replace('/\(%\s+' . $this->p_keyword .'.*?\s%\)/', "no media found in: {$this->image_src['path']}", $output, 1);
}
}

Expand All @@ -104,7 +105,11 @@ private function readMetaArray() {
$img_metas = array();
$pattern = '{,.}*.{[jJ][pP][gG],[jJ][pP][eE][gG],[pP][nN][gG],[gG][iI][fF],dat}';
$filelist = glob($dir . '/' . $pattern, GLOB_BRACE);
usort($filelist, create_function('$a,$b', 'return filemtime($b) - filemtime($a);'));

$timeCompareFunction = function($a, $b) {
return filemtime($b) - filemtime($a);
};
usort($filelist, $timeCompareFunction);

//check if metafile is still up to date or if we have to create a new one
if (strpos($filelist[0], '.fotofolder.dat') == true) {
Expand Down Expand Up @@ -162,7 +167,7 @@ private function readMetaArray() {
/***************************************************************/
private function createOutput($img_metas) {

if ( $image_src['order'] == 'asc') {
if ( $this->image_src['order'] == 'asc') {
usort($img_metas, function($a, $b) {
return $a[$this->image_src['sort']] <=> $b[$this->image_src['sort']];
});
Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ or, if you installed Pico CMS with composer

Add the following expression in your Markdown file:

(% fotofolder (/path/to/your/images [sort] [order]) %)
(% fotofolder (/path/to/your/images, [sort], [order]) %)

E.g.
(% fotofolder (/assets/gallery, date, dsc) %)

Optional arguments:
- `[sort]` Can be 'date or 'name'. This will sort the images according date, which means the exif image date not the file date, or according the filename. Default is 'name'.
Expand Down