Last active
July 5, 2019 16:39
My blog live reviewer
This file contains 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
#!/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() |
This file contains 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 | |
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 |
This file contains 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
livereload==2.5.2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment