mirror of
https://github.com/getpelican/pelican.git
synced 2025-10-15 20:28:56 +02:00
Welcome Pelican 2.0 ! Refactoring of the internals to be more extensible.
--HG-- rename : pelican/bloggenerator.py => pelican/generators.py
This commit is contained in:
parent
5e2ca2dd56
commit
fdb920e50a
9 changed files with 430 additions and 337 deletions
|
|
@ -1,4 +1,51 @@
|
|||
import re
|
||||
from datetime import datetime
|
||||
from codecs import open as _open
|
||||
|
||||
def update_dict(mapping, key, value):
|
||||
"""Update a dict intenal list
|
||||
|
||||
:param mapping: the mapping to update
|
||||
:param key: the key of the mapping to update.
|
||||
:param value: the value to append to the list.
|
||||
"""
|
||||
if key not in mapping:
|
||||
mapping[key] = []
|
||||
mapping[key].append(value)
|
||||
|
||||
|
||||
def get_date(string):
|
||||
"""Return a datetime object from a string.
|
||||
|
||||
If no format matches the given date, raise a ValuEerror
|
||||
"""
|
||||
formats = ['%Y-%m-%d %H:%M', '%Y/%m/%d %H:%M', '%Y-%m-%d', '%Y/%m/%d',
|
||||
'%d/%m/%Y']
|
||||
for date_format in formats:
|
||||
try:
|
||||
return datetime.strptime(string, date_format)
|
||||
except ValueError:
|
||||
pass
|
||||
raise ValueError("'%s' is not a valid date" % string)
|
||||
|
||||
|
||||
def open(filename):
|
||||
"""Open a file and return it's content"""
|
||||
return _open(filename, encoding='utf-8').read()
|
||||
|
||||
|
||||
def slugify(value):
|
||||
"""
|
||||
Normalizes string, converts to lowercase, removes non-alpha characters,
|
||||
and converts spaces to hyphens.
|
||||
|
||||
Took from django sources.
|
||||
"""
|
||||
import unicodedata
|
||||
value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore')
|
||||
value = unicode(re.sub('[^\w\s-]', '', value).strip().lower())
|
||||
return re.sub('[-\s]+', '-', value)
|
||||
|
||||
|
||||
def truncate_html_words(s, num, end_text='...'):
|
||||
"""Truncates HTML to a certain number of words (not counting tags and
|
||||
|
|
@ -13,6 +60,7 @@ def truncate_html_words(s, num, end_text='...'):
|
|||
if length <= 0:
|
||||
return u''
|
||||
html4_singlets = ('br', 'col', 'link', 'base', 'img', 'param', 'area', 'hr', 'input')
|
||||
|
||||
# Set up regular expressions
|
||||
re_words = re.compile(r'&.*?;|<.*?>|(\w[\w-]*)', re.U)
|
||||
re_tag = re.compile(r'<(/)?([^ ]+?)(?: (/)| .*?)?>')
|
||||
|
|
@ -65,3 +113,4 @@ def truncate_html_words(s, num, end_text='...'):
|
|||
out += '</%s>' % tag
|
||||
# Return string
|
||||
return out
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue