def build_pages(config): """ Builds all the pages and writes them into the build directory. """ site_navigation = nav.SiteNavigation(config['pages'], config['use_direcory_urls']) loader = jinja2.FileSystemLoader(config['theme_dir']) env = jinja2.Environment(loader=loader, trim_blocks=True) 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) 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)
def test_empty_toc_item(self): pages = [('index.md', ), ('about.md', 'About')] expected = dedent(""" About - /about/ """) site_navigation = nav.SiteNavigation(pages) self.assertEqual(str(site_navigation).strip(), expected) self.assertEqual(len(site_navigation.nav_items), 1) self.assertEqual(len(site_navigation.pages), 2)
def test_simple_toc(self): pages = [('index.md', 'Home'), ('about.md', 'About')] expected = dedent(""" Home - / About - /about/ """) site_navigation = nav.SiteNavigation(pages) self.assertEqual(str(site_navigation).strip(), expected) self.assertEqual(len(site_navigation.nav_items), 2) self.assertEqual(len(site_navigation.pages), 2)
def test_walk_empty_toc(self): pages = [('index.md', ), ('about.md', 'About')] expected = [ dedent(""" About - /about/ """), dedent(""" About - /about/ [*] """) ] site_navigation = nav.SiteNavigation(pages) for index, page in enumerate(site_navigation.walk_pages()): self.assertEqual(str(site_navigation).strip(), expected[index])
def test_indented_toc(self): pages = [('index.md', 'Home'), ('api-guide/running.md', 'API Guide', 'Running'), ('api-guide/testing.md', 'API Guide', 'Testing'), ('api-guide/debugging.md', 'API Guide', 'Debugging'), ('about/release-notes.md', 'About', 'Release notes'), ('about/license.md', 'About', 'License')] expected = dedent(""" Home - / API Guide Running - /api-guide/running/ Testing - /api-guide/testing/ Debugging - /api-guide/debugging/ About Release notes - /about/release-notes/ License - /about/license/ """) site_navigation = nav.SiteNavigation(pages) self.assertEqual(str(site_navigation).strip(), expected) self.assertEqual(len(site_navigation.nav_items), 3) self.assertEqual(len(site_navigation.pages), 6)
def test_walk_indented_toc(self): pages = [('index.md', 'Home'), ('api-guide/running.md', 'API Guide', 'Running'), ('api-guide/testing.md', 'API Guide', 'Testing'), ('api-guide/debugging.md', 'API Guide', 'Debugging'), ('about/release-notes.md', 'About', 'Release notes'), ('about/license.md', 'About', 'License')] expected = [ dedent(""" Home - / [*] API Guide Running - /api-guide/running/ Testing - /api-guide/testing/ Debugging - /api-guide/debugging/ About Release notes - /about/release-notes/ License - /about/license/ """), dedent(""" Home - / API Guide [*] Running - /api-guide/running/ [*] Testing - /api-guide/testing/ Debugging - /api-guide/debugging/ About Release notes - /about/release-notes/ License - /about/license/ """), dedent(""" Home - / API Guide [*] Running - /api-guide/running/ Testing - /api-guide/testing/ [*] Debugging - /api-guide/debugging/ About Release notes - /about/release-notes/ License - /about/license/ """), dedent(""" Home - / API Guide [*] Running - /api-guide/running/ Testing - /api-guide/testing/ Debugging - /api-guide/debugging/ [*] About Release notes - /about/release-notes/ License - /about/license/ """), dedent(""" Home - / API Guide Running - /api-guide/running/ Testing - /api-guide/testing/ Debugging - /api-guide/debugging/ About [*] Release notes - /about/release-notes/ [*] License - /about/license/ """), dedent(""" Home - / API Guide Running - /api-guide/running/ Testing - /api-guide/testing/ Debugging - /api-guide/debugging/ About [*] Release notes - /about/release-notes/ License - /about/license/ [*] """) ] site_navigation = nav.SiteNavigation(pages) for index, page in enumerate(site_navigation.walk_pages()): self.assertEqual(str(site_navigation).strip(), expected[index])