Skip to content

Commit

Permalink
feat: build a specific page (#1323)
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnaudLigny committed Mar 18, 2022
1 parent 35229cc commit 947c246
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/Builder.php
Expand Up @@ -132,6 +132,7 @@ public function build(array $options): self
$this->options = array_merge([
'drafts' => false, // build drafts or not
'dry-run' => false, // if dry-run is true, generated files are not saved
'page' => '', // specific page to build
], $options);

// process each step
Expand Down
9 changes: 5 additions & 4 deletions src/Command/Build.php
Expand Up @@ -35,6 +35,7 @@ protected function configure()
new InputArgument('path', InputArgument::OPTIONAL, 'Use the given path as working directory'),
new InputOption('config', 'c', InputOption::VALUE_REQUIRED, 'Set the path to extra config files (comma-separated)'),
new InputOption('drafts', 'd', InputOption::VALUE_NONE, 'Include drafts'),
new InputOption('page', 'p', InputOption::VALUE_REQUIRED, 'Build a specific page'),
new InputOption('dry-run', null, InputOption::VALUE_NONE, 'Build without saving'),
new InputOption('baseurl', null, InputOption::VALUE_REQUIRED, 'Set the base URL'),
new InputOption('output', null, InputOption::VALUE_REQUIRED, 'Set the output directory'),
Expand All @@ -59,10 +60,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
}
if ($input->getOption('output')) {
$config['output']['dir'] = $input->getOption('output');
$this->fs->dumpFile(
Util::joinFile($this->getPath(), self::TMP_DIR, 'output'),
(string) $input->getOption('output')
);
$this->fs->dumpFile(Util::joinFile($this->getPath(), self::TMP_DIR, 'output'), (string) $input->getOption('output'));
}
if ($input->getOption('postprocess') === null) {
$config['postprocess']['enabled'] = true;
Expand All @@ -84,6 +82,9 @@ protected function execute(InputInterface $input, OutputInterface $output)
$options['dry-run'] = true;
$messageOpt .= ' (dry-run)';
}
if ($input->getOption('page')) {
$options['page'] = $input->getOption('page');
}

$output->writeln(\sprintf('Building website%s...', $messageOpt));
$output->writeln(
Expand Down
6 changes: 6 additions & 0 deletions src/Command/Serve.php
Expand Up @@ -44,6 +44,7 @@ protected function configure()
new InputArgument('path', InputArgument::OPTIONAL, 'Use the given path as working directory'),
new InputOption('config', 'c', InputOption::VALUE_REQUIRED, 'Set the path to extra config files (comma-separated)'),
new InputOption('drafts', 'd', InputOption::VALUE_NONE, 'Include drafts'),
new InputOption('page', 'p', InputOption::VALUE_REQUIRED, 'Build a specific page'),
new InputOption('open', 'o', InputOption::VALUE_NONE, 'Open browser automatically'),
new InputOption('host', null, InputOption::VALUE_REQUIRED, 'Server host'),
new InputOption('port', null, InputOption::VALUE_REQUIRED, 'Server port'),
Expand All @@ -68,6 +69,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$postprocess = $input->getOption('postprocess');
$clearcache = $input->getOption('clear-cache');
$verbose = $input->getOption('verbose');
$page = $input->getOption('page');

$this->setUpServer($host, $port);

Expand Down Expand Up @@ -112,6 +114,10 @@ protected function execute(InputInterface $input, OutputInterface $output)
if ($verbose) {
$buildProcessArguments[] = '-'.str_repeat('v', $_SERVER['SHELL_VERBOSITY']);
}
if (!empty($page)) {
$buildProcessArguments[] = '--page';
$buildProcessArguments[] = $page;
}

$buildProcess = new Process(array_merge($buildProcessArguments, [$this->getPath()]));

Expand Down
13 changes: 12 additions & 1 deletion src/Step/Content/Load.php
Expand Up @@ -19,6 +19,9 @@
*/
class Load extends AbstractStep
{
/** @var string */
protected $page;

/**
* {@inheritdoc}
*/
Expand All @@ -36,18 +39,26 @@ public function init($options)
if (is_dir($this->builder->getConfig()->getContentPath())) {
$this->canProcess = true;
}
$this->page = $options['page'];
}

/**
* {@inheritdoc}
*/
public function process()
{
$namePattern = '/\.('.implode('|', (array) $this->builder->getConfig()->get('content.ext')).')$/';
$content = Finder::create()
->files()
->in($this->builder->getConfig()->getContentPath())
->name('/\.('.implode('|', (array) $this->builder->getConfig()->get('content.ext')).')$/')
->sortByName(true);
if ($this->page) {
if (!util\File::getFS()->exists(Util::joinFile($this->builder->getConfig()->getContentPath(), $this->page))) {
$this->builder->getLogger()->error(sprintf('File "%s" doesn\'t exist.', $this->page));
}
$namePattern = $this->page;
}
$content->name('index.md')->name($namePattern);
if (file_exists(Util::joinFile($this->builder->getConfig()->getContentPath(), '.gitignore'))) {
$content->ignoreVCSIgnored(true);
}
Expand Down

0 comments on commit 947c246

Please sign in to comment.