mirror of
https://github.com/getpelican/pelican.git
synced 2025-10-15 20:28:56 +02:00
Added strip raw option to wordpress xml importer
This commit is contained in:
parent
1779b66f10
commit
ba8ed9fb18
4 changed files with 728 additions and 8 deletions
|
|
@ -6,6 +6,11 @@ __all__ = [
|
|||
|
||||
import os
|
||||
import subprocess
|
||||
import re
|
||||
import sys
|
||||
import cStringIO
|
||||
|
||||
from functools import wraps
|
||||
from contextlib import contextmanager
|
||||
from tempfile import mkdtemp
|
||||
from shutil import rmtree
|
||||
|
|
@ -28,8 +33,87 @@ def temporary_folder():
|
|||
# do whatever you want
|
||||
"""
|
||||
tempdir = mkdtemp()
|
||||
yield tempdir
|
||||
rmtree(tempdir)
|
||||
try:
|
||||
yield tempdir
|
||||
finally:
|
||||
rmtree(tempdir)
|
||||
|
||||
|
||||
def isplit(s, sep=None):
|
||||
"""
|
||||
Behave like str.split but returns a generator instead of a list.
|
||||
|
||||
>>> list(isplit('\tUse the force\n')) == '\tUse the force\n'.split()
|
||||
True
|
||||
>>> list(isplit('\tUse the force\n')) == ['Use', 'the', 'force']
|
||||
True
|
||||
>>> list(isplit('\tUse the force\n', "e")) == '\tUse the force\n'.split("e")
|
||||
True
|
||||
>>> list(isplit('Use the force', "e")) == 'Use the force'.split("e")
|
||||
True
|
||||
>>> list(isplit('Use the force', "e")) == ['Us', ' th', ' forc', '']
|
||||
True
|
||||
|
||||
"""
|
||||
sep, hardsep = r'\s+' if sep is None else re.escape(sep), sep is not None
|
||||
exp, pos, l = re.compile(sep), 0, len(s)
|
||||
while True:
|
||||
m = exp.search(s, pos)
|
||||
if not m:
|
||||
if pos < l or hardsep:
|
||||
# ^ mimic "split()": ''.split() returns []
|
||||
yield s[pos:]
|
||||
break
|
||||
start = m.start()
|
||||
if pos < start or hardsep:
|
||||
# ^ mimic "split()": includes trailing empty string
|
||||
yield s[pos:start]
|
||||
pos = m.end()
|
||||
|
||||
|
||||
def mute(returns_output=False):
|
||||
"""
|
||||
Decorate a function that prints to stdout, intercepting the output.
|
||||
If "returns_output" is True, the function will return a generator
|
||||
yielding the printed lines instead of the return values.
|
||||
|
||||
The decorator litterally hijack sys.stdout during each function
|
||||
execution, so be careful with what you apply it to.
|
||||
|
||||
>>> def numbers():
|
||||
print "42"
|
||||
print "1984"
|
||||
...
|
||||
>>> numbers()
|
||||
42
|
||||
1984
|
||||
>>> mute()(numbers)()
|
||||
>>> list(mute(True)(numbers)())
|
||||
['42', '1984']
|
||||
|
||||
"""
|
||||
|
||||
def decorator(func):
|
||||
|
||||
@wraps(func)
|
||||
def wrapper(*args, **kwargs):
|
||||
|
||||
saved_stdout = sys.stdout
|
||||
sys.stdout = cStringIO.StringIO()
|
||||
|
||||
try:
|
||||
out = func(*args, **kwargs)
|
||||
if returns_output:
|
||||
out = isplit(sys.stdout.getvalue().strip())
|
||||
finally:
|
||||
sys.stdout = saved_stdout
|
||||
|
||||
return out
|
||||
|
||||
return wrapper
|
||||
|
||||
return decorator
|
||||
|
||||
|
||||
|
||||
def get_article(title, slug, content, lang, extra_metadata=None):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue