forked from Nicolepcx/ibooks2evernote
-
Notifications
You must be signed in to change notification settings - Fork 2
/
tolino_notes_to_md.php
94 lines (79 loc) · 2.92 KB
/
tolino_notes_to_md.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php
/*
* Tolino notes to Markdown converter
* by Fabian Mürmann <[email protected]>
*
* based on https://github.com/jorisw/ibooks2evernote by Joris Witteman <[email protected]>
*
* Reads the notes.txt from your tolino and exports
* them, tagged with their respective book title and imported in
* separate notebooks.
*
* Usage:
*
* Move this script to the top of your personal home directory on your Mac.
* This is the folder that has your name, which the Finder opens if you
* click on the Finder icon in the Dock.
*
* To export your notes to MD Files:
*
* 1. Run the following command in the Terminal:
*
* php ./tolino_notes_to_md.php
*
* 2. Open the newly created "export"
*
*/
// Default file locations for required iBooks data
define('RESULT_DIRECTORY_NAME',"export");
define('NOTES_FILE','/Volumes/tolino/notes.txt');
// // Verify presence of notes file
if(!file_exists(NOTES_FILE)){
die("Sorry, couldn't find the notes file, please attach your tolino\n");
}
$notes = explode("\n\n-----------------------------------\n\n",file_get_contents(NOTES_FILE));
if(count($notes)==0) die("No notes found in your library. Have you added any to iBooks?\n\nIf you did on other devices than this Mac, make sure to enable iBooks notes/bookmarks syncing on all devices.");
// Create a new directory and cd into it
@mkdir(RESULT_DIRECTORY_NAME);
chdir(RESULT_DIRECTORY_NAME);
$i=0;
$books = [];
foreach($notes as $note){
$lines = explode("\n", $note);
$title = array_shift($lines);
$date = array_pop($lines);
$highlight_note = implode("\n", $lines);
$books[$title][] = [
'title' => $title,
'date' => $date,
'highlight_note' => $highlight_note
];
}
foreach ($books as $title => $notes) {
$Body = "# $title\n\n";
foreach ($notes as $note) {
// Keep some counters for commandline feedback
$i++;
$Body .= "## {$note['highlight_note']}\n";
$Body .= "###### Date: {$note['date']}\n";
// todo split into `Note on` and `Highlight on`
$Body .= "\n---\n\n";
}
echo $Body;
$filename = sanitize($title);
file_put_contents($filename.".md", $Body);
}
echo "Done! Exported $i notes into " . count($books) . " separate export files in the '".RESULT_DIRECTORY_NAME."' folder.\n\n";
function sanitize($string, $force_lowercase = true, $anal = false) {
$strip = array("~", "`", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "_", "=", "+", "[", "{", "]",
"}", "\\", "|", ";", ":", "\"", "'", "‘", "’", "“", "”", "–", "—",
"—", "–", ",", "<", ".", ">", "/", "?");
$clean = trim(str_replace($strip, "", strip_tags($string)));
$clean = preg_replace('/\s+/', "-", $clean);
$clean = ($anal) ? preg_replace("/[^a-zA-Z0-9]/", "", $clean) : $clean ;
return ($force_lowercase) ?
(function_exists('mb_strtolower')) ?
mb_strtolower($clean, 'UTF-8') :
strtolower($clean) :
$clean;
}