branch merge

Conflicts:
	pelican/generators.py
	pelican/settings.py
This commit is contained in:
Alexis Metaireau 2011-01-05 18:25:14 +01:00
commit 2f915caff2
21 changed files with 184 additions and 54 deletions

View file

@ -11,12 +11,12 @@ class Page(object):
mandatory_properties = ('title',)
def __init__(self, content, metadatas={}, settings={}, filename=None):
self.content = content
self._content = content
self.translations = []
self.status = "published" # default value
for key, value in metadatas.items():
setattr(self, key, value)
setattr(self, key.lower(), value)
if not hasattr(self, 'author'):
if 'AUTHOR' in settings:
@ -53,6 +53,14 @@ class Page(object):
if not hasattr(self, prop):
raise NameError(prop)
@property
def content(self):
if hasattr(self, "_get_content"):
content = self._get_content()
else:
content = self._content
return content
@property
def summary(self):
return truncate_html_words(self.content, 50)

View file

@ -8,7 +8,7 @@ import os
from jinja2 import Environment, FileSystemLoader
from jinja2.exceptions import TemplateNotFound
from pelican.utils import copytree, process_translations, open
from pelican.utils import copytree, get_relative_path, process_translations, open
from pelican.contents import Article, Page, is_valid_content
from pelican.readers import read_file
@ -34,7 +34,7 @@ class Generator(object):
templates ready to use with Jinja2.
"""
path = os.path.expanduser(os.path.join(self.theme, 'templates'))
env = Environment(loader=FileSystemLoader(path))
env = Environment(loader=FileSystemLoader(path),extensions=self.settings.get('JINJA_EXTENSIONS', []))
templates = {}
for template in _TEMPLATES:
try:
@ -133,19 +133,25 @@ class ArticlesGenerator(Generator):
writer.write_file,
relative_urls = self.settings.get('RELATIVE_URLS')
)
# to minimize the number of relative path stuff modification in writer, articles pass first
for article in chain(self.translations, self.articles):
write('%s' % article.save_as,
templates['article'], self.context, article=article,
category=article.category)
for template in _DIRECT_TEMPLATES:
write('%s.html' % template, templates[template], self.context,
blog=True)
blog=True)
# and subfolders after that
for tag, articles in self.tags.items():
write('tag/%s.html' % tag, templates['tag'], self.context, tag=tag,
articles=articles)
for article in articles:
write('tag/%s.html' % tag, templates['tag'], self.context,
tag=tag, articles=articles)
for cat in self.categories:
write('category/%s.html' % cat, templates['category'], self.context,
category=cat, articles=self.categories[cat])
for article in chain(self.translations, self.articles):
write(article.save_as,
templates['article'], self.context, article=article,
category=article.category)
category=cat, articles=self.categories[cat])
def generate_context(self):
"""change the context"""

View file

@ -23,6 +23,7 @@ _DEFAULT_CONFIG = {'PATH': None,
'RELATIVE_URLS': True,
'DEFAULT_LANG': 'en',
'PELICAN_CLASS': 'pelican.Pelican',
'JINJA_EXTENSIONS': [],
}
def read_settings(filename):

View file

@ -7,18 +7,7 @@
rel="bookmark" title="Permalink to {{ article.title }}">{{ article.title
}}</a></h1> {% include 'twitter.html' %} </header>
<div class="entry-content">
<footer class="post-info">
<abbr class="published" title="{{ article.date.isoformat() }}">
{{ article.date.strftime('%a %d %B %Y') }}
</abbr>
{% if article.author %}
<address class="vcard author">
By <a class="url fn" href="#">{{ article.author }}</a>
</address>
{% endif %}
<p>In <a href="{{ SITEURL }}/category/{{ article.category }}.html">{{ article.category }}</a>.
{% include 'taglist.html' %}
</footer><!-- /.post-info -->
{% include 'article_infos.html' %}
{{ article.content }}
</div><!-- /.entry-content -->
{% if DISQUS_SITENAME %}

View file

@ -0,0 +1,14 @@
<footer class="post-info">
<abbr class="published" title="{{ article.date.isoformat() }}">
{{ article.date.strftime('%a %d %B %Y') }}
</abbr>
{% if article.author %}
<address class="vcard author">
By <a class="url fn" href="#">{{ article.author }}</a>
</address>
{% endif %}
<p>In <a href="{{ SITEURL }}/category/{{ article.category }}.html">{{ article.category }}</a>. {% if PDF_PROCESSOR %}<a href="{{ SITEURL }}/pdf/{{ article.slug }}.pdf">get the pdf</a>{% endif %}</p>
{% include 'taglist.html' %}
{% include 'translations.html' %}
</footer><!-- /.post-info -->

View file

@ -7,19 +7,7 @@
<aside id="featured" class="body"><article>
<h1 class="entry-title"><a href="{{ SITEURL }}/{{ article.url
}}">{{ article.title }}</a></h1>
<footer class="post-info">
<abbr class="published" title="{{ article.date.isoformat() }}">
{{ article.date.strftime('%a %d %B %Y') }}
</abbr>
{% if article.author %}
<address class="vcard author">
By <a class="url fn" href="#">{{ article.author }}</a>
</address>
{% endif %}
<p>In <a href="{{ SITEURL }}/category/{{ article.category }}.html">{{ article.category }}</a>. {% if PDF_PROCESSOR %}<a href="{{ SITEURL }}/pdf/{{ article.slug }}.pdf">get the pdf</a>{% endif %}</p>
{% include 'taglist.html' %}
</footer><!-- /.post-info -->
{% include 'article_infos.html' %}
{{ article.content }}
{% include 'comments.html' %}
</article>
@ -37,18 +25,7 @@
</header>
<div class="entry-content">
<footer class="post-info">
<abbr class="published" title="{{ article.date.isoformat() }}">
{{ article.date.strftime('%a %d %B %Y') }}
</abbr>
<address class="vcard author">
By <a class="url fn" href="#">{{ article.author }}</a>
</address>
<p> In <a href="{{ SITEURL }}/category/{{ article.category }}.html">{{ article.category }}</a></p>
{% include 'taglist.html' %}
<p>{% if PDF_PROCESSOR %}<a href="{{ SITEURL }}/pdf/{{ article.slug }}.pdf">pdf</a>{% endif %}</p>
</footer><!-- /.post-info -->
{% include 'article_infos.html' %}
{{ article.summary }}
<a class="readmore" href="{{ SITEURL }}/{{ article.url }}">read more</a>
{% include 'comments.html' %}

View file

@ -0,0 +1,6 @@
{% if article.translations %}
Translations:
{% for translation in article.translations %}
<a href="{{ SITEURL }}/{{ translation.url }}">{{ translation.lang }}</a>
{% endfor %}
{% endif %}

View file

@ -148,6 +148,7 @@ def process_translations(content_list):
Also, for each content_list item, it
sets attribute 'translations'
"""
content_list.sort(key=attrgetter('slug'))
grouped_by_slugs = groupby(content_list, attrgetter('slug'))
index = []
translations = []

View file

@ -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))