mirror of
https://github.com/getpelican/pelican.git
synced 2025-10-15 20:28:56 +02:00
Merge pull request #139 from borgar/rel-url-fix
Relative URLs rewriter fixes
This commit is contained in:
commit
f05da13f47
2 changed files with 20 additions and 20 deletions
|
|
@ -51,7 +51,7 @@ Setting name (default value) what does it do?
|
||||||
`PDF_GENERATOR` (``False``) Set to True if you want to have PDF versions
|
`PDF_GENERATOR` (``False``) Set to True if you want to have PDF versions
|
||||||
of your documents. You will need to install
|
of your documents. You will need to install
|
||||||
`rst2pdf`.
|
`rst2pdf`.
|
||||||
`RELATIVE_URL` (``True``) Defines if pelican should use relative urls or
|
`RELATIVE_URLS` (``True``) Defines if pelican should use relative urls or
|
||||||
not.
|
not.
|
||||||
`SITEURL` base URL of your website. Note that this is
|
`SITEURL` base URL of your website. Note that this is
|
||||||
not a way to tell pelican to use relative urls
|
not a way to tell pelican to use relative urls
|
||||||
|
|
|
||||||
|
|
@ -111,7 +111,8 @@ class Writer(object):
|
||||||
localcontext['SITEURL'] = get_relative_path(name)
|
localcontext['SITEURL'] = get_relative_path(name)
|
||||||
|
|
||||||
localcontext.update(kwargs)
|
localcontext.update(kwargs)
|
||||||
self.update_context_contents(name, localcontext)
|
if relative_urls:
|
||||||
|
self.update_context_contents(name, localcontext)
|
||||||
|
|
||||||
# check paginated
|
# check paginated
|
||||||
paginated = paginated or {}
|
paginated = paginated or {}
|
||||||
|
|
@ -166,24 +167,23 @@ class Writer(object):
|
||||||
"""
|
"""
|
||||||
content = input._content
|
content = input._content
|
||||||
|
|
||||||
hrefs = re.compile(r'<\s*[^\>]*href\s*=(^!#)\s*(["\'])(.*?)\1')
|
hrefs = re.compile(r"""
|
||||||
srcs = re.compile(r'<\s*[^\>]*src\s*=\s*(["\'])(.*?)\1')
|
(?P<markup><\s*[^\>]* # match tag with src and href attr
|
||||||
|
(?:href|src)\s*=\s*
|
||||||
|
)
|
||||||
|
(?P<quote>["\']) # require value to be quoted
|
||||||
|
(?![#?]) # don't match fragment or query URLs
|
||||||
|
(?![a-z]+:) # don't match protocol URLS
|
||||||
|
(?P<path>.*?) # the url value
|
||||||
|
\2""", re.X)
|
||||||
|
|
||||||
matches = hrefs.findall(content)
|
def replacer(m):
|
||||||
matches.extend(srcs.findall(content))
|
relative_path = m.group('path')
|
||||||
relative_paths = []
|
dest_path = os.path.normpath( os.sep.join( (get_relative_path(name),
|
||||||
for found in matches:
|
"static", relative_path) ) )
|
||||||
found = found[1]
|
return m.group('markup') + m.group('quote') + dest_path + m.group('quote')
|
||||||
if found not in relative_paths:
|
|
||||||
relative_paths.append(found)
|
|
||||||
|
|
||||||
for relative_path in relative_paths:
|
return hrefs.sub(replacer, content)
|
||||||
if not ":" in relative_path: # we don't want to rewrite protocols
|
|
||||||
dest_path = os.sep.join((get_relative_path(name), "static",
|
|
||||||
relative_path))
|
|
||||||
content = content.replace(relative_path, dest_path)
|
|
||||||
|
|
||||||
return content
|
|
||||||
|
|
||||||
if context is None:
|
if context is None:
|
||||||
return
|
return
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue