Do not create a general function for "update_object_content".

This commit is contained in:
Alexis Metaireau 2011-01-05 14:27:46 +01:00
commit 2f6300af46
2 changed files with 32 additions and 31 deletions

View file

@ -4,7 +4,6 @@ from functools import partial
from datetime import datetime
from collections import defaultdict
import os
import re
from jinja2 import Environment, FileSystemLoader
from jinja2.exceptions import TemplateNotFound
@ -17,32 +16,6 @@ _TEMPLATES = ('index', 'tag', 'tags', 'article', 'category', 'categories',
'archives', 'page')
_DIRECT_TEMPLATES = ('index', 'tags', 'categories', 'archives')
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
class Generator(object):
"""Baseclass generator"""

View file

@ -1,13 +1,13 @@
# -*- coding: utf-8 -*-
import os
import re
from codecs import open
from functools import partial
from feedgenerator import Atom1Feed, Rss201rev2Feed
from functools import partial
from pelican.utils import get_relative_path
from pelican.generators import update_object_content
class Writer(object):
@ -127,6 +127,34 @@ class Writer(object):
"""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))
partial(_update_object_content, name, item))