def build_pages(config, dump_json=False): site_navigation = nav.SiteNavigation(config['pages'], config['use_directory_urls']) loader = jinja2.FileSystemLoader(config['theme_dir'] + [config['obdocs_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'], 'obdocs', 'search_index.json') utils.write_file(search_index.encode('utf-8'), json_output_path)
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)
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
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 if site_navigation is not None: context = get_global_context(site_navigation, config) else: 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
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()) if site_navigation is not None: context = get_global_context(site_navigation, config) else: context = {} 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)