Skip to content

Commit

Permalink
refactoring PDF vs EPUB
Browse files Browse the repository at this point in the history
  • Loading branch information
roberto-butti committed Dec 21, 2023
1 parent 63004f6 commit cb6f8f6
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 21 deletions.
12 changes: 6 additions & 6 deletions ibis-next
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/usr/bin/env php
<?php

use Ibis\Commands\BuildCommand;
use Ibis\Commands\EpubCommand;
use Ibis\Commands\BuildEpubCommand;
use Ibis\Commands\BuildPdfCommand;
use Ibis\Commands\InitCommand;
use Ibis\Commands\SampleCommand;
use Ibis\Commands\SortContentCommand;
Expand All @@ -17,14 +17,14 @@ if (file_exists(__DIR__.'/../../autoload.php')) {
/**
* Start the console application.
*/
$app = new Application('Ibis Next', '1.0-dev');
$app->setDefaultCommand("build");
$app = new Application('Ibis Next', '1.0.x');
//$app->setDefaultCommand("build");


// Authentication...
$app->add(new InitCommand());
$app->add(new BuildCommand);
$app->add(new EpubCommand());
$app->add(new BuildPdfCommand);
$app->add(new BuildEpubCommand());
$app->add(new SampleCommand);
$app->add(new SortContentCommand());

Expand Down
5 changes: 3 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ This will create the following files and directories:
- /assets/cover.jpg
- /assets/theme-light.html
- /assets/theme-dark.html
- /assets/style.css
- /content
- /ibis.php

Expand Down Expand Up @@ -167,15 +168,15 @@ Edit your `/ibis.php` configuration files to define the font files to be loaded
## Generating PDF eBook

```
ibis-next build
ibis-next pdf
```

Ibis will parse the files in alphabetical order and store the PDF file in the `export` directory.

By default, for generating the PDF file, the light theme is used. To generate a PDF using the dark theme:

```
ibis-next build dark
ibis-next pdf dark
```

## Generating EPUB eBook 🆕
Expand Down
18 changes: 12 additions & 6 deletions src/Commands/BaseBuildCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use SplFileInfo;

use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
Expand All @@ -17,6 +18,7 @@

use League\CommonMark\Extension\CommonMark\Node\Block\FencedCode;
use League\CommonMark\Extension\CommonMark\Node\Block\IndentedCode;
use League\CommonMark\Extension\FrontMatter\Output\RenderedContentWithFrontMatter;
use League\CommonMark\MarkdownConverter;
use Spatie\CommonMarkHighlighter\IndentedCodeRenderer;
use Spatie\CommonMarkHighlighter\FencedCodeRenderer;
Expand Down Expand Up @@ -87,7 +89,7 @@ protected function buildHtml(string $path, array $config): Collection
$converter = new MarkdownConverter($environment);

return collect($this->disk->allFiles($path))
->map(function (SplFileInfo $file, $i) use ($converter) {
->map(function (SplFileInfo $file, $i) use ($converter, $config) {

$chapter = collect([]);
if ($file->getExtension() != 'md') {
Expand All @@ -111,7 +113,8 @@ protected function buildHtml(string $path, array $config): Collection
}
$chapter["html"] = $this->prepareHtmlForEbook(
$convertedMarkdown->getContent(),
$i + 1
$i + 1,
Arr::get($config, "breakLevel", 2)
);


Expand All @@ -125,17 +128,20 @@ protected function buildHtml(string $path, array $config): Collection
* @param $file
* @return string|string[]
*/
protected function prepareHtmlForEbook(string $html, $file): string
protected function prepareHtmlForEbook(string $html, $file, $breakLevel = 2): string
{
$commands = [
'[break]' => '<div style="page-break-after: always;"></div>'
];

if ($file > 1) {
$html = str_replace('<h1>', '[break]<h1>', $html);
if ($breakLevel >= 1) {
$html = str_replace('<h1>', '[break]<h1>', $html);
}
}
if ($breakLevel >= 2) {
$html = str_replace('<h2>', '[break]<h2>', $html);
}

$html = str_replace('<h2>', '[break]<h2>', $html);
$html = str_replace("<blockquote>\n<p>{notice}", "<blockquote class='notice'><p><strong>Notice:</strong>", $html);
$html = str_replace("<blockquote>\n<p>{warning}", "<blockquote class='warning'><p><strong>Warning:</strong>", $html);
$html = str_replace("<blockquote>\n<p>{quote}", "<blockquote class='quote'><p>", $html);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@

use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Symfony\Component\Console\Command\Command;

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use PHPePub\Core\EPub;
use Symfony\Component\Console\Input\InputOption;

class EpubCommand extends BaseBuildCommand
class BuildEpubCommand extends BaseBuildCommand
{
/**
* Configure the command.
Expand All @@ -23,14 +23,15 @@ protected function configure()
{
$this
->setName('epub')

->addOption(
'content',
'c',
InputOption::VALUE_OPTIONAL,
'The path of the content directory',
''
)
->setDescription('Generate the book in Epub format.');
->setDescription('Generate the book in EPUB format.');
}

/**
Expand All @@ -54,6 +55,7 @@ public function execute(InputInterface $input, OutputInterface $output): int



$this->config["breakLevel"] = 1;
$result = $this->buildEpub(
$this->buildHtml($this->contentDirectory, $this->config),
$this->config,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@

use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Symfony\Component\Console\Command\Command;


use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputOption;

class BuildCommand extends BaseBuildCommand
class BuildPdfCommand extends BaseBuildCommand
{
/**
* @var string|string[]|null
Expand All @@ -39,7 +38,8 @@ class BuildCommand extends BaseBuildCommand
protected function configure()
{
$this
->setName('build')
->setName('pdf')
->setAliases(["build"])
->addArgument('theme', InputArgument::OPTIONAL, 'The name of the theme', 'light')
->addOption(
'content',
Expand All @@ -48,7 +48,7 @@ protected function configure()
'The path of the content directory',
''
)
->setDescription('Generate the book.');
->setDescription('Generate the book in PDF format.');
}

/**
Expand Down

0 comments on commit cb6f8f6

Please sign in to comment.