49
49
from nikola .utils import req_missing
50
50
51
51
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>'''
57
52
MASK = pyinotify .IN_DELETE | pyinotify .IN_CREATE | pyinotify .IN_MODIFY
58
53
error_signal = signal ('error' )
59
54
refresh_signal = signal ('refresh' )
@@ -99,7 +94,6 @@ class CommandAuto(Command):
99
94
100
95
def _execute (self , options , args ):
101
96
"""Start the watcher."""
102
- global LRJS_SNIPPET
103
97
104
98
arguments = ['build' ]
105
99
if self .site .configuration_filename != 'conf.py' :
@@ -111,7 +105,11 @@ def _execute(self, options, args):
111
105
subprocess .call (["nikola" ] + arguments )
112
106
113
107
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 )
115
113
116
114
watched = [
117
115
self .site .configuration_filename ,
@@ -200,7 +198,7 @@ def serve_static(self, environ, start_response):
200
198
if os .path .isfile (f_path ):
201
199
with open (f_path ) as fd :
202
200
start_response (b'200 OK' , [(b'Content-type' , mimetype )])
203
- return inject_js (mimetype , fd .read ())
201
+ return self . inject_js (mimetype , fd .read ())
204
202
elif p_uri .path == '/livereload.js' :
205
203
with open (LRJS_PATH ) as fd :
206
204
start_response (b'200 OK' , [(b'Content-type' , mimetype )])
@@ -209,12 +207,12 @@ def serve_static(self, environ, start_response):
209
207
return ['404 {0}' .format (uri )]
210
208
211
209
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
218
216
219
217
220
218
pending = []
0 commit comments