Skip to content

Commit

Permalink
Compare the original data with the output data (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
cable8mm committed Mar 5, 2024
1 parent b598b3a commit 5670052
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 12 deletions.
28 changes: 21 additions & 7 deletions src/Command/CreateJekyllCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

#[AsCommand(
Expand All @@ -33,11 +34,24 @@ class CreateJekyllCommand extends Command
'상품',
];

protected function configure()
public function __construct()
{
parent::__construct();

$this->database = DB::getInstance()->getConnection();
}

protected function configure()
{
$this->addOption(
'output',
'o',
InputOption::VALUE_OPTIONAL,
'To which destination do the files output?',
__DIR__.'/../../dist/'
);
}

/**
* Create Markdown files for Jekyll
*
Expand All @@ -53,28 +67,28 @@ protected function execute(InputInterface $input, OutputInterface $output): int

foreach ($articles as $row) {
$article = Article::make($map)
->in($row)
->setBodyCallback(
function ($item) {
return preg_replace('/<img[^>]+>/', '', $item);
return preg_replace('/<img[^>]+>/u', '', $item);
}, function ($item) {
return preg_replace('/\\\\\[[^\]]+\]\n?/', '', $item);
return preg_replace('/\[[^\]]+\]\n?/u', '', $item);
}
)
->setAddHours(24 * 365 + 24 * 120)
->resolveCategories($this->categories);
->resolveCategories($this->categories)
->in($row);

$jekyll = new Jekyll(
layout: 'post',
title: $article->title,
date: $article->publishedAt,
author: 'Samgu Lee',
author: 'Companimal',
body: $article->markdown(),
slug: $article->slug,
categories: $article->categories,
);

file_put_contents(__DIR__.'/../../dist/'.$jekyll->path(), $jekyll->render());
file_put_contents($input->getOption('output').$jekyll->path(), $jekyll->render());
}

return Command::SUCCESS;
Expand Down
7 changes: 4 additions & 3 deletions src/Models/Article.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ class Article

private array $map;

private HtmlConverter $converter;

/**
* A row from database.
*/
Expand All @@ -36,6 +38,7 @@ class Article

public function __construct()
{
$this->converter = new HtmlConverter();
}

private function title(): void
Expand Down Expand Up @@ -81,8 +84,6 @@ private function categories(): void

private function body(): void
{
$converter = new HtmlConverter();

$body = $this->row[$this->map['body']];

if (! is_null($this->bodyCallbacks)) {
Expand All @@ -91,7 +92,7 @@ private function body(): void
}
}

$this->body = strip_tags($converter->convert($body));
$this->body = strip_tags($this->converter->convert($body));
}

private function publishedAt(): Carbon
Expand Down
6 changes: 4 additions & 2 deletions tests/ArticleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ public function test_callbacks_to_work_collectly(): void

$faker = Factory::create();

$html = '<p>\[widget id="good" \]</p>'.PHP_EOL;
$html = '<p>[widget id="good" ]</p>'.PHP_EOL;
$html = '<p>[widget type="good" good="1000014584"]</p>'.PHP_EOL;
$html .= '<p><img src="https://www.palgle.com/image.png" /></p>'.PHP_EOL;
$html = '<p>[widget type=&quot;good&quot; good=&quot;1000014584&quot;]</p>'.PHP_EOL;
$html .= '<p>only this to remain</p>';

$row = [
Expand All @@ -36,7 +38,7 @@ function ($item) {
return preg_replace('/<img[^>]+>/', '', $item);
}, function ($item) {
// \[widget type="good" good="1000012875"\]
return preg_replace('/\\\\\[[^\]]+\]\n?/', '', $item);
return preg_replace('/\[[^\]]+\]\n?/u', '', $item);
}
)
->in($row);
Expand Down

0 comments on commit 5670052

Please sign in to comment.