def new(app, draft=True, title=None, section='pages', filename=None): """ Create a new empty post. :kwarg draft: (bool) should the page be marked as published or not. Defaults to false :kwarg title: (str) The unescaped title for the new post or page :kwarg section: (str) Which section to put the new writing in. Options are currently 'pages' or 'posts'. :kwarg filename: (str) file name to be used. If not supplied, one will be determined based on title """ post_date = datetime.today() title = str(title) if title else u'Untitled Post' if not filename: filename = u'%s.md' % slugify(title) pathargs = [filename] if section != 'pages': pathargs = [section] + pathargs filepath = get_file_path(app, pathargs) if os.path.exists(filepath): raise Exception('File %s exists' % filepath) content = '\n'.join([ u'title: {}'.format(title), u'date: {}'.format(post_date.strftime('%Y-%m-%d')), u'type: post', u'published: False', u'tags:\n\n', u'# {}\n'.format(title) ]) try: codecs.open(filepath, 'w', encoding='utf8').write(content) print(u'Created {}'.format(filepath)) except Exception: print('Problem writing to {}'.format(filepath)) raise
def new(app, draft=True, title=None, section="posts", filename=None): """ Create a new empty post. :kwarg: draft (bool) - should the page be marked as published or not. Defaults to false :kwarg: title (str) - The unescaped title for the new post or page :kwarg: section(str) - Which section to put the new writing in. Options are currently 'pages' or 'posts'. :kwarg: filename (str) - file name to be used. If not supplied, one will be determined based on title """ post_date = datetime.today() title = unicode(title) if title else u"Untitled Post" if not filename: filename = u"%s.md" % slugify(title) pathargs = [section, filename] filepath = get_file_path(app, pathargs) if os.path.exists(filepath): raise Exception("File %s exists" % filepath) content = "\n".join( [ u"title: {}".format(title), u"date: {}".format(post_date.strftime("%Y-%m-%d")), u"type: post", u"published: False", u"tags:\n\n", u"# {}\n".format(title), ] ) try: codecs.open(filepath, "w", encoding="utf8").write(content) logger.info(u"Created %s" % filepath) except Exception: print "Problem writing to {}".format(filepath) raise
def test_slugify(title, expected): assert slugify(title) == expected