Created
March 20, 2019 11:58
-
-
Save fmajestic/f56b637e488b8bfc6cd65f5f9781627b to your computer and use it in GitHub Desktop.
A simple linux note-taking script, with live markdown to pdf preview
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #! /bin/bash | |
| # A simple note-taking setup, with automatic markdown to pdf preview | |
| # | |
| # Inspired by: https://redd.it/axsbsf/ | |
| # CSS from: https://github.com/edwardtufte/tufte-css | |
| # | |
| # Dependencies: pandoc, inotify-tools, mupdf | |
| # | |
| # You can replace mupdf with any other pdf viewer, but make sure | |
| # you figure out the file reloading for it. | |
| if [ -z $1 ]; then | |
| echo -e "No file given.\nUsage:\tnotes [markdown file]" | |
| exit 0 | |
| fi | |
| mdfile=$1 | |
| # pandoc is kinda dumb and complains if the path is not hardcoded | |
| stylesheet="/home/filip/scripts/tufte-css/tufte.css" | |
| # make reusable pdf name | |
| pdf=$(basename "$mdfile" .md)".pdf" | |
| # check if using an existing file | |
| if [ ! -f "$mdfile" ]; then | |
| touch "$mdfile" | |
| fi | |
| # check if a pdf already exists, make an empty one if it doesn't | |
| if [ ! -f "$pdf" ]; then | |
| # piping stderr to null to supress pandoc warnings about "no header/title" etc. | |
| pandoc "$mdfile" -f markdown -t html5 -o "$pdf" 2> /dev/null | |
| fi | |
| # Start pdf viewer and save PID | |
| mupdf "$pdf" & | |
| mu_pid=$! | |
| # Watch markdown file for changes | |
| # While loop runs in the background, so the editor can be opened | |
| inotifywait -q -m $mdfile -e modify | | |
| while read file action; do | |
| pandoc "$mdfile" -f markdown -t html5 --css "$stylesheet" -o "$pdf" 2> /dev/null | |
| # from `man mupdf': sending a SIGHUP signal to the mupdf process | |
| # will cause the viewed file to be reloaded automatically | |
| kill -SIGHUP $mu_pid | |
| done & | |
| nano "$mdfile" | |
| # After exiting the editor, close viewer and stop watching file for changes | |
| kill %mupdf %inotifywait |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment