-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxml-md-converter.php
280 lines (247 loc) · 8.3 KB
/
xml-md-converter.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
<!DOCTYPE html>
<html lang="en">
<head>
<title>XML to MD Converter</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<?php
/**
* set up in hosts as day1x.dev
*
* Notes:
* - this only works for the original DayOne app which stored entries as xml files
* - extension of entries must be changed from .doentry to .xml, use cli or automater
* - Destination folder must have write permissions
*
*/
$path = __dir__.'/';
//relative path to your php file where the DayOne Entries are located.
$dirsource = 'entries';
$dirdest = 'md-converted';
$headerEnclosedBy = array('[',']');
$entryExt = 'doentry';
define('NICE_DATE_FORMAT', 'H:i j M Y');
define('FILENAME_DATE_FORMAT', 'Y-m-d_His');
define('TITLE_HEADER_LEVEL', '##');
define('HEADER_IS_FILE_NAME', false);
$xmlfiles = scandir($path.$dirsource);
//print_r($xmlfiles);
$fileCount = 0;
foreach ($xmlfiles as $file) {
$fileparts = pathinfo($file);
$fullfile = $path.$dirsource.'/'.$file;
//print_r($fullfile);
if (file_exists($fullfile) && $fileparts['extension'] == $entryExt) {
//load xml
$xml = simplexml_load_file($fullfile);
$arrXml = parseDict($xml->dict);
//var_dump($arrXml);
date_default_timezone_set($arrXml['Time Zone']);
$fulldestpath = $path.$dirdest;
$result = markdownFile($arrXml, $fulldestpath, $headerEnclosedBy);
$fileCount++;
echo "Wrote file #{$fileCount}: {$result}<br>";
//only write one file for now
//exit;
}
}
echo "<h4>Done ({$fileCount} files written)</h4>";
function resetKey() {
return 'unset';
}
/** returns array **/
function parseDict($dict) {
$arrXml = array();
$key = resetKey();
$subDictNum = 0;
foreach($dict->children() as $value) {
switch ($value->getName()) {
case 'key' :
$key = (string)$value;
break;
case 'dict' :
$arrXml[$key] = parseDict($dict->children()->dict[$subDictNum++]);
$key = resetKey();
break;
case 'integer' :
$arrXml[$key] = intval($value);
$key = resetKey();
break;
case 'real' :
$arrXml[$key] = floatval($value);
$key = resetKey();
break;
case 'false' :
$arrXml[$key] = 0;
$key = resetKey();
break;
case 'true' :
$arrXml[$key] = 1;
$key = resetKey();
break;
case 'array' :
$tagArray = array();
foreach ($dict->array->children() as $value) {
$tagArray[] = (string) $value;
}
$arrXml[$key] = $tagArray;
$key = resetKey();
break;
case 'string' :
case 'date' :
default :
$arrXml[$key] = (string)$value;
$key = resetKey();
}
}
return $arrXml;
}
function niceDate($strTime, $format='j M Y @ H:i') {
$time = strtotime($strTime);
return date($format, $time);
}
function markdownFile($arrXml, $fulldestpath, $headerEnclosedBy) {
$mdFilename = niceDate($arrXml['Creation Date'], FILENAME_DATE_FORMAT). '.md';
$content = $arrXml['Entry Text'];
//try to get headline
$headerText = '';
if (count($headerEnclosedBy) > 0 && substr($content, 0, 1) == $headerEnclosedBy[0]) {
$endPos = strpos($content, $headerEnclosedBy[1]);
$headerText = substr($content, 1, $endPos-1);
//set file name to header text
if (HEADER_IS_FILE_NAME) {
$mdFilename = alphanumericWithSpaces($headerText) . '.md';
}
$content = trim(substr($content, $endPos+1));
}
$strTags = '';
if (count($arrXml['Tags']) > 0 ) {
$strTags = implode(', ', $arrXml['Tags']);
$strTags = "_{$strTags}_";
}
// echo '<h2>Date Raw: ' .$arrXml['Creation Date'] . '</h2>';
// echo '<h2>Filename: ' .$mdFilename . '</h2>';
// echo '<h2>Date: ' . niceDate($arrXml['Creation Date'], NICE_DATE_FORMAT) . '</h2>';
// echo '<h2>Sunrise: ' . niceDate($arrXml['Weather']['Sunrise Date'], NICE_DATE_FORMAT) . '</h2>';
// echo '<h2>Sunset: ' . niceDate($arrXml['Weather']['Sunset Date'], NICE_DATE_FORMAT) . '</h2>';
// echo '<h2>Header: ' .$headerText . '</h2>';
// echo '<p>String: ' .nl2br($content) . '</p>';
$headerLevel = TITLE_HEADER_LEVEL;
//Initialize $niceDate
$niceDate = niceDate($arrXml['Creation Date'], NICE_DATE_FORMAT);
//Initialize $starred
$starred = '';
if ($arrXml['Starred'] == 1) {
$starred = '';
}
//Location
$placename = '';
if (isset($arrXml['Location']['Place Name']) && $arrXml['Location']['Place Name'] != '') {
$placename = "{$arrXml['Location']['Place Name']}, ";
}
$locality = '';
if (isset($arrXml['Location']['Locality']) && $arrXml['Location']['Locality'] != '') {
$locality = "{$arrXml['Location']['Locality']}, ";
}
$area = '';
if (isset($arrXml['Location']['Administrative Area']) && $arrXml['Location']['Administrative Area'] != '') {
$area = "{$arrXml['Location']['Administrative Area']}, ";
}
$country = '';
if (isset($arrXml['Location']['Country']) && $arrXml['Location']['Country'] != '') {
$country = "{$arrXml['Location']['Country']}, ";
}
$longitude = '';
if (isset($arrXml['Location']['Longitude']) && $arrXml['Location']['Longitude'] != '') {
$longitude = "Longitude: {$arrXml['Location']['Longitude']} ";
}
$latitude = '';
if (isset($arrXml['Location']['Latitude']) && $arrXml['Location']['Latitude'] != '') {
$latitude = "Latitude: {$arrXml['Location']['Latitude']} ";
}
//Weather
$tempF = '';
if (isset($arrXml['Weather']['Fahrenheit']) && $arrXml['Weather']['Fahrenheit'] != '') {
$tempF = "{$arrXml['Weather']['Fahrenheit']}F/";
}
$tempC = '';
if (isset($arrXml['Weather']['Celsius']) && $arrXml['Weather']['Celsius'] != '') {
$tempC = "{$arrXml['Weather']['Celsius']}C ";
}
$weatherDesc = '';
if (isset($arrXml['Weather']['Description']) && $arrXml['Weather']['Description'] != '') {
$weatherDesc = "{$arrXml['Weather']['Description']}";
}
$sunrise = '';
if (isset($arrXml['Weather']['Sunrise Date']) && $arrXml['Weather']['Sunrise Date'] != '') {
$sunrise = "Sunrise: {$arrXml['Weather']['Sunrise Date']} ";
}
$sunset = '';
if (isset($arrXml['Weather']['Sunset Date']) && $arrXml['Weather']['Sunset Date'] != '') {
$sunset = "Sunset: {$arrXml['Weather']['Sunset Date']}";
}
$pressure = '';
if (isset($arrXml['Weather']['Pressure MB']) && $arrXml['Weather']['Pressure MB'] != '') {
$pressure = "Pressure: {$arrXml['Weather']['Pressure MB']}MB ";
}
$humidity = '';
if (isset($arrXml['Weather']['Relative Humidity']) && $arrXml['Weather']['Relative Humidity'] != '') {
$humidity = "Humidity: {$arrXml['Weather']['Relative Humidity']}%";
}
$visibility = '';
if (isset($arrXml['Weather']['Visibility KM']) && $arrXml['Weather']['Visibility KM'] != '') {
$visibility = 'Visibility: ' . number_format($arrXml['Weather']['Visibility KM'], 1) . 'KM ';
}
$windchill = '';
if (isset($arrXml['Weather']['Wind Chill Celsius']) && $arrXml['Weather']['Wind Chill Celsius'] != '') {
$windchill = "Wind Chill: {$arrXml['Weather']['Wind Chill Celsius']}C";
}
$locationWeatherSummary = "{$placename}{$locality}{$area}{$country}{$tempF}{$tempC}{$weatherDesc}";
if ($locationWeatherSummary != '') {
$locationWeatherSummary = "\t{$locationWeatherSummary}\n\t";
}
$longLatSummary = "{$longitude}{$latitude}";
if ($longLatSummary != '') {
$longLatSummary = "\n\t{$longLatSummary}";
}
$sunSummary = "{$sunrise}{$sunset}";
if ($sunSummary != '') {
$sunSummary = "\n\t{$sunSummary}";
}
$pressureHumiditySummary = "{$pressure}{$humidity}";
if ($pressureHumiditySummary != '') {
$pressureHumiditySummary = "\n\t{$pressureHumiditySummary}";
}
$visibilityWindchillSummary = "{$visibility}{$windchill}";
if ($visibilityWindchillSummary != '') {
$visibilityWindchillSummary = "\n\t{$visibilityWindchillSummary}";
}
$strMd = <<<EOT
{$headerLevel}{$headerText}
{$starred} *{$niceDate}* {$strTags}
{$content}
{$locationWeatherSummary}{$longLatSummary}{$sunSummary}{$pressureHumiditySummary}{$visibilityWindchillSummary}
Creation Date UTC: {$arrXml['Creation Date']}
UUID: {$arrXml['UUID']}
EOT;
//file path
$fulldestfile = $fulldestpath.'/'.$mdFilename;
$filetime = strtotime($arrXml['Creation Date']);
//create and write file
file_put_contents($fulldestfile, $strMd);
//set file time to creation date
touch($fulldestfile, $filetime);
return $fulldestfile;
}
/*
* @description Allows spaces, but other non-alphanumeric characters are stripped.
*/
function alphanumericWithSpaces($str) {
$strSafe = preg_replace("/[^A-Za-z0-9 ]/", '', $str);
return $strSafe;
}
?>
</body>
</html>