Skip to content

Commit

Permalink
Fixes issue with deeply nested posts.
Browse files Browse the repository at this point in the history
  • Loading branch information
menganha committed Dec 9, 2022
1 parent bfa17a1 commit 4bee001
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 22 deletions.
4 changes: 2 additions & 2 deletions pyblog/blog.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class Blog:
HOME_MAX_POSTS = 10

def __init__(self, main_path: Path):
self.main_path = main_path
self.main_path = main_path.resolve().expanduser()

self.website_path = main_path / self.WEBSITE_DIR_NAME
self.website_posts_path = self.website_path / self.POSTS_DIR_NAME
Expand Down Expand Up @@ -195,7 +195,7 @@ def markdown_post_paths(self) -> Iterator[Path]:
def orphan_target_paths(self) -> Iterator[Path]:
""" Returns the html paths of the current build that do not have a corresponding markdown path """
for target_path in self.website_posts_path.rglob('*.html'):
expected_post_path = target_path.relative_to(self.website_path).with_suffix('.md')
expected_post_path = self.main_path / target_path.relative_to(self.website_path).with_suffix('.md')
if not expected_post_path.exists():
yield target_path

Expand Down
19 changes: 9 additions & 10 deletions pyblog/command_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ def parse_cli_arguments():


def init(path: Path):
pyblog = Blog(path.resolve().expanduser())
pyblog = Blog(path)
pyblog.create()
print(f'New Pyblog successfully created on {path.absolute()}!')
print(f'New Pyblog successfully created on {path.resolve()}!')


def build(blog: Blog, force: bool):
Expand All @@ -48,9 +48,6 @@ def build(blog: Blog, force: bool):
print(f'The config.json file has been modified. Rebuilding whole site...')
force = True

if blog.is_config_file_updated() or blog.is_style_sheet_updated():
blog.update_last_build_file()

for path in blog.markdown_post_paths():
target_path = blog.get_post_target_html_path(path)
post = Post(path, target_path)
Expand All @@ -66,18 +63,20 @@ def build(blog: Blog, force: bool):
print(f'Deleting orphan page: {target_path}')
target_path.unlink()

if not needs_rebuild:
print('No new posts found!')
else:
if force or needs_rebuild:
all_public_posts.sort(key=lambda x: x.date, reverse=True)
# latest_posts = all_public_posts[:blog.HOME_MAX_POSTS] # Maybe handle this within the blog instance
print(f'Building index...')
blog.build_home_page(all_public_posts)
print(f'Building tag pages...')
blog.build_tag_page(all_public_posts)
print(f'Building archive pages...')
blog.build_archive_page(all_public_posts)
print(f'Done!')
else:
print('No new posts found!')

if blog.is_config_file_updated() or blog.is_style_sheet_updated() or needs_rebuild:
blog.update_last_build_file()


def serve(filepath_to_serve: Path):
Expand All @@ -101,7 +100,7 @@ def execute():
else:
pyblog = Blog(Path('.'))
if not pyblog.is_pyblog():
print('Error: The current path does not contain a pyblog')
print(f'Error: The current path "{pyblog.main_path}" does not contain a pyblog')
return 1

if args.command == 'build':
Expand Down
4 changes: 2 additions & 2 deletions tests/test_blog.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ def test_orphan_target_paths(created_blog):
(created_blog.website_posts_path / 'a_test_post_2.html').touch()
(created_blog.website_posts_path / 'a_test_post_3.html').touch()

orphan_paths = list(created_blog.orphan_target_paths())
orphan_paths = set(created_blog.orphan_target_paths())

assert orphan_paths == [created_blog.website_posts_path / 'a_test_post_3.html']
assert orphan_paths == {created_blog.website_posts_path / 'a_test_post_1.html', created_blog.website_posts_path / 'a_test_post_3.html'}
9 changes: 1 addition & 8 deletions tests/test_command_line.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import pathlib
from pathlib import Path

from pyblog import command_line as cli
Expand All @@ -13,12 +12,6 @@ def test_init(mocker):
mocked_blog.assert_called_with(test_path)
mocked_blog_create.assert_called()

test_path = Path('~/one/two/path')
cli.init(test_path)
actual_path = pathlib.Path.home() / 'one/two/path'
mocked_blog.assert_called_with(actual_path)
mocked_blog_create.assert_called()


def test_build_nothing_to_do(created_blog, mocker):
mock_load_config = mocker.patch.object(created_blog, 'load_config')
Expand Down Expand Up @@ -72,7 +65,7 @@ def test_build_with_posts(created_blog, mocker, post, draft_post, post_not_dirty
cli.build(created_blog, force=False)

mock_copy_tree.assert_not_called()
mock_update_last_build_file.assert_not_called()
mock_update_last_build_file.assert_called_once()
mock_build_home_page.assert_called_once()
mock_build_tag_page.assert_called_once()

Expand Down

0 comments on commit 4bee001

Please sign in to comment.