Skip to content

Commit

Permalink
build_node will skip depending on Node.buildPage
Browse files Browse the repository at this point in the history
  • Loading branch information
JasperVanDenBosch committed Sep 25, 2024
1 parent b13ab25 commit 6a4e692
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 14 deletions.
36 changes: 22 additions & 14 deletions syrinx/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,27 @@ def dir_exists_not_empty(path: str) -> bool:
return True
return False

def build_node(
node: ContentNode,
root: ContentNode,
parent_path: str,
template_dir: str,
env: Environment
):
"""Recursive function to render page, then move on to children
"""
if node.buildPage:
fname_tem = choose_template_file(node, isfile, template_dir)
page_template = env.get_template(fname_tem)
html = page_template.render(index=node, root=root)
node_path = join(parent_path, node.name)
os.makedirs(node_path, exist_ok=True)
out_fpath = join(node_path, 'index.html')
with open(out_fpath, 'w') as fhandle:
fhandle.write(html)
for child in node.branches:
build_node(child, root, node_path, template_dir, env)


def build(root: ContentNode, root_dir: str):

Expand All @@ -38,20 +59,7 @@ def build(root: ContentNode, root_dir: str):
shutil.rmtree(dist_dir)
os.makedirs(dist_dir, exist_ok=True)

def build_node(node: ContentNode, root: ContentNode, parent_path: str):
fname_tem = choose_template_file(node, isfile, template_dir)
page_template = env.get_template(fname_tem)
html = page_template.render(index=node, root=root)
node_path = join(parent_path, node.name)
os.makedirs(node_path, exist_ok=True)
out_fpath = join(node_path, 'index.html')
with open(out_fpath, 'w') as fhandle:
fhandle.write(html)
for child in node.branches:
build_node(child, root, node_path)


build_node(root, root, dist_dir)
build_node(root, root, dist_dir, template_dir, env)

dist_assets_dir = join(dist_dir, 'assets')

Expand Down
19 changes: 19 additions & 0 deletions tests/build_node.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from unittest import TestCase
from unittest.mock import Mock, patch

class BuildNodeTests(TestCase):

@patch('syrinx.build.open')
def test_follows_buildPage(self, open):
from syrinx.build import build_node
node = Mock()
root = Mock()
env = Mock()
node.name = 'foo'
node.branches = []
node.buildPage = False
build_node(node, root, '', '', env)
self.assertFalse(env.get_template().render.called)
node.buildPage = True
build_node(node, root, '', '', env)
self.assertTrue(env.get_template().render.called)

0 comments on commit 6a4e692

Please sign in to comment.