-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess-keep-json.php
67 lines (57 loc) · 1.96 KB
/
process-keep-json.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php
/**
* Created by: justinmaurer
* Date: 1/25/22
* Time: 8:26 PM
*/
use KeepToObsidian\FileHelpers;
use KeepToObsidian\KeepJSONMarkdownConverter;
use KeepToObsidian\MocList;
use KeepToObsidian\Starred;
require 'vendor/autoload.php';
if (!isset($argv) || !is_array($argv) || count($argv) < 2) {
exit("No file path given\n");
}
// $argv[0] is the name of the script
$sourcePath = $argv[1];
if (!is_dir($sourcePath)) {
exit('Not a directory: ' . $sourcePath . PHP_EOL);
}
$sourcePath = FileHelpers::trailingSlashIt($sourcePath);
$mocList = new MocList();
$starred = new Starred();
FileHelpers::assertDirectoryExists($sourcePath . 'md/');
FileHelpers::assertDirectoryExists($sourcePath . 'md/.obsidian');
FileHelpers::assertDirectoryExists($sourcePath . 'md/' . KeepJSONMarkdownConverter::ARCHIVE_DIR);
FileHelpers::assertDirectoryExists($sourcePath . 'md/' . KeepJSONMarkdownConverter::ATTACHMENT_DIR);
$files = glob($sourcePath . "*.json");
foreach ($files as $file) {
$note = file_get_contents($file);
try {
$note = json_decode($note, false, 512, JSON_THROW_ON_ERROR);
if (!($note instanceof \stdClass)) {
throw new \RuntimeException('not a json document');
}
$converter = new KeepJSONMarkdownConverter($note);
if ($converter->isTrashed) {
continue;
}
file_put_contents($sourcePath . 'md/' . $converter->filename, $converter->document);
foreach ($converter->getFilesToCopy($sourcePath) as [$srcFilename, $dstFilename]) {
copy(
$sourcePath . $srcFilename,
$sourcePath . 'md/' . $dstFilename
);
}
if ($converter->isPinned) {
$starred->addNote($converter);
}
$mocList->addNote($converter);
} catch (\Throwable $e) {
echo $e->getMessage();
echo 'File: ' . $file;
}
}
$starred->write($sourcePath . 'md/');
$mocList->writeAll($sourcePath . 'md/');
exit("All done\n");