build_a_blog: draft state

This commit is contained in:
Collin Lefeber 2024-06-19 13:49:09 -04:00
parent 363ca6c6b6
commit 6cb7aa53df
2 changed files with 71 additions and 0 deletions

View file

@ -31,6 +31,9 @@ def render_post(fpath):
title = md.Meta.get('title')[0] title = md.Meta.get('title')[0]
date = md.Meta.get('date')[0] date = md.Meta.get('date')[0]
draft = False
if md.Meta.get('draft'):
draft = True
out = convert('# ' + title) + out out = convert('# ' + title) + out
@ -42,6 +45,7 @@ def render_post(fpath):
'date': date, 'date': date,
'fpath': fpath, 'fpath': fpath,
'destpath': destpath, 'destpath': destpath,
'draft': draft,
} }
def render_posts(): def render_posts():
@ -134,6 +138,7 @@ def render_rss_index(posts):
def main(): def main():
posts = render_posts() posts = render_posts()
logging.info('rendered posts: %s', posts) logging.info('rendered posts: %s', posts)
posts = filter(lambda p: not p['draft'], posts)
sorted_posts = sorted(posts, sorted_posts = sorted(posts,
key=lambda p: datetime.datetime.fromisoformat(p['date']), reverse=True) key=lambda p: datetime.datetime.fromisoformat(p['date']), reverse=True)
render_index(sorted_posts) render_index(sorted_posts)

View file

@ -667,6 +667,72 @@ View the complete source for generating this blog:
Or the full repo tree: <https://git.sr.ht/~cfebs/cfebs.srht.site/tree> Or the full repo tree: <https://git.sr.ht/~cfebs/cfebs.srht.site/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
<title>cfebs.com - Test thing</title>
<h1 id="test-thing"><a class="toclink" href="#test-thing">Test thing</a></h1>
grep 'Test thing' ./index.html ./index.xml | wc -l
0
```
[1]: https://crystal-lang.org/ [1]: https://crystal-lang.org/
[2]: https://github.com/crystal-lang/crystal/releases/tag/0.31.0 [2]: https://github.com/crystal-lang/crystal/releases/tag/0.31.0
[3]: https://pkgs.alpinelinux.org/package/edge/main/x86_64/py3-markdown [3]: https://pkgs.alpinelinux.org/package/edge/main/x86_64/py3-markdown