Skip to content

Instantly share code, notes, and snippets.

@alteist
Last active March 30, 2016 12:06
Show Gist options
  • Save alteist/5813082 to your computer and use it in GitHub Desktop.
Save alteist/5813082 to your computer and use it in GitHub Desktop.
This script exports everpad's Evernote client (https://github.com/nvbn/everpad/) database (Schema v5) to zim wiki file sctructure: Evernote/notebook/note -> zim/notebook/note.txt + notebook/note/attachments.ext
#!/bin/bash
### everpad_to_zim.sh v0.1
### Copyleft 2013 by [email protected]
###
### This script exports everpad's Evernote client (https://github.com/nvbn/everpad/)
### database (Schema v5) to zim wiki file sctructure:
### Evernote/notebook/note -> zim/notebook/note.txt + notebook/note/attachments.ext
###
### THIS SCRIPT DOES NOT:
### * copy attachments in the right place, but in the end of the note instead.
### To fix this, one have to add <en-media> tag to pandoc's source (html
### reader + markdown writer) or replace pandoc by some other parser.
### * deal with Evernote's tags and places. To add this feature, one have to
### convert 'places', 'tags' and 'notetags' tables and relations to zim data.
### See: sqlite3 everpad.5.db '.schema tags'.
### * convert zim to everpad back.
###
### KNOWN BUGS:
### * Will break if you use ';' (semicolon) in your notebook or note names/titles.
### * Will strip every symbol that is not in STRIP_REGEXP var (edit if needed).
###
### POSSIBLE BUGS:
### * May break some links and remove some backslash + special character occurences '\[#*_<>~$|:^]'
###
### USAGE:
### 0. Check STRIP_REGEXP if all needed symbols are included.
### 1. packages 'sqlite3' and 'pandoc' required, so run this in Ubuntu/Debian:
### sudo apt-get install sqlite3 pandoc
### 2. chmod +x everpad_to_zim.sh
### 3. ./everpad_to_zim.sh <EXPORT_DIR>
EXPORT_DIR=$1
DB="/home/$USER/.everpad/everpad.5.db"
### FILE NAMES SUPPOSEDLY WILL BE WINDOWS SAFE (DIDN'T CHECK YET):
STRIP_REGEXP='s/[^0-9a-Zа-Я!;,.@()\s\-]/_/g'
if [ "x$1" == "x" ]; then
cat <<-EOF
Usage: $0 <EXPORT_DIR>
Where:
<path_to_use_for_export>
The path of directory where you want your new zim
notebook to be created.
EOF
exit 1;
fi
if [ ! -f ${DB} ]; then
echo "Could not locate everpad's database for user '${USER}'";
echo "Was searching in $DB, please edit script"
exit 1;
fi
if ! hash sqlite3; then
echo "sqlite3 not found or not in PATH, please install or edit script"
exit 1;
fi
if ! hash pandoc; then
echo "pandoc not found or not in PATH, please install or edit script"
exit 1;
fi
echo "WARNING: proceeding will overwrite all files on $EXPORT_DIR. Continue? (y/n)"
read confirm
if ! test $confirm = "y"; then
  exit 0;
fi
echo "everpad -> zim to $EXPORT_DIR started..."
#rm -rf "$EXPORT_DIR"
mkdir -p "$EXPORT_DIR"
### GET NOTEBOOK NAMES
sqlite3 -separator ';' $DB "SELECT id,name from notebooks" |
sed "$STRIP_REGEXP" > /tmp/notebooks
while IFS=';' read nbid nbname; do
mkdir -p "$EXPORT_DIR/$nbname"
### GET TITLE & METADATA OF NOTES IN CURRENT NOTEBOOK
sqlite3 -separator ';' $DB "SELECT id,title,created,updated FROM notes WHERE notebook_id=$nbid" |
sed "$STRIP_REGEXP" > /tmp/notebooks.$nbid
while IFS=';' read id title created updated; do
### ALSO CREATE HTMLFOR DEBUGGING:
#sqlite3 $DB "SELECT content from notes where id=$id" > "$EXPORT_DIR/$nbname/$title.html"
### CONVERT DATES TO UNIXTIMESTAMP
cdate=`date --date="@$(($created/1000))" +%Y-%m-%dT%T%:z`
udate=`date --date="@$(($updated/1000))" +%Y%m%d%H%M.%S`
### CREATE FILE WITH ZIM HEADERS
echo -e "Content-Type: text/x-zim-wiki\nWiki-Format: zim 0.4\nCreation-Date: $cdate\n" > "$EXPORT_DIR/$nbname/$title.txt"
### EXPORT HTML FROM DB
sqlite3 $DB "SELECT content from notes where id=$id" |
### THEN CONVERT IT TO MARKDOWN (VERY CLOSE TO ZIM)
pandoc -f html -t markdown_mmd |
### FIX UNWANTED '\' (BACKSLASHES)
sed 's/\\\([#*_<>~$|:^]\)/\1/g' |
### UNESCAPE '\' (DOUBLE BACKSLASHES TO SINGLE)
sed 's/\\\\/\\/g' |
### FIX BROKEN LINKS '[link](link)'->'link' AND ADD TO FILE
sed 's/\[.*\](\(.*\))/\1/g' >> "$EXPORT_DIR/$nbname/$title.txt"
#!!!!!!! ^POSSIBLE BUGS IN REGEXPS HERE^ !!!!!!!!#
### GET ATTACHMENTS FOR THIS NOTEBOOK
sqlite3 -separator ';' $DB "SELECT file_name,file_path,guid,mime FROM resources WHERE note_id=$id" > /tmp/notebooks.files.$id
while IFS=';' read file_name file_path guid mime; do
mkdir -p "$EXPORT_DIR/$nbname/$title"
### IF THERE IS NO HUMAN-READABLE NAME, THEN THERE IS NO EXTENSION
### IN THE END OF FILE NAME, SO LET'S ADD IT USING LAST PART OF MIME TYPE
### (VERY DUMB BUT WORKS)
if test $file_name == $guid; then
file_ext=$(echo $mime | sed 's/^.*\/\(.*\)$/\1/')
file_name="$file_name.$file_ext"
fi
cp $file_path "$EXPORT_DIR/$nbname/$title/$file_name"
### INSERT ATTACHMENT TAGS IN THE END OF ZIM TEXT FILE
echo "[[./$file_name|$file_name]]: {{./$file_name}}" >> "$EXPORT_DIR/$nbname/$title.txt"
done < /tmp/notebooks.files.$id
### SET EVERNOTE LAST MODIFY DATE TO FILE LAST MODIFY DATE
touch "$EXPORT_DIR/$nbname/$title.txt" -t $udate
done < /tmp/notebooks.$nbid
done < /tmp/notebooks
zim --index $EXPORT_DIR
zim --gui $EXPORT_DIR
echo "everpad -> zim to $EXPORT_DIR finished, hope everything is ok :)"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment