Skip to content

Commit

Permalink
Add basic blog related functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
tanayseven committed Apr 15, 2024
1 parent 0d9e87f commit a47ea12
Show file tree
Hide file tree
Showing 12 changed files with 74 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
_build/
**__pycache__/
venv/
.doctrees
test_results/
_tags/
5 changes: 4 additions & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/personal-website.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 43 additions & 2 deletions _ext/blog.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import re
from datetime import datetime
from pathlib import Path
from typing import TypedDict, Optional

from docutils import nodes
Expand Down Expand Up @@ -53,18 +54,58 @@ def _fetch_date_from_file(node):

class PostDirective(SphinxDirective):
has_content = False
option_spec = {"tags": str, "category": str, "author": str, "draft": bool}
option_spec = {"tags": str, "category": str, "author": str, "draft": bool, "show_modified_date": bool}
separator = ","

def run(self) -> list[Node]:
node = PostNode()
node.document = self.state.document
set_source_info(self, node)
date = _fetch_date_from_file(node)
file_path = Path(node.source)
modified_date = datetime.fromtimestamp(file_path.stat().st_mtime)
node.append(
nodes.line(text=f"Published on: {date.strftime('%d %B %Y')}"),
)
if self.options.get("show_modified_date"):
node.append(
nodes.line(text=f"Last modified on: {modified_date.strftime('%d %B %Y')}"),
)
tags = self.options.get("tags")
if tags:
tags_element = nodes.paragraph()
tags_element["classes"] = ["tags"]
tags_element.append(nodes.strong(text="Tags: "))
for tag in tags.split(self.separator):
tag = tag.strip()
reference = nodes.reference("", f"")
reference["refuri"] = f"_tags/{tag}"
reference.append(nodes.Text(tag))
tags_element.append(reference)
tags_element.append(nodes.Text(self.separator))
node.append(tags_element)
category = self.options.get("category")
if category:
category_element = nodes.paragraph()
category_element["classes"] = ["category"]
category_element.append(nodes.strong(text="Category: "))
reference = nodes.reference("", f"")
reference["refuri"] = f"_categories/{category}"
reference.append(nodes.Text(category))
category_element.append(reference)
node.append(category_element)
author = self.options.get("author")
if author:
author_element = nodes.paragraph()
author_element["classes"] = ["author"]
author_element.append(nodes.strong(text="Author: "))
reference = nodes.reference("", f"")
reference["refuri"] = f"authors/{author}"
reference.append(nodes.Text(author))
author_element.append(reference)
node.append(author_element)
node.append(nodes.transition())
titles = [x for x in node.document.traverse() if hasattr(x, "title")]
titles = [x for x in node.document.findall() if hasattr(x, "title")]
title = titles[0].astext() if titles else None
self.state.nested_parse(self.content, self.content_offset, node)
if not hasattr(self.env, "all_posts"):
Expand Down
3 changes: 3 additions & 0 deletions _ext/templates_md/categories.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
orphan: true
---
3 changes: 3 additions & 0 deletions _ext/templates_md/category.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
orphan: true
---
11 changes: 11 additions & 0 deletions _ext/templates_md/tag.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
orphan: true
---

# {{ tags_page_title }}: {{ selected_tag }}

## Pages matching the tag

{% for page in pages %}
- [{{ page.title }}]({{ page.link }})
{% endfor %}
1 change: 1 addition & 0 deletions _ext/templates_rst/categories.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
:orphan:
1 change: 1 addition & 0 deletions _ext/templates_rst/category.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
:orphan:
1 change: 1 addition & 0 deletions _ext/templates_rst/tag.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
:orphan:
3 changes: 2 additions & 1 deletion conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,11 @@
"blog",
"yasfb",
"myst_parser",
"sphinx_tags",
"sphinxcontrib.googleanalytics",
]

todo_include_todos = True
tags_create_tags = True

# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ sphinxcontrib-googleanalytics==0.4
yasfb==0.8.0
furo==2022.12.7
myst_parser==0.19.1
filelock==3.12.3

0 comments on commit a47ea12

Please sign in to comment.