bin/update_index.py: Latest 4 posts on index Signed-off-by: Anirudh Oppiliappan <x@icyphox.sh>
Anirudh Oppiliappan x@icyphox.sh
Tue, 29 Oct 2019 21:09:06 +0530
3 files changed,
32 insertions(+),
21 deletions(-)
M
bin/update_index.py
→
bin/update_index.py
@@ -10,19 +10,23 @@ os.chdir("bin")
blog = "../pages/blog/" -# bunch of file hacks to get to the most recent file -def getrecent(path): +# order files by recency +def getrecents(path): files = [path + f for f in os.listdir(blog) if f not in ["_index.md", "feed.xml"]] - files.sort(key=os.path.getmtime, reverse=True) - return files[0] + files.sort( + key=lambda f: markdown_path(f, extras=["metadata"]).metadata["date"], + reverse=True, + ) + return files -def update_index(s): +def update_index(posts): path = "../pages/_index.md" with open(path, "r") as f: md = f.readlines() ruler = md.index("| --- | --: |\n") - md[ruler + 1] = s + "\n" + for post, i in zip(posts, range(4)): + md[ruler + i + 1] = post + "\n" with open(path, "w") as f: f.writelines(md)@@ -37,11 +41,19 @@ l = l.replace(l, l + s)
print(l, end=""), -# fetch title and date -meta = markdown_path(getrecent(blog), extras=["metadata"]).metadata -fname = os.path.basename(os.path.splitext(getrecent(blog))[0]) -url = "/blog/" + fname -line = f"| [{meta['title']}]({url}) | `{meta['date']}` |" +top_four = [] +metas = [] +lines = [] +fnames = [] + +for i in range(4): + top_four.append(getrecents(blog)[i]) + metas.append(markdown_path(getrecents(blog)[i], extras=["metadata"]).metadata) + fnames.append(os.path.basename(os.path.splitext(getrecents(blog)[i])[0])) + +for meta, fname in zip(metas, fnames): + url = "/blog/" + fname + lines.append(f"| [{meta['title']}]({url}) | `{meta['date']}` |") -update_index(line) -update_blog(line) +update_index(lines) +update_blog(lines[0])
M
pages/_index.md
→
pages/_index.md
@@ -20,18 +20,17 @@ Want to contact me? The best way is via email, at [x@icyphox.sh](mailto:x@icyphox.sh)
or [Twitter](https://twitter.com/icyphox) DMs. Or if you prefer IRC, I'm `icy` / `icyphox` on Rizon and Freenode. -# latest post +# latest posts ([see all](/blog)) | | | | --- | --: | | [The intelligence conundrum](/blog/intel-conundrum) | `2019-10-28` | +| [Hacky scripts](/blog/hacky-scripts) | `2019-10-24` | +| [Status update](/blog/2019-10-17) | `2019-10-16` | +| [PyCon India 2019 wrap-up](/blog/pycon-wrap-up) | `2019-10-15` | -([see all](/blog)) - -# currently reading +# currently reading ([see all](/reading)) [Kakegurui](https://myanimelist.net/manga/73603/Kakegurui) -started **15th October, 2019** - -([see all](/reading)) +*still reading* | started **15th October, 2019**
M
pages/reading.md
→
pages/reading.md
@@ -8,7 +8,7 @@ # чтение журнала
## I read very infrequently, this might never get updated. [Kakegurui](https://myanimelist.net/manga/73603/Kakegurui) -started **15th October, 2019** +*still reading* | started **15th October, 2019** [Another](https://myanimelist.net/manga/24098/Another) started **12th October, 2019** | opinion: *pretty eh; not scary :/*