Skip to content

Commit

Permalink
Don't build drafts by default
Browse files Browse the repository at this point in the history
A draft will only be built if it has draft: build
during build the metadata line will become draft: build|uuid
with the uuid where you can access the draft (/draft/uuid/index.html)
in the build.
  • Loading branch information
Siecje committed Feb 4, 2024
1 parent ae16673 commit a5daa27
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 29 deletions.
13 changes: 8 additions & 5 deletions htmd/site.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ def post(year: str, month: str, day: str, path: str) -> ResponseReturnValue:
@app.route('/draft/<post_uuid>/')
def draft(post_uuid: str) -> ResponseReturnValue:
for post in posts:
if str(post.meta.get('draft', '')) == post_uuid:
if str(post.meta.get('draft', '')).replace('build|', '') == post_uuid:
return render_template('post.html', post=post)
abort(404) # noqa: RET503

Expand Down Expand Up @@ -382,13 +382,16 @@ def day_view() -> Iterator[dict]: # noqa: F811

@freezer.register_generator # type: ignore[no-redef]
def draft() -> Iterator[dict]: # noqa: F811
draft_posts = [p for p in posts if p.meta.get('draft', False)]
draft_posts = [
p for p in posts
if 'draft' in p.meta and 'build' in str(p.meta['draft'])
]
for post in draft_posts:
if not valid_uuid(str(post.meta['draft'])):
post.meta['draft'] = uuid.uuid4()
if not valid_uuid(post.meta['draft'].replace('build|', '')):
post.meta['draft'] = 'build|' + str(uuid.uuid4())
set_post_metadata(app, post, 'draft', post.meta['draft'])
yield {
'post_uuid': str(post.meta['draft']),
'post_uuid': post.meta['draft'].replace('build|', ''),
}


Expand Down
4 changes: 2 additions & 2 deletions tests/test_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from click.testing import CliRunner
from htmd.cli import build

from utils import remove_fields_from_example_post, SUCCESS_REGEX
from utils import remove_fields_from_post, SUCCESS_REGEX


def test_build(run_start: CliRunner) -> None:
Expand All @@ -17,7 +17,7 @@ def test_build(run_start: CliRunner) -> None:

def test_build_verify_fails(run_start: CliRunner) -> None:
expected_output = 'Post "example" does not have field title.\n'
remove_fields_from_example_post(('title',))
remove_fields_from_post('example', ('title',))
result = run_start.invoke(build)
assert result.exit_code == 1
assert result.output == expected_output
Expand Down
45 changes: 33 additions & 12 deletions tests/test_drafts.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
from htmd.cli import build, start
import pytest

from utils import remove_fields_from_example_post, SUCCESS_REGEX
from utils import remove_fields_from_post, SUCCESS_REGEX


def set_example_as_draft() -> None:
remove_fields_from_example_post(('draft',))
remove_fields_from_post('example', ('draft',))
post_path = Path('posts') / 'example.md'
with post_path.open('r') as post_file:
lines = post_file.readlines()
Expand All @@ -21,13 +21,26 @@ def set_example_as_draft() -> None:
post_file.write(line)


def get_example_draft_uuid() -> str:
draft_path = Path('posts') / 'example.md'
with draft_path.open('r') as draft_file: # pragma: no branch
for line in draft_file.readlines(): # pragma: no branch
def copy_example_as_draft_build() -> None:
post_path = Path('posts') / 'example.md'
copy_path = Path('posts') / 'copy.md'
with post_path.open('r') as post_file:
lines = post_file.readlines()
with copy_path.open('w') as copy_file:
for line in lines:
if 'draft' in line:
return line.replace('draft:', '').strip()
return '' # pragma: no cover
copy_file.write('draft: build\n')
else:
copy_file.write(line)


def get_draft_uuid(path: str) -> str:
draft_path = Path('posts') / f'{path}.md'
with draft_path.open('r') as draft_file:
for line in draft_file.readlines():
if 'draft: build' in line:
return line.replace('draft: build|', '').strip()
return ''


@pytest.fixture(scope='module')
Expand All @@ -36,24 +49,29 @@ def build_draft() -> Generator[CliRunner, None, None]:
with runner.isolated_filesystem():
result = runner.invoke(start)
set_example_as_draft()
copy_example_as_draft_build()
result = runner.invoke(build)
assert result.exit_code == 0
assert re.search(SUCCESS_REGEX, result.output)
# Tests code is run here
yield runner


def test_draft_is_built(build_draft: CliRunner) -> None:
def test_draft_only_draft_build_is_in_build(build_draft: CliRunner) -> None:
post_path = Path('build') / '2014' / '10' / '30' / 'example' / 'index.html'
assert post_path.exists() is False

draft_uuid = get_example_draft_uuid()
example_uuid = get_draft_uuid('example')
assert example_uuid == ''
draft_uuid = get_draft_uuid('copy')
draft_path = Path('build') / 'draft' / draft_uuid / 'index.html'
assert draft_path.is_file() is True

# build again now that draft has uuid
result = build_draft.invoke(build)
assert result.exit_code == 0
assert re.search(SUCCESS_REGEX, result.output)
assert draft_path.is_file() is True


def test_no_drafts_home(build_draft: CliRunner) -> None:
Expand Down Expand Up @@ -105,10 +123,13 @@ def test_no_drafts_for_day(build_draft: CliRunner) -> None:

def test_draft_without_published(run_start: CliRunner) -> None:
set_example_as_draft()
remove_fields_from_example_post(('published', 'updated'))
copy_example_as_draft_build()
example_path = Path('posts') / 'example.md'
example_path.unlink()
remove_fields_from_post('copy', ('published', 'updated'))
result = run_start.invoke(build)
assert result.exit_code == 0
assert re.search(SUCCESS_REGEX, result.output)
draft_uuid = get_example_draft_uuid()
draft_uuid = get_draft_uuid('copy')
draft_path = Path('build') / 'draft' / draft_uuid / 'index.html'
assert draft_path.is_file() is True
8 changes: 4 additions & 4 deletions tests/test_post_dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from click.testing import CliRunner
from htmd.cli import build

from utils import remove_fields_from_example_post
from utils import remove_fields_from_post


def test_build_post_404_invalid_date_year(run_start: CliRunner) -> None:
Expand Down Expand Up @@ -263,7 +263,7 @@ def test_build_published_time_is_added(run_start: CliRunner) -> None:
# build will add it
# verify that time is not there
# ensure correct time is added
remove_fields_from_example_post(('updated',))
remove_fields_from_post('example', ('updated',))
example_path = Path('posts') / 'example.md'
with example_path.open('r') as post_file:
b_lines = post_file.readlines()
Expand Down Expand Up @@ -311,7 +311,7 @@ def test_build_updated_is_added(run_start: CliRunner) -> None:
then build will add updated with time.
"""
# Remove updated from example post
remove_fields_from_example_post(('updated',))
remove_fields_from_post('example', ('updated',))
# First build adds time to published
result = run_start.invoke(build)
assert result.exit_code == 0
Expand Down Expand Up @@ -369,7 +369,7 @@ def test_build_updated_is_added_once(run_start: CliRunner) -> None:


def test_build_without_published(run_start: CliRunner) -> None:
remove_fields_from_example_post(('published', 'updated'))
remove_fields_from_post('example', ('published', 'updated'))

# First build adds published time
result = run_start.invoke(build)
Expand Down
8 changes: 4 additions & 4 deletions tests/test_verify.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from click.testing import CliRunner
from htmd.cli import verify

from utils import remove_fields_from_example_post
from utils import remove_fields_from_post


def test_verify(run_start: CliRunner) -> None:
Expand All @@ -15,7 +15,7 @@ def test_verify(run_start: CliRunner) -> None:

def test_verify_author_missing(run_start: CliRunner) -> None:
# Remove author from example post
remove_fields_from_example_post(('author',))
remove_fields_from_post('example', ('author',))

result = run_start.invoke(verify)
assert result.exit_code == 1
Expand All @@ -25,7 +25,7 @@ def test_verify_author_missing(run_start: CliRunner) -> None:

def test_verify_title_missing(run_start: CliRunner) -> None:
# Remove title from example post
remove_fields_from_example_post(('title',))
remove_fields_from_post('example', ('title',))

result = run_start.invoke(verify)
assert result.exit_code == 1
Expand All @@ -35,7 +35,7 @@ def test_verify_title_missing(run_start: CliRunner) -> None:

def test_verify_published_missing(run_start: CliRunner) -> None:
# Remove published from example post
remove_fields_from_example_post(('published',))
remove_fields_from_post('example', ('published',))

result = run_start.invoke(verify)
# verify doesn't check for published
Expand Down
4 changes: 2 additions & 2 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
)


def remove_fields_from_example_post(field_names: tuple[str, ...]) -> None:
example_post_path = Path('posts') / 'example.md'
def remove_fields_from_post(path: str, field_names: tuple[str, ...]) -> None:
example_post_path = Path('posts') / f'{path}.md'
with example_post_path.open('r') as post:
lines = post.readlines()
with example_post_path.open('w') as post:
Expand Down

0 comments on commit a5daa27

Please sign in to comment.