mirror of
https://github.com/getpelican/pelican.git
synced 2025-10-15 20:28:56 +02:00
branch merge
Conflicts: pelican/generators.py pelican/settings.py
This commit is contained in:
commit
2f915caff2
21 changed files with 184 additions and 54 deletions
|
|
@ -1,15 +1,19 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
import os
|
||||
import re
|
||||
from codecs import open
|
||||
from functools import partial
|
||||
|
||||
from feedgenerator import Atom1Feed, Rss201rev2Feed
|
||||
|
||||
from pelican.utils import get_relative_path
|
||||
|
||||
|
||||
class Writer(object):
|
||||
|
||||
def __init__(self, output_path):
|
||||
self.output_path = output_path
|
||||
self.reminder = dict()
|
||||
|
||||
def _create_new_feed(self, feed_type, context):
|
||||
feed_class = Rss201rev2Feed if feed_type == 'rss' else Atom1Feed
|
||||
|
|
@ -78,6 +82,8 @@ class Writer(object):
|
|||
localcontext['SITEURL'] = get_relative_path(name)
|
||||
|
||||
localcontext.update(kwargs)
|
||||
self.update_context_contents(name, localcontext)
|
||||
|
||||
output = template.render(localcontext)
|
||||
filename = os.sep.join((self.output_path, name))
|
||||
try:
|
||||
|
|
@ -87,3 +93,77 @@ class Writer(object):
|
|||
with open(filename, 'w', encoding='utf-8') as f:
|
||||
f.write(output)
|
||||
print u' [ok] writing %s' % filename
|
||||
|
||||
def update_context_contents(self, name, context):
|
||||
"""Recursively run the context to find elements (articles, pages, etc) whose content getter needs to
|
||||
be modified in order to deal with relative paths.
|
||||
|
||||
:param name: name of the file to output.
|
||||
:param context: dict that will be passed to the templates.
|
||||
"""
|
||||
if context is None:
|
||||
return None
|
||||
|
||||
if type(context) == tuple:
|
||||
context = list(context)
|
||||
|
||||
if type(context) == dict:
|
||||
context = list(context.values())
|
||||
|
||||
for i in xrange(len(context)):
|
||||
if type(context[i]) == tuple or type(context[i]) == list:
|
||||
context[i] = self.update_context_contents(name, context[i])
|
||||
|
||||
elif type(context[i]) == dict:
|
||||
context[i] = self.update_context_content(name, context[i].values())
|
||||
|
||||
elif hasattr(context[i], '_content'):
|
||||
relative_path = get_relative_path(name)
|
||||
item = context[i]
|
||||
|
||||
if item in self.reminder:
|
||||
if relative_path not in self.reminder[item]:
|
||||
l = self.reminder[item]
|
||||
l.append(relative_path)
|
||||
self.inject_update_method(name, item)
|
||||
else:
|
||||
l = list(relative_path)
|
||||
self.reminder[item] = l
|
||||
self.inject_update_method(name, item)
|
||||
return context
|
||||
|
||||
def inject_update_method(self, name, item):
|
||||
"""Replace the content attribute getter of an element by a function that will deals with its
|
||||
relatives paths.
|
||||
"""
|
||||
|
||||
def _update_object_content(name, input):
|
||||
"""Change all the relatives paths of the input content to relatives paths
|
||||
suitable fot the ouput content
|
||||
|
||||
:param name: path of the output.
|
||||
:param input: input resource that will be passed to the templates.
|
||||
"""
|
||||
content = input._content
|
||||
|
||||
hrefs = re.compile(r'<\s*[^\>]*href\s*=\s*(["\'])(.*?)\1')
|
||||
srcs = re.compile(r'<\s*[^\>]*src\s*=\s*(["\'])(.*?)\1')
|
||||
|
||||
matches = hrefs.findall(content)
|
||||
matches.extend(srcs.findall(content))
|
||||
relative_paths = []
|
||||
for found in matches:
|
||||
found = found[1]
|
||||
if found not in relative_paths:
|
||||
relative_paths.append(found)
|
||||
|
||||
for relative_path in relative_paths:
|
||||
if not relative_path.startswith("http://"):
|
||||
dest_path = os.sep.join((get_relative_path(name), "static", relative_path))
|
||||
content = content.replace(relative_path, dest_path)
|
||||
|
||||
return content
|
||||
|
||||
if item:
|
||||
setattr(item, "_get_content",
|
||||
partial(_update_object_content, name, item))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue