-
Notifications
You must be signed in to change notification settings - Fork 10
/
enex-dump.php
executable file
·125 lines (98 loc) · 2.55 KB
/
enex-dump.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
//
// enex-dump by Steven Frank (@stevenf) <http://stevenf.com/>
//
// This script takes an Evernote export (ENEX) file as input
// and exports each individual note as a plain-text file in the
// specified output folder.
//
// All HTML formatting and attachments are stripped out.
//
// The output files are named after the title of the note.
//
// The title of the note is also included as the first line of
// the exported file.
//
// Script will attempt to create the output folder if it doesn't exist.
//
// Configure the variables below before running. Default paths are
// relative to current directory.
//
// Invoke like so:
//
// php enex-dump.php
//
// By default, we look for an input file named "My Notes.enex",
// but you can supply an additional parameter to override this:
//
// php enex-dump.php allnotes.enex
//
if ( $argc > 1 )
{
$file = $argv[1];
}
else
{
$file = "My Notes.enex"; // Path of default input file
}
$outdir = "output"; // Path of output folder
$ext = "txt"; // Extension to use for exported notes
//
$pos = 0;
$nodes = array();
@mkdir($outdir);
if ( !($fp = fopen($file, "r")) )
{
die("could not open XML input");
}
while ( $getline = fread($fp, 4096) )
{
$data = $data . $getline;
}
$count = 0;
$pos = 0;
while ( $node = getElementByName($data, "<note>", "</note>") )
{
$nodes[$count] = $node;
$count++;
$data = substr($data, $pos);
}
for ( $i = 0; $i < $count; $i++)
{
$title = cleanup(getElementByName($nodes[$i], "<title>", "</title>"));
$content = cleanup(getElementByName($nodes[$i], "<content>", "</content>"));
// Obtain note creation timestamp
$timestamp = cleanup(getElementByName($nodes[$i], "<updated>", "</updated>"));
// sanitize the / in titles for filenames
$outfile = sprintf('%s/%s.%s', $outdir, str_replace('/', '-', $title), $ext);
// echo the filename
echo $outfile . PHP_EOL;
file_put_contents($outfile, $title . "\n\n" . $content);
touch($outfile, strtotime($timestamp)); // Change output file timestamp to match note creation timestamp
}
exit;
function getElementByName($xml, $start, $end)
{
global $pos;
$startpos = strpos($xml, $start);
if ( $startpos === false )
{
return false;
}
$endpos = strpos($xml, $end);
$endpos = $endpos + strlen($end);
$pos = $endpos;
$endpos = $endpos - $startpos;
$endpos = $endpos - strlen($end);
$tag = substr($xml, $startpos, $endpos);
$tag = substr($tag, strlen($start));
return $tag;
}
function cleanup($str)
{
$str = strip_tags($str);
$str = preg_replace('/\]\]>$/', '', $str);
$str = trim($str);
$str = html_entity_decode($str);
return $str;
}