From 6e77e0358abff4fe1997a9da882fae35c44a277a Mon Sep 17 00:00:00 2001 From: Suleyman Farajli Date: Tue, 24 Jun 2025 19:46:28 +0400 Subject: init: first commit --- scripts/boiler.py | 55 ++++++++++++++++++++++++++++++++++++++++++++ scripts/build_page | 45 ++++++++++++++++++++++++++++++++++++ scripts/filter_proper_md | 14 +++++++++++ scripts/generate_post_index | 35 ++++++++++++++++++++++++++++ scripts/parse_frontmatter.py | 19 +++++++++++++++ 5 files changed, 168 insertions(+) create mode 100644 scripts/boiler.py create mode 100755 scripts/build_page create mode 100755 scripts/filter_proper_md create mode 100755 scripts/generate_post_index create mode 100644 scripts/parse_frontmatter.py (limited to 'scripts') diff --git a/scripts/boiler.py b/scripts/boiler.py new file mode 100644 index 0000000..b5d75f4 --- /dev/null +++ b/scripts/boiler.py @@ -0,0 +1,55 @@ +import textwrap + +def print_html_head(title, style=None): + print(textwrap.dedent(f"""\ + + + + + + + + {title}""")) + + if style: + print(textwrap.dedent(f"""\ + """)) + + print(textwrap.dedent("""\ + + + + + + + +
Suleyman Farajli
+ + +
+ + +
""")) + +def print_html_tail(): + print(textwrap.dedent("""\ +
+

+ + copyleft (c) 2024-2025 Suleyman Farajli + +

+ + """)) diff --git a/scripts/build_page b/scripts/build_page new file mode 100755 index 0000000..51be92c --- /dev/null +++ b/scripts/build_page @@ -0,0 +1,45 @@ +#!/usr/bin/env python3 + +import boiler +from parse_frontmatter import parse_frontmatter +from pathlib import Path +import subprocess +import sys + +script_dir = Path(__file__).resolve().parent +filepath = Path(sys.argv[1]).resolve() + +# Parse frontmatter and skip draft posts +frontmatter = parse_frontmatter(filepath) +if frontmatter.get("draft"): + print(f"{filepath}: is draft", file=sys.stderr) + sys.exit(1) + +if not frontmatter.get("title"): + print(f"{filepath}: title is missing", file=sys.stderr) + sys.exit(1) + +boiler.print_html_head(frontmatter["title"], frontmatter.get("css")) + +# Read file contents and strip frontmatter manually +inside_frontmatter = False +content = [] + +with open(filepath, encoding="utf-8") as f: + for line in f: + if line.strip() == "---": + inside_frontmatter = not inside_frontmatter + continue + if not inside_frontmatter: + content.append(line) + +# Convert Markdown to HTML using `smu` +html = subprocess.run( + ["smu"], + input="".join(content), + text=True, + capture_output=True +).stdout + +print(html, end="") +boiler.print_html_tail() diff --git a/scripts/filter_proper_md b/scripts/filter_proper_md new file mode 100755 index 0000000..ed1be7f --- /dev/null +++ b/scripts/filter_proper_md @@ -0,0 +1,14 @@ +#!/usr/bin/env python3 + +import sys +from parse_frontmatter import parse_frontmatter +from pathlib import Path + +def isproper(file): + frontmatter = parse_frontmatter(file) + if not frontmatter.get("draft"): + print(file) + +script_dir = Path(__file__).resolve().parent +for arg in sys.argv[1:]: + isproper(arg) diff --git a/scripts/generate_post_index b/scripts/generate_post_index new file mode 100755 index 0000000..44aa756 --- /dev/null +++ b/scripts/generate_post_index @@ -0,0 +1,35 @@ +#!/usr/bin/env python3 + +from parse_frontmatter import parse_frontmatter +from pathlib import Path + +def generate_post_index(): + script_dir = Path(__file__).parent.resolve() + posts_directory = script_dir / "../content/posts" + posts = [] + for file in posts_directory.iterdir(): + if file.is_file() and file.name != "index.md": + frontmatter=parse_frontmatter(file.resolve()) + frontmatter["filename"] = file.name + if frontmatter["draft"]: + continue + posts.append(frontmatter) + + sorted_posts = sorted(posts, key=lambda x: x["created"], reverse=True) + + for i in sorted_posts: + html_file = i['filename'].replace(".md", ".html") + print(f" [{i['title']}](/posts/{html_file})\n") + +print("""--- +title: "Posts" +slug: "posts" +css: | + time { + font-size: 16px; + color: #bbbbbb; + padding-right: 0.5em; + } +---""") + +generate_post_index() diff --git a/scripts/parse_frontmatter.py b/scripts/parse_frontmatter.py new file mode 100644 index 0000000..2d5c60e --- /dev/null +++ b/scripts/parse_frontmatter.py @@ -0,0 +1,19 @@ +import yaml + +def parse_frontmatter(filepath): + with open(filepath, 'r', encoding='utf-8') as f: + lines = f.readlines() + + if not lines or lines[0].strip() != '---': + return {} + + frontmatter_lines = [] + for line in lines[1:]: + if line.strip() == '---': + break + frontmatter_lines.append(line) + + frontmatter_str = ''.join(frontmatter_lines) + frontmatter = yaml.safe_load(frontmatter_str) or {} + + return frontmatter -- cgit v1.2.3