another_notes_to_markdown.pl 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #!/usr/bin/perl
  2. # Author: Trizen
  3. # Date: 27 April 2024
  4. # https://github.com/trizen
  5. # Convert JSON data from the Android app "Another notes" to Markdown format.
  6. # See also:
  7. # https://github.com/maltaisn/another-notes-app
  8. use 5.036;
  9. use JSON qw(from_json);
  10. use File::Slurper qw(read_text);
  11. binmode(STDOUT, ':utf8');
  12. binmode(STDERR, ':utf8');
  13. my $json_file = $ARGV[0] // die "usage: $0 [input.json]\n";
  14. my $json = read_text($json_file);
  15. my $notes = from_json($json)->{notes} // die "Invalid input file";
  16. sub markdown_escape($str) {
  17. $str =~ s/([-*_`\\()\[\]#])/\\$1/gr;
  18. }
  19. foreach my $key (1 .. 1e6) {
  20. if (exists $notes->{$key}) {
  21. my $note = $notes->{$key};
  22. my $title = markdown_escape($note->{title});
  23. my $content = markdown_escape(unpack('A*', $note->{content}));
  24. if ($title !~ /\S/) {
  25. $title = '...';
  26. }
  27. say "# $title\n";
  28. if ($note->{type} == 0) {
  29. say(($content =~ s/\R/\n\n/gr), "\n");
  30. }
  31. elsif ($note->{type} == 1) {
  32. my $meta = from_json($note->{metadata});
  33. my @list = split(/\R/, $content);
  34. my $checked = $meta->{checked};
  35. foreach my $i (0 .. $#list) {
  36. say "- [", ($checked->[$i] ? 'x' : ' '), "] $list[$i]\n";
  37. }
  38. }
  39. else {
  40. warn "Unknown note type: $note->{type}\n";
  41. }
  42. }
  43. }