-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotes2md.bash
More file actions
executable file
·132 lines (115 loc) · 3.44 KB
/
notes2md.bash
File metadata and controls
executable file
·132 lines (115 loc) · 3.44 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
122
123
124
125
126
127
128
129
130
131
132
#!/bin/bash
#
# Notes2MD -- Convert Samsung Notes to markdown
#
# This script converts Samsung Notes to markdown. Export one or more
# notes as PDF files from inside the app (I haven't found a way to automate
# this, yet) into the ${DIR_IN} folder, then run this script. It:
#
# 1. Converts the PDF to a sequence of PNG image files (placing those
# images and the original pdf in an "assets/" subfolder)
# 2. Builds a markdown file with links to the pdf and images.
# 3. Zips the markdown file and assets subfolder into a ZIP file, placing
# the file in the ${DIR_OUT} folder
#
# Requirements:
# - poppler
#
# Usage:
# ./notes2md.sh <data>
#
# Author:
# Scott Walter <sjwalter@gmail.com>
#
# Date:
# 2024-08-21
#
# Version:
# 1.9.0
#
# License:
# MIT
#------------------------------------------------------------------------------
#
notes_2_markdown()
{
# Get the data directory
local data_root=$1
local DIR_IN="${data_root}/in"
local DIR_OUT="${data_root}/out"
local DIR_WORK="${data_root}/work"
echo "Samsung Notes to Markdown"
echo "========================="
echo "Input directory: ${DIR_IN}"
echo "Working directory: ${DIR_WORK}"
echo "Output directory: ${DIR_OUT}"
echo "========================="
# (optional) -- Clean out DIR_WORK
rm -rf ${DIR_WORK}/* && mkdir -p ${DIR_WORK}
# (optional) -- Replace all spaces with underscores in filenames
for file in ${DIR_IN}/*.pdf; do
local new_name="${file// /_}"
# if $new_name doesn't equal $file, then rename it
if [[ "$new_name" != "$file" ]]; then
mv "$file" "$new_name"
fi
done
# loop the DIR_IN directory
for pdf in ${DIR_IN}/*.pdf; do
local timestamp=$(date +"%Y-%m-%d %H:%M:%S")
local fullname="$(basename ${pdf})"
local basename="$(basename ${pdf} .pdf)"
local assets="${basename}_assets"
local data="${DIR_WORK}/${basename}"
echo "Processing ${fullname}..."
# reset processing directory (if present)
rm -rf ${data} && mkdir -p ${data}/${assets}
# move file to processing directory
mv ${pdf} ${data}/${assets}
# convert to png file(s)
echo " -> generating png files..."
pdftoppm -png ${data}/${assets}/${fullname} ${data}/${assets}/${basename}
# generate markdown file
echo " -> generating markdown..."
cat << EOF > ${data}/${basename}.md
---
title: $basename
slug: $basename
created: $timestamp
updated: $timestamp
---
# $basename
## Original PDF file
* [${fullname}](./${assets}/${fullname})
## Pages
EOF
# loop through generated png files, build list
for png in ${data}/${assets}/*.png; do
local fname="$(basename $png)"
local bname="$(basename $png .png)"
cat << EOF >> ${data}/${basename}.md
* [${bname}](./${assets}/${fname})
[](./${assets}/${fname})
EOF
done
# zip up the bundle into DIR_OUT. It creates a
# subshell, setting the root directory of the
# zip file to ${data}.
echo " -> zipping..."
(cd ${data} && zip -qq -r ../../out/${basename}.zip .)
# clean up work diretory
rm -rf ${data}
echo " -> done!"
done
echo "========================="
echo "ALL DONE!"
}
###
# MAIN
###
if [ $# -eq 0 ]; then
echo "Usage: $0 <data_root>"
exit 1
else
notes_2_markdown $1
fi