diff options
author | mattack108 <matt.lenc@gmail.com> | 2013-01-16 17:20:29 +0000 |
---|---|---|
committer | mattack108 <matt.lenc@gmail.com> | 2013-01-16 17:20:29 +0000 |
commit | 62750c23a264ef04ac04e04bda820873ac9e421b (patch) | |
tree | 68da55010eaf495cd78537518151a1c971fac59a /wkhtmltopdf/utils.py | |
parent | d4a938c08539264add40126ff5c8ab673a8f78c1 (diff) | |
parent | a03c1e14b0da1999590442719c42293a88ab9e3b (diff) | |
download | django-wkhtmltopdf-62750c23a264ef04ac04e04bda820873ac9e421b.tar.gz django-wkhtmltopdf-62750c23a264ef04ac04e04bda820873ac9e421b.tar.bz2 django-wkhtmltopdf-62750c23a264ef04ac04e04bda820873ac9e421b.zip |
Merge branch 'master' of github.com:incuna/django-wkhtmltopdf into travis-setup
Diffstat (limited to 'wkhtmltopdf/utils.py')
-rw-r--r-- | wkhtmltopdf/utils.py | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/wkhtmltopdf/utils.py b/wkhtmltopdf/utils.py index 63310c3..e5bd102 100644 --- a/wkhtmltopdf/utils.py +++ b/wkhtmltopdf/utils.py @@ -4,6 +4,7 @@ from copy import copy from functools import wraps from itertools import chain import os +import re import sys import urllib from urlparse import urljoin @@ -121,3 +122,37 @@ def http_quote(string): def pathname2fileurl(pathname): """Returns a file:// URL for pathname. Handles OS-specific conversions.""" return urljoin('file:', urllib.pathname2url(pathname)) + + +def make_absolute_paths(content): + """Convert all MEDIA files into a file://URL paths in order to + correctly get it displayed in PDFs.""" + + overrides = [ + { + 'root': settings.MEDIA_ROOT, + 'url': settings.MEDIA_URL, + }, + { + 'root': settings.STATIC_ROOT, + 'url': settings.STATIC_URL, + } + ] + has_scheme = re.compile(r'^[^:/]+://') + + for x in overrides: + if has_scheme.match(x['url']): + continue + + if not x['root'].endswith('/'): + x['root'] += '/' + + occur_pattern = '''["|']({0}.*?)["|']''' + occurences = re.findall(occur_pattern.format(x['url']), content) + occurences = list(set(occurences)) # Remove dups + for occur in occurences: + content = content.replace(occur, + pathname2fileurl(x['root']) + + occur[len(x['url']):]) + + return content |