diff --git a/main.py b/main.py index bfd9382..52ce57b 100644 --- a/main.py +++ b/main.py @@ -31,6 +31,9 @@ def render_post(fpath): title = md.Meta.get('title')[0] date = md.Meta.get('date')[0] + draft = False + if md.Meta.get('draft'): + draft = True out = convert('# ' + title) + out @@ -42,6 +45,7 @@ def render_post(fpath): 'date': date, 'fpath': fpath, 'destpath': destpath, + 'draft': draft, } def render_posts(): @@ -134,6 +138,7 @@ def render_rss_index(posts): def main(): posts = render_posts() logging.info('rendered posts: %s', posts) + posts = filter(lambda p: not p['draft'], posts) sorted_posts = sorted(posts, key=lambda p: datetime.datetime.fromisoformat(p['date']), reverse=True) render_index(sorted_posts) diff --git a/posts/build_a_blog.md b/posts/build_a_blog.md index f8ceded..a6d0216 100644 --- a/posts/build_a_blog.md +++ b/posts/build_a_blog.md @@ -667,6 +667,72 @@ View the complete source for generating this blog: Or the full repo tree: +## EDIT + +Few additional things that will be added will go here. + +### 2024-06-19, adding draft state + +It might be nice to work on a rough draft, generate it for previewing, track it in git, but skip including it in the posts index. + +So I'll add a piece of post metadata called `Draft` and use `filter()` before the posts are sorted or applied to the `index.html` or RSS `index.xml`. + +This is the result. + +```diff +diff --git a/main.py b/main.py +index bfd9382..52ce57b 100644 +--- a/main.py ++++ b/main.py +@@ -31,6 +31,9 @@ def render_post(fpath): + + title = md.Meta.get('title')[0] + date = md.Meta.get('date')[0] ++ draft = False ++ if md.Meta.get('draft'): ++ draft = True + + out = convert('# ' + title) + out + +@@ -42,6 +45,7 @@ def render_post(fpath): + 'date': date, + 'fpath': fpath, + 'destpath': destpath, ++ 'draft': draft, + } + + def render_posts(): +@@ -134,6 +138,7 @@ def render_rss_index(posts): + def main(): + posts = render_posts() + logging.info('rendered posts: %s', posts) ++ posts = filter(lambda p: not p['draft'], posts) + sorted_posts = sorted(posts, + key=lambda p: datetime.datetime.fromisoformat(p['date']), reverse=True) + render_index(sorted_posts) +``` + +And testing it: +``` +❯ cat ./posts/test_thing.md +Title: Test thing +Date: 2024-06-19T13:38:34-04:00 +Draft: 1 + +❯ python main.py +... +``` + +html should get generated, but not in the index or xml +``` +❯ grep 'Test thing' ./posts/test_thing.html + cfebs.com - Test thing +

Test thing

+ +❯ grep 'Test thing' ./index.html ./index.xml | wc -l +0 +``` + [1]: https://crystal-lang.org/ [2]: https://github.com/crystal-lang/crystal/releases/tag/0.31.0 [3]: https://pkgs.alpinelinux.org/package/edge/main/x86_64/py3-markdown