本文整理汇总了Python中nikola.utils.write_metadata函数的典型用法代码示例。如果您正苦于以下问题:Python write_metadata函数的具体用法?Python write_metadata怎么用?Python write_metadata使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了write_metadata函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_write_metadata_with_formats
def test_write_metadata_with_formats():
data = {'slug': 'hello-world', 'title': 'Hello, world!', 'b': '2', 'a': '1'}
# Nikola: defaults first, then sorted alphabetically
# YAML: all sorted alphabetically
# TOML: insertion order (py3.6), random (py3.5 or older)
assert write_metadata(data, 'nikola') == """\
.. title: Hello, world!
.. slug: hello-world
.. a: 1
.. b: 2
"""
assert write_metadata(data, 'yaml') == """\
---
a: '1'
b: '2'
slug: hello-world
title: Hello, world!
---
"""
toml = write_metadata(data, 'toml')
assert toml.startswith('+++\n')
assert toml.endswith('+++\n')
assert 'slug = "hello-world"' in toml
assert 'title = "Hello, world!"' in toml
assert 'b = "2"' in toml
assert 'a = "1"' in toml
示例2: test_write_metadata_from_site_and_fallbacks
def test_write_metadata_from_site_and_fallbacks():
site = dummy()
site.config = {'METADATA_FORMAT': 'yaml'}
data = {'title': 'xx'}
assert write_metadata(data, site=site) == '---\ntitle: xx\n---\n'
assert write_metadata(data) == '.. title: xx\n\n'
assert write_metadata(data, 'foo') == '.. title: xx\n\n'
assert write_metadata(data, 'filename_regex') == '.. title: xx\n\n'
示例3: test_write_metadata_pelican_detection
def test_write_metadata_pelican_detection():
rest_fake, md_fake, html_fake = dummy(), dummy(), dummy()
rest_fake.name = 'rest'
md_fake.name = 'markdown'
html_fake.name = 'html'
data = {'title': 'xx'}
assert write_metadata(data, 'pelican', compiler=rest_fake) == '==\nxx\n==\n\n'
assert write_metadata(data, 'pelican', compiler=md_fake) == 'title: xx\n\n'
assert write_metadata(data, 'pelican', compiler=html_fake) == '.. title: xx\n\n'
assert write_metadata(data, 'pelican', compiler=None) == '.. title: xx\n\n'
示例4: test_write_metadata_comment_wrap
def test_write_metadata_comment_wrap():
data = {'title': 'Hello, world!', 'slug': 'hello-world'}
assert write_metadata(data, 'nikola') == """\
.. title: Hello, world!
.. slug: hello-world
"""
assert write_metadata(data, 'nikola', True) == """\
<!--
.. title: Hello, world!
.. slug: hello-world
-->
"""
assert write_metadata(data, 'nikola', ('111', '222')) == """\
示例5: create_post
def create_post(self, path, **kw):
content = kw.pop('content', None)
onefile = kw.pop('onefile', False)
kw.pop('is_page', False)
metadata = OrderedDict()
metadata.update(self.default_metadata)
metadata.update(kw)
makedirs(os.path.dirname(path))
with codecs.open(path, "wb+", "utf8") as fd:
if onefile:
fd.write("#+BEGIN_COMMENT\n")
if write_metadata:
fd.write(write_metadata(metadata))
else:
for k, v in metadata.items():
fd.write('.. {0}: {1}\n'.format(k, v))
fd.write("#+END_COMMENT\n")
fd.write("\n\n")
if content:
fd.write(content)
else:
fd.write('Write your post here.')
示例6: create_post
def create_post(self, path, **kw):
"""Create a new post."""
content = kw.pop('content', None)
onefile = kw.pop('onefile', False)
# is_page is not used by create_post as of now.
kw.pop('is_page', False)
metadata = {}
metadata.update(self.default_metadata)
metadata.update(kw)
if not metadata['description']:
# For PHP, a description must be set. Otherwise, Nikola will
# take the first 200 characters of the post as the Open Graph
# description (og:description meta element)!
# If the PHP source leaks there:
# (a) The script will be executed multiple times
# (b) PHP may encounter a syntax error if it cuts too early,
# therefore completely breaking the page
# Here, we just use the title. The user should come up with
# something better, but just using the title does the job.
metadata['description'] = metadata['title']
makedirs(os.path.dirname(path))
if not content.endswith('\n'):
content += '\n'
with io.open(path, "w+", encoding="utf8") as fd:
if onefile:
fd.write(write_metadata(metadata, comment_wrap=True, site=self.site, compiler=self))
fd.write(content)
示例7: test_write_metadata_compiler
def test_write_metadata_compiler():
data = {'title': 'Hello, world!', 'slug': 'hello-world'}
assert write_metadata(data, 'rest_docinfo') == """\
=============
Hello, world!
=============
:slug: hello-world
"""
assert write_metadata(data, 'markdown_meta') in ("""\
title: Hello, world!
slug: hello-world
""", """slug: hello-world
title: Hello, world!
""")
示例8: write_metadata
def write_metadata(filename, title, slug, post_date, description, tags, **kwargs):
if not description:
description = ""
utils.makedirs(os.path.dirname(filename))
with io.open(filename, "w+", encoding="utf8") as fd:
data = {'title': title, 'slug': slug, 'date': post_date, 'tags': ','.join(tags), 'description': description}
data.update(kwargs)
fd.write(utils.write_metadata(data))
示例9: write_metadata
def write_metadata(filename, title, slug, post_date, description, tags, **kwargs):
"""Write metadata to meta file."""
if not description:
description = ""
utils.makedirs(os.path.dirname(filename))
with io.open(filename, "w+", encoding="utf8") as fd:
data = {"title": title, "slug": slug, "date": post_date, "tags": ",".join(tags), "description": description}
data.update(kwargs)
fd.write(utils.write_metadata(data))
示例10: write_metadata
def write_metadata(self, filename, title, slug, post_date, description, tags, **kwargs):
"""Write metadata to meta file."""
if not description:
description = ""
utils.makedirs(os.path.dirname(filename))
with io.open(filename, "w+", encoding="utf8") as fd:
data = {'title': title, 'slug': slug, 'date': post_date, 'tags': ','.join(tags), 'description': description}
data.update(kwargs)
fd.write(utils.write_metadata(data, site=self.site, comment_wrap=False))
示例11: create_post
def create_post(self, path, content=None, onefile=False, is_page=False, **kw):
"""Create post file with optional metadata."""
metadata = OrderedDict()
metadata.update(self.default_metadata)
metadata.update(kw)
makedirs(os.path.dirname(path))
if not content.endswith('\n'):
content += '\n'
with codecs.open(path, "wb+", "utf8") as fd:
if onefile:
fd.write("////\n")
fd.write(write_metadata(metadata))
fd.write("////\n")
fd.write(content)
示例12: create_post
def create_post(self, path, **kw):
content = kw.pop('content', 'Write your post here.')
one_file = kw.pop('onefile', False) # NOQA
is_page = kw.pop('is_page', False) # NOQA
metadata = OrderedDict()
metadata.update(self.default_metadata)
metadata.update(kw)
makedirs(os.path.dirname(path))
if not content.endswith('\n'):
content += '\n'
with codecs.open(path, "wb+", "utf8") as fd:
if one_file:
fd.write(write_metadata(metadata))
fd.write('\n')
fd.write(content)
示例13: create_post
def create_post(self, path, **kw):
content = kw.pop('content', None)
onefile = kw.pop('onefile', False)
# is_page is not used by create_post as of now.
kw.pop('is_page', False)
metadata = {}
metadata.update(self.default_metadata)
metadata.update(kw)
makedirs(os.path.dirname(path))
if not content.endswith('\n'):
content += '\n'
with io.open(path, "w+", encoding="utf8") as fd:
if onefile:
fd.write(write_metadata(metadata))
fd.write('\n' + content)
示例14: create_post
def create_post(self, path, content, onefile=False, is_page=False, **kw):
content = kw.pop("content", "Write your post here.")
onefile = kw.pop("onefile", False)
kw.pop("is_page", False)
metadata = OrderedDict()
metadata.update(self.default_metadata)
metadata.update(kw)
makedirs(os.path.dirname(path))
if not content.endswith("\n"):
content += "\n"
with codecs.open(path, "wb+", "utf8") as fd:
if onefile:
fd.write("<!-- \n")
fd.write(write_metadata(metadata))
fd.write("-->\n\n")
fd.write(content)
示例15: create_post
def create_post(self, path, **kw):
content = kw.pop('content', None)
onefile = kw.pop('onefile', False)
kw.pop('is_page', False)
metadata = OrderedDict()
metadata.update(self.default_metadata)
metadata.update(kw)
makedirs(os.path.dirname(path))
if not content.endswith('\n'):
content += '\n'
with codecs.open(path, "wb+", "utf8") as fd:
if onefile:
fd.write('<notextile> <!--\n')
fd.write(write_metadata(metadata))
fd.write('--></notextile>\n\n')
fd.write(content)