diff --git a/CHANGELOG.md b/CHANGELOG.md index d135a7e..1b14cc9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ +# v2.1.0 +## 22-09-2020 + +1. [](#bugfix) + - Do not end copyMedia() prematurely + - Avoid double-slash prefix + # v2.0.0 ## 14-03-2020 diff --git a/README.md b/README.md index 0be6419..e00253e 100644 --- a/README.md +++ b/README.md @@ -83,12 +83,68 @@ If you're using the Admin plugin, an icon will be available in the quick navigat It will be wrapped for use in JavaScript, like `const GravDataIndex = [...];`. This makes it apt for use with search engines, like [FlexSearch](https://github.com/nextapps-de/flexsearch/). You can include the resulting `.js`-file and use `GravDataIndex` for searching Pages. If viewing a specific Page in Admin, for example at `http://localhost:8000/en/admin/pages/blog`, it will index the descendants of this Page in a specific-file named `blog.full.js`. -The same can be achieved through the command-line, with the command `php bin/plugin static-generator index`. See `php bin/plugin static-generator help index` for options, a normal `php bin/plugin static-generator index "/" -c` will index all Pages including content. +The same can be achieved through the command-line, with the command `php bin/plugin static-generator index`. **See `php bin/plugin static-generator help index` for options**, a normal `php bin/plugin static-generator index "/" -c` will index all Pages including content. + +#### Usage + +```bash +php bin/plugin static-generator index [options] [--] [] +``` + +#### Arguments + +```bash +route The route to the page +target Override target-option or set a custom destination +``` + +#### Available options + +```bash +-b, --basename[=BASENAME] Index basename [default: "index"] +-c, --content Include Page content +-e, --echo Outputs result directly +-w, --wrap Wraps JSON as a JavaScript global +-f, --force Forcefully save data +-h, --help Display this help message +-q, --quiet Do not output any message +-V, --version Display this application version + --ansi Force ANSI output + --no-ansi Disable ANSI output +-n, --no-interaction Do not ask any interactive question +-v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug +``` ### Static Generation If you want to generate static versions of the Page(s), use the `php bin/plugin static-generator page`-command. This will create a `index.html`-file for each Page, located in your preset `content`-location, like `/user/data/persist/static`. The folder-structure of `/user/pages` will remain intact, and assets output alongside, for example to `/user/data/persist/static/assets`. Asset-paths will be rewritten, also for media which will remain alongside each Page's `index.html`-file. +#### Usage + +```bash +php bin/plugin static-generator page [options] [--] [ [ []]] +``` + +#### Available options + +```bash +route The route to the page +collection The Page Collection to store (see https://learn.getgrav.org/16/content/collections#collection-headers) +target Override target-option or set a custom destination +``` + +#### Available options + +```bash +-p, --preset[=PRESET] Name of Config preset +-a, --assets Include Assets +-r, --root-prefix=ROOT-PREFIX Root prefix for assets and images +-s, --static-assets Include Static Assets +-i, --images Include Images +-o, --offline Force offline-mode +-f, --filter[=FILTER] Methods for filtering (multiple values allowed) +``` + For example, `php bin/plugin static-generator page "@page.descendants" "/blog"` results in: ``` diff --git a/blueprints.yaml b/blueprints.yaml index 6e4943a..6390fcf 100644 --- a/blueprints.yaml +++ b/blueprints.yaml @@ -1,5 +1,5 @@ name: Static Generator -version: 2.0.0 +version: 2.1.0 description: Static generation of Page(s) and Index. icon: bolt author: diff --git a/classes/Assets.php b/classes/Assets.php index 617711c..9ba5716 100644 --- a/classes/Assets.php +++ b/classes/Assets.php @@ -100,7 +100,7 @@ public function copy( return [ 'item' => basename($asset), 'location' => $target, - 'time' => Timer::format($this->Timer->getTime()) + 'time' => Timer::format($this->Timer->getTime()), ]; } catch (\Exception $e) { throw new \Exception($e); @@ -108,34 +108,33 @@ public function copy( } /** - * Copy Page Media + * Copy Media * - * @param array $media List of media to copy. + * @param string $filename Name of file to copy. + * @param string $path Path of file to copy. * @param string $location Location to storage media in. * @param boolean $force Forcefully save data. * * @return array Result */ - public function copyMedia(array $media, string $location, bool $force): array + public function copyMedia(string $filename, string $path, string $location, bool $force): array { - if (empty($media)) { + if (empty($filename) || empty($path)) { return []; } $location = rtrim($location, '//') . DS; - foreach ($media as $filename => $data) { - try { - if ($force) { - $this->Filesystem->remove($location . $filename); - } - $this->Filesystem->copy($data->path(), $location . $filename); - return [ - 'item' => $filename, - 'location' => $location . $filename, - 'time' => Timer::format($this->Timer->getTime()) - ]; - } catch (\Exception $e) { - throw new \Exception($e); + try { + if ($force) { + $this->Filesystem->remove($location . $filename); } + $this->Filesystem->copy($path . DS . $filename, $location . $filename); + return [ + 'item' => $filename, + 'location' => $location . $filename, + 'time' => Timer::format($this->Timer->getTime()), + ]; + } catch (\Exception $e) { + throw new \Exception($e); } } } diff --git a/classes/Collection/AbstractCollection.php b/classes/Collection/AbstractCollection.php index 3d19e35..0430812 100644 --- a/classes/Collection/AbstractCollection.php +++ b/classes/Collection/AbstractCollection.php @@ -207,7 +207,7 @@ public function mirrorStaticAssets( array $extensions = ['ttf', 'eot', 'otf', 'woff', 'woff2'] ): void { foreach ($folders as $folder) { - $iterator = new \RecursiveIteratorIterator( + $iterator = new \RecursiveIteratorIterator( new \RecursiveDirectoryIterator(GRAV_ROOT . '/user' . $folder) ); $fileIterator = new \RegexIterator($iterator, '/\.' . implode('|', $extensions) . '$/imu'); @@ -219,7 +219,7 @@ public function mirrorStaticAssets( [ 'override' => true, 'copy_on_windows' => true, - 'delete' => false + 'delete' => false, ] ); } catch (\Exception $e) { @@ -245,7 +245,7 @@ public function mirrorImages(bool $force): void [ 'override' => true, 'copy_on_windows' => true, - 'delete' => false + 'delete' => false, ] ); } catch (\Exception $e) { @@ -270,7 +270,7 @@ public function store(Page $Page): void [ 'item' => $Page->title() . ' (' . $template . ')', 'location' => $message, - 'time' => Timer::format($this->Timer->getTime()) + 'time' => Timer::format($this->Timer->getTime()), ], 'red' ); @@ -302,6 +302,7 @@ public function store(Page $Page): void '/images/', $this->rootPrefix . 'images/' ); + $content = Source::rewritePath($content, '/' . $route, $route); } catch (\Exception $e) { throw new \Exception($e); } @@ -315,17 +316,20 @@ public function store(Page $Page): void [ 'item' => $Page->title() . ' (' . $template . ')', 'location' => $this->location . $route . '/' . $file, - 'time' => Timer::format($this->Timer->getTime()) + 'time' => Timer::format($this->Timer->getTime()), ] ); - $this->reporter( - $this->Assets->copyMedia( - $Page->media()->all(), - $this->location . $Page->route(), - $this->force - ), - 'yellow' - ); + foreach ($Page->media()->all() as $filename => $data) { + $this->reporter( + $this->Assets->copyMedia( + $filename, + $data->path, + $this->location . $Page->route(), + $this->force + ), + 'yellow' + ); + } } catch (\Exception $e) { throw new \Exception($e); }