new.py (view raw)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
#!/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 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+TZ 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(shared.ARROWISO)
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 = shared.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 'bookmark'
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)
|