Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
use snippet better
  • Loading branch information
ralsina committed May 19, 2015
1 parent 45124f2 commit 24676f9
Showing 1 changed file with 12 additions and 14 deletions.
26 changes: 12 additions & 14 deletions nikola/plugins/command/auto/auto.py
Expand Up @@ -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')
Expand Down Expand Up @@ -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':
Expand All @@ -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,
Expand Down Expand Up @@ -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)])
Expand All @@ -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 = []
Expand Down

0 comments on commit 24676f9

Please sign in to comment.