Skip to content

Commit

Permalink
feat(BuildCommand.php): add lifecycle hooks for prehtml and prepdf
Browse files Browse the repository at this point in the history
Adds optional user defined functions to ibis.php that allows users to modify content

Fixes #13
  • Loading branch information
bmcminn committed Nov 15, 2020
1 parent 6263256 commit a04cdf9
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 19 deletions.
32 changes: 28 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<p align="center">
<img src="https://raw.githubusercontent.com/themsaid/ibis/master/art/cover.png" alt="Ibis logo" width="480">

Artwork by <a href="https://twitter.com/ericlbarnes">Eric L. Barnes</a> and <a href="https://twitter.com/Caneco">Caneco</a> from <a href="https://laravel-news.com/ibis-book-maker">Laravel News</a> ❤️.
</p>

---

This PHP tool helps you write eBooks in markdown. Run `ibis build` and an eBook will be generated with:

1. A cover photo.
2. Clickable auto-generated table of contents.
3. Code syntax highlighting.
Expand Down Expand Up @@ -45,7 +45,7 @@ You may configure your book by editing the `/ibis.php` configuration file.

## Writing Your eBook

The `init` command will create sample .md files inside the content folder. You can explore those files to see how you can write your book. This sample content is taken from [Laravel Queues in Action](https://learn-laravel-queues.com).
The `init` command will create sample .md files inside the content folder. You can explore those files to see how you can write your book. This sample content is taken from [Laravel Queues in Action](https://learn-laravel-queues.com).

Inside the content directory, you can write multiple `.md` files. Ibis uses the headings to divide the book into parts and chapters:

Expand All @@ -61,7 +61,7 @@ Inside the content directory, you can write multiple `.md` files. Ibis uses the
### Starting with Ibis
<h3> tags define different titles inside a chapter.
```
```

## Using Fonts

Expand Down Expand Up @@ -91,6 +91,30 @@ ibis sample dark

This command will use the generated files from the `ibis build` command to generate samples from your PDF eBook. You can configure which pages to include in the sample by updating the `/ibis.php` file.


## Extending Ibis

### Build lifecycle hooks

You can customize your Ibis build process by defining lifecycle hook function(s) in your `ibis.php` config;

```php
return [

'prehtml' => function($markdown) {
// preprocesses markdown content before converting to HTML
return $markdown;
},

'prepdf' => function($html) {
// preprocesses converted markdown HTML content before writing to PDF
return $html;
},

// .. rest of ibis.php config
];
```

## Credits

- [Mohamed Said](https://github.com/themsaid)
Expand Down
48 changes: 34 additions & 14 deletions src/Commands/BuildCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ class BuildCommand extends Command
*/
private $disk;

/**
* User ibis.php config settings
* @var array
*/
private $_config;

/**
* Configure the command.
*
Expand Down Expand Up @@ -62,7 +68,7 @@ public function execute(InputInterface $input, OutputInterface $output)
$this->themeName = $input->getArgument('theme');

$currentPath = getcwd();
$config = require $currentPath.'/ibis.php';
$this->_config = require $currentPath.'/ibis.php';

$this->ensureExportDirectoryExists(
$currentPath = getcwd()
Expand All @@ -72,7 +78,6 @@ public function execute(InputInterface $input, OutputInterface $output)

$this->buildPdf(
$this->buildHtml($currentPath.'/content'),
$config,
$currentPath,
$theme
);
Expand Down Expand Up @@ -107,6 +112,10 @@ protected function buildHtml(string $path)
{
$this->output->writeln('<fg=yellow>==></> Parsing Markdown ...');

if (is_callable($this->_config['prehtml'] ?? null)) {
$this->output->writeln('<fg=yellow>==></> Pre-processing Markdown ...');
}

$environment = Environment::createCommonMarkEnvironment();

$environment->addBlockRenderer(FencedCode::class, new FencedCodeRenderer([
Expand All @@ -125,6 +134,12 @@ protected function buildHtml(string $path)
$file->getPathname()
);


if (is_callable($this->_config['prehtml'] ?? null)) {
$markdown = $this->_config['prehtml']($markdown);
}


return $this->prepareForPdf(
$converter->convertToHtml($markdown),
$i + 1
Expand Down Expand Up @@ -160,13 +175,12 @@ private function prepareForPdf(string $html, $file)

/**
* @param string $html
* @param array $config
* @param string $currentPath
* @param string $theme
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
* @throws \Mpdf\MpdfException
*/
protected function buildPdf(string $html, array $config, string $currentPath, string $theme)
protected function buildPdf(string $html, string $currentPath, string $theme)
{
$defaultConfig = (new ConfigVariables())->getDefaults();
$fontDirs = $defaultConfig['fontDir'];
Expand All @@ -176,13 +190,13 @@ protected function buildPdf(string $html, array $config, string $currentPath, st

$pdf = new Mpdf([
'mode' => 'utf-8',
'format' => $config['document']['format'] ?? [210, 297],
'margin_left' => $config['document']['margin_left'] ?? 27,
'margin_right' => $config['document']['margin_right'] ?? 27,
'margin_bottom' => $config['document']['margin_bottom'] ?? 14,
'margin_top' => $config['document']['margin_top'] ?? 14,
'format' => $this->_config['document']['format'] ?? [210, 297],
'margin_left' => $this->_config['document']['margin_left'] ?? 27,
'margin_right' => $this->_config['document']['margin_right'] ?? 27,
'margin_bottom' => $this->_config['document']['margin_bottom'] ?? 14,
'margin_top' => $this->_config['document']['margin_top'] ?? 14,
'fontDir' => array_merge($fontDirs, [getcwd().'/assets/fonts']),
'fontdata' => $this->fonts($config, $fontData),
'fontdata' => $this->fonts($fontData),
]);

$pdf->SetTitle(Ibis::title());
Expand All @@ -207,8 +221,8 @@ protected function buildPdf(string $html, array $config, string $currentPath, st
} else {
$this->output->writeln('<fg=yellow>==></> Adding Book Cover ...');

$coverPosition = $config['cover']['position'] ?? 'position: absolute; left:0; right: 0; top: -.2; bottom: 0;';
$coverDimensions = $config['cover']['dimensions'] ?? 'width: 210mm; height: 297mm; margin: 0;';
$coverPosition = $this->_config['cover']['position'] ?? 'position: absolute; left:0; right: 0; top: -.2; bottom: 0;';
$coverDimensions = $this->_config['cover']['dimensions'] ?? 'width: 210mm; height: 297mm; margin: 0;';

$pdf->WriteHTML(
<<<HTML
Expand All @@ -223,6 +237,12 @@ protected function buildPdf(string $html, array $config, string $currentPath, st

$pdf->SetHTMLFooter('<div id="footer" style="text-align: center">{PAGENO}</div>');


if (is_callable($this->_config['prepdf'] ?? null)) {
$this->output->writeln('<fg=yellow>==></> Pre-processing PDF ...');
$html = $this->_config['prepdf']($html);
}

$this->output->writeln('<fg=yellow>==></> Building PDF ...');

$pdf->WriteHTML(
Expand Down Expand Up @@ -254,9 +274,9 @@ private function getTheme($currentPath, $themeName)
* @param $fontData
* @return array
*/
protected function fonts($config, $fontData)
protected function fonts($fontData)
{
return $fontData + collect($config['fonts'])->mapWithKeys(function ($file, $name) {
return $fontData + collect($this->_config['fonts'])->mapWithKeys(function ($file, $name) {
return [
$name => [
'R' => $file
Expand Down
19 changes: 18 additions & 1 deletion stubs/ibis.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
<?php


if (!function_exists('prehtml')) {
function prehtml($markdown) {
// preprocesses markdown content before converting to HTML
return $markdown;
}
}


if (!function_exists('prepdf')) {
function prepdf($html) {
// preprocesses converted markdown HTML content before writing to PDF
return $html;
}
}


return [
/**
* The book title.
Expand Down Expand Up @@ -56,6 +73,6 @@
/**
* A notice printed at the final page of a generated sample.
*/
'sample_notice' => 'This is a sample from "Laravel Queues in Action" by Mohamed Said. <br>
'sample_notice' => 'This is a sample from "Laravel Queues in Action" by Mohamed Said. <br>
For more information, <a href="https://www.learn-laravel-queues.com/">Click here</a>.',
];

0 comments on commit a04cdf9

Please sign in to comment.