-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.sh
More file actions
executable file
·121 lines (100 loc) · 2.9 KB
/
script.sh
File metadata and controls
executable file
·121 lines (100 loc) · 2.9 KB
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
#!/usr/bin/env bash
#
# Convert Day One Classic plist entries to Markdown.
# Github: https://github.com/ketanketanketan/day-one-classic-to-markdown
# Author: Ketan Patel
set -euo pipefail
# Start
start_time=$(date)
echo "Started at: $start_time"
PATH_TO_ENTRIES="entries"
ENTRIES=$(ls $PATH_TO_ENTRIES)
PATH_TO_EXPORT="export"
# Create directory
# Recreate the export directory every run.
if [[ ! $(find . -type d -name "$PATH_TO_EXPORT") ]]; then
mkdir "$PATH_TO_EXPORT"
else
rm -rf "$PATH_TO_EXPORT"
mkdir "$PATH_TO_EXPORT"
fi
#######################################
# Generate Markdown files.
# Globals:
# PATH_TO_ENTRIES
# PATH_TO_EXPORT
# Arguments:
# entry
# Outputs:
# Creates Markdown file per entry.
#######################################
function generate_markdown() {
local entry
local extracted_created_date
local extracted_entry_text
local has_tags
local entry_md5
local markdown
entry="$PATH_TO_ENTRIES/$1"
extracted_created_date=$(plutil -extract "Creation Date" raw "$entry")
extracted_entry_text=$(plutil -extract "Entry Text" raw "$entry")
has_tags=false
if plutil -extract "Tags" xml1 -o /dev/null "$entry" &>/dev/null; then
has_tags=true
fi
# HAX: Is there a better way to include newlines when appending a string with a value?
local extracted_tags="tags:"
if [[ "$has_tags" == true ]]; then
local entry_tag_count
entry_tag_count=$(plutil -extract "Tags" raw "$entry")
if [[ $entry_tag_count -gt 0 ]]; then
for ((tag_index = 0; tag_index < entry_tag_count; tag_index++)); do
local extracted_tag
extracted_tag=$(plutil -extract "Tags".$tag_index raw "$entry" | tr ' ' '-')
extracted_tags="$extracted_tags
- $extracted_tag"
done
fi
fi
entry_md5=$(md5 -q "$entry")
output_file_name="${extracted_created_date//:/-}"
output_file="$PATH_TO_EXPORT/$output_file_name-$entry_md5.md"
markdown="---
date: $extracted_created_date
$extracted_tags
---
$extracted_entry_text"
echo "$markdown" > "$output_file"
echo "✓ $entry exported to $output_file"
}
#######################################
# Provides a summary of results after running the script.
# Globals:
# PATH_TO_ENTRIES
# PATH_TO_EXPORT
# Arguments:
# None
# Outputs:
# Prints statistics to console.
#######################################
function summary() {
local entries_count
local export_count
entries_count=$(ls $PATH_TO_ENTRIES | wc -l)
export_count=$(ls $PATH_TO_EXPORT | wc -l)
echo "SUMMARY"
echo "------------------------"
echo "Entries: $entries_count"
echo "Exported: $export_count"
}
for entry in ${ENTRIES[@]}; do
generate_markdown "$entry"
done
summary
# End
end_time=$(date)
echo "Completed at: $end_time"
start_time=$(date -j -f "%a %b %d %H:%M:%S %Z %Y" "$start_time" "+%s")
end_time=$(date -j -f "%a %b %d %H:%M:%S %Z %Y" "$end_time" "+%s")
DURATION=$((end_time - start_time))
echo "Duration: $DURATION seconds"