本文整理汇总了Python中mkdocs.utils.write_file函数的典型用法代码示例。如果您正苦于以下问题:Python write_file函数的具体用法?Python write_file怎么用?Python write_file使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了write_file函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: build_pages
def build_pages(config):
"""
Builds all the pages and writes them into the build directory.
"""
site_navigation = nav.SiteNavigation(config['pages'], config['url_format'])
loader = jinja2.FileSystemLoader(config['theme_dir'])
env = jinja2.Environment(loader=loader)
for page in site_navigation.walk_pages():
# Read the input file
input_path = os.path.join(config['docs_dir'], page.input_path)
input_content = open(input_path, 'r').read().decode('utf-8')
# Process the markdown text
html_content, table_of_contents, meta = convert_markdown(input_content)
html_content = post_process_html(html_content, site_navigation, config['url_format'])
context = get_context(
page, html_content, site_navigation,
table_of_contents, meta, config
)
# Allow 'template:' override in md source files.
if 'template' in meta:
template = env.get_template(meta['template'][0])
else:
template = env.get_template('base.html')
# Render the template.
output_content = template.render(context)
# Write the output file.
output_path = os.path.join(config['site_dir'], page.output_path)
utils.write_file(output_content.encode('utf-8'), output_path)
示例2: build_pages
def build_pages(config, dump_json=False):
"""
Builds all the pages and writes them into the build directory.
"""
site_navigation = nav.SiteNavigation(config['pages'], config['use_directory_urls'])
loader = jinja2.FileSystemLoader(config['theme_dir'] + [config['mkdocs_templates'], ])
env = jinja2.Environment(loader=loader)
search_index = search.SearchIndex()
build_template('404.html', env, config, site_navigation)
if not build_template('search.html', env, config, site_navigation):
log.debug("Search is enabled but the theme doesn't contain a "
"search.html file. Assuming the theme implements search "
"within a modal.")
build_sitemap(config, env, site_navigation)
build_extra_templates(config['extra_templates'], config, site_navigation)
for page in site_navigation.walk_pages():
try:
log.debug("Building page %s", page.input_path)
build_result = _build_page(page, config, site_navigation, env,
dump_json)
html_content, table_of_contents, _ = build_result
search_index.add_entry_from_context(
page, html_content, table_of_contents)
except Exception:
log.error("Error building page %s", page.input_path)
raise
search_index = search_index.generate_search_index()
json_output_path = os.path.join(config['site_dir'], 'mkdocs', 'search_index.json')
utils.write_file(search_index.encode('utf-8'), json_output_path)
示例3: build_sitemap
def build_sitemap(config, env, site_navigation):
log.debug("Building sitemap.xml")
template = env.get_template('sitemap.xml')
context = get_global_context(site_navigation, config)
output_content = template.render(context)
output_path = os.path.join(config['site_dir'], 'sitemap.xml')
utils.write_file(output_content.encode('utf-8'), output_path)
示例4: _build_page
def _build_page(page, config, site_navigation, env, dump_json):
# Read the input file
input_path = os.path.join(config['docs_dir'], page.input_path)
try:
input_content = io.open(input_path, 'r', encoding='utf-8').read()
except IOError:
log.error('file not found: %s', input_path)
raise
# Process the markdown text
html_content, table_of_contents, meta = convert_markdown(
markdown_source=input_content,
config=config,
site_navigation=site_navigation
)
context = get_global_context(site_navigation, config)
context.update(get_page_context(
page, html_content, table_of_contents, meta, config
))
# Allow 'template:' override in md source files.
if 'template' in meta:
template = env.get_template(meta['template'][0])
else:
try:
template = env.get_template('main.html')
except jinja2.TemplateNotFound:
# TODO: Remove this in version 1.0
template = env.get_template('base.html')
log.warn(
"Your theme does not appear to contain a 'main.html' template. "
"The 'base.html' template was used instead, which is deprecated. "
"Update your theme so that the primary entry point is 'main.html'."
)
# Render the template.
output_content = template.render(context)
# Write the output file.
output_path = os.path.join(config['site_dir'], page.output_path)
if dump_json:
json_context = {
'content': context['content'],
'title': context['current_page'].title,
'url': context['current_page'].abs_url,
'language': 'en',
}
json_output = json.dumps(json_context, indent=4).encode('utf-8')
utils.write_file(json_output, output_path.replace('.html', '.json'))
else:
utils.write_file(output_content.encode('utf-8'), output_path)
return html_content, table_of_contents, meta
示例5: build_pages
def build_pages(config, dump_json=False):
"""
Builds all the pages and writes them into the build directory.
"""
site_navigation = nav.SiteNavigation(config['pages'], config['use_directory_urls'])
loader = jinja2.FileSystemLoader(config['theme_dir'])
env = jinja2.Environment(loader=loader)
build_404(config, env, site_navigation)
for page in site_navigation.walk_pages():
# Read the input file
input_path = os.path.join(config['docs_dir'], page.input_path)
try:
input_content = open(input_path, 'r').read()
except IOError:
log.error('file not found: %s' % input_path)
continue
if PY2:
input_content = input_content.decode('utf-8')
# Process the markdown text
html_content, table_of_contents, meta = convert_markdown(
input_content, site_navigation,
extensions=config['markdown_extensions'], strict=config['strict']
)
context = get_global_context(site_navigation, config)
context.update(get_page_context(
page, html_content, site_navigation,
table_of_contents, meta, config
))
# Allow 'template:' override in md source files.
if 'template' in meta:
template = env.get_template(meta['template'][0])
else:
template = env.get_template('base.html')
# Render the template.
output_content = template.render(context)
# Write the output file.
output_path = os.path.join(config['site_dir'], page.output_path)
if dump_json:
json_context = {
'content': context['content'],
'title': context['current_page'].title,
'url': context['current_page'].abs_url,
'language': 'en',
}
utils.write_file(json.dumps(json_context, indent=4).encode('utf-8'), output_path.replace('.html', '.json'))
else:
utils.write_file(output_content.encode('utf-8'), output_path)
示例6: build_404
def build_404(config, env, site_navigation):
try:
template = env.get_template("404.html")
except TemplateNotFound:
return
global_context = get_global_context(site_navigation, config)
output_content = template.render(global_context)
output_path = os.path.join(config["site_dir"], "404.html")
utils.write_file(output_content.encode("utf-8"), output_path)
示例7: build_404
def build_404(config, env, site_navigation):
log.debug("Building 404.html page")
try:
template = env.get_template('404.html')
except TemplateNotFound:
return
global_context = get_global_context(site_navigation, config)
output_content = template.render(global_context)
output_path = os.path.join(config['site_dir'], '404.html')
utils.write_file(output_content.encode('utf-8'), output_path)
示例8: _build_page
def _build_page(page, config, site_navigation, env, dirty=False):
""" Build a Markdown page and pass to theme template. """
# Run the `pre_page` plugin event
page = config['plugins'].run_event(
'pre_page', page, config=config, site_navigation=site_navigation
)
page.read_source(config=config)
# Run `page_markdown` plugin events.
page.markdown = config['plugins'].run_event(
'page_markdown', page.markdown, page=page, config=config, site_navigation=site_navigation
)
page.render(config, site_navigation)
# Run `page_content` plugin events.
page.content = config['plugins'].run_event(
'page_content', page.content, page=page, config=config, site_navigation=site_navigation
)
context = get_context(site_navigation, config, page)
# Allow 'template:' override in md source files.
if 'template' in page.meta:
template = env.get_template(page.meta['template'])
else:
template = env.get_template('main.html')
# Run `page_context` plugin events.
context = config['plugins'].run_event(
'page_context', context, page=page, config=config, site_navigation=site_navigation
)
# Render the template.
output_content = template.render(context)
# Run `post_page` plugin events.
output_content = config['plugins'].run_event(
'post_page', output_content, page=page, config=config
)
# Write the output file.
if output_content.strip():
utils.write_file(output_content.encode('utf-8'), page.abs_output_path)
else:
log.info("Page skipped: '{}'. Generated empty output.".format(page.title))
示例9: _build_page
def _build_page(page, config, site_navigation, env, dump_json):
# Read the input file
input_path = os.path.join(config['docs_dir'], page.input_path)
try:
input_content = io.open(input_path, 'r', encoding='utf-8').read()
except IOError:
log.error('file not found: %s', input_path)
raise
# Process the markdown text
html_content, table_of_contents, meta = convert_markdown(
markdown_source=input_content,
config=config,
site_navigation=site_navigation
)
context = get_global_context(site_navigation, config)
context.update(get_page_context(
page, html_content, table_of_contents, meta, config
))
# Allow 'template:' override in md source files.
if 'template' in meta:
template = env.get_template(meta['template'][0])
else:
template = env.get_template('base.html')
# Render the template.
output_content = template.render(context)
# Write the output file.
output_path = os.path.join(config['site_dir'], page.output_path)
if dump_json:
json_context = {
'content': context['content'],
'title': context['current_page'].title,
'url': context['current_page'].abs_url,
'language': 'en',
}
json_output = json.dumps(json_context, indent=4).encode('utf-8')
utils.write_file(json_output, output_path.replace('.html', '.json'))
else:
utils.write_file(output_content.encode('utf-8'), output_path)
return html_content, table_of_contents, meta
示例10: build_pages
def build_pages(config, dump_json=False):
"""
Builds all the pages and writes them into the build directory.
"""
site_navigation = nav.SiteNavigation(config["pages"], config["use_directory_urls"])
loader = jinja2.FileSystemLoader(config["theme_dir"])
env = jinja2.Environment(loader=loader)
build_404(config, env, site_navigation)
for page in site_navigation.walk_pages():
# Read the input file
input_path = os.path.join(config["docs_dir"], page.input_path)
input_content = open(input_path, "r").read()
if PY2:
input_content = input_content.decode("utf-8")
# Process the markdown text
html_content, table_of_contents, meta = convert_markdown(
input_content, extensions=config["markdown_extensions"]
)
html_content = post_process_html(html_content, site_navigation)
context = get_global_context(site_navigation, config)
context.update(get_page_context(page, html_content, site_navigation, table_of_contents, meta, config))
# Allow 'template:' override in md source files.
if "template" in meta:
template = env.get_template(meta["template"][0])
else:
template = env.get_template("base.html")
# Render the template.
output_content = template.render(context)
# Write the output file.
output_path = os.path.join(config["site_dir"], page.output_path)
if dump_json:
json_context = {
"content": context["content"],
"title": context["current_page"].title,
"url": context["current_page"].abs_url,
"language": "en",
}
utils.write_file(json.dumps(json_context, indent=4).encode("utf-8"), output_path.replace(".html", ".json"))
else:
utils.write_file(output_content.encode("utf-8"), output_path)
示例11: _build_page
def _build_page(page, config, files, nav, env, dirty=False):
""" Pass a Page to theme template and write output to site_dir. """
try:
# When --dirty is used, only build the page if the file has been modified since the
# previous build of the output.
if dirty and not page.file.is_modified():
return
log.debug("Building page {}".format(page.file.src_path))
# Activate page. Signals to theme that this is the current page.
page.active = True
context = get_context(nav, files, config, page)
# Allow 'template:' override in md source files.
if 'template' in page.meta:
template = env.get_template(page.meta['template'])
else:
template = env.get_template('main.html')
# Run `page_context` plugin events.
context = config['plugins'].run_event(
'page_context', context, page=page, config=config, nav=nav
)
# Render the template.
output = template.render(context)
# Run `post_page` plugin events.
output = config['plugins'].run_event(
'post_page', output, page=page, config=config
)
# Write the output file.
if output.strip():
utils.write_file(output.encode('utf-8', errors='xmlcharrefreplace'), page.file.abs_dest_path)
else:
log.info("Page skipped: '{}'. Generated empty output.".format(page.file.src_path))
# Deactivate page
page.active = False
except Exception as e:
log.error("Error building page '{}': {}".format(page.file.src_path, e))
raise
示例12: build_template
def build_template(template_name, env, config, site_navigation=None):
log.debug("Building template: %s", template_name)
try:
template = env.get_template(template_name)
except TemplateNotFound:
return False
context = {'page': None}
if site_navigation is not None:
context.update(get_global_context(site_navigation, config))
output_content = template.render(context)
output_path = os.path.join(config['site_dir'], template_name)
utils.write_file(output_content.encode('utf-8'), output_path)
return True
示例13: build_extra_templates
def build_extra_templates(extra_templates, config, site_navigation=None):
log.debug("Building extra_templates page")
for extra_template in extra_templates:
input_path = os.path.join(config['docs_dir'], extra_template)
with io.open(input_path, 'r', encoding='utf-8') as template_file:
template = jinja2.Template(template_file.read())
context = {'page': None}
if site_navigation is not None:
context.update(get_global_context(site_navigation, config))
output_content = template.render(context)
output_path = os.path.join(config['site_dir'], extra_template)
utils.write_file(output_content.encode('utf-8'), output_path)
示例14: build_template
def build_template(template_name, env, config, site_navigation=None, extra_context=None):
try:
template = env.get_template(template_name)
except TemplateNotFound:
return False
if site_navigation is not None:
context = get_global_context(site_navigation, config)
else:
context = {}
if extra_context is not None:
context.update(extra_context)
output_content = template.render(context)
output_path = os.path.join(config['site_dir'], template_name)
utils.write_file(output_content.encode('utf-8'), output_path)
return True
示例15: build_template
def build_template(template_name, env, config, site_navigation=None):
""" Build a template using the theme environment. """
log.debug("Building template: %s", template_name)
try:
template = env.get_template(template_name)
except TemplateNotFound:
log.info("Template skipped: '{}'. Not found in template directories.".format(template_name))
return
# Run `pre_template` plugin events.
template = config['plugins'].run_event(
'pre_template', template, template_name=template_name, config=config
)
context = get_context(site_navigation, config)
# Run `template_context` plugin events.
context = config['plugins'].run_event(
'template_context', context, template_name=template_name, config=config
)
output_content = template.render(context)
# Run `post_template` plugin events.
output_content = config['plugins'].run_event(
'post_template', output_content, template_name=template_name, config=config
)
if output_content.strip():
output_path = os.path.join(config['site_dir'], template_name)
utils.write_file(output_content.encode('utf-8'), output_path)
if template_name == 'sitemap.xml':
log.debug("Gzipping template: %s", template_name)
with gzip.open('{}.gz'.format(output_path), 'wb') as f:
f.write(output_content.encode('utf-8'))
else:
log.info("Template skipped: '{}'. Generated empty output.".format(template_name))