Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re-<base> serve command on the fly #1930

Merged
merged 2 commits into from Aug 6, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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