nasg/new.py
2017-05-23 11:14:47 +01:00

132 lines
4.6 KiB
Python

#!/usr/bin/env python3
import os
import sys
import arrow
import argparse
import frontmatter
import glob
import sys
import tempfile
from slugify import slugify
import nasg
import shared
if __name__ == '__main__':
# --- set params
slugs = [os.path.splitext(i)[0] for i in list(map(
os.path.basename, glob.glob(
os.path.join(
shared.config.get('source', 'contentdir'),
"*",
"*.md"
)
)
))]
categories = list(map(
os.path.basename, glob.glob(
os.path.join(
shared.config.get('source', 'contentdir'),
"*",
)
)
))
now = arrow.utcnow()
parser = argparse.ArgumentParser(description='create doc and print it to stdout')
parser.add_argument('--tags', '-t', help='; separated, quoted list of tags')
parser.add_argument('--date', '-d', help=' YYYY-mm-ddTHH:MM:SS+TZTZ formatted date, if not now')
parser.add_argument('--slug', '-s', help='slug (normally autogenerated from title or pubdate)')
parser.add_argument('--title', '-l', help='title of new entry')
parser.add_argument('--bookmark', '-b', help='URL to bookmark')
parser.add_argument('--reply', '-r', help='URL to reply to')
parser.add_argument('--repost', '-p', help='URL to repost')
parser.add_argument('--content', '-c', help='content of entry')
parser.add_argument('--summary', '-u', help='summary of entry')
parser.add_argument('--redirect', '-i', help='; separated, quoted list of redirects')
args = vars(parser.parse_args())
if not args['date']:
d = now.format("YYYY-MM-DDTHH:mm:ssZ")
args['date'] = input('Date [%s]: ' % (d)) or d
if not args['title']:
args['title'] = input('Title []: ') or ''
if not args['tags']:
args['tags'] = input('Tags (separated by ;) []: ') or None
if args['tags']:
args['tags'] = args['tags'].split(';')
if not args['bookmark']:
args['bookmark'] = input('Bookmark of URL []: ') or ''
if not args['reply']:
args['reply'] = input('Reply to URL []: ') or ''
if not args['repost']:
args['repost'] = input('Repost of URL []: ') or ''
if not args['slug']:
if args['title']:
slug = slugify(args['title'], only_ascii=True, lower=True)
elif args['bookmark']:
slug = slugify("re: %s" % (args['bookmark']), only_ascii=True, lower=True)
elif args['reply']:
slug = slugify("re: %s" % (args['reply']), only_ascii=True, lower=True)
elif args['repost']:
slug = slugify("re: %s" % (args['repost']), only_ascii=True, lower=True)
else:
slug = nasg.Singular.baseN(now.timestamp)
args['slug'] = input('Slug [%s]: ' % (slug)) or slug
if args['slug'] in slugs:
print("This slug already exists: %s", args['slug'])
slugbase = args['slug']
inc = 1
while args['slug'] in slugs:
args['slug'] = "%s-%d" % (slugbase, inc)
inc = inc+1
print("Using %s as slug", args['slug'])
if not args['summary']:
args['summary'] = input('Summary []: ') or ''
if not args['content']:
args['content'] = input('Content []: ') or ''
if not args['redirect']:
args['redirect'] = input('Additional slugs (separated by ;) []: ') or None
if args['redirect']:
args['redirect'] = args['redirect'].split(';')
doc = frontmatter.loads('')
slug = args['slug']
del(args['slug'])
content = args['content']
del(args['content'])
repl = {
'repost': 'repost-of',
'bookmark': 'bookmark-of',
'reply': 'in-reply-to',
'date': 'published',
}
for orig, new in repl.items():
args[new] = args[orig]
del(args[orig])
doc.metadata = dict((k, v) for k, v in args.items() if v)
doc.content = content
tmpsave = os.path.join(tempfile.gettempdir(), "%s.md" % slug)
saveto = input('Save to: [%s]: ' % categories) or tmpsave
if tmpsave != saveto:
saveto = os.path.join(shared.config.get('source', 'contentdir'), saveto, "%s.md" % slug)
with open(saveto, 'wt') as f:
f.write(frontmatter.dumps(doc))
print("wrote file to:\n%s" % saveto)