From 6809268fb190b0fc7fc233ad07d5ee2a33464a0f Mon Sep 17 00:00:00 2001 From: Annika Backstrom Date: Sat, 21 May 2022 21:21:48 +0100 Subject: [PATCH] Complete run.php using new classes --- run.php | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 50 insertions(+), 6 deletions(-) diff --git a/run.php b/run.php index 8f6e2d9..40c6e2f 100644 --- a/run.php +++ b/run.php @@ -4,12 +4,56 @@ declare(strict_types=1); require __DIR__ . '/vendor/autoload.php'; -$parser = new LogseqGem\Parser; +$opts = getopt('o:', rest_index: $rest_index); +$rest = array_slice($argv, $rest_index); -$page = file_get_contents("/Users/annika/sync/BTSync/logseq/pages/Gemlog.md"); -$page = file_get_contents("/Users/annika/sync/BTSync/logseq/pages/Publishing to the Gemlog with logseq.md"); +$gemlog_input_path = $rest[0] ?? null; +$gemlog_output_path = $opts['o'] ?? null; -$document = $parser->parse($page); -foreach ($document->iterator() as $node) { - echo 'Current node: ' . get_class($node) . "\n"; +if (is_null($gemlog_input_path) || !file_exists($gemlog_input_path)) { + throw new \Exception('Please provide an input gemlog file listing some articles. Got: ' . $gemlog_input_path); +} + +if (is_null($gemlog_output_path) || !is_dir($gemlog_output_path)) { + throw new \Exception('Please provide a destination directory using -o'); +} + +$dir = dirname($gemlog_input_path); + +$gemlog = new LogseqGem\GemlogParser; +$article = new LogseqGem\ArticleParser; + +$gemlog->load($gemlog_input_path); +$titles = $gemlog->getTitles(); + +foreach ($titles as $title) { + $article_path = $dir . '/' . $title . '.md'; + $article->load($article_path); + $gemtext = $article->convert(); + + if (!$article->isPublished() || empty($gemtext)) { + continue; + } + + $sanitize_title = strtolower($title); + $sanitize_title = str_replace(' ', '-', $sanitize_title); + $sanitize_title .= '.gmi'; + + $output_path = $gemlog_output_path . '/' . $sanitize_title; + + echo "Writing to ", $sanitize_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 + ); + + file_put_contents($output_path, $output); }