Skip to content

Commit

Permalink
Compress text with gzip.
Browse files Browse the repository at this point in the history
  • Loading branch information
sfiera committed Aug 21, 2012
1 parent 54986c9 commit 37fb34e
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 6 deletions.
1 change: 1 addition & 0 deletions Makefile
Expand Up @@ -29,6 +29,7 @@ clean:

html:
$(SPHINXBUILD) -b dirhtml $(ALLSPHINXOPTS) $(BUILDDIR)/html
cp -f htaccess $(BUILDDIR)/html/.htaccess
@echo
@echo "Build finished. The HTML pages are in $(BUILDDIR)/html."

Expand Down
2 changes: 0 additions & 2 deletions _static/.htaccess

This file was deleted.

6 changes: 2 additions & 4 deletions conf.py
Expand Up @@ -22,6 +22,7 @@
# Add any Sphinx extension module names here, as strings. They can be extensions
# coming with Sphinx (named "sphinx.ext.*") or your custom ones.
extensions = [
"ext.compress",
"ext.feed",
"ext.issuetracker",
"ext.meta",
Expand Down Expand Up @@ -102,10 +103,7 @@
#html_logo = None
html_favicon = "favicon.ico"

html_static_path = [
"_static",
"_static/.htaccess",
]
html_static_path = ["_static"]

#html_last_updated_fmt = "%b %d, %Y"
#html_show_sphinx = True
Expand Down
25 changes: 25 additions & 0 deletions ext/compress.py
@@ -0,0 +1,25 @@
import gzip
import os.path

EXTENSIONS = [".html", ".css", ".js"]

def setup(app):
app.connect('build-finished', gzip_static_files)

def visit_file(arg, d, paths):
for path in paths:
path = os.path.join(d, path)
if not os.path.isfile(path):
continue
if os.path.splitext(path)[-1] not in EXTENSIONS:
continue
gz_path = path + ".gz"
if os.path.isfile(gz_path):
if os.stat(path).st_mtime <= os.stat(gz_path).st_mtime:
continue
with open(path, "r") as f_in:
with gzip.open(gz_path, "w") as f_out:
f_out.write(f_in.read())

def gzip_static_files(app, builder):
os.path.walk(app.builder.outdir, visit_file, None)
11 changes: 11 additions & 0 deletions htaccess
@@ -0,0 +1,11 @@
Header add Vary Accept-Encoding

ExpiresActive on
ExpiresDefault A604800
ExpiresByType text/html A60

RewriteEngine on
RewriteCond %{HTTP:accept-encoding} gzip
RewriteCond %{REQUEST_FILENAME} !\.gz$
RewriteCond %{REQUEST_FILENAME}.gz -f
RewriteRule (.*) $1.gz [L]

0 comments on commit 37fb34e

Please sign in to comment.