-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy path_joplin-functions.sh
executable file
·330 lines (283 loc) · 8.51 KB
/
_joplin-functions.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
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#!/usr/bin/env bash
#---
## Switch to given Joplin notebook
## Usage: switchToNotebook notebook
#---
function switchToNotebook {
joplin use "$1"
if [[ $? -ne 0 ]] ; then
if [[ "$AUTO_CREATE_NOTEBOOK" == "true" ]]; then
echo "Info: notebook $1 not found - creating automatically"
joplin mkbook "$1"
joplin use "$1"
else
echo "Warning: notebook $1 not found - using default $DEFAULT_NOTEBOOK instead"
joplin use "$DEFAULT_NOTEBOOK"
if [[ $? -ne 0 ]] ; then
echo "Error: default notebook $DEFAULT_NOTEBOOK not found - creating automatically"
joplin mkbook "$DEFAULT_NOTEBOOK"
joplin use "$DEFAULT_NOTEBOOK"
fi
fi
fi
}
#---
## Create a new Joplin note
## Usage: createNewNote unique-name
## @return the note id
#---
function createNewNote {
joplin mknote "$1"
local LS_OUTPUT=(`joplin ls -l "$1"`)
echo ${LS_OUTPUT[0]}
}
#---
## Usage: setNoteTitle note-id title
#---
function setNoteTitle {
local TITLE="$2"
if [[ "${TITLE}x" == "x" ]] ; then
TITLE="${DEFAULT_TITLE_PREFIX} - `date`"
fi
echo "Set title to: $TITLE"
joplin set "$1" title "$TITLE"
}
#---
## Usage: setNoteTags note-id tags
#---
function setNoteTags {
for T in $2 ; do
echo "Add tag: $T"
joplin tag add "$T" "$1"
done
}
#---
## Usage: extractMailParts mail-file dest-dir
#---
function extractMailParts {
ripmime -i ${1} --mailbox -d ${2}
}
#---
## Usage: determineMailPartType mail-part-file
#---
function determineMailPartType {
if [[ "$1" =~ ^.*\/textfile[0-9]*$ ]] ; then
echo "CONTENT"
elif [[ "$1" =~ ^.*\.txt$ ]] ; then
echo "TXT"
elif [[ "$1" =~ ^.*\.md$ ]] ; then
echo "TXT"
elif [[ "$1" =~ ^.*\.pdf$ ]] ; then
echo "PDF"
elif [[ "$1" =~ ^.*\.jpg$ ]] ; then
echo "IMG"
elif [[ "$1" =~ ^.*\.jpeg$ ]] ; then
echo "IMG"
elif [[ "$1" =~ ^.*\.png$ ]] ; then
echo "IMG"
elif [[ "$1" =~ ^.*\.gif$ ]] ; then
echo "IMG"
else
echo "UNKNOWN"
fi
}
#---
## Usage: createNoteBodyFromTextParts mail-parts-dir
#---
function getNoteBodyFromTextParts {
find "$1" -type f -print0 | sort -z | while read -d $'\0' F; do
local T=`determineMailPartType "${F}"`
if [[ "$T" == "CONTENT" ]] ; then
cat ${F}
fi
done
}
#---
## Usage: setNoteBodyFromTextParts note-id mail-parts-dir
#---
function setNoteBodyFromTextParts {
local BODY=`getNoteBodyFromTextParts "${2}"`
echo "Setting body"
joplin set "$1" body "$BODY"
}
#---
## Usage: attachFile note-id file
#---
function attachFile {
echo "Attach file `basename "$2"`"
joplin attach "$1" "$2"
}
#---
## Usage: attachTextFromFile note-id text-file
#---
function attachTextFromFile {
echo "Add text from `basename "$2"`"
local ORIG_BODY=`joplin cat "$1" | tail -n +2`
local TXT=`cat "$2"`
joplin set "$1" body "`echo -e "${ORIG_BODY}\n${TXT}"`"
}
#---
## Usage: addPdfThumbnails note-id pdf-file
#---
function addPdfThumbnails {
local TEMP_DIR=`mktemp -d`
pdftoppm -scale-to 300 -png "$2" "$TEMP_DIR/thumb"
find "$TEMP_DIR" -type f -name "thumb-*.png" -print0 | sort -z | while read -d $'\0' T
do
echo "Add pdf thumbnail: $T"
joplin attach "$1" "$T"
rm "$T"
done
rmdir ${TEMP_DIR}
}
#---
## Usage: addPdfThumbnails note-id
#---
function addLastImageAsLink {
echo "Add explicit image link"
local OLD_BODY=`joplin cat "$1" | tail -n +2`
local LAST_LINE=`echo "$OLD_BODY" | tail -n 1`
local LINK=`echo "$LAST_LINE" | cut -c 2-999`
joplin set "$1" body "`echo -e "${OLD_BODY}\n${LINK}\n"`"
}
#---
## Usage: addAttachmentFromFile note-id file
#---
function addAttachmentFromFile {
local T=`determineMailPartType "$2"`
if [[ "$T" == "TXT" ]]; then
attachTextFromFile "$1" "$2"
elif [[ "$T" == "PDF" ]] ; then
addPdfThumbnails "$1" "$2"
attachFile "$1" "$2"
elif [[ "$T" == "IMG" ]] ; then
attachFile "$1" "$2"
addLastImageAsLink "$1"
elif [[ "$T" == "UNKNOWN" ]] ; then
attachFile "$1" "$2"
else
:
fi
}
#---
## Usage: addAttachmentsFromFileParts note-id mail-parts-dir
#---
function addAttachmentsFromFileParts {
find "$2" -type f -print0 | sort -z | while read -d $'\0' F; do
addAttachmentFromFile "$1" "$F"
done
}
#---
## Usage: addPdfFulltext note-id pdf-file
#---
function addPdfFulltext {
echo "Add pdf fulltext for `basename "$2"`"
local ORIG_BODY=`joplin cat "$1" | tail -n +2`
local TXT=`pdftotext -raw -nopgbrk "$2" -`
joplin set "$1" body "`echo -e "${ORIG_BODY}\n\n---\n${TXT}"`"
}
#---
## Usage: addImageFulltext note-id image-file
#---
function addImageFulltext {
echo "Add image fulltext for `basename "$2"`"
local ORIG_BODY=`joplin cat "$1" | tail -n +2`
local TXT=`tesseract -l deu+eng "$2" -`
joplin set "$1" body "`echo -e "${ORIG_BODY}\n\n---\n${TXT}"`"
}
#---
## Usage: addFulltextFromFile note-id file
#---
function addFulltextFromFile {
local T=`determineMailPartType "$2"`
if [[ "$T" == "PDF" ]] ; then
addPdfFulltext "$1" "$2"
elif [[ "$T" == "IMG" ]] ; then
addImageFulltext "$1" "$2"
else
:
fi
}
#---
## Usage: addFulltextFromFileParts note-id mail-parts-dir
#---
function addFulltextFromFileParts {
find "$2" -type f -print0 | sort -z | while read -d $'\0' F; do
addFulltextFromFile "$1" "$F"
done
}
#---
## Usage: setCreationDate note-id date
#---
function setCreationDate {
if [[ "$2" != "" ]]; then
local DATINT=`date -jf "%Y-%m-%d %H.%M.%S" "$2" +%s`
echo "Set creation date $2 (${DATINT}000)"
joplin set "$1" user_created_time ${DATINT}000
fi
}
## Usage: addNewNoteFromMailFile mail-file
function addNewNoteFromMailFile {
local FILE="$1"
local NOTE_NAME=`basename "$FILE"`
local SUBJECT=`getMailSubject "$FILE"`
local TITLE=`getTitleFromSubject "$SUBJECT"`
local TAGS=`getTagsFromSubject "$SUBJECT"`
local NOTEBOOK=`getNotebookFromSubject "$SUBJECT" "$DEFAULT_NOTEBOOK"`
switchToNotebook "${NOTEBOOK}"
echo "Create new note with name '${NOTE_NAME}' in '${NOTEBOOK}'"
local NOTE_ID=`createNewNote "${NOTE_NAME}"`
echo "New note created - ID is: $NOTE_ID"
setNoteTitle "$NOTE_ID" "$TITLE"
setNoteTags "$NOTE_ID" "$TAGS"
local TEMP_DIR=`mktemp -d`
echo "Using temp dir: $TEMP_DIR"
extractMailParts "${FILE}" "${TEMP_DIR}"
setNoteBodyFromTextParts "$NOTE_ID" "${TEMP_DIR}"
addAttachmentsFromFileParts "$NOTE_ID" "${TEMP_DIR}"
addFulltextFromFileParts "$NOTE_ID" "${TEMP_DIR}"
echo "Removing temp dir: $TEMP_DIR"
rm -r ${TEMP_DIR}
}
## Usage: getCreationDateFromFilename filename
function getCreationDateFromFilename {
echo -n "$1" | python3 -c 'import sys,re; s=sys.stdin.read(); s=re.search("^(\d\d\d\d-\d\d-\d\d)(?:\s(\d\d.\d\d.\d\d)\s)?",s); print() if (s is None) else print(s.group(1)+" 00.00.00") if (s.group(2) is None) else print(s.group(1)+" "+s.group(2));'
}
## Usage: getTitleFromFilename filename
function getTitleFromFilename {
echo -n "$1" | python3 -c 'import sys,re; s=sys.stdin.read(); s=re.search("^(?:\d\d\d\d-\d\d-\d\d(?:\s\d\d.\d\d.\d\d)?\s?-?\s?)?(.+?)(?:\[\s*\w+(?:\s+\w+)*\s*\])?(?:\.\w+)*?$",s); print(s.group(1)) if s else print();'
}
## Usage: getNotebookFromFilename filename default-notebook
function getNotebookFromFilename {
# todo
echo "$2"
}
## Usage: getTagsFromFilename filename
function getTagsFromFilename {
echo -n "$1" | python3 -c 'import sys,re; s=sys.stdin.read(); s=re.search("^.*\[\s*(\w+(?:\s+\w+)*)\s*\](?:\.\w+)*?$",s); print(s.group(1)) if s else print();'
}
## Usage: addNewNoteFromGenericFile notebook file
function addNewNoteFromGenericFile {
local FILE="$2"
local FILE_NAME=`basename "$FILE"`
if [[ "$FILE_NAME" =~ ^\..*$ ]] ; then
echo "Ignore hidden file $FILE_NAME"
return 1
fi
local TITLE=`getTitleFromFilename "$FILE_NAME"`
local TAGS=`getTagsFromFilename "$FILE_NAME"`
local NOTEBOOK="$1"
if [[ "${NOTEBOOK}x" == "x" ]]; then
NOTEBOOK="$DEFAULT_NOTEBOOK"
fi
local CREATION_DATE=`getCreationDateFromFilename "$FILE_NAME"`
switchToNotebook "${NOTEBOOK}"
echo "Create new note with name '${FILE_NAME}' in '${NOTEBOOK}'"
local NOTE_ID=`createNewNote "${FILE_NAME}"`
echo "New note created - ID is: $NOTE_ID"
setNoteTitle "$NOTE_ID" "$TITLE"
setNoteTags "$NOTE_ID" "$TAGS"
setCreationDate "$NOTE_ID" "$CREATION_DATE"
addAttachmentFromFile "$NOTE_ID" "$FILE"
addFulltextFromFile "$NOTE_ID" "$FILE"
}