add support for relative cross-site links

This commit is contained in:
Bruno Binet 2012-12-01 18:22:43 +01:00
commit a0504aeabe
7 changed files with 26 additions and 19 deletions

View file

@ -5,7 +5,6 @@ import logging
import functools
import os
import re
import urlparse
from datetime import datetime
from sys import platform, stdin
@ -132,11 +131,13 @@ class Page(object):
key = key if self.in_default_lang else 'lang_%s' % key
return self._expand_settings(key)
def _update_content(self, content):
def _update_content(self, content, siteurl):
"""Change all the relative paths of the content to relative paths
suitable for the ouput content.
:param content: content resource that will be passed to the templates.
:param siteurl: siteurl which is locally generated by the writer in
case of RELATIVE_URLS.
"""
hrefs = re.compile(r"""
(?P<markup><\s*[^\>]* # match tag with src and href attr
@ -163,8 +164,8 @@ class Page(object):
)
if value in self._context['filenames']:
origin = urlparse.urljoin(self._context['SITEURL'],
self._context['filenames'][value].url)
origin = '/'.join((siteurl,
self._context['filenames'][value].url))
else:
logger.warning(u"Unable to find {fn}, skipping url"
" replacement".format(fn=value))
@ -174,15 +175,16 @@ class Page(object):
return hrefs.sub(replacer, content)
@property
@memoized
def get_content(self, siteurl):
return self._update_content(
self._get_content() if hasattr(self, "_get_content")
else self._content,
siteurl)
@property
def content(self):
if hasattr(self, "_get_content"):
content = self._get_content()
else:
content = self._content
content = self._update_content(content)
return content
return self.get_content(self._context['localsiteurl'])
def _get_summary(self):
"""Returns the summary of an article, based on the summary metadata