generated from librarianphp/command-help
-
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.
- Loading branch information
1 parent
bc55d60
commit 869792c
Showing
7 changed files
with
142 additions
and
53 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,66 @@ | ||
<?php | ||
|
||
namespace Librarian\Create; | ||
|
||
use Minicli\Stencil; | ||
use Minicli\Command\CommandController; | ||
use Minicli\Input; | ||
|
||
class ContentController extends CommandController | ||
{ | ||
public function handle(): void | ||
{ | ||
if (!$this->getApp()->config->has('stencil_dir')) { | ||
$this->getApp()->getPrinter()->error("You must define a stencil_dir config option."); | ||
return; | ||
} | ||
|
||
if (!$this->getApp()->config->has('stencil_locations')) { | ||
$this->getApp()->getPrinter()->error("You must define a stencil_locations array config option."); | ||
return; | ||
} | ||
|
||
$args = $this->getArgs(); | ||
$template_name = $args[3] ?? null; | ||
if (!$template_name) { | ||
$template_name = 'post'; | ||
} | ||
|
||
$stencil = new Stencil($this->getApp()->config->stencil_dir); | ||
|
||
$input = new Input(' '); | ||
|
||
$this->getPrinter()->info("Content Title: "); | ||
$title = $input->read(); | ||
|
||
$this->getPrinter()->info("Content Description: "); | ||
$description = $input->read(); | ||
|
||
$content = $stencil->applyTemplate($template_name, [ | ||
'title' => $title, | ||
'description' => $description | ||
]); | ||
|
||
$save_locations = $this->getApp()->config->stencil_locations; | ||
|
||
if (!array_key_exists($template_name, $save_locations)) { | ||
$this->getPrinter()->error("Save location not found for template $template_name"); | ||
return; | ||
} | ||
|
||
$path = $save_locations[$template_name]; | ||
$save_name = date('Ymd') . '_' . $this->slugify($title) . '.md'; | ||
$file = fopen($path . '/' . $save_name, 'a+'); | ||
|
||
fwrite($file, $content); | ||
$this->getPrinter()->info("Content generated at " . $path . '/' . $save_name); | ||
} | ||
|
||
public function slugify($title) | ||
{ | ||
$slug = strtolower($title); | ||
$slug = str_replace(' ', '-', $slug); | ||
|
||
return preg_replace('/[^A-Za-z0-9\-]/', '', $slug); | ||
} | ||
} |
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,15 @@ | ||
<?php | ||
|
||
namespace Librarian\Create; | ||
|
||
use Minicli\App; | ||
use Minicli\Command\CommandController; | ||
|
||
class DefaultController extends CommandController | ||
{ | ||
public function handle(): void | ||
{ | ||
$this->getPrinter()->info("./librarian create [subcommand]", true); | ||
$this->getPrinter()->info("Run \"./librarian create content\" to create a content file based on a template."); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,2 +1,7 @@ | ||
# command-demo | ||
demo command repository | ||
# command-create | ||
|
||
Librarian's built-in create command. | ||
|
||
```shell | ||
./librarian create content [template] | ||
``` |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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