Skip to content

Commit 24676f9

Browse files
committedMay 19, 2015
use snippet better
1 parent 45124f2 commit 24676f9

File tree

1 file changed

+12
-14
lines changed

1 file changed

+12
-14
lines changed
 

‎nikola/plugins/command/auto/auto.py

+12-14
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,6 @@
4949
from nikola.utils import req_missing
5050

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

10095
def _execute(self, options, args):
10196
"""Start the watcher."""
102-
global LRJS_SNIPPET
10397

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

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

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

211209

212-
def inject_js(mimetype, data):
213-
"""Inject livereload.js in HTML files."""
214-
if mimetype == 'text/html':
215-
# FIXME: use re.IGNORECASE
216-
data = data.replace('</head>', LRJS_SNIPPET, 1)
217-
return data
210+
def inject_js(self, mimetype, data):
211+
"""Inject livereload.js in HTML files."""
212+
if mimetype == 'text/html':
213+
# FIXME: use re.IGNORECASE
214+
data = data.replace('</head>', self.snippet, 1)
215+
return data
218216

219217

220218
pending = []

0 commit comments

Comments
 (0)
Please sign in to comment.