仔細想想,這已經是我第五次換部落格架構了。幾年來,從 Hexo、Typecho、Hugo 到 Nuxt 和 React Router v7;從 Cloudflare Pages 與各路 VPS 遷移到 Zeabur,再到 Zeabur 因故停運[1],感覺投入在個人網站上的時間大半都浪費在了精校設定和架構規劃上,正經的一篇沒寫。

我由衷期盼這是我最後一次寫題為 Hello World,用於測試的文章。

圖片

正常來說,它會在構建時自動產生多種尺寸與格式(包含 JPEG XL、AVIF 與 WebP),並去除 EXIF:

在台東隨手拍的電線杆
在台東隨手拍的電線杆(現在是我 X 帳號的封面照片)

上面的原檔是 HEIF。下面這兩張則都和我在首頁提過的筆電有關,而原檔是 JPEG:

正在啟用開發人員模式的 Chromebook
正在啟用開發人員模式的 Chromebook
SuzyQ-Adapter
SuzyQ-Adapter

程式碼

一段由 Claude Opus 5.5 產生,曾經短暫 AC TOJ 756(但很快就被新測資卡掉)的 C++ 程式碼:

#include <bits/stdc++.h>
using namespace std;

typedef vector<string> Toks;

static bool isIdStart(char c) { return isalpha((unsigned char)c) || c == '_'; }
static bool isIdChar(char c) { return isalnum((unsigned char)c) || c == '_'; }

// set to false if the judge compiles without trigraphs
static const bool TRIGRAPHS = true;

// translation phase 1: trigraphs (enabled under -std=c99)
static string replaceTrigraphs(const string &s) {
    static const string from = "=/'()!<>-", to = "#\\^[]|{}~";
    string r;
    r.reserve(s.size());
    for (size_t i = 0; i < s.size(); i++) {
        size_t k;
        if (s[i] == '?' && i + 2 < s.size() && s[i + 1] == '?' &&
            (k = from.find(s[i + 2])) != string::npos) {
            r += to[k];
            i += 2;
        } else {
            r += s[i];
        }
    }
    return r;
}

// Rewrite a string/char literal so that it contains no letters at all (hence
// no keyword text, whatever the checker looks for): every letter and '?'
// becomes a 3-digit octal escape with the same value.
static string hideText(const string &lit) {
    auto oct = [](unsigned v) {
        char buf[8];
        snprintf(buf, sizeof buf, "\\%03o", v);
        return string(buf);
    };
    size_t start = lit.find(lit.back());  // opening quote (skips an L prefix)
    size_t end = lit.size() - 1, i = start + 1;
    string r = lit.substr(0, start + 1);
    while (i < end) {
        unsigned char c = lit[i];
        if (c == '\\' && i + 1 < end) {
            char d = lit[i + 1];
            size_t j = i + 2;
            static const string simple = "abfnrtv";
            static const unsigned val[] = {7, 8, 12, 10, 13, 9, 11};
            size_t k = simple.find(d);
            if (k != string::npos) {
                r += oct(val[k]);
            } else {
                if (d == 'x') {
                    while (j < end && isxdigit((unsigned char)lit[j])) j++;
                } else if (d >= '0' && d <= '7') {
                    while (j < end && j < i + 4 && lit[j] >= '0' && lit[j] <= '7') j++;
                } else if (d == 'u' || d == 'U') {
                    j = min(end, j + (d == 'u' ? 4 : 8));
                }
                r += lit.substr(i, j - i);
            }
            i = j;
        } else if (c < 128 && (isalpha(c) || c == '?')) {
            r += oct(c);
            i++;
        } else {
            r += (char)c;
            i++;
        }
    }
    r += lit.substr(end);
    return r;
}

static Toks tokenize(const string &raw) {
    string s = TRIGRAPHS ? replaceTrigraphs(raw) : raw;
    for (size_t p = 0; (p = s.find('\\', p)) != string::npos;) {   // phase 2 (just in case)
        size_t len = s.compare(p + 1, 2, "\r\n") == 0 ? 3 : (p + 1 < s.size() && s[p + 1] == '\n') ? 2 : 0;
        if (len) s.erase(p, len);
        else p += (p + 1 < s.size() && s[p + 1] == '\\') ? 2 : 1;
    }
    static const char *ops[] = {
        "%:%:", "...", "<<=", ">>=", "->", "++", "--", "<<", ">>", "<=", ">=",
        "==", "!=", "&&", "||", "+=", "-=", "*=", "/=", "%=", "&=", "^=", "|=",
        "##", "<:", ":>", "<%", "%>", "%:"};
    static const map<string, string> digraph = {
        {"<:", "["}, {":>", "]"}, {"<%", "{"}, {"%>", "}"},
        {"%:", "#"}, {"%:%:", "##"}};
    Toks t;
    size_t i = 0, n = s.size();
    while (i < n) {
        char c = s[i];
        if (isspace((unsigned char)c)) { i++; continue; }
        if (c == '/' && i + 1 < n && s[i + 1] == '/') {          // line comment
            while (i < n && s[i] != '\n') i++;
            continue;
        }
        if (c == '/' && i + 1 < n && s[i + 1] == '*') {          // block comment
            size_t e = s.find("*/", i + 2);
            i = (e == string::npos) ? n : e + 2;
            continue;
        }
        size_t q = i + (c == 'L' && i + 1 < n ? 1 : 0);
        if (s[q] == '"' || s[q] == '\'') {                       // string / char
            char d = s[q];
            size_t j = q + 1;
            while (j < n && s[j] != d) j += (s[j] == '\\') ? 2 : 1;
            t.push_back(hideText(s.substr(i, j + 1 - i)));
            i = j + 1;
            continue;
        }
        if (isdigit((unsigned char)c) ||
            (c == '.' && i + 1 < n && isdigit((unsigned char)s[i + 1]))) {  // pp-number
            size_t j = i + 1;
            while (j < n) {
                if ((s[j] == 'e' || s[j] == 'E' || s[j] == 'p' || s[j] == 'P') &&
                    j + 1 < n && (s[j + 1] == '+' || s[j + 1] == '-'))
                    j += 2;
                else if (isIdChar(s[j]) || s[j] == '.')
                    j++;
                else
                    break;
            }
            t.push_back(s.substr(i, j - i));
            i = j;
            continue;
        }
        if (isIdStart(c)) {
            size_t j = i + 1;
            while (j < n && isIdChar(s[j])) j++;
            t.push_back(s.substr(i, j - i));
            i = j;
            continue;
        }
        string op(1, c);
        for (const char *o : ops) {                              // longest first
            size_t len = strlen(o);
            if (s.compare(i, len, o) == 0) { op = o; break; }
        }
        i += op.size();
        auto it = digraph.find(op);
        t.push_back(it == digraph.end() ? op : it->second);
    }
    return t;
}

struct Work {
    enum Kind { STMT, EMIT, BLOCK, ELSE, ENDBODY, DOTAIL } kind;
    Toks toks;                            // EMIT: tokens; DOTAIL: {L, C, B}
};

struct Converter {
    Toks t, out;
    size_t i = 0;
    int n = 0;
    vector<pair<string, string>> loops;   // (continue label, break label)
    vector<Work> work;                    // explicit stack instead of recursion
    const string eof;

    bool has() const { return i < t.size(); }
    const string &peek() const { return has() ? t[i] : eof; }
    string next() { return has() ? t[i++] : eof; }
    void emit(const string &x) { out.push_back(x); }
    void emit(initializer_list<string> xs) { for (auto &x : xs) out.push_back(x); }
    void emit(const Toks &xs) { out.insert(out.end(), xs.begin(), xs.end()); }

    Toks parenGroup() {                   // consume "( ... )", return inside
        next();
        int depth = 1;
        Toks inner;
        while (has()) {
            string x = next();
            if (x == "(") depth++;
            else if (x == ")" && --depth == 0) break;
            inner.push_back(x);
        }
        return inner;
    }

    void emitExit(const Toks &cond, const string &B) {   // if (!(cond)) goto B;
        emit({"if", "(", "!", "("});
        emit(cond);
        emit({")", ")", "goto", B, ";"});
    }

    // schedule: '{' stmt '}' then `after`
    void openBody(const string &cont, const string &brk, Work after) {
        emit("{");
        loops.push_back({cont, brk});
        work.push_back(std::move(after));
        work.push_back({Work::ENDBODY, {}});
        work.push_back({Work::STMT, {}});
    }

    void stmt() {
        if (!has()) return;
        const string tok = peek();
        if (tok == "{") {
            emit(next());
            work.push_back({Work::BLOCK, {}});
        } else if (tok == "if") {
            next();
            Toks c = parenGroup();
            emit({"if", "("});
            emit(c);
            emit(")");
            work.push_back({Work::ELSE, {}});
            work.push_back({Work::STMT, {}});
        } else if (tok == "for" || tok == "while" || tok == "do") {
            next();
            string id = to_string(++n);
            string L = "hsi_L" + id, C = "hsi_C" + id, B = "hsi_B" + id;
            if (tok == "for") {
                vector<Toks> parts(1);
                int depth = 0;
                for (auto &x : parenGroup()) {
                    if (x == "(" || x == "[") depth++;
                    else if (x == ")" || x == "]") depth--;
                    if (x == ";" && depth == 0) parts.emplace_back();
                    else parts.back().push_back(x);
                }
                parts.resize(3);
                emit("{");
                emit(parts[0]);
                emit({";", L, ":", ";"});
                if (!parts[1].empty()) emitExit(parts[1], B);
                Toks tail = {C, ":", ";"};
                tail.insert(tail.end(), parts[2].begin(), parts[2].end());
                for (const string &x : {string(";"), string("goto"), L, string(";"),
                                        B, string(":"), string(";"), string("}")})
                    tail.push_back(x);
                openBody(C, B, {Work::EMIT, tail});
            } else if (tok == "while") {
                Toks cond = parenGroup();
                emit({"{", L, ":", ";"});
                emitExit(cond, B);
                openBody(L, B, {Work::EMIT, {"goto", L, ";", B, ":", ";", "}"}});
            } else {
                emit({"{", L, ":", ";"});
                openBody(C, B, {Work::DOTAIL, {L, C, B}});
            }
        } else if ((tok == "break" || tok == "continue") && !loops.empty()) {
            next();
            next();                           // ;
            emit({"goto", tok == "break" ? loops.back().second : loops.back().first, ";"});
        } else {                              // declaration / expression statement
            while (has()) {
                string x = next();
                emit(x);
                if (x == ";") break;
            }
        }
    }

    void step(Work w) {
        switch (w.kind) {
        case Work::STMT:
            stmt();
            break;
        case Work::EMIT:
            emit(w.toks);
            break;
        case Work::BLOCK:
            if (peek() == "}") {
                emit(next());
            } else if (has()) {
                work.push_back({Work::BLOCK, {}});
                work.push_back({Work::STMT, {}});
            }
            break;
        case Work::ELSE:
            if (peek() == "else") {
                emit(next());
                work.push_back({Work::STMT, {}});
            }
            break;
        case Work::ENDBODY:
            loops.pop_back();
            emit("}");
            break;
        case Work::DOTAIL: {
            next();                           // while
            Toks cond = parenGroup();
            next();                           // ;
            emit({w.toks[1], ":", ";", "if", "("});
            emit(cond);
            emit({")", "goto", w.toks[0], ";", w.toks[2], ":", ";", "}"});
            break;
        }
        }
    }

    string run() {
        while (has()) {
            work.push_back({Work::STMT, {}});
            while (!work.empty()) {
                Work w = std::move(work.back());
                work.pop_back();
                step(std::move(w));
            }
        }
        string res;
        for (auto &x : out) {
            res += x;
            res += (x == ";" || x == "{" || x == "}") ? '\n' : ' ';
        }
        return res;
    }
};

int main() {
    string src((istreambuf_iterator<char>(cin)), istreambuf_iterator<char>());
    Converter c;
    c.t = tokenize(src);
    fputs(c.run().c_str(), stdout);
    return 0;
}

還有本站切換亮暗色模式的 theme.js 腳本:

import moon from '~icons/lucide/moon?raw';
import sun from '~icons/lucide/sun?raw';

const root = document.documentElement;
const button = document.getElementById('theme-toggle');
const system = matchMedia('(prefers-color-scheme: dark)');

const current = () => root.dataset.theme ?? (system.matches ? 'dark' : 'light');

function render() {
  const dark = current() === 'dark';

  button.innerHTML = dark ? sun : moon;
  button.setAttribute('aria-label', dark ? '切換到淺色模式' : '切換到深色模式');
}

button.addEventListener('click', () => {
  const next = current() === 'dark' ? 'light' : 'dark';

  root.dataset.theme = next;
  try {
    localStorage.setItem(KEY, next);
  } catch {}
  render();
});
system.addEventListener('change', render);

render();
button.hidden = false;

數學式

無論是行內的 E=mc2,或是獨立成行的數學式:

x=−b±b2−4ac2a

皆為 MathML,前端不引入其它函式庫。


  1. https://zeabur.com/zh-TW/blogs/zeabur-is-shutting-down ↩︎