Skip to content

Commit

Permalink
Adding md_file_list option in config
Browse files Browse the repository at this point in the history
  • Loading branch information
roberto-butti committed May 26, 2024
1 parent 1e77fe8 commit c078045
Show file tree
Hide file tree
Showing 14 changed files with 660 additions and 471 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## 1.1.0 - 26th May 2024
- Adding `md_file_list` for selecting a subset of Markdown file in the "content" directory, for ebook creation
- Updating dependencies (Illuminate 11)
- Updating Rector 1
- Supporting an additional blockquote syntax like `[!NOTE]` and `[!WARNING]`

## 1.0.12 - 4th February 2024
- Exporting HTML

Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
"ext-mbstring": "*",
"ext-zlib": "*",
"hi-folks/phpepub": "^1.0.0",
"illuminate/filesystem": "^10.0",
"illuminate/support": "^10.0",
"illuminate/filesystem": "^11.0",
"illuminate/support": "^11.0",
"mpdf/mpdf": "^8.0",
"spatie/commonmark-highlighter": "^3.0",
"symfony/console": "^6.0|^7.0",
Expand All @@ -44,7 +44,7 @@
"laravel/pint": "^1.2",
"pestphp/pest": "^2.33",
"phpstan/phpstan": "^1.10",
"rector/rector": "^0.19.2",
"rector/rector": "^1",
"symfony/var-dumper": "^6.0|^7.0"
},
"autoload": {
Expand Down
968 changes: 560 additions & 408 deletions composer.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@
SetList::EARLY_RETURN,
SetList::TYPE_DECLARATION,
LevelSetList::UP_TO_PHP_81,
SetList::CODING_STYLE
SetList::CODING_STYLE,
]);
};
30 changes: 21 additions & 9 deletions src/Commands/BaseBuildCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ protected function buildHtml(string $path, array $config): Collection
$environment->addExtension(new AsideExtension());

$environment->addRenderer(FencedCode::class, new FencedCodeRenderer([
'html', 'php', 'js', 'bash', 'json'
'html', 'php', 'js', 'bash', 'json',
]));
$environment->addRenderer(IndentedCode::class, new IndentedCodeRenderer([
'html', 'php', 'js', 'bash', 'json'
'html', 'php', 'js', 'bash', 'json',
]));
$environment->addRenderer(Aside::class, new AsideRenderer());

Expand All @@ -90,7 +90,17 @@ protected function buildHtml(string $path, array $config): Collection

$converter = new MarkdownConverter($environment);

return collect($this->disk->allFiles($path))
$fileList = [];
if (array_key_exists("md_file_list", $config)) {
foreach ($config["md_file_list"] as $filename) {
$filefound = new SplFileInfo($path . '/' . $filename);
$fileList[] = $filefound;
}
} else {
$fileList = $this->disk->allFiles($path);
}

return collect($fileList)
->map(function (SplFileInfo $file, $i) use ($converter, $config) {

$chapter = collect([]);
Expand All @@ -102,7 +112,7 @@ protected function buildHtml(string $path, array $config): Collection
}

$markdown = $this->disk->get(
$file->getPathname()
$file->getPathname(),
);

$convertedMarkdown = $converter->convert($markdown);
Expand All @@ -115,7 +125,7 @@ protected function buildHtml(string $path, array $config): Collection
$chapter->put("html", $this->prepareHtmlForEbook(
$convertedMarkdown->getContent(),
$i + 1,
Arr::get($config, "breakLevel", 2)
Arr::get($config, "breakLevel", 2),
));


Expand All @@ -132,7 +142,7 @@ protected function buildHtml(string $path, array $config): Collection
protected function prepareHtmlForEbook(string $html, $file, $breakLevel = 2): string
{
$commands = [
'[break]' => '<div style="page-break-after: always;"></div>'
'[break]' => '<div style="page-break-after: always;"></div>',
];

if ($file > 1 && $breakLevel >= 1) {
Expand All @@ -146,6 +156,8 @@ protected function prepareHtmlForEbook(string $html, $file, $breakLevel = 2): st
$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("<blockquote>\n<p>[!NOTE]", "<blockquote class='notice'><p><strong>Note:</strong>", $html);
$html = str_replace("<blockquote>\n<p>[!WARNING]", "<blockquote class='warning'><p><strong>Warning:</strong>", $html);

return str_replace(array_keys($commands), array_values($commands), $html);
}
Expand All @@ -157,15 +169,15 @@ protected function ensureExportDirectoryExists(string $currentPath): void
$this->output->writeln('<fg=yellow>==></> Preparing Export Directory ...');

if (!$this->disk->isDirectory(
Config::buildPath($currentPath, "export")
Config::buildPath($currentPath, "export"),
)) {
$this->disk->makeDirectory(
Config::buildPath(
$currentPath,
"export"
"export",
),
0755,
true
true,
);
}
}
Expand Down
14 changes: 7 additions & 7 deletions src/Commands/BuildEpubCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ protected function configure()
'c',
InputOption::VALUE_OPTIONAL,
'The path of the content directory',
''
'',
)
->addOption(
'workingdir',
'd',
InputOption::VALUE_OPTIONAL,
'The path of the working directory where `ibis.php` and `assets` directory are located',
''
'',
)
->setDescription('Generate the book in EPUB format.');
}
Expand Down Expand Up @@ -65,7 +65,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$result = $this->buildEpub(
$this->buildHtml($this->config->contentPath, $this->config->config),
$this->config->config,
$this->config->workingPath
$this->config->workingPath,
);

$this->output->writeln('');
Expand Down Expand Up @@ -151,25 +151,25 @@ protected function buildEpub(Collection $chapters, array $config, string $curren
chapterName: Arr::get($chapter, "frontmatter.title", "Chapter " . ($key + 1)),
fileName: "Chapter" . $key . ".html",
chapterData: $content_start . $chapter["html"] . $content_end,
externalReferences: EPub::EXTERNAL_REF_ADD
externalReferences: EPub::EXTERNAL_REF_ADD,
);
//file_put_contents('export/' . "Chapter" . $key . " .html", $content_start . $chapter["html"] . $content_end);
}

$book->buildTOC(
title: "Index",
addReferences: false,
addToIndex: false
addToIndex: false,
);

$book->finalize();

$epubFilename = Config::buildPath(
$currentPath,
"export",
$this->config->outputFileName() . '.epub'
$this->config->outputFileName() . '.epub',
);
$book->saveBook($epubFilename);
@$book->saveBook($epubFilename);


$this->output->writeln('<fg=green>==></> EPUB file ' . $epubFilename . ' created');
Expand Down
8 changes: 4 additions & 4 deletions src/Commands/BuildHtmlCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ protected function configure()
'c',
InputOption::VALUE_OPTIONAL,
'The path of the content directory',
''
'',
)
->addOption(
'workingdir',
'd',
InputOption::VALUE_OPTIONAL,
'The path of the working directory where `ibis.php` and `assets` directory are located',
''
'',
)
->setDescription('Generate the book in HTML format.');
}
Expand All @@ -62,7 +62,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$result = $this->buildHtmlFile(
$this->buildHtml($this->config->contentPath, $this->config->config),
$this->config->config,
$this->config->workingPath
$this->config->workingPath,
);

$this->output->writeln('');
Expand Down Expand Up @@ -103,7 +103,7 @@ protected function buildHtmlFile(Collection $chapters, array $config, string $cu
$htmlFilename = Config::buildPath(
$currentPath,
"export",
$this->config->outputFileName() . '.html'
$this->config->outputFileName() . '.html',
);
file_put_contents($htmlFilename, $outputHtml);

Expand Down
20 changes: 10 additions & 10 deletions src/Commands/BuildPdfCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ protected function configure()
'c',
InputOption::VALUE_OPTIONAL,
'The path of the content directory',
''
'',
)
->addOption(
'workingdir',
'd',
InputOption::VALUE_OPTIONAL,
'The path of the working directory where `ibis.php` and `assets` directory are located',
''
'',
)
->setDescription('Generate the book in PDF format.');
}
Expand Down Expand Up @@ -82,7 +82,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$this->buildHtml($this->config->contentPath, $this->config->config),
$this->config->config,
$this->config->workingPath,
$theme
$theme,
);

$this->output->writeln('');
Expand Down Expand Up @@ -149,7 +149,7 @@ protected function buildPdf(Collection $chapters, array $config, string $current
<div style="{$coverPosition}">
<img src="{$currentPath}/assets/{$coverImage}" style="{$coverDimensions}"/>
</div>
HTML
HTML,
);

$pdf->AddPage();
Expand All @@ -170,7 +170,7 @@ protected function buildPdf(Collection $chapters, array $config, string $current
$this->output->writeln('<fg=yellow>==></> Building PDF ...');

$pdf->WriteHTML(
$theme
$theme,
);
//dd($chapters);
foreach ($chapters as $chapter) {
Expand All @@ -181,12 +181,12 @@ protected function buildPdf(Collection $chapters, array $config, string $current
'
<div style="' . $config['header'] . '">
' . Arr::get($chapter, "frontmatter.title", $config["title"]) . '
</div>'
</div>',
);
}

$pdf->WriteHTML(
$chapter["html"]
$chapter["html"],
);
}

Expand All @@ -201,7 +201,7 @@ protected function buildPdf(Collection $chapters, array $config, string $current


$pdf->Output(
$pdfFilename
$pdfFilename,
);

$this->output->writeln('<fg=green>==></> PDF file ' . $pdfFilename . ' created');
Expand All @@ -227,8 +227,8 @@ protected function fonts(array $config, $fontData): float|int|array
{
return $fontData + collect($config['fonts'])->mapWithKeys(static fn($file, $name): array => [
$name => [
'R' => $file
]
'R' => $file,
],
])->toArray();
}
}
28 changes: 14 additions & 14 deletions src/Commands/InitCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ protected function configure()
'd',
InputOption::VALUE_OPTIONAL,
'The path of the working directory where `ibis.php` and `assets` directory will be created',
''
'',
)
->setDescription('Initialize a new project in the working directory (current dir by default).');
}
Expand All @@ -55,15 +55,15 @@ protected function execute(InputInterface $input, OutputInterface $output): int

$ibisConfigPath = Config::buildPath(
$workingPath,
'ibis.php'
'ibis.php',
);
$contentPath = Config::buildPath(
$workingPath,
'content'
'content',
);
$assetsPath = Config::buildPath(
$workingPath,
'assets'
'assets',
);

$this->output->writeln('<info>Creating directory/files for:</info>');
Expand All @@ -77,11 +77,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}

$this->disk->makeDirectory(
$assetsPath
$assetsPath,
);

$this->disk->makeDirectory(
Config::buildPath($assetsPath, 'fonts')
Config::buildPath($assetsPath, 'fonts'),
);

$assetsToCopy = [
Expand All @@ -102,25 +102,25 @@ protected function execute(InputInterface $input, OutputInterface $output): int
'..',
'..',
'stubs/assets',
$assetToCopy
)
)
$assetToCopy,
),
),
);
}


$this->output->writeln('<info>✨ content directory as: ' . $contentPath . '</info>');

$this->disk->makeDirectory(
$contentPath
$contentPath,
);

$this->disk->copyDirectory(
Config::buildPath(
__DIR__,
'../../stubs/content'
'../../stubs/content',
),
$contentPath
$contentPath,
);

$this->output->writeln('<info>✨ config file as: ' . $ibisConfigPath . '</info>');
Expand All @@ -129,8 +129,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$ibisConfigPath,
$this->disk->get(Config::buildPath(
__DIR__,
'../../stubs/ibis.php'
))
'../../stubs/ibis.php',
)),
);


Expand Down
Loading

0 comments on commit c078045

Please sign in to comment.