-
Notifications
You must be signed in to change notification settings - Fork 0
/
drafts.sh
210 lines (147 loc) · 4.1 KB
/
drafts.sh
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
#! /usr/bin/env bash
# drafts.sh, version 0.1, 2023-08-19
FILE="$@"
set -e
set -u
set -o pipefail
if [ ! -s "${FILE}" ] ; then
echo "Usage: $0 <DRAFTS_JSON_EXPORT>" >&2
exit 1
fi
####
ERROR=
if ! type mlr >/dev/null ; then
echo "ERROR: Install \"mlr\"" >&2
echo "#sudo port install miller" >&2
echo "#sudo brew install miller" >&2
ERROR=true
fi
if ! type jq >/dev/null ; then
echo "ERROR: Install \"jq\"" >&2
echo "#sudo port install jq" >&2
echo "#sudo brew install jq" >&2
ERROR=true
fi
if [ $ERROR ] ; then
exit 1
fi
####
split_drafts_json_export() {
mlr --json --from "$@" split -g uuid >/dev/null
return $?
}
handle_split() {
sed '1d;$d' "$@" > .tmp
#### get relevant data
UUID=$( jq --raw-output '.uuid?' < ".tmp" )
TAGS=$( jq --raw-output '.tags?' < ".tmp" | sed '1d;$d' )
CREATED=$( jq --raw-output '.created_at?' < ".tmp" )
MODIFIED=$( jq --raw-output '.modified_at?' < ".tmp" )
ACCESSED=$( jq --raw-output '.accessed_at?' < ".tmp" )
# determine file type, currently just .txt or .md
GRAMMAR=$( jq --raw-output '.languageGrammar?' < ".tmp" )
END=".txt"
case ${GRAMMAR} in
Markdown) END=".md" ;;
esac
#### output draft content
jq --unbuffered --raw-output '.content?' < ".tmp" | sed '1s/^"//g;$s/\"$//g' > DRAFTS/"${UUID}_${CREATED}${END}"
#### Create a speaking file name as hard link
create_named_links DRAFTS/"${UUID}_${CREATED}${END}"
rm -f .tmp
#### set the timestamps
touch -m -d "${MODIFIED}" DRAFTS/"${UUID}_${CREATED}"*
touch -a -d "${ACCESSED}" DRAFTS/"${UUID}_${CREATED}"*
touch -d "${CREATED}" DRAFTS/"${UUID}_${CREATED}"*
return 0
}
create_named_links() {
local file="$@"
#### generate name from first line
NAME=$( sed '/^$/d;s/^\.$//g' "${file}" | head -1 | tr -cd '[:print:]' | tr '/:;@' '_' | tr -cd '[:alnum:][:blank:]_' | tr '[:blank:]' '_' | sed 's/^-*//g;s/^_*//g;s/^ *//g' | sed 's/__*/_/g' | cut -c -39 ) || true
#### hard-links with above name, add numbers if required
APPEND=
while ! ln DRAFTS/"${UUID}_${CREATED}${END}" LINKS/"${NAME}${APPEND}${END}" 2>/dev/null ; do
if [ ! "${APPEND}" ] ; then
i=1
APPEND="_${i}"
else
i=$(( i + 1 ))
APPEND="_${i}"
fi
done
#### created file and hard link for flagged drafts
FLAGGED=$( jq --raw-output '.flagged?' < ".tmp" )
case ${FLAGGED} in
true)
touch DRAFTS/"${UUID}_${CREATED}.flagged"
ln DRAFTS/"${UUID}_${CREATED}.flagged" LINKS/"${NAME}${APPEND}.flagged"
;;
esac
#### create file for tags from the draft
if [ "${TAGS}" ] ; then
printf "%s\n" ${TAGS} > DRAFTS/"${UUID}_${CREATED}.tags"
ln DRAFTS/"${UUID}_${CREATED}.tags" LINKS/"${NAME}${APPEND}.tags"
fi
return 0
}
metadata_flag_tags() {
#### create new files
for f in LINKS/*.txt LINKS/*.md ; do
cp -p "${f}" DOCS/
done
####
for f in LINKS/*.txt LINKS/*.md ; do
N=$( sed 's/\.txt$//g;s/\.md$//g' <<< "${f}" )
BN=$( basename "${N}" )
FLAGGED=
if [ -f "${N}.flagged" ] ; then
FLAGGED=true
fi
TAGGED=
if [ -s "${N}.tags" ] ; then
TAGGED=true
fi
if [ $FLAGGED ] || [ $TAGGED ] ; then
if [[ "${f}" =~ \.txt$ ]] ; then
{ echo '---' ; [ $FLAGGED ] && echo "#flagged" ; [ $TAGGED ] && sed 's/\"//g;s/,$//g;s/^/#/g' "${N}.tags" ; echo '---' ; echo ; cat "${N}.txt" ; } > DOCS/"${BN}".txt
elif [[ "${f}" =~ \.md$ ]] ; then
{ echo '---' ; [ $FLAGGED ] && echo "#flagged" ; [ $TAGGED ] && sed 's/\"//g;s/,$//g;s/^/#/g' "${N}.tags" ; echo '---' ; echo ; cat "${N}.md" ; } > DOCS/"${BN}".md
else
echo "WARN: Unsupported file type: ${f}" >&2
fi
fi
done
for f in DOCS/* ; do
BN=$( basename "${f}" )
touch -r "LINKS/${BN}" DOCS/"${BN}"
done
return 0
}
sort_files() {
for f in DOCS/* ; do
Y=$( stat -f "%SB" "${f}" | awk '{print $NF}' )
if [ ! -d "DOCS/${Y}" ] ; then
mkdir DOCS/${Y}
fi
mv "${f}" DOCS/${Y}/
done
return 0
}
#### MAIN
for DIR in DRAFTS LINKS DOCS ; do
if [ -d "${DIR}" ] ; then
echo "ERROR: ${DIR} already exists" >&2
exit 1
fi
mkdir "${DIR}"
done
split_drafts_json_export "${FILE}"
for file in split_* ; do
handle_split "${file}"
rm "${file}"
done
#### add flag and tags as meta data in new files
metadata_flag_tags
sort_files
exit 0