Skip to content

Instantly share code, notes, and snippets.

@falgon
Last active July 5, 2019 16:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save falgon/5d3fe6838e7f6cb4090823df417680e5 to your computer and use it in GitHub Desktop.
Save falgon/5d3fe6838e7f6cb4090823df417680e5 to your computer and use it in GitHub Desktop.
My blog live reviewer
#!/usr/bin/env python3
from livereload import Server, shell
from sys import argv
class live_view:
def __init__(self):
self.sv = Server()
self.is_run = False
def __enter__(self):
return self
def run(self, is_open):
self.is_run = True
self.sv.watch('./content', shell('make html -f ./Makefile'))
if is_open:
self.sv.serve(open_url_delay=1, debug=False, root='./output', port=8000, host='localhost')
else:
self.sv.serve(root='./output', port=8000, host='localhost')
def __exit__(self, exception_t, exception_v, traceback):
if self.is_run == False:
self.run(False)
def run(is_open):
with live_view() as lv:
lv.run(is_open)
def main():
if len(argv) == 2 and argv[1] == 'browse':
run(True)
elif len(argv) >= 2:
print ('Usage: {} [OPTION]\nOPTION:\n\tbrowse -- Opening new page in default browser.'.format(argv[0]))
else:
run(False)
if __name__ == '__main__':
main()
#!/bin/bash
BASE_DIR=$(pwd)
LIVE_PREV_PID=${BASE_DIR}/live_preview.pid
GREEN=$(tput setaf 2)
RED=$(tput setaf 1)
NORMAL=$(tput sgr0)
COLUMNS=$(tput cols)
alive() {
kill -0 $1 >/dev/null 2>&1
}
failed_message() {
failed="[Failed]"
cl_width=$(($COLUMNS-$1))
printf '%s%*s%s\n' "$RED" $cl_width "$failed" "$NORMAL"
}
ok_message() {
ok="[OK]"
cl_width=$(($COLUMNS-$1))
printf '%s%*s%s\n' "$GREEN" $cl_width "$ok" "$NORMAL"
}
usage() {
echo -e "Usage: $0 [OPTION]\nOPTION"
echo -e "\tstart [start_option] -- start live preview on background\n\tstart_option:\n\t\tbrowse -- Opening new page in default browser\n"
echo -e "\tstop -- stop live preview on background\n"
exit 1
}
shut_down() {
mes="shutting down live preview..."
echo -n $mes
if [ ! -e $LIVE_PREV_PID ]; then
failed_message ${#mes}
echo "not found pid file in ${BASE_DIR}"
exit 1
fi
pid=$(cat $LIVE_PREV_PID)
if alive $pid; then
kill $pid
while alive $pid; do
sleep 0.5
done
ok_message ${#mes}
else
failed_message ${#mes}
echo "stale pid detected, cleaning"
fi
rm $LIVE_PREV_PID
}
start_up() {
if [ -e $LIVE_PREV_PID ]; then
echo "Pid file detected. Checking status..."
pid=$(cat $LIVE_PREV_PID)
if alive $pid; then
echo "Live preview is alive, restarting"
shut_down
start_up $1
else
echo "stale pid detected, cleaning and restarting"
rm $LIVE_PREV_PID
start_up $1
fi
else
if [ $# -eq 1 ] && [ $1 != "browse" ]; then
./live_preview.py
else
echo "starting live preview..."
./live_preview.py $1 &
pid=$!
echo $pid > $LIVE_PREV_PID
fi
fi
}
### main
if [ $# -eq 0 ] || [ $1 != "start" -a $1 != "stop" ]; then
usage
elif [ $1 = "start" ]; then
start_up $2
elif [ $1 = "stop" ]; then
shut_down
fi
livereload==2.5.2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment