Add ArticleRender for article templates

This commit is contained in:
Annika Backstrom 2022-05-22 09:48:26 +01:00
parent 12a604d1b2
commit cb0e7654b6
6 changed files with 92 additions and 21 deletions

22
run.php
View File

@ -2,6 +2,8 @@
declare(strict_types=1); declare(strict_types=1);
use LogseqGem\ArticleRender;
require __DIR__ . '/vendor/autoload.php'; require __DIR__ . '/vendor/autoload.php';
$opts = getopt('o:t:', rest_index: $rest_index); $opts = getopt('o:t:', rest_index: $rest_index);
@ -37,12 +39,11 @@ foreach ($titles as $title) {
continue; continue;
} }
$sanitized_title = $title; if ($trim_prefix && strpos($title, $trim_prefix) === 0) {
$title = substr($title, strlen($trim_prefix));
if ($trim_prefix && strpos($sanitized_title, $trim_prefix) === 0) {
$sanitized_title = substr($sanitized_title, strlen($trim_prefix));
} }
$sanitized_title = $title;
$sanitized_title = strtolower($sanitized_title); $sanitized_title = strtolower($sanitized_title);
$sanitized_title = str_replace([' ', ','], ['-'], $sanitized_title); $sanitized_title = str_replace([' ', ','], ['-'], $sanitized_title);
$sanitized_title .= '.gmi'; $sanitized_title .= '.gmi';
@ -51,17 +52,8 @@ foreach ($titles as $title) {
echo "Writing to ", $sanitized_title, "...\n"; echo "Writing to ", $sanitized_title, "...\n";
$date = strtotime($article->getProperties()['date'] ?? null); $render = new ArticleRender($title, $gemtext, $article->getProperties());
if (empty($date)) { $output = $render->render(__DIR__ . '/templates/article.tpl');
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
);
file_put_contents($output_path, $output); file_put_contents($output_path, $output);
} }

View File

@ -4,7 +4,6 @@ declare(strict_types=1);
namespace LogseqGem; namespace LogseqGem;
use League\CommonMark\Node\NodeWalker;
use League\CommonMark\Node\Inline\Text; use League\CommonMark\Node\Inline\Text;
use League\CommonMark\Extension\CommonMark\Node\Inline\Link; use League\CommonMark\Extension\CommonMark\Node\Inline\Link;
use League\CommonMark\Extension\CommonMark\Node\Block\ListBlock; use League\CommonMark\Extension\CommonMark\Node\Block\ListBlock;

34
src/ArticleRender.php Normal file
View File

@ -0,0 +1,34 @@
<?php
declare(strict_types=1);
namespace LogseqGem;
class ArticleRender {
private string $date_formatted;
const DATE_FORMAT = 'j F Y';
public function __construct(
private string $title,
private string $content,
private array $properties = [],
) {
$date_str = $this->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;
}
}

8
templates/article.tpl Normal file
View File

@ -0,0 +1,8 @@
# %TITLE%
%CONTENT%
- annika // %DATE_FORMATTED%
=> /gemlog.gmi gemlog
=> / home

View File

@ -4,16 +4,11 @@ declare(strict_types=1);
namespace LogseqGem\Tests; namespace LogseqGem\Tests;
use InvalidArgumentException;
use Exception;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use org\bovigo\vfs\vfsStream; use org\bovigo\vfs\vfsStream;
use org\bovigo\vfs\vfsStreamDirectory; use org\bovigo\vfs\vfsStreamDirectory;
use LogseqGem\ArticleParser; use LogseqGem\ArticleParser;
use RuntimeException;
use SebastianBergmann\RecursionContext\InvalidArgumentException as RecursionContextInvalidArgumentException;
use PHPUnit\Framework\ExpectationFailedException;
class ArticleParserTest extends TestCase { class ArticleParserTest extends TestCase {
private vfsStreamDirectory $root; private vfsStreamDirectory $root;

View File

@ -0,0 +1,43 @@
<?php
declare(strict_types=1);
namespace LogseqGem\Tests;
use PHPUnit\Framework\TestCase;
use org\bovigo\vfs\vfsStream;
use org\bovigo\vfs\vfsStreamDirectory;
use LogseqGem\ArticleRender;
class ArticleRenderTest extends TestCase {
private vfsStreamDirectory $root;
public function setUp(): void {
$this->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",
);
}
}