2022-05-21 15:47:50 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
require __DIR__ . '/vendor/autoload.php';
|
|
|
|
|
2022-05-21 21:50:23 +00:00
|
|
|
$opts = getopt('o:t:', rest_index: $rest_index);
|
2022-05-21 20:21:48 +00:00
|
|
|
$rest = array_slice($argv, $rest_index);
|
2022-05-21 15:47:50 +00:00
|
|
|
|
2022-05-21 20:21:48 +00:00
|
|
|
$gemlog_input_path = $rest[0] ?? null;
|
|
|
|
$gemlog_output_path = $opts['o'] ?? null;
|
2022-05-21 21:50:23 +00:00
|
|
|
$trim_prefix = $opts['t'] ?? null;
|
2022-05-21 15:47:50 +00:00
|
|
|
|
2022-05-21 20:21:48 +00:00
|
|
|
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) {
|
2022-05-21 21:50:23 +00:00
|
|
|
$encoded_title = str_replace('/', '%2F', $title);
|
|
|
|
$article_path = $dir . '/' . $encoded_title . '.md';
|
2022-05-21 20:21:48 +00:00
|
|
|
$article->load($article_path);
|
|
|
|
$gemtext = $article->convert();
|
|
|
|
|
|
|
|
if (!$article->isPublished() || empty($gemtext)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-05-21 21:50:23 +00:00
|
|
|
$sanitized_title = $title;
|
2022-05-21 20:21:48 +00:00
|
|
|
|
2022-05-21 21:50:23 +00:00
|
|
|
if ($trim_prefix && strpos($sanitized_title, $trim_prefix) === 0) {
|
|
|
|
$sanitized_title = substr($sanitized_title, strlen($trim_prefix));
|
|
|
|
}
|
|
|
|
|
|
|
|
$sanitized_title = strtolower($sanitized_title);
|
|
|
|
$sanitized_title = str_replace([' ', ','], ['-'], $sanitized_title);
|
|
|
|
$sanitized_title .= '.gmi';
|
|
|
|
|
|
|
|
$output_path = $gemlog_output_path . '/' . $sanitized_title;
|
2022-05-21 20:21:48 +00:00
|
|
|
|
2022-05-21 21:50:23 +00:00
|
|
|
echo "Writing to ", $sanitized_title, "...\n";
|
2022-05-21 20:21:48 +00:00
|
|
|
|
|
|
|
$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);
|
2022-05-21 15:47:50 +00:00
|
|
|
}
|