aboutsummaryrefslogtreecommitdiff
path: root/build.py
blob: bde34a4d7c890fcee4f0b3587aa82056f57e7992 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
#!/usr/bin/env python3
"""
This script automates the process of building, testing, and deploying the website.
It handles tasks such as:

- Removing and recreating the build directory.
- Minifying CSS assets.
- Running the Emacs publishing script to generate site content.
- Updating the index.html file with the latest blog posts.
- Optionally deploying the built site to a remote server.
- Starting a local development server for previewing changes.

Usage:
    Set the environment variable ENV to 'prod' for production builds.
    Run the script to perform the build process accordingly.

Dependencies:
    - Python 3
    - Emacs with the publish.el script
    - minify tool for CSS minification
    - rsync for deployment

Author:
    Christian Cleberg <[email protected]>
"""

import os
import re
import shutil
import subprocess
import sys
from urllib.parse import quote
from datetime import datetime
from pathlib import Path


def update_index_html(html_snippet, template_path="./.build/index.html"):
    """
    Read the index.html file at `template_path`, replace everything between
    <!-- BEGIN_POSTS --> and <!-- END_POSTS --> with the provided html_snippet,
    and write the updated content back to the same file.
    """
    # Read the current contents of index.html
    with open(template_path, "r", encoding="utf-8") as f:
        content = f.read()

    begin_marker = "<!-- BEGIN_POSTS -->"
    end_marker = "<!-- END_POSTS -->"

    # Find the indices of the markers
    begin_index = content.find(begin_marker)
    end_index = content.find(end_marker)

    if begin_index == -1 or end_index == -1:
        raise ValueError(f"Markers not found in {template_path}")

    # Compute insertion points: after the end of begin_marker line, before end_marker
    # Include the newline after BEGIN_POSTS
    insert_start = begin_index + len(begin_marker)
    # Ensure we capture the newline character if present
    if content[insert_start : insert_start + 1] == "\n":
        insert_start += 1

    # If there is a newline before END_POSTS, trim trailing whitespace from snippet block
    # We will preserve indentation of BEGIN_POSTS line
    indent = ""
    # Determine the indentation by looking at characters after the newline that follows begin_marker
    lines_after_begin = content[begin_index:].splitlines(True)
    if len(lines_after_begin) > 1:
        # The second line starts with the indentation to preserve
        second_line = lines_after_begin[1]
        indent = ""
        for ch in second_line:
            if ch.isspace():
                indent += ch
            else:
                break

    # Prepare the replacement block: indent each line of html_snippet
    snippet_lines = html_snippet.splitlines()
    indented_snippet = "\n".join(indent + line for line in snippet_lines) + "\n"

    # Compute the position just before end_marker (excluding any preceding whitespace/newline)
    end_line_start = content.rfind("\n", 0, end_index)
    if end_line_start == -1:
        end_line_start = end_index

    # Construct the new content
    new_content = content[:insert_start] + indented_snippet + content[end_line_start:]

    # Write back to index.html
    with open(template_path, "w", encoding="utf-8") as f:
        f.write(new_content)


def get_recent_posts_html(content_dir="./content/blog", num_posts=3):
    """
    Scan the `content_dir` for .org blog post files, extract their headers,
    and return an HTML snippet for the `num_posts` most recent posts.

    Expects each .org file to contain headers of the form:
      #+title:    Post Title
      #+date:     <YYYY-MM-DD Day HH:MM:SS>
      #+slug:     post-slug

    Returns a string containing the section with the three most recent posts,
    formatted like the snippet in the prompt.
    """
    posts = []

    header_patterns = {
        "title": re.compile(r"^#\+title:\s*(.+)$", re.IGNORECASE),
        "date": re.compile(r"^#\+date:\s*[\[<](\d{4}-\d{2}-\d{2})"),
        "slug": re.compile(r"^#\+slug:\s*(.+)$", re.IGNORECASE),
        "draft": re.compile(r"^#\+draft:\s*(.+)$", re.IGNORECASE),
    }

    for org_path in Path(content_dir).glob("*.org"):
        title = None
        date_str = None
        slug = None
        is_draft = False

        with org_path.open("r", encoding="utf-8") as f:
            for line in f:
                if title is None:
                    m = header_patterns["title"].match(line)
                    if m:
                        title = m.group(1).strip()
                        continue

                if date_str is None:
                    m = header_patterns["date"].match(line)
                    if m:
                        # date_str is just YYYY-MM-DD
                        date_str = m.group(1)
                        continue

                if slug is None:
                    m = header_patterns["slug"].match(line)
                    if m:
                        slug = m.group(1).strip()
                        continue

                m = header_patterns["draft"].match(line)
                if m:
                    draft_value = m.group(1).strip().lower()
                    if draft_value != "nil":
                        is_draft = True
                        break
                    continue

                # Stop scanning once we have all required fields
                if title and date_str and slug:
                    break

        if is_draft:
            continue

        if title and date_str and slug:
            try:
                date_obj = datetime.strptime(date_str, "%Y-%m-%d")
                date_full = date_obj.strftime("%Y-%m-%d")
            except ValueError:
                # Skip files with invalid date format
                continue

            posts.append(
                {
                    "title": title,
                    "date_str": date_str,
                    "date_obj": date_obj,
                    "date_full": date_full,
                    "slug": slug,
                }
            )

    # Sort posts by date (newest first)
    posts.sort(key=lambda x: x["date_obj"], reverse=True)

    # Take the top `num_posts`
    recent = posts[:num_posts]

    # Build HTML lines
    lines = []
    for post in recent:
        lines.append('\t<li class="post-list-item">')
        lines.append(
            f'\t\t<time datetime="{post["date_str"]}">{post["date_full"]}</time>'
        )
        lines.append(f'\t\t<a href="/blog/{post["slug"]}.html">{post["title"]}</a>')
        lines.append("\t</li>")

    return "\n".join(lines)


def prompt(prompt_text):
    try:
        return input(prompt_text).strip()
    except EOFError:
        return ""


def remove_build_directory(build_dir):
    if build_dir.exists():
        print(f"Removing previous build directory: {build_dir}/")
        shutil.rmtree(build_dir)
    build_dir.mkdir(parents=True, exist_ok=True)


def minify_css(src_css, dest_css):
    print(f"Minifying CSS: {src_css}{dest_css}")
    result = subprocess.run(
        ["minify", "-o", str(dest_css), str(src_css)],
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
        text=True,
    )
    if result.returncode != 0:
        print("Error during CSS minification:")
        print(result.stderr, file=sys.stderr)
        sys.exit(1)


def minify_html(src_html, dest_html):
    print(f"Minifying HTML: {src_html}{dest_html}")
    result = subprocess.run(
        ["minify", "-o", str(dest_html), str(src_html)],
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
        text=True,
    )
    if result.returncode != 0:
        print("Error during HTML minification:")
        print(result.stderr, file=sys.stderr)
        sys.exit(1)


def run_emacs_publish(dev_mode=True):
    if dev_mode:
        print("Running Emacs publish script (development)...")
        result = subprocess.run(
            ["emacs", "--script", "publish.el"],
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
            text=True,
        )
    else:
        print("Running Emacs publish script (production)...")
        result = subprocess.run(
            ["emacs", "--script", "publish.el"],
            stdout=subprocess.DEVNULL,
            stderr=subprocess.DEVNULL,
            text=True,
        )

    if result.returncode != 0:
        print("Error running publish.el:")
        print(result.stderr, file=sys.stderr)
        sys.exit(1)

    annoying_file = Path(".build/cleberg-net.html")
    if annoying_file.exists():
        os.remove(annoying_file)
    else:
        print(
            "Warning: .build/cleberg-net.html not found, but Emacs exited successfully."
        )


def copy_org_sources(content_dir="./content", build_dir="./.build/org"):
    print(f"Copying org sources: {content_dir}{build_dir}")
    if os.path.exists(build_dir):
        shutil.rmtree(build_dir)

    slug_pattern = re.compile(r"^#\+slug:\s*(.+)$", re.IGNORECASE)

    for src_path in Path(content_dir).rglob("*.org"):
        rel_dir = src_path.parent.relative_to(content_dir)
        dest_dir = Path(build_dir) / rel_dir
        dest_dir.mkdir(parents=True, exist_ok=True)

        # Try to extract slug from file headers
        slug = None
        with src_path.open("r", encoding="utf-8") as f:
            for line in f:
                m = slug_pattern.match(line)
                if m:
                    slug = m.group(1).strip()
                    break

        dest_name = f"{slug}.org" if slug else src_path.name
        shutil.copy2(src_path, dest_dir / dest_name)
        if slug:
            print(f"  {src_path.name}{dest_name}")


def generate_sitemap(build_dir=".build", base_url="https://cleberg.net"):
    """
    Generates a sitemap.xml based on contents of the .build directory.
    Only includes .html files (except 404.html).
    """
    sitemap_entries = []
    for root, dirs, files in os.walk(build_dir):
        for filename in files:
            if filename.endswith(".html") and filename != "404.html":
                full_path = os.path.join(root, filename)
                rel_path = os.path.relpath(full_path, build_dir)
                url_path = "/" + quote(rel_path.replace(os.sep, "/"))
                # Remove index.html for cleaner URLs
                if url_path.endswith("/index.html"):
                    url_path = url_path[:-10] or "/"
                elif url_path == "/index.html":
                    url_path = "/"
                loc = f"{base_url}{url_path}"

                # Last modified time
                lastmod = datetime.fromtimestamp(os.path.getmtime(full_path)).strftime(
                    "%Y-%m-%d"
                )

                sitemap_entries.append(f"""  <url>
    <loc>{loc}</loc>
    <lastmod>{lastmod}</lastmod>
  </url>""")

    sitemap_xml = f"""<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
{os.linesep.join(sitemap_entries)}
</urlset>
"""
    # Write to .build/sitemap.xml
    sitemap_path = os.path.join(build_dir, "sitemap.xml")
    with open(sitemap_path, "w", encoding="utf-8") as f:
        f.write(sitemap_xml)
    print(f"Sitemap generated at {sitemap_path} with {len(sitemap_entries)} entries.")


def inject_blog_year_separators(blog_index_path="./.build/blog/index.html"):
    """
    Post-processes the rendered blog index to inject year separator <li> elements
    between groups of posts. Weblorg/templatel doesn't support mutable loop state,
    so this runs after the HTML is generated.

    Finds each <li class="post-list-item"> that contains a <time datetime="YYYY-MM-DD">,
    and inserts <li class="post-list-year">YYYY</li> before the first post of each year.
    """
    path = Path(blog_index_path)
    if not path.exists():
        print(f"Warning: {blog_index_path} not found, skipping year separators.")
        return

    content = path.read_text(encoding="utf-8")

    # Match each post list item, capturing the date and the full element
    item_pattern = re.compile(
        r'(<li class="post-list-item">.*?</li>)',
        re.DOTALL,
    )
    date_pattern = re.compile(r"datetime=['\"]?(\d{4})-\d{2}-\d{2}['\"]?")

    current_year = None

    def replace_item(m):
        nonlocal current_year
        item_html = m.group(1)
        date_match = date_pattern.search(item_html)
        if not date_match:
            return item_html
        year = date_match.group(1)
        if year != current_year:
            current_year = year
            separator = f'<li class="post-list-year">{year}</li>'
            return f"{separator}\n{item_html}"
        return item_html

    new_content = item_pattern.sub(replace_item, content)
    path.write_text(new_content, encoding="utf-8")
    print(f"Blog year separators injected into {blog_index_path}")


def generate_tags_page(content_dir="./content/blog", build_dir="./.build"):
    """
    Scans all blog org files for #+filetags, then writes .build/tags/index.html
    with one section per tag, each post listed under its tags, sorted newest first.
    Tags link to anchor IDs on the same page.
    """
    TAG_ORDER = [
        "audit",
        "emacs",
        "linux",
        "personal",
        "privacy",
        "security",
        "self-hosting",
        "web",
    ]

    header_patterns = {
        "title": re.compile(r"^#\+title:\s*(.+)$", re.IGNORECASE),
        "date": re.compile(r"^#\+date:\s*[\[<](\d{4}-\d{2}-\d{2})"),
        "slug": re.compile(r"^#\+slug:\s*(.+)$", re.IGNORECASE),
        "tags": re.compile(r"^#\+filetags:\s*(.+)$", re.IGNORECASE),
        "draft": re.compile(r"^#\+draft:\s*(.+)$", re.IGNORECASE),
    }

    tag_map = {tag: [] for tag in TAG_ORDER}

    for org_path in Path(content_dir).glob("*.org"):
        title = date_str = slug = None
        tags = []
        is_draft = False

        with org_path.open("r", encoding="utf-8") as f:
            for line in f:
                if not title:
                    m = header_patterns["title"].match(line)
                    if m:
                        title = m.group(1).strip()
                        continue
                if not date_str:
                    m = header_patterns["date"].match(line)
                    if m:
                        date_str = m.group(1)
                        continue
                if not slug:
                    m = header_patterns["slug"].match(line)
                    if m:
                        slug = m.group(1).strip()
                        continue
                if not tags:
                    m = header_patterns["tags"].match(line)
                    if m:
                        raw = m.group(1).strip().strip(":")
                        tags = [t.strip() for t in raw.split(":") if t.strip()]
                        continue
                m = header_patterns["draft"].match(line)
                if m and m.group(1).strip().lower() != "nil":
                    is_draft = True
                    break
                if title and date_str and slug and tags:
                    break

        if is_draft or not (title and date_str and slug):
            continue

        try:
            date_obj = datetime.strptime(date_str, "%Y-%m-%d")
        except ValueError:
            continue

        for tag in tags:
            if tag in tag_map:
                tag_map[tag].append(
                    {
                        "title": title,
                        "slug": slug,
                        "date_obj": date_obj,
                        "date_str": date_str,
                    }
                )

    for tag in tag_map:
        tag_map[tag].sort(key=lambda x: x["date_obj"], reverse=True)

    # Build TOC
    toc_items = "".join(
        f'<li><a href="#{tag}">{tag}</a> <span class="tag-count">({len(tag_map[tag])})</span></li>'
        for tag in TAG_ORDER
    )

    # Build sections
    sections = []
    for tag in TAG_ORDER:
        posts = tag_map[tag]
        items = "\n".join(
            f'<li class="post-list-item">'
            f'<time datetime="{p["date_str"]}">{p["date_str"]}</time>'
            f'<a href="/blog/{p["slug"]}.html">{p["title"]}</a>'
            f"</li>"
            for p in posts
        )
        sections.append(
            f'<h2 id="{tag}">{tag}</h2>\n<ul class="post-list">\n{items}\n</ul>'
        )

    html = f"""<!doctype html>
<html lang=en-us>
<head>
<meta charset=utf-8>
<title>cleberg.net</title>
<meta name=viewport content="width=device-width,initial-scale=1">
<meta name=author content="Christian Cleberg <[email protected]>">
<meta name=description content="Stillness amidst the chaos.">
<link rel=stylesheet href=http://localhost:8000/styles.min.css>
<link rel=icon href=data:,>
</head>
<body>
<a href=#main-content class=skip-link>Skip to content</a>
<nav aria-label="Site Navigation">
<ul>
<li><a href=/>Home</a>
<li><a href=/apps/>Apps</a>
<li><a href=/blog/>Blog</a>
<li><a href=/garden/>Garden</a>
<li><a href=/guides/>Guides</a>
<li><a href=/salary/>Salary</a>
<li><a href=/tags/>Tags</a>
</ul>
</nav>
<main id=main-content>
<h1>Tags</h1>
<ul class="tag-toc">{toc_items}</ul>
{"".join(f"<section>{s}</section>" for s in sections)}
</main>
<footer>
<a href=https://github.com/ccleberg/cleberg.net>Source</a>&#183;
<a href=/feed.xml>RSS</a>&#183;
<a href=http://paske4urhs6nttrtlkuwa5cowum3fjkc6yv6kl4ncx3mjxcd77764nqd.onion/>Onion</a>&#183;
<a href=/tips/>Tips</a>
</footer>
</body>
</html>"""

    out_dir = Path(build_dir) / "tags"
    out_dir.mkdir(parents=True, exist_ok=True)
    out_path = out_dir / "index.html"
    out_path.write_text(html, encoding="utf-8")
    print(f"Tags page written to {out_path}")


def deploy_to_server(build_dir, server):
    remote_path = f"{server}:/var/www/cleberg.net/"
    print(f"Deploying .build/ → {remote_path}")
    result = subprocess.run(
        ["rsync", "-r", "--delete-before", f"{build_dir}/", remote_path],
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
        text=True,
    )
    if result.returncode != 0:
        print("Error during rsync deployment:")
        print(result.stderr, file=sys.stderr)
        sys.exit(1)


def start_dev_server(build_dir):
    print(f"Starting development HTTP server from {build_dir}/ on port 8000")
    os.chdir(build_dir)
    # This will run until interrupted (Ctrl+C)
    try:
        subprocess.run([sys.executable, "-m", "http.server", "8000"], check=True)
    except KeyboardInterrupt:
        print("\nDevelopment server stopped.")
    except subprocess.CalledProcessError as e:
        print(f"Error starting development server: {e}", file=sys.stderr)
        sys.exit(1)


def main():
    html_snippet = get_recent_posts_html("./content/blog", num_posts=3)

    build_dir = Path(".build")
    theme_dir = Path("theme/static")
    css_src = theme_dir / "styles.css"
    css_min = theme_dir / "styles.min.css"

    env = os.environ.get("ENV", "").casefold()
    build = os.environ.get("BUILD", "").casefold() == "true"
    deploy = os.environ.get("DEPLOY", "").casefold() == "true"

    if env == "prod":
        print("Environment: Production")
        if build:
            remove_build_directory(build_dir)
            minify_css(css_src, css_min)
            run_emacs_publish(dev_mode=False)
            copy_org_sources()
            update_index_html(html_snippet)
            inject_blog_year_separators()
            generate_tags_page()
            minify_html("./.build/index.html", "./.build/index.html")
            generate_sitemap()
        if deploy:
            print("Deploying to production...")
            deploy_to_server(build_dir, "homelab")
            return
    else:
        print("Environment: Development")
        if build:
            remove_build_directory(build_dir)
            minify_css(css_src, css_min)
            run_emacs_publish(dev_mode=True)
            copy_org_sources()
            update_index_html(html_snippet)
            inject_blog_year_separators()
            generate_tags_page()
            minify_html("./.build/index.html", "./.build/index.html")
            generate_sitemap()
        if deploy:
            start_dev_server(build_dir)


if __name__ == "__main__":
    main()