forked from themsaid/ibis
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactoring common code for PDF and EPUB commands
- Loading branch information
1 parent
e1266ab
commit b2938b0
Showing
3 changed files
with
217 additions
and
305 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
<?php | ||
|
||
namespace Ibis\Commands; | ||
|
||
use SplFileInfo; | ||
|
||
use Illuminate\Filesystem\Filesystem; | ||
use Illuminate\Support\Collection; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use League\CommonMark\Environment\Environment; | ||
use League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension; | ||
use League\CommonMark\Extension\FrontMatter\FrontMatterExtension; | ||
use League\CommonMark\Extension\GithubFlavoredMarkdownExtension; | ||
use League\CommonMark\Extension\Table\TableExtension; | ||
|
||
use League\CommonMark\Extension\CommonMark\Node\Block\FencedCode; | ||
use League\CommonMark\Extension\CommonMark\Node\Block\IndentedCode; | ||
use League\CommonMark\MarkdownConverter; | ||
use Spatie\CommonMarkHighlighter\IndentedCodeRenderer; | ||
use Spatie\CommonMarkHighlighter\FencedCodeRenderer; | ||
|
||
class BaseBuildCommand extends Command | ||
{ | ||
protected OutputInterface $output; | ||
|
||
protected Filesystem $disk; | ||
|
||
protected string $contentDirectory; | ||
|
||
protected string $currentPath; | ||
|
||
protected array $config; | ||
|
||
|
||
protected function preExecute(InputInterface $input, OutputInterface $output) | ||
{ | ||
$this->disk = new Filesystem(); | ||
$this->output = $output; | ||
|
||
$this->currentPath = getcwd(); | ||
|
||
$this->contentDirectory = $input->getOption('content'); | ||
if ($this->contentDirectory === "") { | ||
$this->contentDirectory = getcwd() . DIRECTORY_SEPARATOR . "content"; | ||
} | ||
if (!$this->disk->isDirectory($this->contentDirectory)) { | ||
$this->output->writeln('<error>Error, check if ' . $this->contentDirectory . ' exists.</error>'); | ||
exit -1; | ||
} | ||
$this->output->writeln('<info>Loading content from: ' . $this->contentDirectory . '</info>'); | ||
|
||
$configIbisFile = $this->currentPath . '/ibis.php'; | ||
if (!$this->disk->isFile($configIbisFile)) { | ||
$this->output->writeln('<error>Error, check if ' . $configIbisFile . ' exists.</error>'); | ||
exit -1; | ||
} | ||
|
||
$this->config = require $configIbisFile; | ||
|
||
} | ||
|
||
|
||
/** | ||
* @param string $path | ||
* @param array $config | ||
* @return Collection | ||
*/ | ||
protected function buildHtml(string $path, array $config): Collection | ||
{ | ||
$this->output->writeln('<fg=yellow>==></> Parsing Markdown ...'); | ||
|
||
|
||
$environment = new Environment([]); | ||
$environment->addExtension(new CommonMarkCoreExtension()); | ||
$environment->addExtension(new GithubFlavoredMarkdownExtension()); | ||
$environment->addExtension(new TableExtension()); | ||
$environment->addExtension(new FrontMatterExtension()); | ||
|
||
$environment->addRenderer(FencedCode::class, new FencedCodeRenderer([ | ||
'html', 'php', 'js', 'bash', 'json' | ||
])); | ||
$environment->addRenderer(IndentedCode::class, new IndentedCodeRenderer([ | ||
'html', 'php', 'js', 'bash', 'json' | ||
])); | ||
|
||
if (is_callable($config['configure_commonmark'])) { | ||
call_user_func($config['configure_commonmark'], $environment); | ||
} | ||
|
||
$converter = new MarkdownConverter($environment); | ||
|
||
return collect($this->disk->allFiles($path)) | ||
->map(function (SplFileInfo $file, $i) use ($converter) { | ||
|
||
$chapter = collect([]); | ||
if ($file->getExtension() != 'md') { | ||
$chapter["mdfile"] = $file->getFilename(); | ||
$chapter["frontmatter"] = false; | ||
$chapter["html"] = ""; | ||
return $chapter; | ||
} | ||
|
||
$markdown = $this->disk->get( | ||
$file->getPathname() | ||
); | ||
|
||
|
||
//$chapter = collect([]); | ||
$convertedMarkdown = $converter->convert($markdown); | ||
$chapter["mdfile"] = $file->getFilename(); | ||
$chapter["frontmatter"] = false; | ||
if ($convertedMarkdown instanceof RenderedContentWithFrontMatter) { | ||
$chapter["frontmatter"] = $convertedMarkdown->getFrontMatter(); | ||
} | ||
$chapter["html"] = $this->prepareHtmlForEbook( | ||
$convertedMarkdown->getContent(), | ||
$i + 1 | ||
); | ||
|
||
|
||
return $chapter; | ||
}); | ||
//->implode(' '); | ||
} | ||
|
||
|
||
/** | ||
* @param string $html | ||
* @param $file | ||
* @return string|string[] | ||
*/ | ||
protected function prepareHtmlForEbook(string $html, $file) | ||
{ | ||
$commands = [ | ||
'[break]' => '<div style="page-break-after: always;"></div>' | ||
]; | ||
|
||
if ($file > 1) { | ||
$html = str_replace('<h1>', '[break]<h1>', $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); | ||
|
||
$html = str_replace(array_keys($commands), array_values($commands), $html); | ||
|
||
return $html; | ||
} | ||
|
||
|
||
|
||
/** | ||
* @param string $currentPath | ||
*/ | ||
protected function ensureExportDirectoryExists(string $currentPath): void | ||
{ | ||
$this->output->writeln('<fg=yellow>==></> Preparing Export Directory ...'); | ||
|
||
if (!$this->disk->isDirectory($currentPath . '/export')) { | ||
$this->disk->makeDirectory( | ||
$currentPath . '/export', | ||
0755, | ||
true | ||
); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.