mirror of
https://github.com/getpelican/pelican.git
synced 2025-10-15 20:28:56 +02:00
This commit is contained in:
commit
1ca2a77c05
20 changed files with 438 additions and 101 deletions
|
|
@ -1,3 +1,4 @@
|
|||
import copy
|
||||
import os
|
||||
import re
|
||||
import sys
|
||||
|
|
@ -7,11 +8,11 @@ import argparse
|
|||
|
||||
from pelican import signals
|
||||
|
||||
from pelican.generators import (ArticlesGenerator, PagesGenerator,
|
||||
from pelican.generators import (Generator, ArticlesGenerator, PagesGenerator,
|
||||
StaticGenerator, PdfGenerator, LessCSSGenerator)
|
||||
from pelican.log import init
|
||||
from pelican.settings import read_settings, _DEFAULT_CONFIG
|
||||
from pelican.utils import clean_output_dir, files_changed, file_changed
|
||||
from pelican.utils import clean_output_dir, files_changed, file_changed, NoFilesError
|
||||
from pelican.writers import Writer
|
||||
|
||||
__major__ = 3
|
||||
|
|
@ -29,7 +30,7 @@ class Pelican(object):
|
|||
before doing anything else.
|
||||
"""
|
||||
if settings is None:
|
||||
settings = _DEFAULT_CONFIG
|
||||
settings = copy.deepcopy(_DEFAULT_CONFIG)
|
||||
|
||||
self.path = path or settings['PATH']
|
||||
if not self.path:
|
||||
|
|
@ -184,6 +185,18 @@ class Pelican(object):
|
|||
generators.append(PdfGenerator)
|
||||
if self.settings['LESS_GENERATOR']: # can be True or PATH to lessc
|
||||
generators.append(LessCSSGenerator)
|
||||
|
||||
for pair in signals.get_generators.send(self):
|
||||
(funct, value) = pair
|
||||
|
||||
if not isinstance(value, (tuple, list)):
|
||||
value = (value, )
|
||||
|
||||
for v in value:
|
||||
if isinstance(v, type):
|
||||
logger.debug('Found generator: {0}'.format(v))
|
||||
generators.append(v)
|
||||
|
||||
return generators
|
||||
|
||||
def get_writer(self):
|
||||
|
|
@ -265,6 +278,7 @@ def main():
|
|||
|
||||
try:
|
||||
if args.autoreload:
|
||||
files_found_error = True
|
||||
while True:
|
||||
try:
|
||||
# Check source dir for changed files ending with the given
|
||||
|
|
@ -274,6 +288,8 @@ def main():
|
|||
# have.
|
||||
if files_changed(pelican.path, pelican.markup) or \
|
||||
files_changed(pelican.theme, ['']):
|
||||
if files_found_error == False:
|
||||
files_found_error = True
|
||||
pelican.run()
|
||||
|
||||
# reload also if settings.py changed
|
||||
|
|
@ -287,6 +303,10 @@ def main():
|
|||
except KeyboardInterrupt:
|
||||
logger.warning("Keyboard interrupt, quitting.")
|
||||
break
|
||||
except NoFilesError:
|
||||
if files_found_error == True:
|
||||
logger.warning("No valid files found in content. Nothing to generate.")
|
||||
files_found_error = False
|
||||
except Exception, e:
|
||||
logger.warning(
|
||||
"Caught exception \"{}\". Reloading.".format(e)
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
import copy
|
||||
import locale
|
||||
import logging
|
||||
import functools
|
||||
|
|
@ -29,7 +30,7 @@ class Page(object):
|
|||
if not metadata:
|
||||
metadata = {}
|
||||
if not settings:
|
||||
settings = _DEFAULT_CONFIG
|
||||
settings = copy.deepcopy(_DEFAULT_CONFIG)
|
||||
|
||||
self.settings = settings
|
||||
self._content = content
|
||||
|
|
|
|||
208
pelican/plugins/sitemap.py
Normal file
208
pelican/plugins/sitemap.py
Normal file
|
|
@ -0,0 +1,208 @@
|
|||
import os.path
|
||||
|
||||
from datetime import datetime
|
||||
from logging import debug, warning, error, info
|
||||
from codecs import open
|
||||
|
||||
from pelican import signals, contents
|
||||
|
||||
TXT_HEADER = u"""{0}/index.html
|
||||
{0}/archives.html
|
||||
{0}/tags.html
|
||||
{0}/categories.html
|
||||
"""
|
||||
|
||||
XML_HEADER = u"""<?xml version="1.0" encoding="utf-8"?>
|
||||
<urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"
|
||||
xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
|
||||
|
||||
<url>
|
||||
<loc>{0}/index.html</loc>
|
||||
<lastmod>{1}</lastmod>
|
||||
<changefreq>{2}</changefreq>
|
||||
<priority>{3}</priority>
|
||||
</url>
|
||||
|
||||
<url>
|
||||
<loc>{0}/archives.html</loc>
|
||||
<lastmod>{1}</lastmod>
|
||||
<changefreq>{2}</changefreq>
|
||||
<priority>{3}</priority>
|
||||
</url>
|
||||
|
||||
<url>
|
||||
<loc>{0}/tags.html</loc>
|
||||
<lastmod>{1}</lastmod>
|
||||
<changefreq>{2}</changefreq>
|
||||
<priority>{3}</priority>
|
||||
</url>
|
||||
|
||||
<url>
|
||||
<loc>{0}/categories.html</loc>
|
||||
<lastmod>{1}</lastmod>
|
||||
<changefreq>{2}</changefreq>
|
||||
<priority>{3}</priority>
|
||||
</url>
|
||||
"""
|
||||
|
||||
XML_URL = u"""
|
||||
<url>
|
||||
<loc>{0}/{1}</loc>
|
||||
<lastmod>{2}</lastmod>
|
||||
<changefreq>{3}</changefreq>
|
||||
<priority>{4}</priority>
|
||||
</url>
|
||||
"""
|
||||
|
||||
XML_FOOTER = u"""
|
||||
</urlset>
|
||||
"""
|
||||
|
||||
|
||||
def format_date(date):
|
||||
if date.tzinfo:
|
||||
tz = date.strftime('%s')
|
||||
tz = tz[:-2] + ':' + tz[-2:]
|
||||
else:
|
||||
tz = "-00:00"
|
||||
return date.strftime("%Y-%m-%dT%H:%M:%S") + tz
|
||||
|
||||
|
||||
|
||||
class SitemapGenerator(object):
|
||||
|
||||
def __init__(self, context, settings, path, theme, output_path, *null):
|
||||
|
||||
self.output_path = output_path
|
||||
self.context = context
|
||||
self.now = datetime.now()
|
||||
self.siteurl = settings.get('SITEURL')
|
||||
|
||||
self.format = 'xml'
|
||||
|
||||
self.changefreqs = {
|
||||
'articles': 'monthly',
|
||||
'indexes': 'daily',
|
||||
'pages': 'monthly'
|
||||
}
|
||||
|
||||
self.priorities = {
|
||||
'articles': 0.5,
|
||||
'indexes': 0.5,
|
||||
'pages': 0.5
|
||||
}
|
||||
|
||||
config = settings.get('SITEMAP', {})
|
||||
|
||||
if not isinstance(config, dict):
|
||||
warning("sitemap plugin: the SITEMAP setting must be a dict")
|
||||
else:
|
||||
fmt = config.get('format')
|
||||
pris = config.get('priorities')
|
||||
chfreqs = config.get('changefreqs')
|
||||
|
||||
if fmt not in ('xml', 'txt'):
|
||||
warning("sitemap plugin: SITEMAP['format'] must be `txt' or `xml'")
|
||||
warning("sitemap plugin: Setting SITEMAP['format'] on `xml'")
|
||||
elif fmt == 'txt':
|
||||
self.format = fmt
|
||||
return
|
||||
|
||||
valid_keys = ('articles', 'indexes', 'pages')
|
||||
valid_chfreqs = ('always', 'hourly', 'daily', 'weekly', 'monthly',
|
||||
'yearly', 'never')
|
||||
|
||||
if isinstance(pris, dict):
|
||||
for k, v in pris.iteritems():
|
||||
if k in valid_keys and not isinstance(v, (int, float)):
|
||||
default = self.priorities[k]
|
||||
warning("sitemap plugin: priorities must be numbers")
|
||||
warning("sitemap plugin: setting SITEMAP['priorities']"
|
||||
"['{0}'] on {1}".format(k, default))
|
||||
pris[k] = default
|
||||
self.priorities.update(pris)
|
||||
elif pris is not None:
|
||||
warning("sitemap plugin: SITEMAP['priorities'] must be a dict")
|
||||
warning("sitemap plugin: using the default values")
|
||||
|
||||
if isinstance(chfreqs, dict):
|
||||
for k, v in chfreqs.iteritems():
|
||||
if k in valid_keys and v not in valid_chfreqs:
|
||||
default = self.changefreqs[k]
|
||||
warning("sitemap plugin: invalid changefreq `{0}'".format(v))
|
||||
warning("sitemap plugin: setting SITEMAP['changefreqs']"
|
||||
"['{0}'] on '{1}'".format(k, default))
|
||||
chfreqs[k] = default
|
||||
self.changefreqs.update(chfreqs)
|
||||
elif chfreqs is not None:
|
||||
warning("sitemap plugin: SITEMAP['changefreqs'] must be a dict")
|
||||
warning("sitemap plugin: using the default values")
|
||||
|
||||
|
||||
|
||||
def write_url(self, page, fd):
|
||||
|
||||
if getattr(page, 'status', 'published') != 'published':
|
||||
return
|
||||
|
||||
lastmod = format_date(getattr(page, 'date', self.now))
|
||||
|
||||
if isinstance(page, contents.Article):
|
||||
pri = self.priorities['articles']
|
||||
chfreq = self.changefreqs['articles']
|
||||
elif isinstance(page, contents.Page):
|
||||
pri = self.priorities['pages']
|
||||
chfreq = self.changefreqs['pages']
|
||||
else:
|
||||
pri = self.priorities['indexes']
|
||||
chfreq = self.changefreqs['indexes']
|
||||
|
||||
|
||||
if self.format == 'xml':
|
||||
fd.write(XML_URL.format(self.siteurl, page.url, lastmod, chfreq, pri))
|
||||
else:
|
||||
fd.write(self.siteurl + '/' + loc + '\n')
|
||||
|
||||
|
||||
def generate_output(self, writer):
|
||||
path = os.path.join(self.output_path, 'sitemap.{0}'.format(self.format))
|
||||
|
||||
pages = self.context['pages'] + self.context['articles'] \
|
||||
+ [ c for (c, a) in self.context['categories']] \
|
||||
+ [ t for (t, a) in self.context['tags']] \
|
||||
+ [ a for (a, b) in self.context['authors']]
|
||||
|
||||
for article in self.context['articles']:
|
||||
pages += article.translations
|
||||
|
||||
|
||||
info('writing {0}'.format(path))
|
||||
|
||||
with open(path, 'w', encoding='utf-8') as fd:
|
||||
|
||||
if self.format == 'xml':
|
||||
fd.write(XML_HEADER.format(
|
||||
self.siteurl,
|
||||
format_date(self.now),
|
||||
self.changefreqs['indexes'],
|
||||
self.priorities['indexes']
|
||||
)
|
||||
)
|
||||
else:
|
||||
fd.write(TXT_HEADER.format(self.siteurl))
|
||||
|
||||
for page in pages:
|
||||
self.write_url(page, fd)
|
||||
|
||||
if self.format == 'xml':
|
||||
fd.write(XML_FOOTER)
|
||||
|
||||
|
||||
|
||||
def get_generators(generators):
|
||||
return SitemapGenerator
|
||||
|
||||
|
||||
def register():
|
||||
signals.get_generators.connect(get_generators)
|
||||
|
|
@ -1,4 +1,5 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
import copy
|
||||
import imp
|
||||
import inspect
|
||||
import os
|
||||
|
|
@ -81,7 +82,7 @@ def read_settings(filename=None):
|
|||
if filename:
|
||||
local_settings = get_settings_from_file(filename)
|
||||
else:
|
||||
local_settings = _DEFAULT_CONFIG
|
||||
local_settings = copy.deepcopy(_DEFAULT_CONFIG)
|
||||
configured_settings = configure_settings(local_settings, None, filename)
|
||||
return configured_settings
|
||||
|
||||
|
|
@ -89,10 +90,9 @@ def read_settings(filename=None):
|
|||
def get_settings_from_module(module=None, default_settings=_DEFAULT_CONFIG):
|
||||
"""
|
||||
Load settings from a module, returning a dict.
|
||||
|
||||
"""
|
||||
|
||||
context = default_settings.copy()
|
||||
context = copy.deepcopy(default_settings)
|
||||
if module is not None:
|
||||
context.update(
|
||||
(k, v) for k, v in inspect.getmembers(module) if k.isupper()
|
||||
|
|
@ -114,7 +114,7 @@ def get_settings_from_file(filename, default_settings=_DEFAULT_CONFIG):
|
|||
def configure_settings(settings, default_settings=None, filename=None):
|
||||
"""Provide optimizations, error checking, and warnings for loaded settings"""
|
||||
if default_settings is None:
|
||||
default_settings = _DEFAULT_CONFIG
|
||||
default_settings = copy.deepcopy(_DEFAULT_CONFIG)
|
||||
|
||||
# Make the paths relative to the settings file
|
||||
if filename:
|
||||
|
|
@ -138,7 +138,7 @@ def configure_settings(settings, default_settings=None, filename=None):
|
|||
for locale_ in locales:
|
||||
try:
|
||||
locale.setlocale(locale.LC_ALL, locale_)
|
||||
break # break if it is successfull
|
||||
break # break if it is successful
|
||||
except locale.Error:
|
||||
pass
|
||||
else:
|
||||
|
|
|
|||
|
|
@ -3,5 +3,6 @@ from blinker import signal
|
|||
initialized = signal('pelican_initialized')
|
||||
article_generate_context = signal('article_generate_context')
|
||||
article_generator_init = signal('article_generator_init')
|
||||
get_generators = signal('get_generators')
|
||||
pages_generate_context = signal('pages_generate_context')
|
||||
pages_generator_init = signal('pages_generator_init')
|
||||
|
|
|
|||
|
|
@ -308,7 +308,8 @@ img.left, figure.left {float: left; margin: 0 2em 2em 0;}
|
|||
.social a[type$='atom+xml'], .social a[type$='rss+xml'] {background-image: url('../images/icons/rss.png');}
|
||||
.social a[href*='twitter.com'] {background-image: url('../images/icons/twitter.png');}
|
||||
.social a[href*='linkedin.com'] {background-image: url('../images/icons/linkedin.png');}
|
||||
.social a[href*='gitorious.org'] {background-image: url('../images/icons/gitorious.org');}
|
||||
.social a[href*='gitorious.org'] {background-image: url('../images/icons/gitorious.png');}
|
||||
.social a[href*='gittip.com'] {background-image: url('../images/icons/gittip.png');}
|
||||
|
||||
/*
|
||||
About
|
||||
|
|
|
|||
BIN
pelican/themes/notmyidea/static/images/icons/gittip.png
Normal file
BIN
pelican/themes/notmyidea/static/images/icons/gittip.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 671 B |
|
|
@ -1,7 +1,7 @@
|
|||
PELICAN=$pelican
|
||||
PELICANOPTS=$pelicanopts
|
||||
|
||||
BASEDIR=$$(PWD)
|
||||
BASEDIR=$$(CURDIR)
|
||||
INPUTDIR=$$(BASEDIR)/content
|
||||
OUTPUTDIR=$$(BASEDIR)/output
|
||||
CONFFILE=$$(BASEDIR)/pelicanconf.py
|
||||
|
|
|
|||
|
|
@ -1,11 +1,11 @@
|
|||
#!/bin/bash
|
||||
#!/usr/bin/env bash
|
||||
##
|
||||
# This section should match your Makefile
|
||||
##
|
||||
PELICAN=$pelican
|
||||
PELICANOPTS=$pelicanopts
|
||||
|
||||
BASEDIR=$$(PWD)
|
||||
BASEDIR=$$(pwd)
|
||||
INPUTDIR=$$BASEDIR/content
|
||||
OUTPUTDIR=$$BASEDIR/output
|
||||
CONFFILE=$$BASEDIR/pelicanconf.py
|
||||
|
|
@ -65,6 +65,7 @@ function start_up(){
|
|||
python -m SimpleHTTPServer &
|
||||
echo $$! > $$SRV_PID
|
||||
cd $$BASEDIR
|
||||
sleep 1 && echo 'Pelican and SimpleHTTPServer processes now running in background.'
|
||||
}
|
||||
|
||||
###
|
||||
|
|
|
|||
|
|
@ -14,6 +14,9 @@ from operator import attrgetter
|
|||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
class NoFilesError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
def get_date(string):
|
||||
"""Return a datetime object from a string.
|
||||
|
|
@ -241,10 +244,13 @@ def files_changed(path, extensions):
|
|||
yield os.stat(os.path.join(root, f)).st_mtime
|
||||
|
||||
global LAST_MTIME
|
||||
mtime = max(file_times(path))
|
||||
if mtime > LAST_MTIME:
|
||||
LAST_MTIME = mtime
|
||||
return True
|
||||
try:
|
||||
mtime = max(file_times(path))
|
||||
if mtime > LAST_MTIME:
|
||||
LAST_MTIME = mtime
|
||||
return True
|
||||
except ValueError:
|
||||
raise NoFilesError("No files with the given extension(s) found.")
|
||||
return False
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue