Make the internal link replacer function public.

So it can be used from outside.
This commit is contained in:
Vladimír Vondruš 2017-09-19 18:22:56 +02:00
commit 1f30306e23

View file

@ -228,30 +228,7 @@ class Content(object):
key = key if self.in_default_lang else 'lang_%s' % key key = key if self.in_default_lang else 'lang_%s' % key
return self._expand_settings(key) return self._expand_settings(key)
def _update_content(self, content, siteurl): def _link_replacer(self, siteurl, m):
"""Update the content attribute.
Change all the relative paths of the content to relative paths
suitable for the output 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.
"""
if not content:
return content
instrasite_link_regex = self.settings['INTRASITE_LINK_REGEX']
regex = r"""
(?P<markup><[^\>]+ # match tag with all url-value attributes
(?:href|src|poster|data|cite|formaction|action)\s*=\s*)
(?P<quote>["\']) # require value to be quoted
(?P<path>{0}(?P<value>.*?)) # the url value
\2""".format(instrasite_link_regex)
hrefs = re.compile(regex, re.X)
def replacer(m):
what = m.group('what') what = m.group('what')
value = urlparse(m.group('value')) value = urlparse(m.group('value'))
path = value.path path = value.path
@ -313,7 +290,30 @@ class Content(object):
return ''.join((m.group('markup'), m.group('quote'), origin, return ''.join((m.group('markup'), m.group('quote'), origin,
m.group('quote'))) m.group('quote')))
return hrefs.sub(replacer, content) def _update_content(self, content, siteurl):
"""Update the content attribute.
Change all the relative paths of the content to relative paths
suitable for the output 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.
"""
if not content:
return content
instrasite_link_regex = self.settings['INTRASITE_LINK_REGEX']
regex = r"""
(?P<markup><[^\>]+ # match tag with all url-value attributes
(?:href|src|poster|data|cite|formaction|action)\s*=\s*)
(?P<quote>["\']) # require value to be quoted
(?P<path>{0}(?P<value>.*?)) # the url value
\2""".format(instrasite_link_regex)
hrefs = re.compile(regex, re.X)
return hrefs.sub(lambda m: self._link_replacer(siteurl, m), content)
def get_siteurl(self): def get_siteurl(self):
return self._context.get('localsiteurl', '') return self._context.get('localsiteurl', '')