Skip to content
Permalink
Browse files

Merge pull request #1928 from getnikola/auto_re-base

Comment out <base> elements when serving via auto
  • Loading branch information...
da2x committed Aug 5, 2015
2 parents 2244cca + 25b3f94 commit 682f80d9efe6a33d6647aa44ebea198035238622
Showing with 18 additions and 6 deletions.
  1. +18 −6 nikola/plugins/command/auto/__init__.py
@@ -301,21 +301,33 @@ def serve_static(self, environ, start_response):
elif os.path.isfile(f_path):
with open(f_path, 'rb') as fd:
start_response('200 OK', [('Content-type', mimetype)])
return [self.inject_js(mimetype, fd.read())]
return [self.file_filter(mimetype, fd.read())]
elif p_uri.path == '/livereload.js':
with open(LRJS_PATH, 'rb') as fd:
start_response('200 OK', [('Content-type', mimetype)])
return [self.inject_js(mimetype, fd.read())]
return [self.file_filter(mimetype, fd.read())]
start_response('404 ERR', [])
return [self.inject_js('text/html', ERROR_N.format(404).format(uri).encode('utf-8'))]
return [self.file_filter('text/html', ERROR_N.format(404).format(uri).encode('utf-8'))]

def inject_js(self, mimetype, data):
"""Inject livereload.js in HTML files."""
def file_filter(self, mimetype, data):
"""Apply necessary changes to document before serving."""
if mimetype == 'text/html':
data = re.sub('</head>', self.snippet, data.decode('utf8'), 1, re.IGNORECASE)
data = data.decode('utf8')
data = self.remove_base_tag(data)
data = self.inject_js(data)
data = data.encode('utf8')
return data

def inject_js(self, data):
"""Inject livereload.js."""
data = re.sub('</head>', self.snippet, data, 1, re.IGNORECASE)
return data

def remove_base_tag(self, data):
"""Comment out any <base> to allow local resolution of relative URLs."""
data = re.sub(r'<base\s([^>]*)>', '<!--base \g<1>-->', data, re.IGNORECASE)
return data


pending = []

0 comments on commit 682f80d

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