Skip to content

Commit

Permalink
Page media referencing
Browse files Browse the repository at this point in the history
  • Loading branch information
OleVik committed Sep 21, 2020
1 parent 2b67abc commit 3bd1a82
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 33 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
58 changes: 57 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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] [--] <route> [<target>]
```

#### 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] [--] [<route> [<collection> [<target>]]]
```

#### 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:

```
Expand Down
2 changes: 1 addition & 1 deletion blueprints.yaml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
35 changes: 17 additions & 18 deletions classes/Assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,42 +100,41 @@ 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);
}
}

/**
* 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);
}
}
}
30 changes: 17 additions & 13 deletions classes/Collection/AbstractCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -219,7 +219,7 @@ public function mirrorStaticAssets(
[
'override' => true,
'copy_on_windows' => true,
'delete' => false
'delete' => false,
]
);
} catch (\Exception $e) {
Expand All @@ -245,7 +245,7 @@ public function mirrorImages(bool $force): void
[
'override' => true,
'copy_on_windows' => true,
'delete' => false
'delete' => false,
]
);
} catch (\Exception $e) {
Expand All @@ -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'
);
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand Down

0 comments on commit 3bd1a82

Please sign in to comment.