Skip to content

Instantly share code, notes, and snippets.

@Utopiah
Last active August 26, 2023 15:24
Show Gist options
  • Save Utopiah/6728a2477de1c09be4150e43c439e0dd to your computer and use it in GitHub Desktop.
Save Utopiah/6728a2477de1c09be4150e43c439e0dd to your computer and use it in GitHub Desktop.
On device offline dictionary for reMarkable
#!/bin/bash
#
# opkg requirements :
# opkg install 7z git jq
#
# git clone https://github.com/wordset/wordset-dictionary/
#
# assumes ~/xochitl-data links to /home/root/.local/share/remarkable/xochitl/
#
# warning ~/epubTemplate/ is required. See instead https://gist.github.com/Utopiah/119f5c96fae048609a2091f7f3d81f53
# It's the most basic ePub one can use so for now use yours.
addEpubWithMetadata(){
cp "$1" ~/xochitl-data && echo "{'parent':'','type':'DocumentType','visibleName':'$1'}" | sed s/\'/\"/g > ~/xochitl-data/`echo $1 | sed "s/.epub//"`.metadata;
echo '{ "fileType": "epub" }' > ~/xochitl-data/`echo $1 | sed "s/.epub//"`.content;
}
getLatestRemarkableHighlights(){
cd ~/xochitl-data
grep text THE_FUTURE_OF_TEXT_2020.highlights/* | sed 's/.*: \"\(.*\)\"/\1/' # hardcoded for testing
#grep text $(ls -rt | grep -v mynewwords | tail -1 | sed "s/\..*//").highlights/* | sed 's/.*: \"\(.*\)\"/\1/'
}
getDefinitions(){
[ -f /tmp/newdefinitions ] && rm /tmp/newdefinitions
cd
for W in $(getLatestRemarkableHighlights); do
[[ $(echo $W | wc -w) -ge 2 ]] && continue
AW=$(echo $W|sed "s/[^[:alpha:]]//g")
# could remove stopwords e.g a an the ... with a for sed s///i but making sure there is a trailing space for articles
grep $AW: /tmp/definitionscache && continue
DEF=$(jq -r .$AW.meanings[0].def /home/root/wordset-dictionary-master/data/$(echo $AW|sed "s/^\(.\).*/\1/").json)
echo "<p><b>$AW:</b> $DEF</p>" >> /tmp/newdefinitions
done
[ -f /tmp/newdefinitions ] && cat /tmp/newdefinitions >> /tmp/definitionscache
}
generateNewEpub(){
cd
rm -rf newWords
cp -r epubTemplate/ newWords
head -6 newWords/OEBPS/content.xhtml > newWords/OEBPS/newcontent.xhtml
cat /tmp/definitionscache >> newWords/OEBPS/newcontent.xhtml
# these lookups can take up to 2s per word so better do some caching or try in RAM.
tail -2 newWords/OEBPS/content.xhtml >> newWords/OEBPS/newcontent.xhtml
mv newWords/OEBPS/newcontent.xhtml newWords/OEBPS/content.xhtml
cd newWords/
7z a -r my.epub mimetype META-INF/ OEBPS/
mv my.epub ~/mynewwords.epub
if [ -f /home/root/.local/share/remarkable/xochitl/mynewwords.epub ]; then
rm /home/root/.local/share/remarkable/xochitl/mynewwords.pdf #cache busting
cp ~/mynewwords.epub /home/root/.local/share/remarkable/xochitl/mynewwords.epub
else
cd
addEpubWithMetadata mynewwords.epub
systemctl restart xochitl
fi
}
# if there is no .lock file then xochitl is not active
# one can then check if there are new highlights since the last epub generation
# if not then just give up.
getDefinitions
[ -f /tmp/newdefinitions ] && generateNewEpub
@Nicolasaldn
Copy link

Hi,
Sound amazing, thanks.
do you have a tutor of how to install?
Best

@Utopiah
Copy link
Author

Utopiah commented Aug 26, 2023

Hi, thanks for the interest. It's an old, more than a year old, prototype so I can't say for sure if it still works. In fact due to reMarkable OS v3 not yet being supported by Toltec (details on toltec-dev.org ) some of the dependencies might be tricky to get. Anyway it runs via ssh on the device so the first step would be connecting this way (information on the device itself for root password) then running the script assuming everything is setup correctly (directories, dependencies, etc) as briefly mentioned in the comments at the beginning of the file. Let me know if that's sufficient.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment