diff --git a/run.php b/run.php index 55f281a..5e5fd91 100644 --- a/run.php +++ b/run.php @@ -3,14 +3,16 @@ declare(strict_types=1); use LogseqGem\ArticleRender; +use LogseqGem\GemlogRender; require __DIR__ . '/vendor/autoload.php'; -$opts = getopt('o:t:', rest_index: $rest_index); +$opts = getopt('o:t:i:', rest_index: $rest_index); $rest = array_slice($argv, $rest_index); $gemlog_input_path = $rest[0] ?? null; $gemlog_output_path = $opts['o'] ?? null; +$gemlog_index_path = $opts['i'] ?? null; $trim_prefix = $opts['t'] ?? null; if (is_null($gemlog_input_path) || !file_exists($gemlog_input_path)) { @@ -29,6 +31,8 @@ $article = new LogseqGem\ArticleParser; $gemlog->load($gemlog_input_path); $titles = $gemlog->getTitles(); +$gemlog_out = new GemlogRender; + foreach ($titles as $title) { $encoded_title = str_replace('/', '%2F', $title); $article_path = $dir . '/' . $encoded_title . '.md'; @@ -56,4 +60,12 @@ foreach ($titles as $title) { $output = $render->render(__DIR__ . '/templates/article.tpl'); file_put_contents($output_path, $output); + + $gemlog_out->addArticle($title, $article->getProperties()['date'], 'log/' . $sanitized_title); +} + +if ($gemlog_index_path) { + echo "Writing gemlog index to ", $gemlog_index_path, "...\n"; + $gemlog_index = $gemlog_out->render(); + file_put_contents($gemlog_index_path, $gemlog_index); } diff --git a/src/GemlogParser.php b/src/GemlogParser.php index 75c5eaa..05cffc5 100644 --- a/src/GemlogParser.php +++ b/src/GemlogParser.php @@ -17,12 +17,11 @@ class GemlogParser extends Parser { } $text = $node->getLiteral(); - - if (substr($text, 0, 2) !== '[[' && substr($text, -2) !== ']]') { + if (!preg_match('/\[\[(.+)\]\]/', $text, $matches)) { continue; } - $titles[] = substr($text, 2, -2); + $titles[] = $matches[1]; } return $titles; diff --git a/src/GemlogRender.php b/src/GemlogRender.php new file mode 100644 index 0000000..5ac95a0 --- /dev/null +++ b/src/GemlogRender.php @@ -0,0 +1,36 @@ +articles[] = [$title, $date, $path]; + } + + public function render() { + $gemlog = []; + + usort($this->articles, fn ($a, $b) => $b[1] <=> $a[1]); + + foreach ($this->articles as $article) { + $gemlog[] = sprintf('=> %s %s %s', $article[2], $article[1], $article[0]); + } + + + $gemlog = implode("\n", $gemlog); + + return << atom.xml Atom feed + + => / home + EOF; + } +} diff --git a/tests/GemlogRenderTest.php b/tests/GemlogRenderTest.php new file mode 100644 index 0000000..5e574b8 --- /dev/null +++ b/tests/GemlogRenderTest.php @@ -0,0 +1,23 @@ +addArticle('second', '2021-01-01', 'foo.gmi'); + $render->addArticle('third', '2020-01-01', 'old.gmi'); + $render->addArticle('first', '2022-01-01', 'new.gmi'); + $this->assertStringContainsString(<< new.gmi 2022-01-01 first + => foo.gmi 2021-01-01 second + => old.gmi 2020-01-01 third + EOF, $render->render()); + } +}