mirror of
https://github.com/getpelican/pelican.git
synced 2025-10-15 20:28:56 +02:00
Merge 230fa6bd8e into 8005f675a7
This commit is contained in:
commit
67893592f3
4 changed files with 97 additions and 2 deletions
63
pelican/tools/pelican_article.py
Normal file
63
pelican/tools/pelican_article.py
Normal file
|
|
@ -0,0 +1,63 @@
|
||||||
|
#!/usr/bin/env python
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
import argparse
|
||||||
|
import os
|
||||||
|
import shutil
|
||||||
|
import sys
|
||||||
|
import datetime
|
||||||
|
|
||||||
|
from pelican.utils import slugify
|
||||||
|
from pelican.tools.pelican_import import build_markdown_header, build_header
|
||||||
|
|
||||||
|
|
||||||
|
def create_template(args):
|
||||||
|
|
||||||
|
if not os.path.exists(args.path):
|
||||||
|
try:
|
||||||
|
os.mkdir(args.path)
|
||||||
|
except OSError:
|
||||||
|
error = 'Unable to create the output folder: ' + args.path
|
||||||
|
exit(error)
|
||||||
|
|
||||||
|
title = args.title.decode('utf-8')
|
||||||
|
slug = args.slug if args.slug else slugify(title)
|
||||||
|
date = datetime.datetime.now().strftime("%Y-%m-%d %H:%M")
|
||||||
|
categories = args.categories.split('\W+') if args.categories else ''
|
||||||
|
tags = args.tags.split('\W+') if args.tags else ''
|
||||||
|
|
||||||
|
if (args.markup == 'markdown') or (args.markup == 'md') :
|
||||||
|
ext = '.md'
|
||||||
|
header = build_markdown_header(title, date, args.author, categories, tags, slug)
|
||||||
|
else:
|
||||||
|
ext = '.rst'
|
||||||
|
header = build_header(title, date, args.author, categories, tags, slug)
|
||||||
|
|
||||||
|
filename = os.path.join(args.path, slug+ext)
|
||||||
|
header = header.encode('utf-8')
|
||||||
|
|
||||||
|
with open(filename, 'w') as fs:
|
||||||
|
fs.write(header)
|
||||||
|
|
||||||
|
print 'Article template create in "%s"' % filename
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
|
||||||
|
argv = sys.argv[1:]
|
||||||
|
argv.append('-h') if len(argv) == 0 else None
|
||||||
|
|
||||||
|
parser = argparse.ArgumentParser(description='Create article with templation')
|
||||||
|
|
||||||
|
parser.add_argument('title', help='article title')
|
||||||
|
parser.add_argument('-a', '--author', dest='author', default='[name]', help='Article author')
|
||||||
|
parser.add_argument('-t', '--tags', dest='tags', default=None, help='Article tags(separated by comma)')
|
||||||
|
parser.add_argument('-c', '--categories', dest='categories', default=None, help='Article categories')
|
||||||
|
parser.add_argument('-s', '--slug', dest='slug', default=None, help='Article slug')
|
||||||
|
parser.add_argument('-p', '--path', dest='path', default='content/posts', help='Article save directory')
|
||||||
|
parser.add_argument('-m', '--markup', dest='markup', default='md', help='Article markup format (supports rst & markdown)')
|
||||||
|
args = parser.parse_args(argv)
|
||||||
|
|
||||||
|
create_template(args);
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -14,6 +14,7 @@ import re
|
||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
import time
|
import time
|
||||||
|
import re
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from codecs import open
|
from codecs import open
|
||||||
|
|
@ -326,6 +327,29 @@ def posterous2fields(api_token, email, password):
|
||||||
yield (post.get('title'), post.get('body_cleaned'), slug, date,
|
yield (post.get('title'), post.get('body_cleaned'), slug, date,
|
||||||
post.get('user').get('display_name'), [], tags, kind, "html")
|
post.get('user').get('display_name'), [], tags, kind, "html")
|
||||||
|
|
||||||
|
def chyrp2fields(atom):
|
||||||
|
"""Opens a Chyrp Atom file, and yield pelican fields"""
|
||||||
|
import feedparser
|
||||||
|
import markdown
|
||||||
|
|
||||||
|
d = feedparser.parse(atom)
|
||||||
|
for entry in d.entries:
|
||||||
|
|
||||||
|
if entry.chyrp_status == 'public' and entry.chyrp_feather == 'text':
|
||||||
|
# Chyrp support both html and markdown, must convert by finding type
|
||||||
|
# content = markdown.markdown(entry.summary)
|
||||||
|
content = HTMLParser().unescape(entry.summary)
|
||||||
|
|
||||||
|
date = (time.strftime("%Y-%m-%d %H:%M", entry.updated_parsed)
|
||||||
|
if hasattr(entry, "updated_parsed") else None)
|
||||||
|
author = entry.author if hasattr(entry, "author") else None
|
||||||
|
tags = entry.tags if hasattr(entry, "tags") else None
|
||||||
|
slug = entry.chyrp_url if hasattr(entry, "chyrp_url") else None
|
||||||
|
tags = [tag[1] for tag in re.findall(r"(.*)\:\s*\"(.*)\"", entry.tags)] if hasattr(entry, "tags") else None
|
||||||
|
|
||||||
|
yield (entry.title, content, slug, date, author, [], tags, "html")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def tumblr2fields(api_key, blogname):
|
def tumblr2fields(api_key, blogname):
|
||||||
""" Imports Tumblr posts (API v2)"""
|
""" Imports Tumblr posts (API v2)"""
|
||||||
|
|
@ -561,6 +585,8 @@ def main():
|
||||||
help='Wordpress XML export')
|
help='Wordpress XML export')
|
||||||
parser.add_argument('--dotclear', action='store_true', dest='dotclear',
|
parser.add_argument('--dotclear', action='store_true', dest='dotclear',
|
||||||
help='Dotclear export')
|
help='Dotclear export')
|
||||||
|
parser.add_argument('--chyrp', action='store_true', dest='chyrp',
|
||||||
|
help='Chyrp Atom export')
|
||||||
parser.add_argument('--posterous', action='store_true', dest='posterous',
|
parser.add_argument('--posterous', action='store_true', dest='posterous',
|
||||||
help='Posterous export')
|
help='Posterous export')
|
||||||
parser.add_argument('--tumblr', action='store_true', dest='tumblr',
|
parser.add_argument('--tumblr', action='store_true', dest='tumblr',
|
||||||
|
|
@ -600,6 +626,8 @@ def main():
|
||||||
input_type = 'wordpress'
|
input_type = 'wordpress'
|
||||||
elif args.dotclear:
|
elif args.dotclear:
|
||||||
input_type = 'dotclear'
|
input_type = 'dotclear'
|
||||||
|
elif args.chyrp:
|
||||||
|
input_type = 'chyrp'
|
||||||
elif args.posterous:
|
elif args.posterous:
|
||||||
input_type = 'posterous'
|
input_type = 'posterous'
|
||||||
elif args.tumblr:
|
elif args.tumblr:
|
||||||
|
|
@ -607,7 +635,7 @@ def main():
|
||||||
elif args.feed:
|
elif args.feed:
|
||||||
input_type = 'feed'
|
input_type = 'feed'
|
||||||
else:
|
else:
|
||||||
error = "You must provide either --wpfile, --dotclear, --posterous, --tumblr or --feed options"
|
error = "You must provide either --wpfile, --dotclear, --posterous, --tumblr, --chyrp or --feed options"
|
||||||
exit(error)
|
exit(error)
|
||||||
|
|
||||||
if not os.path.exists(args.output):
|
if not os.path.exists(args.output):
|
||||||
|
|
@ -621,6 +649,8 @@ def main():
|
||||||
fields = wp2fields(args.input)
|
fields = wp2fields(args.input)
|
||||||
elif input_type == 'dotclear':
|
elif input_type == 'dotclear':
|
||||||
fields = dc2fields(args.input)
|
fields = dc2fields(args.input)
|
||||||
|
elif input_type == 'chyrp':
|
||||||
|
fields = chyrp2fields(args.input)
|
||||||
elif input_type == 'posterous':
|
elif input_type == 'posterous':
|
||||||
fields = posterous2fields(args.input, args.email, args.password)
|
fields = posterous2fields(args.input, args.email, args.password)
|
||||||
elif input_type == 'tumblr':
|
elif input_type == 'tumblr':
|
||||||
|
|
|
||||||
3
setup.py
3
setup.py
|
|
@ -9,7 +9,8 @@ entry_points = {
|
||||||
'pelican = pelican:main',
|
'pelican = pelican:main',
|
||||||
'pelican-import = pelican.tools.pelican_import:main',
|
'pelican-import = pelican.tools.pelican_import:main',
|
||||||
'pelican-quickstart = pelican.tools.pelican_quickstart:main',
|
'pelican-quickstart = pelican.tools.pelican_quickstart:main',
|
||||||
'pelican-themes = pelican.tools.pelican_themes:main'
|
'pelican-themes = pelican.tools.pelican_themes:main',
|
||||||
|
'pelican-article = pelican.tools.pelican_article:main'
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
1
tests/content/chyrpexport.atom
Executable file
1
tests/content/chyrpexport.atom
Executable file
File diff suppressed because one or more lines are too long
Loading…
Add table
Add a link
Reference in a new issue