logseq2gemtext/src/ArticleRender.php

35 lines
920 B
PHP

<?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;
}
}