How to use the mkdocs.utils.write_file function in mkdocs

To help you get started, we’ve selected a few mkdocs examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github mkdocs / mkdocs / mkdocs / contrib / search / __init__.py View on Github external
def on_post_build(self, config, **kwargs):
        "Build search index."
        output_base_path = os.path.join(config['site_dir'], 'search')
        search_index = self.search_index.generate_search_index()
        json_output_path = os.path.join(output_base_path, 'search_index.json')
        utils.write_file(search_index.encode('utf-8'), json_output_path)

        if not ('search_index_only' in config['theme'] and config['theme']['search_index_only']):
            # Include language support files in output. Copy them directly
            # so that only the needed files are included.
            files = []
            if len(self.config['lang']) > 1 or 'en' not in self.config['lang']:
                files.append('lunr.stemmer.support.js')
            if len(self.config['lang']) > 1:
                files.append('lunr.multi.js')
            for lang in self.config['lang']:
                if (lang != 'en'):
                    files.append('lunr.{}.js'.format(lang))

            for filename in files:
                from_path = os.path.join(base_path, 'lunr-language', filename)
                to_path = os.path.join(output_base_path, filename)
github mkdocs / mkdocs / mkdocs / commands / build.py View on Github external
file = files.get_file_from_path(template_name)
    if file is None:
        log.warning("Template skipped: '{}' not found in docs_dir.".format(template_name))
        return

    try:
        with open(file.abs_src_path, 'r', encoding='utf-8', errors='strict') as f:
            template = jinja2.Template(f.read())
    except Exception as e:
        log.warning("Error reading template '{}': {}".format(template_name, e))
        return

    output = _build_template(template_name, template, files, config, nav)

    if output.strip():
        utils.write_file(output.encode('utf-8'), file.abs_dest_path)
    else:
        log.info("Template skipped: '{}' generated empty output.".format(template_name))
github restaction / mkdocs-autodoc / mkdocs_autodoc / __init__.py View on Github external
raise
    # render autodoc contents
    tmplstr = io.open(TEMPLATE_PATH).read()
    template = env.from_string(tmplstr)
    contents, titles = parse_selected(input_content)
    table_of_contents = create_toc(titles)
    html_content = template.render(contents=contents)
    # render page
    meta = None
    context = get_global_context(site_navigation, config)
    context.update(get_page_context(
        page, html_content, table_of_contents, meta, config
    ))
    template = env.get_template('base.html')
    output_content = template.render(context)
    utils.write_file(output_content.encode('utf-8'), output_path)
    return html_content, table_of_contents, None
github mkdocs / mkdocs / mkdocs / commands / build.py View on Github external
def _build_theme_template(template_name, env, files, config, nav):
    """ Build a template using the theme environment. """

    log.debug("Building theme template: {}".format(template_name))

    try:
        template = env.get_template(template_name)
    except TemplateNotFound:
        log.warning("Template skipped: '{}' not found in theme directories.".format(template_name))
        return

    output = _build_template(template_name, template, files, config, nav)

    if output.strip():
        output_path = os.path.join(config['site_dir'], template_name)
        utils.write_file(output.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.encode('utf-8'))
    else:
        log.info("Template skipped: '{}' generated empty output.".format(template_name))
github mkdocs / mkdocs / mkdocs / commands / build.py View on Github external
# 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