-
Notifications
You must be signed in to change notification settings - Fork 34
/
another_notes_to_markdown.pl
60 lines (43 loc) · 1.39 KB
/
another_notes_to_markdown.pl
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
#!/usr/bin/perl
# Author: Trizen
# Date: 27 April 2024
# https://github.com/trizen
# Convert JSON data from the Android app "Another notes" to Markdown format.
# See also:
# https://github.com/maltaisn/another-notes-app
use 5.036;
use JSON qw(from_json);
use File::Slurper qw(read_text);
binmode(STDOUT, ':utf8');
binmode(STDERR, ':utf8');
my $json_file = $ARGV[0] // die "usage: $0 [input.json]\n";
my $json = read_text($json_file);
my $notes = from_json($json)->{notes} // die "Invalid input file";
sub markdown_escape($str) {
$str =~ s/([-*_`\\()\[\]#])/\\$1/gr;
}
foreach my $key (1 .. 1e6) {
if (exists $notes->{$key}) {
my $note = $notes->{$key};
my $title = markdown_escape($note->{title});
my $content = markdown_escape(unpack('A*', $note->{content}));
if ($title !~ /\S/) {
$title = '...';
}
say "# $title\n";
if ($note->{type} == 0) {
say(($content =~ s/\R/\n\n/gr), "\n");
}
elsif ($note->{type} == 1) {
my $meta = from_json($note->{metadata});
my @list = split(/\R/, $content);
my $checked = $meta->{checked};
foreach my $i (0 .. $#list) {
say "- [", ($checked->[$i] ? 'x' : ' '), "] $list[$i]\n";
}
}
else {
warn "Unknown note type: $note->{type}\n";
}
}
}