-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathBuildCommand.php
More file actions
183 lines (153 loc) · 5.23 KB
/
BuildCommand.php
File metadata and controls
183 lines (153 loc) · 5.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
namespace Dandy\Book;
use Illuminate\Filesystem\Filesystem;
use SplFileInfo;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class BuildCommand extends Command
{
/**
* @var Filesystem
*/
private Filesystem $disk;
/**
* Configure the command.
*
* @return void
*/
protected function configure()
{
$this
->setName('build')
->setDescription('Generate the book.');
}
/**
* Execute the command.
*
* @param InputInterface $input
* @param OutputInterface $output
*
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
* @throws \Mpdf\MpdfException
*
* @return int
*/
public function execute(InputInterface $input, OutputInterface $output): int
{
$this->disk = new Filesystem();
$currentPath = getcwd();
$config = require $currentPath.'/config.php';
$output->writeln('<fg=yellow>==></> Preparing Export Directory ...');
$this->ensureExportDirectoryExists($currentPath);
$theme = $this->getTheme($currentPath);
$pdf = (new Book($config))
->withCover($this->cover($currentPath, $config))
->withCover($this->cover($currentPath, $config, 'cover-back'))
->withColophon(file_get_contents($currentPath.'/assets/colophon.html'))
->withTheme($theme)
->withTitle($config['title'])
->withAuthor($config['author'])
->setFooter('<div id="footer" style="text-align: center">{PAGENO}</div>');
$files = collect($this->disk->files($currentPath.'/content'))
->filter(fn (SplFileInfo $file) => $file->getExtension() === 'md')
->values();
$processor = new MarkdownProcessor();
$progressBar = new ProgressBar($output, $files->count());
$progressBar->start();
foreach ($files as $index => $file) {
$html = $processor->convert(
$this->disk->get($file->getPathname()),
$index + 1
);
// Добавляем страницу, кроме последней
$pdf->chapter($html, $index < $files->count() - 1);
$progressBar->advance();
}
$progressBar->finish();
$pdfFilePath = sprintf('%s/export/%s.pdf', $currentPath, $config['title']);
$pdf->Output($pdfFilePath);
$output->writeln('');
$output->writeln(
sprintf('<fg=yellow>==></> Writing %s PDF Pages To Disk ...', $pdf->getPageCount())
);
// Создаем кликабельную ссылку для поддерживающих терминалов
$output->writeln(sprintf(
' <href=file://%s>📄 Click to open: %s</>',
$pdfFilePath,
$pdfFilePath
));
$output->writeln('<info>Book Built Successfully!</info>');
return Command::SUCCESS;
}
/**
* @param string $currentPath
*/
protected function ensureExportDirectoryExists(string $currentPath): void
{
if (! $this->disk->isDirectory($currentPath.'/export')) {
$this->disk->makeDirectory(
$currentPath.'/export',
0755,
true
);
}
}
/**
* Возвращает HTML для обложки или пустую строку, если обложки нет.
*
* @param string $currentPath
* @param array $config
*
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
*
* @return string
*/
protected function cover(string $currentPath, array $config, string $filename = 'cover'): string
{
$jpgPath = $currentPath.'/assets/'.$filename.'.jpg';
$htmlPath = $currentPath.'/assets/'.$filename.'.html';
if ($this->disk->isFile($jpgPath)) {
$coverPosition = $config['cover']['position'] ?? 'position: absolute; left:0; right: 0; top: -.2; bottom: 0;';
$coverDimensions = $config['cover']['dimensions'] ?? 'width: 148mm; height: 210mm; margin: 0;';
return <<<HTML
<div style="{$coverPosition}">
<img src="assets/{$filename}.jpg" style="{$coverDimensions}"/>
</div>
HTML;
}
if ($this->disk->isFile($htmlPath)) {
return $this->disk->get($htmlPath);
}
return '';
}
/**
* @param $currentPath
* @param string $themeName
*
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
*
* @return string
*/
private function getTheme($currentPath, string $themeName = 'theme'): string
{
return $this->disk->get($currentPath."/assets/$themeName.html");
}
/**
* @param $config
* @param $fontData
*
* @return array
*/
protected function fonts($config, $fontData): array
{
return $fontData + collect($config['fonts'] ?? [])->mapWithKeys(function ($file, $name) {
return [
$name => [
'R' => $file,
],
];
})->toArray();
}
}