Skip to content
Permalink
Browse files

use snippet better

  • Loading branch information
ralsina committed May 19, 2015
1 parent 45124f2 commit 24676f9d1df4d4182f5e41372fec435bdace2182
Showing with 12 additions and 14 deletions.
  1. +12 −14 nikola/plugins/command/auto/auto.py
@@ -49,11 +49,6 @@
from nikola.utils import req_missing

LRJS_PATH = os.path.join(os.path.dirname(__file__), 'livereload.js')
LRJS_SNIPPET = '''<script>document.write('<script src="http://'
+ (location.host || 'localhost').split(':')[0]
+ ':{0}/livereload.js?snipver=1"></'
+ 'script>')</script>
</head>'''
MASK = pyinotify.IN_DELETE | pyinotify.IN_CREATE | pyinotify.IN_MODIFY
error_signal = signal('error')
refresh_signal = signal('refresh')
@@ -99,7 +94,6 @@ class CommandAuto(Command):

def _execute(self, options, args):
"""Start the watcher."""
global LRJS_SNIPPET

arguments = ['build']
if self.site.configuration_filename != 'conf.py':
@@ -111,7 +105,11 @@ def _execute(self, options, args):
subprocess.call(["nikola"] + arguments)

port = options and options.get('port')
LRJS_SNIPPET = LRJS_SNIPPET.format(port)
self.snippet = '''<script>document.write('<script src="http://'
+ (location.host || 'localhost').split(':')[0]
+ ':{0}/livereload.js?snipver=1"></'
+ 'script>')</script>
</head>'''.format(port)

watched = [
self.site.configuration_filename,
@@ -200,7 +198,7 @@ def serve_static(self, environ, start_response):
if os.path.isfile(f_path):
with open(f_path) as fd:
start_response(b'200 OK', [(b'Content-type', mimetype)])
return inject_js(mimetype, fd.read())
return self.inject_js(mimetype, fd.read())
elif p_uri.path == '/livereload.js':
with open(LRJS_PATH) as fd:
start_response(b'200 OK', [(b'Content-type', mimetype)])
@@ -209,12 +207,12 @@ def serve_static(self, environ, start_response):
return ['404 {0}'.format(uri)]


def inject_js(mimetype, data):
"""Inject livereload.js in HTML files."""
if mimetype == 'text/html':
# FIXME: use re.IGNORECASE
data = data.replace('</head>', LRJS_SNIPPET, 1)
return data
def inject_js(self, mimetype, data):
"""Inject livereload.js in HTML files."""
if mimetype == 'text/html':
# FIXME: use re.IGNORECASE
data = data.replace('</head>', self.snippet, 1)
return data


pending = []

0 comments on commit 24676f9

Please sign in to comment.
You can’t perform that action at this time.