diff --git a/run.php b/run.php index df9c051..55f281a 100644 --- a/run.php +++ b/run.php @@ -2,6 +2,8 @@ declare(strict_types=1); +use LogseqGem\ArticleRender; + require __DIR__ . '/vendor/autoload.php'; $opts = getopt('o:t:', rest_index: $rest_index); @@ -37,12 +39,11 @@ foreach ($titles as $title) { continue; } - $sanitized_title = $title; - - if ($trim_prefix && strpos($sanitized_title, $trim_prefix) === 0) { - $sanitized_title = substr($sanitized_title, strlen($trim_prefix)); + if ($trim_prefix && strpos($title, $trim_prefix) === 0) { + $title = substr($title, strlen($trim_prefix)); } + $sanitized_title = $title; $sanitized_title = strtolower($sanitized_title); $sanitized_title = str_replace([' ', ','], ['-'], $sanitized_title); $sanitized_title .= '.gmi'; @@ -51,17 +52,8 @@ foreach ($titles as $title) { echo "Writing to ", $sanitized_title, "...\n"; - $date = strtotime($article->getProperties()['date'] ?? null); - if (empty($date)) { - throw new Exception('Article ' . $title . ' has no date property'); - } - - $output = sprintf( - "# %s\n\n%s\n\n%s\n\n=> /gemlog.gmi gemlog\n=> / home\n", - $title, - date('j F Y', $date), - $gemtext - ); + $render = new ArticleRender($title, $gemtext, $article->getProperties()); + $output = $render->render(__DIR__ . '/templates/article.tpl'); file_put_contents($output_path, $output); } diff --git a/src/ArticleParser.php b/src/ArticleParser.php index 9a89d76..f10dcb2 100644 --- a/src/ArticleParser.php +++ b/src/ArticleParser.php @@ -4,7 +4,6 @@ declare(strict_types=1); namespace LogseqGem; -use League\CommonMark\Node\NodeWalker; use League\CommonMark\Node\Inline\Text; use League\CommonMark\Extension\CommonMark\Node\Inline\Link; use League\CommonMark\Extension\CommonMark\Node\Block\ListBlock; diff --git a/src/ArticleRender.php b/src/ArticleRender.php new file mode 100644 index 0000000..54504c3 --- /dev/null +++ b/src/ArticleRender.php @@ -0,0 +1,34 @@ +properties['date'] ?? null; + if (empty($date_str)) { + throw new \Exception('Article ' . $title . ' has no date property'); + } + + $this->date_formatted = date(self::DATE_FORMAT, strtotime($date_str)); + } + + public function render(string $tpl_path) { + $template = file_get_contents($tpl_path); + + $template = str_replace('%TITLE%', $this->title, $template); + $template = str_replace('%CONTENT%', $this->content, $template); + $template = str_replace('%DATE_FORMATTED%', $this->date_formatted, $template); + + return $template; + } +} diff --git a/templates/article.tpl b/templates/article.tpl new file mode 100644 index 0000000..c4d2b7e --- /dev/null +++ b/templates/article.tpl @@ -0,0 +1,8 @@ +# %TITLE% + +%CONTENT% + +- annika // %DATE_FORMATTED% + +=> /gemlog.gmi gemlog +=> / home diff --git a/tests/ArticleParserTest.php b/tests/ArticleParserTest.php index 32d4fe7..adfb8f7 100644 --- a/tests/ArticleParserTest.php +++ b/tests/ArticleParserTest.php @@ -4,16 +4,11 @@ declare(strict_types=1); namespace LogseqGem\Tests; -use InvalidArgumentException; -use Exception; use PHPUnit\Framework\TestCase; use org\bovigo\vfs\vfsStream; use org\bovigo\vfs\vfsStreamDirectory; use LogseqGem\ArticleParser; -use RuntimeException; -use SebastianBergmann\RecursionContext\InvalidArgumentException as RecursionContextInvalidArgumentException; -use PHPUnit\Framework\ExpectationFailedException; class ArticleParserTest extends TestCase { private vfsStreamDirectory $root; diff --git a/tests/ArticleRenderTest.php b/tests/ArticleRenderTest.php new file mode 100644 index 0000000..f9c4eb5 --- /dev/null +++ b/tests/ArticleRenderTest.php @@ -0,0 +1,43 @@ +root = vfsStream::setup(); + } + + public function testRender() { + $render = new ArticleRender( + "cool title", + "my gemtext", + ['date' => '2021-01-01'] + ); + + + $output = $render->render(__DIR__ . "/../templates/article.tpl"); + $this->assertStringContainsString("# cool title\n\n", $output); + $this->assertStringContainsString("\n\nmy gemtext\n\n", $output); + $this->assertStringContainsString("\n\n=> /gemlog.gmi", $output); + } + + public function testMissingDate() { + $this->expectException(\Exception::class); + $this->expectExceptionMessage('no date property'); + + $render = new ArticleRender( + "cool title", + "my gemtext", + ); + } +}