Skip to content

Commit

Permalink
copy local images mentioned in pages to output folder, this closes #1
Browse files Browse the repository at this point in the history
  • Loading branch information
konecnyjakub committed Oct 5, 2017
1 parent 206165c commit 7ff9ca0
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 2 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG
Expand Up @@ -3,8 +3,9 @@ Version 0.2.0+dev
- added dependency on symfony/options-resolver
- it is now possible to include stylesheets and scripts in generated pages
- Generator no longer tries to guess source and output folders
- added events onBeforeGenerate and onAfterGenerate to Generator
- added events onBeforeGenerate, onAfterGenerate and onCreatePage to Generator
- introduced meta normalizers for Generator
- local image mentioned in pages are now copied to output folder

Version 0.2.0
- raised minimal version of PHP to 7
Expand Down
2 changes: 2 additions & 0 deletions docs/index.md
Expand Up @@ -62,3 +62,5 @@ styles:
scripts:
- script.js
```

If you mention a local image in your page, the file will be copied to output folder.
21 changes: 21 additions & 0 deletions src/Generator.php
Expand Up @@ -17,6 +17,7 @@
* @property string $source
* @property string $output
* @method void onBeforeGenerate()
* @method void onCreatePage(string $html, Generator $generator, string $filename)
* @method void onAfterGenerate()
*/
class Generator {
Expand All @@ -35,6 +36,8 @@ class Generator {
/** @var callable[] */
public $onBeforeGenerate = [];
/** @var callable[] */
public $onCreatePage = [];
/** @var callable[] */
public $onAfterGenerate = [];

public function __construct(string $source, string $output) {
Expand All @@ -43,6 +46,7 @@ public function __construct(string $source, string $output) {
$this->setOutput($output);
$this->templateFile = __DIR__ . "/template.html";
$this->onBeforeGenerate[] = [$this, "clearOutputFolder"];
$this->onCreatePage[] = [$this, "processImages"];
$this->onAfterGenerate[] = [$this, "copyAssets"];
$this->addMetaNormalizer([$this, "normalizeTitle"]);
$this->addMetaNormalizer([$this, "normalizeStyles"]);
Expand Down Expand Up @@ -185,6 +189,22 @@ public function copyAssets(): void {
}
}

/**
* @internal
*/
public function processImages(string $html, self $generator, string $filename): void {
$dom = new \DOMDocument();
$dom->loadHTML($html);
$images = $dom->getElementsByTagName("img");
/** @var \DOMElement $image */
foreach($images as $image) {
$path = dirname($filename) . "/" . $image->getAttribute("src");
if(file_exists($path)) {
$generator->addAsset($path);
}
}
}

/**
* Generate the site
*/
Expand All @@ -206,6 +226,7 @@ public function generate(): void {
$filename = "$this->output$path/$basename";
FileSystem::write($filename, $html);
echo "Created $path/$basename\n";
$this->onCreatePage($html, $this, $file->getRealPath());
}
$this->onAfterGenerate();
}
Expand Down
2 changes: 2 additions & 0 deletions tests/Nexendrie/SiteGenerator/GeneratorTest.phpt
Expand Up @@ -88,6 +88,8 @@ class GeneratorTest extends \Tester\TestCase {
}
Assert::true(file_exists("{$this->generator->output}/style.css"));
Assert::true(file_exists("{$this->generator->output}/script.js"));
Assert::true(file_exists("{$this->generator->output}/blank.jpg"));
Assert::false(file_exists("{$this->generator->output}/nonexisting.png"));
}
}

Expand Down
2 changes: 2 additions & 0 deletions tests/Nexendrie/SiteGenerator/pageAssets.html
Expand Up @@ -9,5 +9,7 @@
</head>
<body>
<h1>Assets</h1>
<p><img src="blank.jpg" alt="blank"><br>
<img src="nonexisting.png" alt="non existing"></p>
</body>
</html>
5 changes: 4 additions & 1 deletion tests/sources/assets.md
@@ -1,2 +1,5 @@
Assets
======
======

![blank](blank.jpg)
![non existing](nonexisting.png)
Empty file added tests/sources/blank.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 7ff9ca0

Please sign in to comment.