Skip to content

Commit

Permalink
Build draft posts
Browse files Browse the repository at this point in the history
Fixes #12
  • Loading branch information
Siecje committed Jan 27, 2024
1 parent f90335c commit 184e7a7
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 10 deletions.
2 changes: 2 additions & 0 deletions htmd/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ def set_posts_datetime(app: Flask, posts: FlatPages) -> None:
# Ensure each post has a published date
# set time for correct date field
for post in posts:
if post.meta.get('draft', False):
continue
if 'updated' not in post.meta:
published = post.meta.get('published')
if isinstance(published, datetime.datetime):
Expand Down
33 changes: 23 additions & 10 deletions htmd/site.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ def get_project_dir() -> Path:


posts = FlatPages(app)
published_posts = [p for p in posts if not p.meta.get('draft', False)]
freezer = Freezer(app)

# Allow config settings (even new user created ones) to be used in templates
Expand Down Expand Up @@ -173,7 +174,7 @@ def pygments_css() -> ResponseReturnValue:

@app.route('/')
def index() -> ResponseReturnValue:
latest = sorted(posts, reverse=True, key=lambda p: p.meta.get('published'))
latest = sorted(published_posts, reverse=True, key=lambda p: p.meta['published'])
return render_template('index.html', active='home', posts=latest[:4])


Expand All @@ -188,7 +189,7 @@ def feed() -> ResponseReturnValue:
title=name,
url=url,
)
for post in posts:
for post in published_posts:
url = url_for(
'post',
year=post.meta.get('published').strftime('%Y'),
Expand All @@ -212,7 +213,7 @@ def feed() -> ResponseReturnValue:

@app.route('/all/')
def all_posts() -> ResponseReturnValue:
latest = sorted(posts, reverse=True, key=lambda p: p.meta.get('published'))
latest = sorted(published_posts, reverse=True, key=lambda p: p.meta['published'])
return render_template('all_posts.html', active='posts', posts=latest)


Expand All @@ -231,7 +232,7 @@ def post(year: str, month: str, day: str, path: str) -> ResponseReturnValue:
@app.route('/tags/')
def all_tags() -> ResponseReturnValue:
tag_counts: dict[str, int] = {}
for post in posts:
for post in published_posts:
for tag in post.meta.get('tags', []):
if tag not in tag_counts:
tag_counts[tag] = 0
Expand All @@ -241,7 +242,7 @@ def all_tags() -> ResponseReturnValue:

@app.route('/tags/<string:tag>/')
def tag(tag: str) -> ResponseReturnValue:
tagged = [p for p in posts if tag in p.meta.get('tags', [])]
tagged = [p for p in published_posts if tag in p.meta.get('tags', [])]
sorted_posts = sorted(
tagged,
reverse=True,
Expand All @@ -252,7 +253,7 @@ def tag(tag: str) -> ResponseReturnValue:

@app.route('/author/<author>/')
def author(author: str) -> ResponseReturnValue:
author_posts = [p for p in posts if author == p.meta.get('author', '')]
author_posts = [p for p in published_posts if author == p.meta.get('author', '')]
sorted_posts = sorted(
author_posts,
reverse=True,
Expand All @@ -277,7 +278,7 @@ def year_view(year: int) -> ResponseReturnValue:
if len(year_str) != len('YYYY'):
abort(404)
year_posts = [
p for p in posts if year_str == p.meta.get('published', []).strftime('%Y')
p for p in published_posts if year_str == p.meta['published'].strftime('%Y')
]
if not year_posts:
abort(404)
Expand Down Expand Up @@ -338,15 +339,15 @@ def page_not_found(_e: Exception | int) -> ResponseReturnValue:
# Telling Frozen-Flask about routes that are not linked to in templates
@freezer.register_generator # type: ignore[no-redef]
def year_view() -> Iterator[dict]: # noqa: F811
for post in posts:
for post in published_posts:
yield {
'year': post.meta.get('published').year,
}


@freezer.register_generator # type: ignore[no-redef]
def month_view() -> Iterator[dict]: # noqa: F811
for post in posts:
for post in published_posts:
yield {
'month': post.meta.get('published').strftime('%m'),
'year': post.meta.get('published').year,
Expand All @@ -355,9 +356,21 @@ def month_view() -> Iterator[dict]: # noqa: F811

@freezer.register_generator # type: ignore[no-redef]
def day_view() -> Iterator[dict]: # noqa: F811
for post in posts:
for post in published_posts:
yield {
'day': post.meta.get('published').strftime('%d'),
'month': post.meta.get('published').strftime('%m'),
'year': post.meta.get('published').year,
}


@freezer.register_generator # type: ignore[no-redef]
def post() -> Iterator[dict]: # noqa: F811
draft_posts = [p for p in posts if p.meta.get('draft', False)]
for post in draft_posts:
yield {
'day': post.meta.get('published').strftime('%d'),
'month': post.meta.get('published').strftime('%m'),
'year': post.meta.get('published').year,
'path': post.path,
}
83 changes: 83 additions & 0 deletions tests/test_drafts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
from pathlib import Path

from click.testing import CliRunner
from htmd.cli import build, start
import pytest

from utils import remove_fields_from_example_post


def set_example_as_draft():
remove_fields_from_example_post(('draft',))
post_path = Path('posts') / 'example.md'
with post_path.open('r') as post_file:
lines = post_file.readlines()
with post_path.open('w') as post_file:
for line in lines:
if line == '...\n':
post_file.write('draft: true\n')
post_file.write(line)


@pytest.fixture(scope='module')
def build_draft():
runner = CliRunner()
with runner.isolated_filesystem():
result = runner.invoke(start)
set_example_as_draft()
result = runner.invoke(build)
assert result.exit_code == 0
# Tests code is run here
yield


def test_draft_is_built(build_draft) -> None:
with (Path('build') / '2014' / '10' / '30' / 'example' / 'index.html').open('r') as post_page:
assert 'Example Post' in post_page.read()


def test_no_drafts_home(build_draft) -> None:
with (Path('build') / 'index.html').open('r') as home_page:
assert 'Example Post' not in home_page.read()


def test_no_drafts_atom_feed(build_draft) -> None:
with (Path('build') / 'feed.atom').open('r') as feed_page:
assert 'Example Post' not in feed_page.read()


def test_no_drafts_all_posts(build_draft) -> None:
with (Path('build') / 'all' / 'index.html').open('r') as web_page:
assert 'Example Post' not in web_page.read()


def test_no_drafts_all_tags(build_draft) -> None:
with (Path('build') / 'tags' / 'index.html').open('r') as web_page:
assert 'first' not in web_page.read()


def test_no_drafts_in_tag(build_draft) -> None:
# tag page exists because the draft links to it
with (Path('build') / 'tags' / 'first' / 'index.html').open('r') as web_page:
assert 'Example Post' not in web_page.read()


def test_no_drafts_for_author(build_draft) -> None:
# author page exists because the draft links to it
with (Path('build') / 'author' / 'taylor' / 'index.html').open('r') as web_page:
assert 'Example Post' not in web_page.read()


def test_no_drafts_for_year(build_draft) -> None:
# folder exists becaues of URL for post
assert (Path('build') / '2014' / 'index.html').exists() is False


def test_no_drafts_for_month(build_draft) -> None:
# folder exists becaues of URL for post
assert (Path('build') / '2014' / '10' / 'index.html').exists() is False


def test_no_drafts_for_day(build_draft) -> None:
# folder exists becaues of URL for post
assert (Path('build') / '2014' / '10' / '30' / 'index.html').exists() is False

0 comments on commit 184e7a7

Please sign in to comment.