Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #1930 from getnikola/serve_re-base
Re-<base> serve command on the fly
  • Loading branch information
Kwpolska committed Aug 6, 2015
2 parents 1d83dca + 652bf11 commit dc4cedd
Showing 1 changed file with 27 additions and 3 deletions.
30 changes: 27 additions & 3 deletions nikola/plugins/command/serve.py
Expand Up @@ -28,6 +28,7 @@

from __future__ import print_function
import os
import re
import socket
import webbrowser
try:
Expand All @@ -37,6 +38,12 @@
from http.server import HTTPServer # NOQA
from http.server import SimpleHTTPRequestHandler # NOQA

try:
from StringIO import StringIO
except ImportError:
from io import BytesIO as StringIO # NOQA


from nikola.plugin_categories import Command
from nikola.utils import get_logger, STDERR_HANDLER

Expand Down Expand Up @@ -221,14 +228,31 @@ def send_head(self):
except IOError:
self.send_error(404, "File not found")
return None

filtered_bytes = None
if ctype == 'text/html':
"""Comment out any <base> to allow local resolution of relative URLs."""
data = f.read().decode('utf8')
f.close()
data = re.sub(r'<base\s([^>]*)>', '<!--base \g<1>-->', data, re.IGNORECASE)
data = data.encode('utf8')
f = StringIO()
f.write(data)
f.seek(0)
filtered_bytes = f.tell()

self.send_response(200)
self.send_header("Content-type", ctype)
if os.path.splitext(path)[1] == '.svgz':
# Special handling for svgz to make it work nice with browsers.
self.send_header("Content-Encoding", 'gzip')
fs = os.fstat(f.fileno())
self.send_header("Content-Length", str(fs[6]))
self.send_header("Last-Modified", self.date_time_string(fs.st_mtime))

if filtered_bytes is None:
fs = os.fstat(f.fileno())
self.send_header('Content-Length', str(fs[6]))
else:
self.send_header('Content-Length', filtered_bytes)

# begin no-cache patch
# For standard requests.
self.send_header("Cache-Control", "no-cache, no-store, "
Expand Down

0 comments on commit dc4cedd

Please sign in to comment.