build: vendor bb static site generator

Vendor the C99 bb sources, share directory, tests and man page under third_party/bb (MIT, upstream f5373fb) and add a make bb target. A checkout can now build project sites without an installed bb.

Assisted-by: OpenCode:DeepSeek-V4.1-Flash
This commit is contained in:
lotherk committed 2026-11-22 12:27:43 +00:00
1 parent feda8568da
commit 3eae0cc954
100 files changed
+5239

No files matched your search

+9
View File
@@ -124,3 +124,12 @@ Makefile.local
# Built binaries
/gotea
/cgi/gotea-cgi
# Vendored bb build products
/third_party/bb/bb
/third_party/bb/src/*.o
/third_party/bb/build/
/third_party/bb/dist/
/third_party/bb/tests/md_test
/third_party/bb/tests/tmpl_test
/third_party/bb/tests/theme_test
+8
View File
@@ -489,6 +489,14 @@ test-e2e: playwright frontend backend
.PHONY: build
build: frontend backend ## build everything
.PHONY: bb
bb: ## build the vendored bb static site generator
$(MAKE) -C third_party/bb
.PHONY: bb-check
bb-check: bb ## run bb's own test suite
$(MAKE) -C third_party/bb test
.PHONY: frontend
frontend: $(FRONTEND_DEST) ## build frontend files
+80
View File
@@ -0,0 +1,80 @@
# bb — C99 static site generator
# POSIX make. No GNU-isms. Builds ./bb, then `make` in a project dir runs it.
CC ?= cc
CFLAGS ?= -O2 -std=c99 -Wall -Wextra -pedantic
LDFLAGS ?=
# Compiled-in share dir (installed layout). Override: make PREFIX=/usr
BB_SHAREDIR_FLAG = -DBB_SHAREDIR=\"$(SHAREDIR)\"
# Render shell for layouts (override: make SHELL=/bin/bash)
BB_SHELL_FLAG = -DBB_SHELL=\"$(SHELL)\"
SRC = src/main.c src/markdown.c src/template.c src/theme.c src/config.c src/strbuf.c src/post.c src/serve.c
OBJ = $(SRC:.c=.o)
PREFIX ?= /usr/local
BINDIR = $(PREFIX)/bin
SHAREDIR = $(PREFIX)/share/bb
MANDIR = $(PREFIX)/share/man
# Theme for project builds (override: make THEME=kde11)
THEME ?= oli7
all: bb
bb: $(OBJ)
$(CC) $(CFLAGS) -o $@ $(OBJ) $(LDFLAGS)
%.o: %.c
$(CC) $(CFLAGS) $(BB_SHAREDIR_FLAG) $(BB_SHELL_FLAG) -c -o $@ $<
# Project targets (run inside a bb project dir)
# make THEME=oli7 -> dist/
build:
./bb build --theme $(THEME)
clean:
rm -f bb $(OBJ)
# Unit tests: markdown parser + sh templating engine
TEST_CFLAGS = -std=c99 -Wall -Wextra -pedantic -Isrc
test: bb
$(CC) $(TEST_CFLAGS) tests/md_test.c src/markdown.c src/strbuf.c -o tests/md_test
$(CC) $(TEST_CFLAGS) tests/tmpl_test.c src/template.c src/strbuf.c -o tests/tmpl_test
$(CC) $(TEST_CFLAGS) tests/theme_test.c src/theme.c src/config.c src/strbuf.c -o tests/theme_test
./tests/md_test
./tests/tmpl_test
./tests/theme_test
@command -v curl >/dev/null && sh tests/serve_test.sh || echo "serve_test: skipped (no curl)"
install: bb
mkdir -p $(DESTDIR)$(BINDIR) $(DESTDIR)$(SHAREDIR) $(DESTDIR)$(MANDIR)/man1
cp bb $(DESTDIR)$(BINDIR)/bb
cp -r share/layouts $(DESTDIR)$(SHAREDIR)/
cp -r share/themes $(DESTDIR)$(SHAREDIR)/
cp -r share/fonts $(DESTDIR)$(SHAREDIR)/
cp -r share/bb/lib $(DESTDIR)$(SHAREDIR)/
cp man/bb.1 $(DESTDIR)$(MANDIR)/man1/bb.1
uninstall:
rm -f $(DESTDIR)$(BINDIR)/bb
rm -rf $(DESTDIR)$(SHAREDIR)
rm -f $(DESTDIR)$(MANDIR)/man1/bb.1
# man page -> HTML dump for the website (www/overlay/docs/). Uses man2html
# if present, else pandoc. bb build copies overlay/ verbatim into dist/.
man-html:
mkdir -p www/overlay/docs
@if command -v man2html >/dev/null 2>&1; then \
man2html man/bb.1 > www/overlay/docs/bb.1.html; \
elif command -v pandoc >/dev/null 2>&1; then \
pandoc -s -t html man/bb.1 -o www/overlay/docs/bb.1.html; \
else \
echo "error: need man2html or pandoc to build www/overlay/docs/bb.1.html"; \
exit 1; \
fi
.PHONY: all build clean test install uninstall man-html
+28
View File
@@ -0,0 +1,28 @@
bb — C99 static site generator
==============================
Vendored from https://git.hiddenbox.org/lotherk/bb-c
Upstream commit: f5373fb (www/README/man: Texte neu, ehrlich zu KI-Entstehung)
bb is MIT-licensed. The upstream source tree carries the same grant in its
README; this notice preserves it for the vendored copy.
Copyright (c) 2026 The bb Authors
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
+108
View File
@@ -0,0 +1,108 @@
# bb
A content-first static site generator written in pure C99 + POSIX `/bin/sh`.
Write Markdown, run `make`, get a complete static site. No framework, no
JavaScript in the output, no runtime backend. Developed on Linux against
C99 and POSIX; the output is plain HTML 4.01 and reads fine in `lynx`.
bb was written together with an AI (Claude). I steered and tested, the model
wrote most of the code.
## Why
bb does one thing: turn Markdown into a static site. The layout is a plain
`/bin/sh` script, so there is no template engine to learn. The output is
HTML 4.01 with one stylesheet.
## Build
```sh
make # builds ./bb
make install # installs to /usr/local (override: make PREFIX=/usr)
```
Requires a C99 compiler and POSIX make. No external dependencies.
## Quick start
```sh
bb init my-site
cd my-site
make # builds into dist/
lynx dist/index.html
```
`bb init` scaffolds a project with `site.conf`, `pages/`, `posts/`, and a
`Makefile`. `make` runs `bb build` with the default theme.
## Usage
```sh
bb init [dir] Scaffold a new project
bb new post "Title" Create a new blog post (opens $EDITOR if set)
bb new page "Title" Create a new page (opens $EDITOR if set)
bb build [--theme N] Build current project into dist/
bb serve [--port N] Preview dist/ at http://127.0.0.1:8000/
bb theme check Report theme contrast problems
```
## Project structure
```
my-site/
site.conf # site title, description, optional font
pages/ # pages (index.md -> /, about.md -> /about/)
posts/ # blog posts (YYYY-MM-DD-slug.md)
overlay/ # optional: files copied verbatim into dist/
pages.de/ # optional: translations, built into dist/de/
posts.pt/ # optional: translated posts, built into dist/pt/blog/
Makefile # runs bb build
dist/ # output (generated, wiped on every build)
```
Pages opt into the nav bar with `menu: true`, `menuLabel` and `menuOrder`
in their frontmatter. `overlay/` is for images, `robots.txt`, a favicon:
anything that must survive `dist/` being rebuilt from scratch. See
`man bb` for the details of i18n and the templating helpers.
## Layouts
The layout is a POSIX `/bin/sh` script at `share/layouts/layout.sh`. BB
sources `share/bb/lib/bb.sh` transparently before running it, so the
`bb_*` helpers are always available — and the layout can override them.
See the [Templating Library](https://git.hiddenbox.org/lotherk/bb) docs
for `bb_nav`, `bb_content`, `bb_each_post`, `bb_each_page`, and more.
## Themes
A theme is a small file of design tokens (`bg`, `text`, `link`, `heading`,
... plus `frame = bevel` for raised/sunken Motif and Windows 95 looks) in
`share/themes/*.conf`. Pick one with `make THEME=breeze` or
`bb build --theme breeze`. Only `bg` is required; the rest is derived.
bb makes every theme readable: colours are moved towards black or white
until they reach WCAG AA contrast, so imported palettes cannot produce
unreadable pages. `bb theme check` shows what it changed.
70+ themes ship, imported from real desktop toolkits with the scripts in
`scripts/` (CDE `.dp`, KDE `.colors`, GTK2 `gtk-color-scheme`), plus `oli7`,
the one custom theme, extracted from a photo of Konrad's son and wife. See
`man bb` (THEME FORMAT) for the full token list.
## Fonts
Set `font = monospace` (or `sans`, `serif`) in `site.conf` to bundle a
font from `share/fonts/` into `dist/assets/fonts/` and emit `@font-face`
CSS. If unset, the OS default is used.
## Install layout
`make install` copies the binary to `$(PREFIX)/bin` and the layouts,
themes, fonts, and `lib/bb.sh` to `$(PREFIX)/share/bb`. The binary finds
its share dir via the compiled-in `BB_SHAREDIR` (default
`/usr/local/share/bb`), falling back to `./share` in the source tree.
## License
MIT.
+465
View File
@@ -0,0 +1,465 @@
.TH BB 1 "September 2026" "bb 1.0" "User Commands"
.SH NAME
bb \- a content-first static site generator in C99 and POSIX sh
.SH SYNOPSIS
.B bb
.RI [ command ]
.RI [ options ]
.SH DESCRIPTION
.B bb
turns Markdown into a static website. You write pages and posts as plain
Markdown files, run
.BR bb " build"
(or
.BR make ),
and get a complete static site in
.BR dist/ .
.PP
The output is a single pure HTML 4.01 version. There is no JavaScript, no
runtime backend, and no template engine: layouts are plain POSIX
.B /bin/sh
scripts that emit HTML. The result renders in
.B lynx
and in current browsers.
.B bb
is written in C99 and POSIX and is developed on Linux.
.PP
.B bb
is written in C99 and has no dependencies beyond a C99 compiler and POSIX
.BR make .
.SH COMMANDS
.TP
.B bb init
.RI [ dir ]
Scaffold a new project. Creates
.BR site.conf ,
.BR pages/ ,
.BR posts/ ,
and a
.BR Makefile .
If
.I dir
is given, the project is created there; otherwise in the current directory.
.TP
.B bb new post
.RI "[" title ]
Create a new blog post in
.BR posts/ ,
named
.IR YYYY-MM-DD-slug.md .
Opens
.B $EDITOR
if set.
.TP
.B bb new page
.RI "[" title ]
Create a new page in
.BR pages/ .
Opens
.B $EDITOR
if set.
.TP
.B bb build
.RI [ --theme " " name ]
Build the current project into
.BR dist/ .
The
.B --theme
option selects a color palette from
.BR share/themes/ .
.TP
.B bb serve
.RI [ --port " " n ]
.RI [ --bind " " addr ]
.RI [ --dir " " path ]
Serve the built site over HTTP for local preview. Defaults:
.B 127.0.0.1
port
.B 8000
directory
.BR dist/ .
GET and HEAD only; requests are confined to the directory (symlinks that
leave it are refused). It does not rebuild; run
.B bb build
first. Not intended for exposure to the internet.
.TP
.B bb theme check
.RI [ --strict ]
.RI [ name " ..." ]
Report which theme palettes are not readable as written and what bb adjusts
at build time (all themes without a name). With
.B --strict
the exit status is 1 if anything needed fixing.
.TP
.B bb help
Print usage.
.SH PROJECT STRUCTURE
A bb project is a plain directory:
.PP
.nf
.RS
my-site/
site.conf site title, description, optional font
pages/ pages (index.md -> /, about.md -> /about/)
posts/ blog posts (YYYY-MM-DD-slug.md)
overlay/ extra files copied verbatim into dist/ (optional)
pages.de/ german pages, built into dist/de/ (optional, i18n)
posts.pt/ portuguese posts, built into dist/pt/blog/ (optional, i18n)
Makefile runs bb build
dist/ output (generated)
.RE
.fi
.SH OVERLAY
.B bb build
always rebuilds
.B dist/
from scratch, so stale files never linger. Any files you want deployed
verbatim (images, extra HTML, a favicon, a custom
.BR robots.txt )
go in
.BR overlay/ ;
they are copied 1:1 into
.B dist/
after the build, overwriting generated files of the same name.
.SH I18N
Translations live in sibling directories named after the language:
.BR pages.de/ ,
.BR posts.pt/ ,
and so on. Each one builds into
.BR dist/<lang>/
with the same routes as the default language
.RB ( pages.de/index.md -> dist/de/index.html ,
.BR posts.pt/2026-01-01-ola.md " -> " dist/pt/blog/ola/ ).
The layout receives
.B LANG
(the current language, empty for the default),
.BR BB_LANGS
(space-separated list of all translation languages) and
.BR HTML_LANG_ATTR
(which the default layout uses for
.BR <html\ lang="..."> ),
and the default layout renders a text-language switcher automatically.
A site may translate only part of its content; untranslated sections
simply do not exist under
.BR dist/<lang>/ .
The default language can be marked with a
.B pages.<code>
symlink pointing at
.BR pages/ ,
for example
.BR "ln -s pages pages.en" .
The marked language builds no separate tree; instead root pages get
.B lang="<code>"
in the
.B <html>
tag and the switcher links the default language to
.B /
instead of
.BR /<code>/ .
.SH CONFIGURATION
.B site.conf
is a plain
.IR "key = value"
file. Recognized keys:
.TP
.B title
The site title, shown in the header and page titles.
.TP
.B description
A short site description, used in the header and RSS feed.
.TP
.B theme
The theme palette used when
.B --theme
is not given (e.g.
.BR oli7 ).
The command-line option takes precedence.
.TP
.B base_url
The site's absolute URL. When set,
.B bb
also writes
.BR sitemap.xml .
.TP
.B font
Optional. One of
.BR monospace ,
.BR sans ,
.BR serif .
When set, the matching font from
.B share/fonts/
is bundled into
.B dist/assets/fonts/
and an
.B @font-face
rule is emitted; the font applies to body text. When unset, the OS default
is used.
.TP
.BR font.h1 , " font.h2, " font.h3 ", " font.h4 ", " font.h5 ", " font.h6 ", " font.code
Optional per-element font overrides. The value is either a generic CSS
family
.RB ( monospace ,
.BR sans ,
.BR serif ,
or a full
.B font-family
stack like
.BR "\"Georgia, serif\"" )
or the name of a bundled font in
.BR share/fonts/ ,
which is then copied into
.B dist/assets/fonts/
with an
.B @font-face
rule and applied to that element only:
.B font.h1
targets
.BR h1 ,
.B font.code
targets
.BR "pre, code" .
Element overrides take precedence over the base
.BR font .
.SH FRONTMATTER
Each page and post may start with a YAML frontmatter block between two
.B ---
lines. Any key becomes an environment variable prefixed with
.B FM_
(uppercased) in the layout. For example:
.PP
.nf
.RS
---
title: My Post
author: Konrad
robots: Disallow: /private/
---
.RE
.fi
.PP
makes
.BR $FM_TITLE ,
.BR $FM_AUTHOR ,
and
.BR $FM_ROBOTS
available to the layout. The frontmatter is fully custom: you can add any
key and read it in the layout.
.SH LAYOUTS
The layout is a POSIX
.B /bin/sh
script at
.BR share/layouts/layout.sh .
Before it runs,
.B bb
sources
.B share/bb/lib/bb.sh
transparently, so the
.B bb_*
helper functions are always available and can be overridden by the layout.
.PP
The layout receives these environment variables:
.TP
.B SITE_TITLE
Site title.
.TP
.B SITE_DESC
Site description.
.TP
.B PAGE_TITLE
Current page or post title.
.TP
.B CONTENT_FILE
Path to the rendered Markdown content (a temp file).
.TP
.B THEME
Selected theme name.
.TP
.B NAV
Pipe-separated nav:
.IR "Label|/route/|Label2|/route2/" .
.TP
.B BB_POSTS_FILE
Path to a pipe-separated post list
.RI ( slug|title|date|url
per line).
.TP
.B BB_PAGES_FILE
Path to a pipe-separated page list
.RI ( label|url
per line).
.TP
.B BB_LIB
Path to
.BR lib/bb.sh .
.TP
.B FONT_CSS
.B @font-face
CSS if a font is configured, else empty.
.TP
.B FM_*
Every frontmatter key, uppercased.
.PP
Theme colors are also passed as environment variables
.RB ( BG ,
.BR TEXT ,
.BR LINK ,
.BR BORDER ,
.BR HEADING ,
.BR MUTED ,
.BR CODEBG ).
.SH TEMPLATING LIBRARY
.B lib/bb.sh
provides helper functions:
.TP
.B bb_nav
Emit the nav menu from
.BR $NAV .
Only pages with
.B menu: true
frontmatter appear (index.md as "Home");
.B menuLabel
overrides the label,
.B menuOrder
sorts.
.TP
.B bb_content
Emit the rendered Markdown content (reads
.BR $CONTENT_FILE ).
.TP
.B bb_each_post
.I block
Run a block for each post. Inside the block,
.BR $slug ,
.BR $title ,
.BR $date ,
and
.BR $url
are set.
.TP
.B bb_each_page
.I block
Run a block for each page. Inside the block,
.BR $label
and
.BR $url
are set.
.TP
.B bb_post_list
Emit a
.B <ul>
of posts.
.TP
.B bb_page_list
Emit a
.B <ul>
of pages.
.SH THEMES
A theme is a file of design tokens,
.IR "key = value" ,
in
.BR share/themes/<name>.conf .
Select one with
.BR "bb build --theme " name
or
.BR "make THEME=" name ,
or set
.B theme
in
.BR site.conf
(the command-line option wins).
.SH THEME FORMAT
Colours are
.B #rgb
or
.BR #rrggbb .
Only
.B bg
is required; everything else is derived when missing.
.TP
.B bg text
Page background and body text.
.TP
.B link link.visited
Link colours. Derived from
.B text
if missing.
.TP
.B heading muted
Headings and secondary text (blockquotes, footer).
.TP
.B border codebg
Rules and table borders; background of code blocks.
.TP
.B title.bg title.fg
Header bar colours, used by
.BR "frame = bevel" .
.TP
.B select.bg select.fg
Text selection.
.TP
.B light dark
Highlight and shadow edges for bevels. Derived from
.BR bg .
.TP
.B frame
.B flat
(default) or
.BR bevel :
the header becomes a raised title bar and code blocks are sunken, using
plain CSS borders, so it works in old browsers.
.TP
.B frame.width
Bevel border width (default
.BR 2px ).
.TP
.B font font.code font.h1 ... font.h6
Font stacks, e.g.
.BR "font = Georgia, serif" .
A bare name that matches a bundled font in
.B share/fonts/
is copied into the site.
.PP
At build time bb normalizes every theme: missing tokens are derived, and
text, links, headings, muted text, code text and the title bar are moved
towards black or white (keeping their hue) until they reach WCAG AA
contrast: 4.5:1, or 3:1 for headings. Imported palettes therefore stay
readable whatever their source. The stylesheet comes from the template
.BR share/layouts/theme.css ,
in which
.B {{token}}
is replaced by the normalized value.
.PP
Importers that map other formats onto these tokens live in
.BR scripts/ :
.BR import-cde.sh " (CDE " .dp ),
.BR import-kde.sh " (KDE " .colors )
and
.BR import-gtk2.sh " (GTK2 " gtk-color-scheme ).
.SH FILES
.TP
.B share/layouts/layout.sh
The layout script.
.TP
.B share/bb/lib/bb.sh
The templating library.
.TP
.B share/themes/*.conf
Theme palettes.
.TP
.B share/fonts/
Bundled fonts.
.SH ENVIRONMENT
.TP
.B EDITOR
Opened by
.B bb new
to edit a new post or page.
.SH EXIT STATUS
.B bb
exits 0 on success and non-zero on error.
.SH SEE ALSO
.BR make (1),
.BR sh (1),
.BR lynx (1)
.SH LICENSE
MIT.
+92
View File
@@ -0,0 +1,92 @@
# lib/bb.sh — templating helpers for bb layouts.
# Sourced into every layout script. Provides functions to iterate over
# posts and pages, render the nav, and emit common HTML.
#
# Data comes in as env vars set by bb:
# BB_POSTS_FILE path to a file, one post per line: slug|title|date|url
# BB_PAGES_FILE path to a file, one page per line: label|url
# NAV pipe-separated nav: Label|/route/|Label2|/route2/
# BB_PREFIX relative prefix ("../" * page depth) for root-relative links
#
# All URLs in the data files are root-relative (start with "/"). bb_*
# helpers prepend $BB_PREFIX so links work no matter where the site is
# deployed (/, /bb/, ...). Override bb_prefix() to change the behaviour.
# Resolve a root-relative URL to a page-relative one.
bb_url() {
case "$1" in
/*)
rel="${1#/}"
if [ -z "$rel" ]; then
# "/" -> the site root, relative to this page
printf '%s.' "$BB_PREFIX"
else
printf '%s%s' "$BB_PREFIX" "$rel"
fi
;;
*) printf '%s' "$1" ;;
esac
}
# Emit the nav as HTML links (pipe-separated NAV).
bb_nav() {
[ -n "$NAV" ] || return 0
oldifs="$IFS"
IFS='|'
set -- $NAV
IFS="$oldifs"
first=1
while [ $# -ge 2 ]; do
if [ $first -eq 1 ]; then
printf '<a href="%s">%s</a>' "$(bb_url "$2")" "$1"
first=0
else
printf ' | <a href="%s">%s</a>' "$(bb_url "$2")" "$1"
fi
shift 2
done
printf '\n'
}
# Run a block for each post. Usage:
# bb_each_post 'echo "<li><a href=\"$url\">$title</a></li>"'
# Inside the block: $slug, $title, $date, $url are set ($url is page-relative).
bb_each_post() {
[ -n "$BB_POSTS_FILE" ] && [ -f "$BB_POSTS_FILE" ] || return 0
while IFS='|' read -r slug title date url; do
[ -n "$slug" ] || continue
url="$(bb_url "$url")"
eval "$1"
done < "$BB_POSTS_FILE"
}
# Run a block for each page. Usage:
# bb_each_page 'echo "<a href=\"$url\">$label</a>"'
# Inside the block: $label, $url are set ($url is page-relative).
bb_each_page() {
[ -n "$BB_PAGES_FILE" ] && [ -f "$BB_PAGES_FILE" ] || return 0
while IFS='|' read -r label url; do
[ -n "$label" ] || continue
url="$(bb_url "$url")"
eval "$1"
done < "$BB_PAGES_FILE"
}
# Emit a post list as <ul>.
bb_post_list() {
echo '<ul>'
bb_each_post 'printf "<li><a href=\"%s\">%s</a> <small>(%s)</small></li>\n" "$url" "$title" "$date"'
echo '</ul>'
}
# Emit a page list as <ul>.
bb_page_list() {
echo '<ul>'
bb_each_page 'printf "<li><a href=\"%s\">%s</a></li>\n" "$url" "$label"'
echo '</ul>'
}
# Inject the rendered Markdown content.
bb_content() {
[ -n "$CONTENT_FILE" ] && [ -f "$CONTENT_FILE" ] && cat "$CONTENT_FILE"
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
+62
View File
@@ -0,0 +1,62 @@
#!/bin/sh
# layout.sh — single pure HTML 4.01 layout, works in lynx/links and old browsers.
# No JS, no CSS-grid, no light/dark split. One output, everywhere.
# Theme colors come in as env vars (BG, TEXT, LINK, BORDER, HEADING, MUTED, CODEBG).
# lib/bb.sh is sourced transparently by bb before this script runs, so the
# bb_* helpers (bb_nav, bb_content, bb_each_post) are already available
# and can be overridden here.
# language switcher: BB_LANGS="de pt" -> one entry per language, pointing at
# that language's home page via bb_url (respects BB_PREFIX). The default
# language (BB_LANG_DEFAULT, marked by a pages.<code> symlink to pages/)
# links to / instead of /<code>/. The current language is shown unlinked.
# Rendered only when the site really has two or more languages.
bb_langs() {
out=""
for l in $BB_LANGS; do
# current language: shown unlinked
if [ "$l" = "$LANG" ] || { [ -z "$LANG" ] && [ "$l" = "$BB_LANG_DEFAULT" ]; }; then
if [ -n "$out" ]; then out="$out | "; fi
out="$out$l"
continue
fi
if [ "$l" = "$BB_LANG_DEFAULT" ]; then href="$(bb_url /)"; else href="$(bb_url /$l/)"; fi
if [ -n "$out" ]; then out="$out | "; fi
out="$out<a href=\"$href\">$l</a>"
done
[ -n "$out" ] && printf '%s' "$out"
}
cat <<EOF
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html$HTML_LANG_ATTR>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>$PAGE_TITLE · $SITE_TITLE</title>
<meta name="description" content="$SITE_DESC">
<link rel="stylesheet" href="$(bb_url /css/style.css)">
<link rel="stylesheet" href="$(bb_url /css/custom.css)">
</head>
<body>
<div class="site-header">
<h1><a href="$(bb_url /)">$SITE_TITLE</a></h1>
<p>$SITE_DESC</p>
<p class="site-nav">
EOF
bb_nav
langs=$(bb_langs)
if [ -n "$langs" ] ; then
printf ' <span class="site-langs">%s</span>\n' "$langs"
fi
cat <<EOF
</p>
</div>
<div class="post-content">
$(bb_content)
</div>
<div class="site-footer">
<p>Generated by bb · theme $THEME</p>
</div>
</body>
</html>
EOF
+19
View File
@@ -0,0 +1,19 @@
/* theme.css -- stylesheet template. bb fills the double-brace tokens from
* the normalized theme and writes dist/css/style.css (man bb, THEME FORMAT).
* Plain CSS 1/2: no variables, no JS. */
body { background: {{bg}}; color: {{text}}; font-family: {{font.body.css}}; margin: 2em auto; max-width: 760px; padding: 0 1em; }
a { color: {{link}}; text-decoration: underline; }
a:visited { color: {{link.visited}}; }
a:hover, a:focus { color: {{heading}}; }
h1, h2, h3 { color: {{heading}}; }
::selection { background: {{select.bg}}; color: {{select.fg}}; }
pre, code { font-family: {{font.code.css}}; background: {{codebg}}; color: {{text}}; padding: 0.2em; }
pre { padding: 1em; overflow-x: auto; border-style: solid; border-width: {{pre.bw}}; border-color: {{pre.bc}}; }
table { border-collapse: collapse; }
th, td { border: 1px solid {{border}}; padding: 0.4em 0.8em; }
blockquote { border-left: 4px solid {{border}}; margin-left: 0; padding-left: 1em; color: {{muted}}; }
.site-header { background: {{hdr.bg}}; color: {{hdr.text}}; border-style: solid; border-width: {{hdr.bw}}; border-color: {{hdr.bc}}; padding: {{hdr.pad}}; margin-bottom: 1.5em; }
.site-header h1 { color: {{hdr.h1}}; }
.site-header a, .site-header a:visited { color: {{hdr.link}}; text-decoration: underline; }
.site-header a:hover, .site-header a:focus { color: {{hdr.h1}}; }
.site-footer { border-top: 1px solid {{border}}; margin-top: 2em; color: {{muted}}; font-size: 0.9em; }
+11
View File
@@ -0,0 +1,11 @@
# Adwaita-dark -- GTK2 colour scheme, imported by scripts/import-gtk2.sh
bg = #33393b
text = #eeeeec
codebg = #232729
link = #215d9c
heading = #215d9c
title.bg = #215d9c
select.bg = #215d9c
title.fg = #ffffff
select.fg = #ffffff
frame = flat
+11
View File
@@ -0,0 +1,11 @@
# Adwaita -- GTK2 colour scheme, imported by scripts/import-gtk2.sh
bg = #e8e8e7
text = #2e3436
codebg = #ffffff
link = #4a90d9
heading = #4a90d9
title.bg = #4a90d9
select.bg = #4a90d9
title.fg = #ffffff
select.fg = #ffffff
frame = flat
+12
View File
@@ -0,0 +1,12 @@
# b00merang-dark — imported from Node-bb
bg = #252525
text = #f2f2f2
link = #798fcb
border = #999999
heading = #f2f2f2
muted = #f2f2f2
codebg = #999999
# original fonts (from Node-bb theme.css)
font = 'Bold', sans-serif
font.code = ui-monospace, monospace
+17
View File
@@ -0,0 +1,17 @@
# BreezeClassic -- KDE colour scheme, imported by scripts/import-kde.sh
bg = #eff0f1
text = #232627
link = #2980b9
link.visited = #7f8c8d
muted = #7f8c8d
codebg = #eff0f1
heading = #3daee9
select.bg = #3daee9
select.fg = #fcfcfc
title.bg = #475057
title.fg = #fcfcfc
frame = flat
# original fonts (from Node-bb theme.css)
font = system-ui, -apple-system, sans-serif
font.code = ui-monospace, monospace
+17
View File
@@ -0,0 +1,17 @@
# BreezeDark -- KDE colour scheme, imported by scripts/import-kde.sh
bg = #202326
text = #fcfcfc
link = #1d99f3
link.visited = #9b59b6
muted = #a1a9b1
codebg = #1d1f22
heading = #3daee9
select.bg = #3daee9
select.fg = #fcfcfc
title.bg = #272c31
title.fg = #fcfcfc
frame = flat
# original fonts (from Node-bb theme.css)
font = system-ui, -apple-system, sans-serif
font.code = ui-monospace, monospace
+17
View File
@@ -0,0 +1,17 @@
# BreezeLight -- KDE colour scheme, imported by scripts/import-kde.sh
bg = #eff0f1
text = #232629
link = #2980b9
link.visited = #9b59b6
muted = #707d8a
codebg = #f7f7f7
heading = #3daee9
select.bg = #3daee9
select.fg = #ffffff
title.bg = #e3e5e7
title.fg = #232629
frame = flat
# original fonts (from Node-bb theme.css)
font = system-ui, -apple-system, sans-serif
font.code = ui-monospace, monospace
+12
View File
@@ -0,0 +1,12 @@
# breeze — imported from Node-bb
bg = #eff0f1
text = #232629
link = #236e9f
border = #999999
heading = #232629
muted = #232629
codebg = #999999
# original fonts (from Node-bb theme.css)
font = system-ui, -apple-system, sans-serif
font.code = ui-monospace, monospace
+9
View File
@@ -0,0 +1,9 @@
# cde-alpine -- CDE palette Alpine, imported by scripts/import-cde.sh
# Slots: 1 active frame, 2 inactive frame, 4 text areas, 5 background, 8 front panel.
bg = #bf9279
link = #afbffb
heading = #bfc0c5
border = #78aab8
codebg = #bfc0c5
title.bg = #afbffb
frame = bevel
+9
View File
@@ -0,0 +1,9 @@
# cde-arizona -- CDE palette Arizona, imported by scripts/import-cde.sh
# Slots: 1 active frame, 2 inactive frame, 4 text areas, 5 background, 8 front panel.
bg = #c37d79
link = #ffab78
heading = #84545f
border = #a86b79
codebg = #885770
title.bg = #ffab78
frame = bevel
+9
View File
@@ -0,0 +1,9 @@
# cde-beigerose -- CDE palette BeigeRose, imported by scripts/import-cde.sh
# Slots: 1 active frame, 2 inactive frame, 4 text areas, 5 background, 8 front panel.
bg = #919191
link = #618888
heading = #90b0ac
border = #d1c0ae
codebg = #f0eceb
title.bg = #618888
frame = bevel
+9
View File
@@ -0,0 +1,9 @@
# cde-black -- CDE palette Black, imported by scripts/import-cde.sh
# Slots: 1 active frame, 2 inactive frame, 4 text areas, 5 background, 8 front panel.
bg = #000000
link = #000000
heading = #000000
border = #000000
codebg = #000000
title.bg = #000000
frame = bevel
+9
View File
@@ -0,0 +1,9 @@
# cde-blackwhite -- CDE palette BlackWhite, imported by scripts/import-cde.sh
# Slots: 1 active frame, 2 inactive frame, 4 text areas, 5 background, 8 front panel.
bg = #000000
link = #000000
heading = #000000
border = #ffffff
codebg = #000000
title.bg = #000000
frame = bevel
+9
View File
@@ -0,0 +1,9 @@
# cde-broica -- CDE palette Broica, imported by scripts/import-cde.sh
# Slots: 1 active frame, 2 inactive frame, 4 text areas, 5 background, 8 front panel.
bg = #c6b2a8
link = #eda870
heading = #93abbf
border = #999999
codebg = #686f82
title.bg = #eda870
frame = bevel
+9
View File
@@ -0,0 +1,9 @@
# cde-cabernet -- CDE palette Cabernet, imported by scripts/import-cde.sh
# Slots: 1 active frame, 2 inactive frame, 4 text areas, 5 background, 8 front panel.
bg = #b65c71
link = #d88775
heading = #495c65
border = #638a7a
codebg = #4b5e61
title.bg = #d88775
frame = bevel
+9
View File
@@ -0,0 +1,9 @@
# cde-camouflage -- CDE palette Camouflage, imported by scripts/import-cde.sh
# Slots: 1 active frame, 2 inactive frame, 4 text areas, 5 background, 8 front panel.
bg = #8e8e77
link = #acbeb4
heading = #4e6b65
border = #6e7d76
codebg = #82829b
title.bg = #acbeb4
frame = bevel
+9
View File
@@ -0,0 +1,9 @@
# cde-charcoal -- CDE palette Charcoal, imported by scripts/import-cde.sh
# Slots: 1 active frame, 2 inactive frame, 4 text areas, 5 background, 8 front panel.
bg = #828585
link = #87b4b0
heading = #af9696
border = #767279
codebg = #896d6d
title.bg = #87b4b0
frame = bevel
+9
View File
@@ -0,0 +1,9 @@
# cde-chocolate -- CDE palette Chocolate, imported by scripts/import-cde.sh
# Slots: 1 active frame, 2 inactive frame, 4 text areas, 5 background, 8 front panel.
bg = #b9a0a0
link = #a9a9ff
heading = #9696b9
border = #708c8d
codebg = #967878
title.bg = #a9a9ff
frame = bevel
+9
View File
@@ -0,0 +1,9 @@
# cde-cinnamon -- CDE palette Cinnamon, imported by scripts/import-cde.sh
# Slots: 1 active frame, 2 inactive frame, 4 text areas, 5 background, 8 front panel.
bg = #9b6862
link = #b14b50
heading = #78695d
border = #926866
codebg = #836960
title.bg = #b14b50
frame = bevel
+9
View File
@@ -0,0 +1,9 @@
# cde-clay -- CDE palette Clay, imported by scripts/import-cde.sh
# Slots: 1 active frame, 2 inactive frame, 4 text areas, 5 background, 8 front panel.
bg = #af9191
link = #ffaaaa
heading = #2f8b8d
border = #968282
codebg = #8c6464
title.bg = #ffaaaa
frame = bevel
+9
View File
@@ -0,0 +1,9 @@
# cde-crimson -- CDE palette Crimson, imported by scripts/import-cde.sh
# Slots: 1 active frame, 2 inactive frame, 4 text areas, 5 background, 8 front panel.
bg = #68a6a0
link = #b24d7a
heading = #9793b5
border = #aeb2c3
codebg = #fff7e9
title.bg = #b24d7a
frame = bevel
+9
View File
@@ -0,0 +1,9 @@
# cde-darkgold -- CDE palette DarkGold, imported by scripts/import-cde.sh
# Slots: 1 active frame, 2 inactive frame, 4 text areas, 5 background, 8 front panel.
bg = #a8a8a8
link = #b89663
heading = #6b94b2
border = #7a7c7f
codebg = #ffffe2
title.bg = #b89663
frame = bevel
+9
View File
@@ -0,0 +1,9 @@
# cde-default -- CDE palette Default, imported by scripts/import-cde.sh
# Slots: 1 active frame, 2 inactive frame, 4 text areas, 5 background, 8 front panel.
bg = #c6b2a8
link = #eda870
heading = #93abbf
border = #999999
codebg = #686f82
title.bg = #eda870
frame = bevel
+9
View File
@@ -0,0 +1,9 @@
# cde-delphinium -- CDE palette Delphinium, imported by scripts/import-cde.sh
# Slots: 1 active frame, 2 inactive frame, 4 text areas, 5 background, 8 front panel.
bg = #78b4be
link = #85c4ed
heading = #90a0be
border = #c8c8c8
codebg = #ffffe6
title.bg = #85c4ed
frame = bevel
+9
View File
@@ -0,0 +1,9 @@
# cde-desert -- CDE palette Desert, imported by scripts/import-cde.sh
# Slots: 1 active frame, 2 inactive frame, 4 text areas, 5 background, 8 front panel.
bg = #9faeb5
link = #e5b793
heading = #c4929b
border = #c2bba7
codebg = #fdfdfa
title.bg = #e5b793
frame = bevel
+9
View File
@@ -0,0 +1,9 @@
# cde-golden -- CDE palette Golden, imported by scripts/import-cde.sh
# Slots: 1 active frame, 2 inactive frame, 4 text areas, 5 background, 8 front panel.
bg = #757ca6
link = #faad49
heading = #b1705f
border = #4d648d
codebg = #47634f
title.bg = #faad49
frame = bevel
+9
View File
@@ -0,0 +1,9 @@
# cde-grass -- CDE palette Grass, imported by scripts/import-cde.sh
# Slots: 1 active frame, 2 inactive frame, 4 text areas, 5 background, 8 front panel.
bg = #a4a9aa
link = #d4c77d
heading = #829694
border = #bec6b7
codebg = #e2f3e2
title.bg = #d4c77d
frame = bevel
+9
View File
@@ -0,0 +1,9 @@
# cde-grayscale -- CDE palette GrayScale, imported by scripts/import-cde.sh
# Slots: 1 active frame, 2 inactive frame, 4 text areas, 5 background, 8 front panel.
bg = #bcbcbc
link = #c3c3c3
heading = #939393
border = #787777
codebg = #949493
title.bg = #c3c3c3
frame = bevel
+9
View File
@@ -0,0 +1,9 @@
# cde-lilac -- CDE palette Lilac, imported by scripts/import-cde.sh
# Slots: 1 active frame, 2 inactive frame, 4 text areas, 5 background, 8 front panel.
bg = #a5a5ac
link = #d3d178
heading = #917e9f
border = #a5a5ac
codebg = #c3c3c9
title.bg = #d3d178
frame = bevel
+9
View File
@@ -0,0 +1,9 @@
# cde-makroslate -- CDE palette MakroSlate, imported by scripts/import-cde.sh
# Slots: 1 active frame, 2 inactive frame, 4 text areas, 5 background, 8 front panel.
bg = #8ea8b8
link = #181f26
heading = #f8fcff
border = #2b3a44
codebg = #607888
title.bg = #181f26
frame = bevel
+9
View File
@@ -0,0 +1,9 @@
# cde-mustard -- CDE palette Mustard, imported by scripts/import-cde.sh
# Slots: 1 active frame, 2 inactive frame, 4 text areas, 5 background, 8 front panel.
bg = #887ca3
link = #d5802e
heading = #2a6889
border = #b75f5f
codebg = #5c6a58
title.bg = #d5802e
frame = bevel
+9
View File
@@ -0,0 +1,9 @@
# cde-neptune -- CDE palette Neptune, imported by scripts/import-cde.sh
# Slots: 1 active frame, 2 inactive frame, 4 text areas, 5 background, 8 front panel.
bg = #637e95
link = #a2e5c6
heading = #616d8e
border = #3f938d
codebg = #7889a5
title.bg = #a2e5c6
frame = bevel
+9
View File
@@ -0,0 +1,9 @@
# cde-northernsky -- CDE palette NorthernSky, imported by scripts/import-cde.sh
# Slots: 1 active frame, 2 inactive frame, 4 text areas, 5 background, 8 front panel.
bg = #41525c
link = #a47591
heading = #3d407c
border = #1b5d6c
codebg = #00577a
title.bg = #a47591
frame = bevel
+9
View File
@@ -0,0 +1,9 @@
# cde-nutmeg -- CDE palette Nutmeg, imported by scripts/import-cde.sh
# Slots: 1 active frame, 2 inactive frame, 4 text areas, 5 background, 8 front panel.
bg = #b39c8f
link = #cec2bb
heading = #457293
border = #256593
codebg = #cdb2a6
title.bg = #cec2bb
frame = bevel
+9
View File
@@ -0,0 +1,9 @@
# cde-olive -- CDE palette Olive, imported by scripts/import-cde.sh
# Slots: 1 active frame, 2 inactive frame, 4 text areas, 5 background, 8 front panel.
bg = #cdbdaa
link = #83835a
heading = #9a919a
border = #c3c3b9
codebg = #cec2b8
title.bg = #83835a
frame = bevel
+9
View File
@@ -0,0 +1,9 @@
# cde-orchid -- CDE palette Orchid, imported by scripts/import-cde.sh
# Slots: 1 active frame, 2 inactive frame, 4 text areas, 5 background, 8 front panel.
bg = #9bbed7
link = #b9b9ff
heading = #b0b0bf
border = #9b9bb9
codebg = #8282ab
title.bg = #b9b9ff
frame = bevel
+9
View File
@@ -0,0 +1,9 @@
# cde-pbnj -- CDE palette PBNJ, imported by scripts/import-cde.sh
# Slots: 1 active frame, 2 inactive frame, 4 text areas, 5 background, 8 front panel.
bg = #c69076
link = #b65c61
heading = #ebd7bf
border = #d9c1b2
codebg = #f1e3d2
title.bg = #b65c61
frame = bevel
+9
View File
@@ -0,0 +1,9 @@
# cde-sand -- CDE palette Sand, imported by scripts/import-cde.sh
# Slots: 1 active frame, 2 inactive frame, 4 text areas, 5 background, 8 front panel.
bg = #af8181
link = #eabe93
heading = #969696
border = #c7afaf
codebg = #d4d4da
title.bg = #eabe93
frame = bevel
+9
View File
@@ -0,0 +1,9 @@
# cde-santafe -- CDE palette SantaFe, imported by scripts/import-cde.sh
# Slots: 1 active frame, 2 inactive frame, 4 text areas, 5 background, 8 front panel.
bg = #83a5b6
link = #ef9d9d
heading = #9c9992
border = #7eabab
codebg = #ceaf9a
title.bg = #ef9d9d
frame = bevel
+9
View File
@@ -0,0 +1,9 @@
# cde-savannah -- CDE palette Savannah, imported by scripts/import-cde.sh
# Slots: 1 active frame, 2 inactive frame, 4 text areas, 5 background, 8 front panel.
bg = #c6b2a8
link = #da9a68
heading = #93abbf
border = #b4aa94
codebg = #d2bfa8
title.bg = #da9a68
frame = bevel
+9
View File
@@ -0,0 +1,9 @@
# cde-seafoam -- CDE palette SeaFoam, imported by scripts/import-cde.sh
# Slots: 1 active frame, 2 inactive frame, 4 text areas, 5 background, 8 front panel.
bg = #afbfba
link = #aacbdb
heading = #c8ada0
border = #a7a8a7
codebg = #97b0b2
title.bg = #aacbdb
frame = bevel
+9
View File
@@ -0,0 +1,9 @@
# cde-skyred -- CDE palette SkyRed, imported by scripts/import-cde.sh
# Slots: 1 active frame, 2 inactive frame, 4 text areas, 5 background, 8 front panel.
bg = #6e91aa
link = #cc686f
heading = #5e84b9
border = #afc0c2
codebg = #e1ebe1
title.bg = #cc686f
frame = bevel
+9
View File
@@ -0,0 +1,9 @@
# cde-softblue -- CDE palette SoftBlue, imported by scripts/import-cde.sh
# Slots: 1 active frame, 2 inactive frame, 4 text areas, 5 background, 8 front panel.
bg = #94d5df
link = #7fa9eb
heading = #5d7cb8
border = #76a99c
codebg = #6c6c6c
title.bg = #7fa9eb
frame = bevel
+9
View File
@@ -0,0 +1,9 @@
# cde-southwest -- CDE palette SouthWest, imported by scripts/import-cde.sh
# Slots: 1 active frame, 2 inactive frame, 4 text areas, 5 background, 8 front panel.
bg = #b86f4b
link = #ff7d79
heading = #989ca8
border = #a97561
codebg = #b18885
title.bg = #ff7d79
frame = bevel
+9
View File
@@ -0,0 +1,9 @@
# cde-summer -- CDE palette Summer, imported by scripts/import-cde.sh
# Slots: 1 active frame, 2 inactive frame, 4 text areas, 5 background, 8 front panel.
bg = #86eae7
link = #ff5664
heading = #bcc5db
border = #ffd690
codebg = #ffffff
title.bg = #ff5664
frame = bevel
+9
View File
@@ -0,0 +1,9 @@
# cde-tundra -- CDE palette Tundra, imported by scripts/import-cde.sh
# Slots: 1 active frame, 2 inactive frame, 4 text areas, 5 background, 8 front panel.
bg = #859680
link = #f1bc7f
heading = #837e75
border = #a59680
codebg = #767e63
title.bg = #f1bc7f
frame = bevel
+9
View File
@@ -0,0 +1,9 @@
# cde-urchin -- CDE palette Urchin, imported by scripts/import-cde.sh
# Slots: 1 active frame, 2 inactive frame, 4 text areas, 5 background, 8 front panel.
bg = #3792a2
link = #d5a591
heading = #328392
border = #0f7c7a
codebg = #0c6864
title.bg = #d5a591
frame = bevel
+9
View File
@@ -0,0 +1,9 @@
# cde-wheat -- CDE palette Wheat, imported by scripts/import-cde.sh
# Slots: 1 active frame, 2 inactive frame, 4 text areas, 5 background, 8 front panel.
bg = #94d5df
link = #d1a57a
heading = #5d7cb8
border = #dbc291
codebg = #bfc0c5
title.bg = #d1a57a
frame = bevel
+9
View File
@@ -0,0 +1,9 @@
# cde-white -- CDE palette White, imported by scripts/import-cde.sh
# Slots: 1 active frame, 2 inactive frame, 4 text areas, 5 background, 8 front panel.
bg = #ffffff
link = #ffffff
heading = #ffffff
border = #ffffff
codebg = #ffffff
title.bg = #ffffff
frame = bevel
+9
View File
@@ -0,0 +1,9 @@
# cde-whiteblack -- CDE palette WhiteBlack, imported by scripts/import-cde.sh
# Slots: 1 active frame, 2 inactive frame, 4 text areas, 5 background, 8 front panel.
bg = #ffffff
link = #ffffff
heading = #ffffff
border = #000000
codebg = #ffffff
title.bg = #ffffff
frame = bevel
+14
View File
@@ -0,0 +1,14 @@
# cde — imported from Node-bb
bg = #3a5564
text = #f2f5f7
link = #9fd3ff
border = #5b6f7e
heading = #f2f5f7
muted = #f2f5f7
codebg = #5b6f7e
# original fonts (from Node-bb theme.css)
font = 'IBM Plex Sans', 'Lucida Grande', 'Tahoma', system-ui, sans-serif
font.code = 'IBM Plex Mono', 'Lucida Console', monospace
frame = bevel
+12
View File
@@ -0,0 +1,12 @@
# cepheus — imported from Node-bb
bg = #f2f6f3
text = #16231d
link = #0f6f8a
border = #bfd0c7
heading = #16231d
muted = #16231d
codebg = #bfd0c7
# original fonts (from Node-bb theme.css)
font = "Iowan Old Style", "Palatino Linotype", "Book Antiqua", Palatino, Georgia, "Times New Roman", serif
font.code = "SFMono-Regular", "Cascadia Code", "Source Code Pro", Menlo, Consolas, monospace
+12
View File
@@ -0,0 +1,12 @@
# clearlooks — imported from Node-bb
bg = #c9c9c8
text = #535353
link = #0000cc
border = #999999
heading = #535353
muted = #535353
codebg = #999999
# original fonts (from Node-bb theme.css)
font = system-ui, -apple-system, sans-serif
font.code = ui-monospace, monospace
+12
View File
@@ -0,0 +1,12 @@
# crux — imported from Node-bb
bg = #f5f5b5
text = #6b6b6b
link = #0000cc
border = #999999
heading = #6b6b6b
muted = #6b6b6b
codebg = #999999
# original fonts (from Node-bb theme.css)
font = system-ui, -apple-system, sans-serif
font.code = ui-monospace, monospace
+12
View File
@@ -0,0 +1,12 @@
# ember-tide — imported from Node-bb
bg = #f3eee7
text = #2d241f
link = #8a4b28
border = #d7c9ba
heading = #2d241f
muted = #2d241f
codebg = #d7c9ba
# original fonts (from Node-bb theme.css)
font = Georgia, "Times New Roman", Times, serif
font.code = "SFMono-Regular", Consolas, "Liberation Mono", Menlo, monospace
+12
View File
@@ -0,0 +1,12 @@
# fantome — imported from Node-bb
bg = #2f343f
text = #d8dee8
link = #d08b91
border = #999999
heading = #d8dee8
muted = #d8dee8
codebg = #999999
# original fonts (from Node-bb theme.css)
font = system-ui, -apple-system, sans-serif
font.code = ui-monospace, monospace
+14
View File
@@ -0,0 +1,14 @@
# fvwm-default — imported from Node-bb
bg = #003c3c
text = #ffffff
link = #9595e9
border = #999999
heading = #ffffff
muted = #ffffff
codebg = #999999
# original fonts (from Node-bb theme.css)
font = system-ui, -apple-system, sans-serif
font.code = ui-monospace, monospace
frame = bevel
+14
View File
@@ -0,0 +1,14 @@
# fvwm — imported from Node-bb
bg = #7e8a92
text = #111111
link = #003399
border = #6f6f6f
heading = #111111
muted = #111111
codebg = #6f6f6f
# original fonts (from Node-bb theme.css)
font = 'IBM Plex Sans', 'Tahoma', 'MS Sans Serif', system-ui, sans-serif
font.code = 'IBM Plex Mono', 'Lucida Console', monospace
frame = bevel
+12
View File
@@ -0,0 +1,12 @@
# industrial — imported from Node-bb
bg = #d9d9d9
text = #5c5c5c
link = #0000cc
border = #999999
heading = #5c5c5c
muted = #5c5c5c
codebg = #999999
# original fonts (from Node-bb theme.css)
font = system-ui, -apple-system, sans-serif
font.code = ui-monospace, monospace
+14
View File
@@ -0,0 +1,14 @@
# irix5 — imported from Node-bb
bg = #9fb8d9
text = #000000
link = #0000aa
border = #5f5f66
heading = #000000
muted = #000000
codebg = #5f5f66
# original fonts (from Node-bb theme.css)
font = "Trebuchet MS", "Helvetica Neue", Arial, sans-serif
font.code = "Courier New", "Lucida Console", monospace
frame = bevel
+14
View File
@@ -0,0 +1,14 @@
# irixium — imported from Node-bb
bg = #c1c1c1
text = #000000
link = #0000ff
border = #999999
heading = #000000
muted = #000000
codebg = #999999
# original fonts (from Node-bb theme.css)
font = system-ui, -apple-system, sans-serif
font.code = ui-monospace, monospace
frame = bevel
+12
View File
@@ -0,0 +1,12 @@
# iwanttobelieve — imported from Node-bb
bg = #f6f3ef
text = #31281f
link = #5a4937
border = #c7bcaf
heading = #31281f
muted = #31281f
codebg = #c7bcaf
# original fonts (from Node-bb theme.css)
font = "Arial Narrow", Arial, "Helvetica Neue", Helvetica, sans-serif
font.code = "SFMono-Regular", Consolas, "Liberation Mono", Menlo, monospace
+12
View File
@@ -0,0 +1,12 @@
# kde-whitesur-dark — imported from Node-bb
bg = #363636
text = #fcfcfc
link = #849cf6
border = #999999
heading = #fcfcfc
muted = #fcfcfc
codebg = #999999
# original fonts (from Node-bb theme.css)
font = system-ui, -apple-system, sans-serif
font.code = ui-monospace, monospace
+12
View File
@@ -0,0 +1,12 @@
# kde-whitesur — imported from Node-bb
bg = #ffffff
text = #242424
link = #277ab0
border = #999999
heading = #242424
muted = #242424
codebg = #999999
# original fonts (from Node-bb theme.css)
font = system-ui, -apple-system, sans-serif
font.code = ui-monospace, monospace
+14
View File
@@ -0,0 +1,14 @@
# kde11 — imported from Node-bb
bg = #c0c0c0
text = #000000
link = #0000cc
border = #999999
heading = #000000
muted = #000000
codebg = #999999
# original fonts (from Node-bb theme.css)
font = system-ui, -apple-system, sans-serif
font.code = ui-monospace, monospace
frame = bevel
+12
View File
@@ -0,0 +1,12 @@
# lumiere — imported from Node-bb
bg = #ffffff
text = #474c57
link = #317b92
border = #999999
heading = #474c57
muted = #474c57
codebg = #999999
# original fonts (from Node-bb theme.css)
font = system-ui, -apple-system, sans-serif
font.code = ui-monospace, monospace
+14
View File
@@ -0,0 +1,14 @@
# macos9 — imported from Node-bb
bg = #c9c8e8
text = #111111
link = #0000cc
border = #8f8fb0
heading = #111111
muted = #111111
codebg = #8f8fb0
# original fonts (from Node-bb theme.css)
font = "Charcoal", "Chicago", "Geneva", "MS Sans Serif", "Segoe UI", sans-serif
font.code = "Monaco", "Courier New", monospace
frame = bevel
+12
View File
@@ -0,0 +1,12 @@
# mist — imported from Node-bb
bg = #eaeaea
text = #666666
link = #0000cc
border = #999999
heading = #666666
muted = #666666
codebg = #999999
# original fonts (from Node-bb theme.css)
font = system-ui, -apple-system, sans-serif
font.code = ui-monospace, monospace
+12
View File
@@ -0,0 +1,12 @@
# nashville-gruvbox — imported from Node-bb
bg = #282828
text = #ebdbb2
link = #dc7430
border = #282828
heading = #ebdbb2
muted = #ebdbb2
codebg = #282828
# original fonts (from Node-bb theme.css)
font = system-ui, -apple-system, sans-serif
font.code = ui-monospace, monospace
+12
View File
@@ -0,0 +1,12 @@
# nashville-solarized — imported from Node-bb
bg = #073642
text = #eee8d5
link = #e37aac
border = #073642
heading = #eee8d5
muted = #eee8d5
codebg = #073642
# original fonts (from Node-bb theme.css)
font = system-ui, -apple-system, sans-serif
font.code = ui-monospace, monospace
+13
View File
@@ -0,0 +1,13 @@
# oli7 — extracted from a photo of my son and my wife.
# The warmth and love of that image, turned into a color palette.
bg = #faf5ec
text = #3f3128
link = #5f7f98
border = #d8c3ad
heading = #2f5fbf
muted = #7c6758
codebg = #f1e5d6
# original fonts (from Node-bb theme.css)
font = ui-serif, Georgia, Cambria, "Times New Roman", Times, serif
font.code = ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace
+14
View File
@@ -0,0 +1,14 @@
# qnx6 — imported from Node-bb
bg = #d5d4e3
text = #1f1f22
link = #8f5e12
border = #8d8d96
heading = #1f1f22
muted = #1f1f22
codebg = #8d8d96
# original fonts (from Node-bb theme.css)
font = Georgia, "Times New Roman", Times, serif
font.code = "Courier New", Courier, monospace
frame = bevel
+14
View File
@@ -0,0 +1,14 @@
# redmond — imported from Node-bb
bg = #e6e6e6
text = #666666
link = #0000cc
border = #999999
heading = #666666
muted = #666666
codebg = #999999
# original fonts (from Node-bb theme.css)
font = system-ui, -apple-system, sans-serif
font.code = ui-monospace, monospace
frame = bevel
+12
View File
@@ -0,0 +1,12 @@
# sunset — imported from Node-bb
bg = #f6ead9
text = #253240
link = #8a4f17
border = #d6b890
heading = #253240
muted = #253240
codebg = #d6b890
# original fonts (from Node-bb theme.css)
font = Georgia, "Times New Roman", Times, serif
font.code = "SFMono-Regular", Consolas, "Liberation Mono", Menlo, monospace
+12
View File
@@ -0,0 +1,12 @@
# thinice — imported from Node-bb
bg = #c1c1cc
text = #4f4f4f
link = #0000cc
border = #999999
heading = #4f4f4f
muted = #4f4f4f
codebg = #999999
# original fonts (from Node-bb theme.css)
font = 'Sans', sans-serif
font.code = ui-monospace, monospace
+12
View File
@@ -0,0 +1,12 @@
# whitesur — imported from Node-bb
bg = #eef2f7
text = #1b2430
link = #0860f2
border = #cfd7e6
heading = #1b2430
muted = #1b2430
codebg = #cfd7e6
# original fonts (from Node-bb theme.css)
font = 'SF Pro Text', 'San Francisco', 'Helvetica Neue', 'Lucida Grande', 'Segoe UI', system-ui, sans-serif
font.code = 'SFMono-Regular', 'Menlo', 'JetBrains Mono', ui-monospace, monospace
+84
View File
@@ -0,0 +1,84 @@
/* bb.h — shared declarations for the bb C99 static site generator */
#ifndef BB_H
#define BB_H
/* Enable POSIX extensions (strdup, getline, etc.) under strict C99 */
#define _POSIX_C_SOURCE 200809L
#include <stddef.h>
#include <stdio.h>
/* Growable string buffer */
typedef struct {
char *data;
size_t len;
size_t cap;
} strbuf;
void sb_init(strbuf *sb);
void sb_free(strbuf *sb);
void sb_append(strbuf *sb, const char *s);
void sb_append_n(strbuf *sb, const char *s, size_t n);
void sb_append_char(strbuf *sb, char c);
void sb_printf(strbuf *sb, const char *fmt, ...);
/* HTML escaping */
void html_escape(strbuf *out, const char *s);
/* Markdown -> HTML, streamed from FILE* to FILE* (never buffered in RAM) */
void markdown_to_html(FILE *out, FILE *in, const char *prefix);
/* config: simple key=value lines, # comments, blank lines skipped */
typedef struct {
char **keys;
char **vals;
size_t count;
size_t cap;
} config;
void config_init(config *c);
void config_free(config *c);
int config_load(config *c, const char *path);
const char *config_get(const config *c, const char *key);
int config_add(config *c, const char *key, const char *val);
int config_set(config *c, const char *key, const char *val); /* replace or add */
/* Themes (theme.c). A theme is a config of design tokens (see man bb,
* "THEME FORMAT"). theme_normalize() fills in missing tokens, enforces
* readable contrast and adds the computed tokens the CSS template uses; the
* adjustments it made are appended to `report` (may be NULL). */
double theme_contrast(const char *fg, const char *bg); /* WCAG ratio, <0 on bad colour */
void theme_normalize(config *t, strbuf *report);
void theme_render(strbuf *out, const char *template_css, const config *cfg);
/* Load + normalize one theme file and print a check report to `out`.
* Returns the number of adjustments that were necessary (0 = clean). */
int theme_check_file(const char *path, const char *name, FILE *out);
/* Shell templating: run `sh script` with env vars, stream stdout to `out`.
* Returns 0 on success, -1 on failure. */
int template_run(const char *script_path, char *const envp[], FILE *out);
/* A blog post: date, slug, title, excerpt, content (markdown) */
typedef struct {
char *date; /* YYYY-MM-DD */
char *slug; /* from filename */
char *title;
char *excerpt;
char *robots; /* optional robots.txt lines from frontmatter */
char *content; /* raw markdown body */
config fm; /* all frontmatter key/value pairs */
} Post;
Post *post_new(void);
void post_free(Post *p);
/* Parse a _posts/YYYY-MM-DD-slug.md file. Returns NULL on failure. */
Post *post_parse(const char *path);
/* Parse all frontmatter key/value pairs from any .md file into a config.
* Returns 0 on success (even if no frontmatter), -1 on read error. */
int frontmatter_load(const char *path, config *out);
/* Read a frontmatter value from any .md file (pages too). malloc'd or NULL. */
char *frontmatter_value(const char *path, const char *key);
/* `bb serve`: preview server for dist/ (serve.c) */
int cmd_serve(int argc, char **argv);
#endif /* BB_H */
+101
View File
@@ -0,0 +1,101 @@
/* config.c — simple key=value config parser (INI-ish, no sections) */
#include "bb.h"
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
void config_init(config *c) {
c->keys = NULL;
c->vals = NULL;
c->count = 0;
c->cap = 0;
}
void config_free(config *c) {
for (size_t i = 0; i < c->count; i++) {
free(c->keys[i]);
free(c->vals[i]);
}
free(c->keys);
free(c->vals);
c->keys = NULL;
c->vals = NULL;
c->count = 0;
c->cap = 0;
}
int config_add(config *c, const char *key, const char *val) {
if (c->count == c->cap) {
size_t ncap = c->cap ? c->cap * 2 : 16;
char **nk = realloc(c->keys, ncap * sizeof(char *));
char **nv = realloc(c->vals, ncap * sizeof(char *));
if (!nk || !nv) return -1;
c->keys = nk;
c->vals = nv;
c->cap = ncap;
}
c->keys[c->count] = strdup(key);
c->vals[c->count] = strdup(val);
if (!c->keys[c->count] || !c->vals[c->count]) return -1;
c->count++;
return 0;
}
int config_load(config *c, const char *path) {
FILE *f = fopen(path, "r");
if (!f) return -1;
char *line = NULL;
size_t cap = 0;
ssize_t len;
while ((len = getline(&line, &cap, f)) != -1) {
/* strip newline */
while (len > 0 && (line[len-1] == '\n' || line[len-1] == '\r')) line[--len] = '\0';
/* trim */
char *s = line;
while (*s && isspace((unsigned char)*s)) s++;
if (*s == '\0' || *s == '#') continue;
/* find = */
char *eq = strchr(s, '=');
if (!eq) continue;
*eq = '\0';
char *key = s;
char *val = eq + 1;
/* trim key */
char *ke = key + strlen(key) - 1;
while (ke >= key && isspace((unsigned char)*ke)) *ke-- = '\0';
/* trim val */
while (*val && isspace((unsigned char)*val)) val++;
char *ve = val + strlen(val) - 1;
while (ve >= val && isspace((unsigned char)*ve)) *ve-- = '\0';
/* strip quotes */
if (*val == '"' && val[strlen(val)-1] == '"') {
val[strlen(val)-1] = '\0';
val++;
}
config_add(c, key, val);
}
free(line);
fclose(f);
return 0;
}
/* Replace the value of the first matching key, or add it. */
int config_set(config *c, const char *key, const char *val) {
for (size_t i = 0; i < c->count; i++) {
if (strcmp(c->keys[i], key) == 0) {
char *nv = strdup(val);
if (!nv) return -1;
free(c->vals[i]);
c->vals[i] = nv;
return 0;
}
}
return config_add(c, key, val);
}
const char *config_get(const config *c, const char *key) {
for (size_t i = 0; i < c->count; i++) {
if (strcmp(c->keys[i], key) == 0) return c->vals[i];
}
return NULL;
}
+1435
View File
File diff suppressed because it is too large. Load diff
+565
View File
@@ -0,0 +1,565 @@
/* markdown.c — minimal CommonMark-subset Markdown -> HTML parser.
* Supports: ATX headings, paragraphs, unordered/ordered lists, fenced code
* blocks, indented code, blockquotes, horizontal rules, tables, and inline
* emphasis/strong/code/links/images. Pure C99, no dependencies.
*
* Memory-efficient: output streams directly to a FILE* (the content file),
* never buffered in RAM. Only one line at a time is held in memory. */
#include "bb.h"
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdio.h>
/* ---- inline parsing ---- */
/* Relative prefix ("../" * depth) prepended to root-relative links in
* Markdown content, so links work no matter where the site is deployed.
* Set per render by markdown_to_html(). */
static const char *g_md_prefix = "";
/* ---- heading anchors ---- */
/* Turn a heading's text into a URL-safe anchor id: lowercase, spaces and
* runs of non-alphanumerics become a single '-'. Empty result -> "sec". */
static void heading_slug(const char *s, size_t n, char *out, size_t outsz) {
size_t o = 0;
int dash = 0;
for (size_t i = 0; i < n && o + 1 < outsz; i++) {
unsigned char c = (unsigned char)s[i];
if (isalnum(c)) {
out[o++] = (char)tolower(c);
dash = 0;
} else if (!dash) {
out[o++] = '-';
dash = 1;
}
}
while (o > 0 && out[o - 1] == '-') o--;
if (o == 0) { out[o++] = 's'; out[o++] = 'e'; out[o++] = 'c'; }
out[o] = '\0';
}
static void inline_parse(FILE *out, const char *s, size_t n);
/* Find matching closing delimiter for emphasis/strong. Returns index or n. */
static size_t find_close(const char *s, size_t n, char c, size_t start) {
for (size_t i = start; i < n; i++) {
if (s[i] == c) return i;
}
return n;
}
/* Escape a span into a small stack buffer, then write it. */
static void emit_escaped(FILE *out, const char *s, size_t n) {
for (size_t i = 0; i < n; i++) {
switch (s[i]) {
case '&': fputs("&amp;", out); break;
case '<': fputs("&lt;", out); break;
case '>': fputs("&gt;", out); break;
case '"': fputs("&quot;", out); break;
case '\'': fputs("&#39;", out); break;
default: fputc(s[i], out); break;
}
}
}
static void inline_parse(FILE *out, const char *s, size_t n) {
size_t i = 0;
while (i < n) {
char c = s[i];
if (c == '\\' && i + 1 < n) {
fputc(s[i + 1], out);
i += 2;
continue;
}
if (c == '`') {
/* code span */
size_t j = i + 1;
while (j < n && s[j] == '`') j++;
size_t ticks = j - i;
size_t close = i + ticks;
while (close < n) {
if (s[close] == '`') {
size_t k = close;
size_t cnt = 0;
while (k < n && s[k] == '`') { cnt++; k++; }
if (cnt == ticks) break;
close = k;
} else {
close++;
}
}
if (close < n) {
fputs("<code>", out);
emit_escaped(out, s + i + ticks, close - (i + ticks));
fputs("</code>", out);
i = close + ticks;
continue;
}
fputc(c, out);
i++;
continue;
}
if (c == '*' || c == '_') {
/* emphasis or strong */
int strong = (i + 1 < n && s[i + 1] == c);
size_t delim = strong ? 2 : 1;
size_t close = find_close(s, n, c, i + delim);
if (close < n) {
if (strong) {
fputs("<strong>", out);
inline_parse(out, s + i + 2, close - (i + 2));
fputs("</strong>", out);
i = close + 2;
} else {
fputs("<em>", out);
inline_parse(out, s + i + 1, close - (i + 1));
fputs("</em>", out);
i = close + 1;
}
continue;
}
fputc(c, out);
i++;
continue;
}
if (c == '!') {
/* image: ![alt](url) -> <img src="..." alt="..."> */
if (i + 1 < n && s[i + 1] == '[') {
size_t close = find_close(s, n, ']', i + 2);
if (close < n && close + 1 < n && s[close + 1] == '(') {
size_t pend = close + 2;
while (pend < n && s[pend] != ')') pend++;
if (pend < n) {
size_t u = close + 2;
while (u < pend && isspace((unsigned char)s[u])) u++;
size_t uend = pend;
while (uend > u && isspace((unsigned char)s[uend - 1])) uend--;
fputs("<img src=\"", out);
if (uend > u && s[u] == '/') {
fputs(g_md_prefix, out);
emit_escaped(out, s + u + 1, uend - (u + 1));
} else {
emit_escaped(out, s + u, uend - u);
}
fputs("\" alt=\"", out);
emit_escaped(out, s + i + 2, close - (i + 2));
fputs("\">", out);
i = pend + 1;
continue;
}
}
}
fputc(c, out);
i++;
continue;
}
if (c == '[') {
/* link — if the text starts with ![alt](url), render the outer
* link around the image (nested image-in-link, CommonMark) */
if (i + 3 < n && s[i + 1] == '!' && s[i + 2] == '[') {
size_t iclose = find_close(s, n, ']', i + 3);
if (iclose < n && iclose + 1 < n && s[iclose + 1] == '(') {
size_t ipend = iclose + 2;
while (ipend < n && s[ipend] != ')') ipend++;
if (ipend < n) {
/* the outer link: next ]( after the inner url ) */
size_t close = find_close(s, n, ']', ipend + 1);
if (close < n && close + 1 < n && s[close + 1] == '(') {
size_t pend = close + 2;
while (pend < n && s[pend] != ')') pend++;
if (pend < n) {
size_t u = close + 2;
while (u < pend && isspace((unsigned char)s[u])) u++;
size_t uend = pend;
while (uend > u && isspace((unsigned char)s[uend - 1])) uend--;
fputs("<a href=\"", out);
if (uend > u && s[u] == '/') {
fputs(g_md_prefix, out);
emit_escaped(out, s + u + 1, uend - (u + 1));
} else {
emit_escaped(out, s + u, uend - u);
}
fputs("\">", out);
/* the image */
size_t iu = iclose + 2;
while (iu < ipend && isspace((unsigned char)s[iu])) iu++;
size_t iuend = ipend;
while (iuend > iu && isspace((unsigned char)s[iuend - 1])) iuend--;
fputs("<img src=\"", out);
if (iuend > iu && s[iu] == '/') {
fputs(g_md_prefix, out);
emit_escaped(out, s + iu + 1, iuend - (iu + 1));
} else {
emit_escaped(out, s + iu, iuend - iu);
}
fputs("\" alt=\"", out);
emit_escaped(out, s + i + 3, iclose - (i + 3));
fputs("\"></a>", out);
i = pend + 1;
continue;
}
}
}
}
}
/* link */
size_t close = find_close(s, n, ']', i + 1);
if (close < n && close + 1 < n && s[close + 1] == '(') {
size_t pend = close + 2;
while (pend < n && s[pend] != ')') pend++;
if (pend < n) {
/* extract url (trimmed) */
size_t u = close + 2;
while (u < pend && isspace((unsigned char)s[u])) u++;
size_t uend = pend;
while (uend > u && isspace((unsigned char)s[uend - 1])) uend--;
fputs("<a href=\"", out);
if (uend > u && s[u] == '/') {
/* root-relative link: make it page-relative */
fputs(g_md_prefix, out);
emit_escaped(out, s + u + 1, uend - (u + 1));
} else {
emit_escaped(out, s + u, uend - u);
}
fputs("\">", out);
inline_parse(out, s + i + 1, close - (i + 1));
fputs("</a>", out);
i = pend + 1;
continue;
}
}
fputc(c, out);
i++;
continue;
}
switch (c) {
case '&': fputs("&amp;", out); break;
case '<': fputs("&lt;", out); break;
case '>': fputs("&gt;", out); break;
case '"': fputs("&quot;", out); break;
default: fputc(c, out); break;
}
i++;
}
}
/* ---- block parsing ---- */
static int is_atx_heading(const char *line) {
int i = 0;
while (line[i] == ' ') i++;
if (line[i] != '#') return 0;
int j = i;
while (line[j] == '#') j++;
if (j - i > 6) return 0;
if (line[j] != '\0' && line[j] != ' ' && line[j] != '\t') return 0;
return j - i;
}
static int is_fence(const char *line, char *which) {
int i = 0;
while (line[i] == ' ') i++;
if (line[i] != '`' && line[i] != '~') return 0;
char c = line[i];
int j = i;
while (line[j] == c) j++;
if (j - i < 3) return 0;
*which = c;
return 1;
}
static int is_hr(const char *line) {
int i = 0;
while (line[i] == ' ') i++;
if (line[i] == '\0') return 0;
char c = line[i];
if (c != '-' && c != '*' && c != '_') return 0;
int count = 0;
while (line[i] != '\0') {
if (line[i] == c) count++;
else if (line[i] == ' ' || line[i] == '\t') { /* ok */ }
else return 0;
i++;
}
return count >= 3;
}
static int is_blockquote(const char *line) {
int i = 0;
while (line[i] == ' ') i++;
return line[i] == '>';
}
static int is_ul(const char *line) {
int i = 0;
while (line[i] == ' ') i++;
if (line[i] != '-' && line[i] != '*' && line[i] != '+') return 0;
/* require a space (or end) after the marker, else it's emphasis */
i++;
if (line[i] == '\0') return 0;
return line[i] == ' ' || line[i] == '\t';
}
static int is_ol(const char *line) {
int i = 0;
while (line[i] == ' ') i++;
if (!isdigit((unsigned char)line[i])) return 0;
while (isdigit((unsigned char)line[i])) i++;
return line[i] == '.' || line[i] == ')';
}
static int is_table_sep(const char *line) {
int i = 0;
int has_dash = 0;
while (line[i] != '\0') {
char c = line[i];
if (c == '|' || c == ' ' || c == '\t' || c == ':') { /* ok */ }
else if (c == '-') has_dash = 1;
else return 0;
i++;
}
return has_dash;
}
/* Split a table row into cells. Cells are written into a fixed array of
* pointers into a caller-provided buffer (no per-cell malloc). */
static void split_cells(const char *line, char *buf, size_t bufsz,
char **cells, int *count, int max) {
*count = 0;
const char *p = line;
size_t bi = 0;
if (*p == '|') p++;
while (*p && *count < max) {
const char *start = p;
while (*p && *p != '|') p++;
size_t len = (size_t)(p - start);
while (len > 0 && isspace((unsigned char)start[0])) { start++; len--; }
while (len > 0 && isspace((unsigned char)start[len - 1])) len--;
if (bi + len + 1 <= bufsz) {
memcpy(buf + bi, start, len);
buf[bi + len] = '\0';
cells[*count] = buf + bi;
bi += len + 1;
(*count)++;
}
if (*p == '|') p++;
}
}
void markdown_to_html(FILE *out, FILE *in, const char *prefix) {
if (!out || !in) return;
g_md_prefix = prefix ? prefix : "";
/* read lines dynamically (getline), no fixed-size limit */
char *line = NULL;
size_t line_cap = 0;
int in_para = 0;
int in_list = 0; /* 0 none, 1 ul, 2 ol */
int in_blockquote = 0;
int in_table = 0;
int first_line = 1;
while (getline(&line, &line_cap, in) != -1) {
/* strip trailing newline */
size_t li = strlen(line);
while (li > 0 && (line[li-1] == '\n' || line[li-1] == '\r')) line[--li] = '\0';
const char *l = line;
/* strip YAML frontmatter on the first line — only if a closing
* --- is found, else it's a horizontal rule */
if (first_line) {
first_line = 0;
if (strncmp(l, "---", 3) == 0) {
long pos = ftell(in);
int found = 0;
char *pl = NULL;
size_t pl_cap = 0;
while (getline(&pl, &pl_cap, in) != -1) {
size_t pll = strlen(pl);
while (pll > 0 && (pl[pll-1] == '\n' || pl[pll-1] == '\r')) pl[--pll] = '\0';
if (strncmp(pl, "---", 3) == 0) { found = 1; break; }
}
free(pl);
if (found) {
if (getline(&line, &line_cap, in) == -1) break;
li = strlen(line);
while (li > 0 && (line[li-1] == '\n' || line[li-1] == '\r')) line[--li] = '\0';
l = line;
} else {
/* not frontmatter — rewind, treat as hr below */
fseek(in, pos, SEEK_SET);
}
}
}
/* blank line */
int blank = 1;
for (size_t i = 0; i < li; i++) {
if (!isspace((unsigned char)line[i])) { blank = 0; break; }
}
if (blank) {
if (in_para) { fputs("</p>\n", out); in_para = 0; }
if (in_list) { fputs(in_list == 1 ? "</ul>\n" : "</ol>\n", out); in_list = 0; }
if (in_blockquote) { fputs("</blockquote>\n", out); in_blockquote = 0; }
if (in_table) { in_table = 0; }
continue;
}
/* fenced code */
char fence_char = 0;
if (is_fence(l, &fence_char)) {
if (in_para) { fputs("</p>\n", out); in_para = 0; }
if (in_list) { fputs(in_list == 1 ? "</ul>\n" : "</ol>\n", out); in_list = 0; }
if (in_blockquote) { fputs("</blockquote>\n", out); in_blockquote = 0; }
fputs("<pre><code>", out);
while (getline(&line, &line_cap, in) != -1) {
size_t li2 = strlen(line);
while (li2 > 0 && (line[li2-1] == '\n' || line[li2-1] == '\r')) line[--li2] = '\0';
char fc2 = 0;
if (is_fence(line, &fc2) && fc2 == fence_char) break;
emit_escaped(out, line, li2);
fputc('\n', out);
}
fputs("</code></pre>\n", out);
continue;
}
/* ATX heading */
int hlevel = is_atx_heading(l);
if (hlevel) {
if (in_para) { fputs("</p>\n", out); in_para = 0; }
if (in_list) { fputs(in_list == 1 ? "</ul>\n" : "</ol>\n", out); in_list = 0; }
if (in_blockquote) { fputs("</blockquote>\n", out); in_blockquote = 0; }
if (in_table) { in_table = 0; }
const char *h = l;
while (*h == ' ') h++;
h += hlevel;
while (*h == ' ' || *h == '\t') h++;
size_t hlen = strlen(h);
while (hlen > 0 && h[hlen - 1] == '#') hlen--;
while (hlen > 0 && isspace((unsigned char)h[hlen - 1])) hlen--;
char slug[128];
heading_slug(h, hlen, slug, sizeof slug);
fprintf(out, "<h%d id=\"%s\">", hlevel, slug);
inline_parse(out, h, hlen);
fprintf(out, "</h%d>\n", hlevel);
continue;
}
/* horizontal rule */
if (is_hr(l)) {
if (in_para) { fputs("</p>\n", out); in_para = 0; }
if (in_list) { fputs(in_list == 1 ? "</ul>\n" : "</ol>\n", out); in_list = 0; }
if (in_blockquote) { fputs("</blockquote>\n", out); in_blockquote = 0; }
if (in_table) { in_table = 0; }
fputs("<hr>\n", out);
continue;
}
/* blockquote */
if (is_blockquote(l)) {
if (in_para) { fputs("</p>\n", out); in_para = 0; }
if (in_list) { fputs(in_list == 1 ? "</ul>\n" : "</ol>\n", out); in_list = 0; }
if (!in_blockquote) { fputs("<blockquote>\n", out); in_blockquote = 1; }
const char *q = l;
while (*q == ' ') q++;
if (*q == '>') q++;
if (*q == ' ') q++;
fputs("<p>", out);
inline_parse(out, q, strlen(q));
fputs("</p>\n", out);
continue;
}
/* table separator */
if (in_table && is_table_sep(l)) {
continue;
}
/* table header start: a line with | and a following separator */
if (!in_table && strchr(l, '|')) {
/* peek next line to see if it's a separator */
long pos = ftell(in);
char *next = NULL;
size_t next_cap = 0;
if (getline(&next, &next_cap, in) != -1) {
size_t ni = strlen(next);
while (ni > 0 && (next[ni-1] == '\n' || next[ni-1] == '\r')) next[--ni] = '\0';
if (is_table_sep(next)) {
if (in_para) { fputs("</p>\n", out); in_para = 0; }
if (in_list) { fputs(in_list == 1 ? "</ul>\n" : "</ol>\n", out); in_list = 0; }
if (in_blockquote) { fputs("</blockquote>\n", out); in_blockquote = 0; }
in_table = 1;
fputs("<table>\n<thead>\n<tr>\n", out);
char cells[64][128];
char *cellp[64];
int n = 0;
split_cells(l, (char *)cells, sizeof(cells), cellp, &n, 64);
for (int i = 0; i < n; i++) {
fputs("<th>", out);
inline_parse(out, cellp[i], strlen(cellp[i]));
fputs("</th>\n", out);
}
fputs("</tr>\n</thead>\n<tbody>\n", out);
free(next);
continue;
}
/* not a table: rewind and fall through */
fseek(in, pos, SEEK_SET);
free(next);
}
}
/* table body row */
if (in_table && strchr(l, '|')) {
fputs("<tr>\n", out);
char cells[64][128];
char *cellp[64];
int n = 0;
split_cells(l, (char *)cells, sizeof(cells), cellp, &n, 64);
for (int i = 0; i < n; i++) {
fputs("<td>", out);
inline_parse(out, cellp[i], strlen(cellp[i]));
fputs("</td>\n", out);
}
fputs("</tr>\n", out);
continue;
}
/* list item */
int is_ul_item = is_ul(l);
int is_ol_item = is_ol(l);
if (is_ul_item || is_ol_item) {
if (in_para) { fputs("</p>\n", out); in_para = 0; }
if (in_blockquote) { fputs("</blockquote>\n", out); in_blockquote = 0; }
if (in_table) { in_table = 0; }
int want = is_ul_item ? 1 : 2;
if (in_list != want) {
if (in_list) fputs(in_list == 1 ? "</ul>\n" : "</ol>\n", out);
fputs(want == 1 ? "<ul>\n" : "<ol>\n", out);
in_list = want;
}
const char *item = l;
while (*item == ' ') item++;
if (is_ul_item) { item++; while (*item == ' ') item++; }
else { while (isdigit((unsigned char)*item)) item++; if (*item=='.'||*item==')') item++; while (*item==' ') item++; }
fputs("<li>", out);
inline_parse(out, item, strlen(item));
fputs("</li>\n", out);
continue;
}
/* paragraph */
if (!in_para) { fputs("<p>", out); in_para = 1; }
else fputc('\n', out);
inline_parse(out, l, li);
}
free(line);
if (in_para) fputs("</p>\n", out);
if (in_list) fputs(in_list == 1 ? "</ul>\n" : "</ol>\n", out);
if (in_blockquote) fputs("</blockquote>\n", out);
if (in_table) fputs("</tbody>\n</table>\n", out);
}
+216
View File
@@ -0,0 +1,216 @@
/* post.c — blog post parsing.
* Format: _posts/YYYY-MM-DD-slug.md with optional YAML frontmatter:
* ---
* title: "..."
* excerpt: "..."
* ---
* body markdown
*/
#include "bb.h"
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
Post *post_new(void) {
Post *p = calloc(1, sizeof(Post));
return p;
}
void post_free(Post *p) {
if (!p) return;
free(p->date);
free(p->slug);
free(p->title);
free(p->excerpt);
free(p->robots);
free(p->content);
config_free(&p->fm);
free(p);
}
/* Extract a YAML frontmatter value for a key. Returns malloc'd string or NULL. */
static char *frontmatter_get(const char *fm, const char *key) {
size_t klen = strlen(key);
const char *p = fm;
while (*p) {
/* line start */
if (strncmp(p, key, klen) == 0 && p[klen] == ':') {
const char *v = p + klen + 1;
while (*v == ' ' || *v == '\t') v++;
/* strip quotes */
if (*v == '"') {
v++;
const char *end = strchr(v, '"');
if (end) {
size_t len = (size_t)(end - v);
char *out = malloc(len + 1);
if (out) { memcpy(out, v, len); out[len] = '\0'; }
return out;
}
}
/* unquoted: to end of line */
const char *end = v;
while (*end && *end != '\n') end++;
while (end > v && isspace((unsigned char)end[-1])) end--;
size_t len = (size_t)(end - v);
char *out = malloc(len + 1);
if (out) { memcpy(out, v, len); out[len] = '\0'; }
return out;
}
/* next line */
const char *nl = strchr(p, '\n');
if (!nl) break;
p = nl + 1;
}
return NULL;
}
/* Parse a frontmatter block (key: value lines) into a config.
* fm is a NUL-terminated string of the block between the --- markers. */
static void frontmatter_parse_str(const char *fm, config *out) {
const char *p = fm;
while (*p) {
/* skip to line start */
while (*p == '\n') p++;
if (!*p) break;
const char *eol = strchr(p, '\n');
size_t len = eol ? (size_t)(eol - p) : strlen(p);
/* trim */
const char *s = p;
size_t slen = len;
while (slen > 0 && (*s == ' ' || *s == '\t')) { s++; slen--; }
while (slen > 0 && (s[slen-1] == ' ' || s[slen-1] == '\t' || s[slen-1] == '\r')) slen--;
if (slen > 0 && s[0] != '#') {
const char *colon = memchr(s, ':', slen);
if (colon) {
size_t klen = (size_t)(colon - s);
const char *v = colon + 1;
size_t vlen = slen - klen - 1;
while (vlen > 0 && (*v == ' ' || *v == '\t')) { v++; vlen--; }
while (vlen > 0 && (v[vlen-1] == ' ' || v[vlen-1] == '\t')) vlen--;
/* strip quotes */
if (vlen >= 2 && v[0] == '"' && v[vlen-1] == '"') { v++; vlen -= 2; }
char *key = malloc(klen + 1);
char *val = malloc(vlen + 1);
if (key && val) {
memcpy(key, s, klen); key[klen] = '\0';
memcpy(val, v, vlen); val[vlen] = '\0';
config_add(out, key, val);
}
free(key);
free(val);
}
}
p = eol ? eol + 1 : p + len;
}
}
/* Parse all frontmatter key/value pairs from a .md file into a config.
* Returns 0 on success (even if no frontmatter), -1 on read error. */
int frontmatter_load(const char *path, config *out) {
config_init(out);
FILE *f = fopen(path, "rb");
if (!f) return -1;
char *line = NULL;
size_t cap = 0;
ssize_t n = getline(&line, &cap, f);
if (n < 0 || strncmp(line, "---", 3) != 0) {
free(line);
fclose(f);
return 0; /* no frontmatter */
}
free(line);
line = NULL;
cap = 0;
/* read until closing --- */
strbuf fm; sb_init(&fm);
while ((n = getline(&line, &cap, f)) >= 0) {
if (strncmp(line, "---", 3) == 0) break;
sb_append_n(&fm, line, (size_t)n);
}
free(line);
fclose(f);
frontmatter_parse_str(fm.data, out);
sb_free(&fm);
return 0;
}
/* Read a frontmatter value from any .md file (pages and posts).
* Returns malloc'd string or NULL if the key is absent. */
char *frontmatter_value(const char *path, const char *key) {
config fm;
if (frontmatter_load(path, &fm) != 0) return NULL;
const char *v = config_get(&fm, key);
char *out = v ? strdup(v) : NULL;
config_free(&fm);
return out;
}
Post *post_parse(const char *path) {
char *raw = NULL;
FILE *f = fopen(path, "rb");
if (!f) return NULL;
fseek(f, 0, SEEK_END);
long sz = ftell(f);
fseek(f, 0, SEEK_SET);
if (sz < 0) { fclose(f); return NULL; }
raw = malloc((size_t)sz + 1);
if (!raw) { fclose(f); return NULL; }
size_t rd = fread(raw, 1, (size_t)sz, f);
raw[rd] = '\0';
fclose(f);
Post *p = post_new();
if (!p) { free(raw); return NULL; }
config_init(&p->fm);
/* filename: YYYY-MM-DD-slug.md */
const char *base = strrchr(path, '/');
base = base ? base + 1 : path;
size_t blen = strlen(base);
if (blen >= 3 && strcmp(base + blen - 3, ".md") == 0) {
/* date = first 10 chars if they look like YYYY-MM-DD */
if (blen >= 10 && base[4] == '-' && base[7] == '-') {
p->date = malloc(11);
if (p->date) { memcpy(p->date, base, 10); p->date[10] = '\0'; }
/* slug = after date + dash, before .md */
const char *slug_start = base + 11;
size_t slug_len = blen - 3 - (size_t)(slug_start - base);
if (slug_len > 0) {
p->slug = malloc(slug_len + 1);
if (p->slug) { memcpy(p->slug, slug_start, slug_len); p->slug[slug_len] = '\0'; }
}
}
}
/* frontmatter */
const char *body = raw;
if (strncmp(raw, "---", 3) == 0) {
const char *fm_end = strstr(raw + 3, "\n---");
if (fm_end) {
/* frontmatter block between raw+3 and fm_end */
size_t fm_len = (size_t)(fm_end - (raw + 3));
char *fm = malloc(fm_len + 1);
if (fm) {
memcpy(fm, raw + 3, fm_len);
fm[fm_len] = '\0';
frontmatter_parse_str(fm, &p->fm);
p->title = frontmatter_get(fm, "title");
p->excerpt = frontmatter_get(fm, "excerpt");
p->robots = frontmatter_get(fm, "robots");
free(fm);
}
body = fm_end + 4; /* skip \n--- */
if (*body == '\n') body++;
}
}
/* content = rest */
p->content = strdup(body);
/* fallback title = slug */
if (!p->title && p->slug) p->title = strdup(p->slug);
free(raw);
return p;
}
+319
View File
@@ -0,0 +1,319 @@
/* serve.c — `bb serve`: tiny static HTTP server for previewing dist/.
* Usage: bb serve [--port N] [--bind ADDR] [--dir DIR]
* GET and HEAD only, one forked child per connection, no keep-alive.
* Meant for local preview, not for the open internet. C99 + POSIX. */
#define _XOPEN_SOURCE 700 /* realpath() under strict -std=c99 */
#include "bb.h"
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <netdb.h>
#define REQ_MAX 8192
static const struct { const char *ext, *type; } mime_types[] = {
{ "html", "text/html; charset=utf-8" },
{ "htm", "text/html; charset=utf-8" },
{ "css", "text/css; charset=utf-8" },
{ "js", "text/javascript; charset=utf-8" },
{ "json", "application/json" },
{ "xml", "application/xml" },
{ "txt", "text/plain; charset=utf-8" },
{ "svg", "image/svg+xml" },
{ "png", "image/png" },
{ "jpg", "image/jpeg" },
{ "jpeg", "image/jpeg" },
{ "gif", "image/gif" },
{ "webp", "image/webp" },
{ "ico", "image/x-icon" },
{ "woff", "font/woff" },
{ "woff2","font/woff2" },
{ "ttf", "font/ttf" },
{ "otf", "font/otf" },
{ "pdf", "application/pdf" },
{ NULL, NULL }
};
static const char *mime_for(const char *path) {
const char *dot = strrchr(path, '.');
const char *slash = strrchr(path, '/');
if (!dot || (slash && dot < slash)) return "application/octet-stream";
dot++;
for (int i = 0; mime_types[i].ext; i++)
if (strcmp(dot, mime_types[i].ext) == 0) return mime_types[i].type;
return "application/octet-stream";
}
static int write_all(int fd, const char *buf, size_t n) {
while (n > 0) {
ssize_t w = write(fd, buf, n);
if (w < 0) {
if (errno == EINTR) continue;
return -1;
}
buf += w;
n -= (size_t)w;
}
return 0;
}
/* Send a complete small response (status line, headers, body). */
static void respond(int fd, int head_only, int code, const char *reason,
const char *extra, const char *body) {
strbuf out; sb_init(&out);
size_t blen = body ? strlen(body) : 0;
sb_printf(&out,
"HTTP/1.0 %d %s\r\n"
"Content-Type: text/html; charset=utf-8\r\n"
"Content-Length: %lu\r\n"
"%s"
"Connection: close\r\n\r\n",
code, reason, (unsigned long)blen, extra ? extra : "");
if (!head_only && body) sb_append(&out, body);
write_all(fd, out.data, out.len);
sb_free(&out);
}
static void respond_error(int fd, int head_only, int code, const char *reason) {
strbuf body; sb_init(&body);
sb_printf(&body,
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\">\n"
"<html><head><title>%d %s</title></head>\n"
"<body><h1>%d %s</h1></body></html>\n",
code, reason, code, reason);
respond(fd, head_only, code, reason, NULL, body.data);
sb_free(&body);
}
static int hexval(int c) {
if (c >= '0' && c <= '9') return c - '0';
if (c >= 'a' && c <= 'f') return c - 'a' + 10;
if (c >= 'A' && c <= 'F') return c - 'A' + 10;
return -1;
}
/* Decode %XX in place. Returns 0 on success, -1 on bad escape or NUL. */
static int url_decode(char *s) {
char *w = s;
for (const char *r = s; *r; r++) {
if (*r == '%') {
int h = hexval((unsigned char)r[1]);
int l = h >= 0 ? hexval((unsigned char)r[2]) : -1;
if (h < 0 || l < 0) return -1;
int c = h * 16 + l;
if (c == 0) return -1;
*w++ = (char)c;
r += 2;
} else {
*w++ = *r;
}
}
*w = '\0';
return 0;
}
/* True if any path segment is "..". */
static int has_dotdot(const char *p) {
while (*p) {
while (*p == '/') p++;
const char *seg = p;
while (*p && *p != '/') p++;
if (p - seg == 2 && seg[0] == '.' && seg[1] == '.') return 1;
}
return 0;
}
static void handle(int fd, const char *root) {
struct timeval tv = { 10, 0 };
setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof tv);
char req[REQ_MAX + 1];
size_t got = 0;
while (got < REQ_MAX) {
ssize_t r = read(fd, req + got, REQ_MAX - got);
if (r < 0 && errno == EINTR) continue;
if (r <= 0) break;
got += (size_t)r;
req[got] = '\0';
if (strstr(req, "\r\n\r\n") || strstr(req, "\n\n")) break;
}
req[got] = '\0';
char *eol = strpbrk(req, "\r\n");
if (!eol) { respond_error(fd, 0, 400, "Bad Request"); return; }
*eol = '\0';
char *method = req;
char *target = strchr(method, ' ');
if (!target) { respond_error(fd, 0, 400, "Bad Request"); return; }
*target++ = '\0';
char *ver = strchr(target, ' ');
if (!ver) { respond_error(fd, 0, 400, "Bad Request"); return; }
*ver = '\0';
int head = strcmp(method, "HEAD") == 0;
if (!head && strcmp(method, "GET") != 0) {
respond(fd, 0, 405, "Method Not Allowed", "Allow: GET, HEAD\r\n",
"<html><body><h1>405 Method Not Allowed</h1></body></html>\n");
fprintf(stderr, "%s %s -> 405\n", method, target);
return;
}
char *q = strpbrk(target, "?#");
if (q) *q = '\0';
char logpath[256];
snprintf(logpath, sizeof logpath, "%s", target);
if (target[0] != '/' || url_decode(target) != 0 || has_dotdot(target)) {
respond_error(fd, head, 400, "Bad Request");
fprintf(stderr, "%s %s -> 400\n", method, logpath);
return;
}
strbuf path; sb_init(&path);
sb_printf(&path, "%s%s", root, target);
struct stat st;
if (stat(path.data, &st) == 0 && S_ISDIR(st.st_mode)) {
size_t tl = strlen(target);
if (target[tl - 1] != '/') {
strbuf loc; sb_init(&loc);
sb_printf(&loc, "Location: %s/\r\n", logpath);
respond(fd, head, 301, "Moved Permanently", loc.data, "");
sb_free(&loc);
sb_free(&path);
fprintf(stderr, "%s %s -> 301\n", method, logpath);
return;
}
sb_append(&path, "index.html");
}
/* Resolve symlinks and make sure the result is still inside root. */
char *real = realpath(path.data, NULL);
sb_free(&path);
size_t rl = strlen(root);
if (!real || strncmp(real, root, rl) != 0 ||
(real[rl] != '/' && real[rl] != '\0') ||
stat(real, &st) != 0 || !S_ISREG(st.st_mode)) {
free(real);
respond_error(fd, head, 404, "Not Found");
fprintf(stderr, "%s %s -> 404\n", method, logpath);
return;
}
FILE *f = fopen(real, "rb");
if (!f) {
free(real);
respond_error(fd, head, 403, "Forbidden");
fprintf(stderr, "%s %s -> 403\n", method, logpath);
return;
}
strbuf hdr; sb_init(&hdr);
sb_printf(&hdr,
"HTTP/1.0 200 OK\r\n"
"Content-Type: %s\r\n"
"Content-Length: %lu\r\n"
"Cache-Control: no-cache\r\n"
"Connection: close\r\n\r\n",
mime_for(real), (unsigned long)st.st_size);
int ok = write_all(fd, hdr.data, hdr.len) == 0;
sb_free(&hdr);
if (ok && !head) {
char buf[16384];
size_t n;
while ((n = fread(buf, 1, sizeof buf, f)) > 0)
if (write_all(fd, buf, n) != 0) break;
}
fclose(f);
free(real);
fprintf(stderr, "%s %s -> 200\n", method, logpath);
}
int cmd_serve(int argc, char **argv) {
const char *port = "8000", *bind_addr = "127.0.0.1", *dir = "dist";
for (int i = 2; i < argc; i++) {
if (strcmp(argv[i], "--port") == 0 && i + 1 < argc) {
port = argv[++i];
char *end;
long p = strtol(port, &end, 10);
if (*end || p < 1 || p > 65535) {
fprintf(stderr, "bb: invalid port '%s'\n", port);
return 1;
}
} else if (strcmp(argv[i], "--bind") == 0 && i + 1 < argc) {
bind_addr = argv[++i];
} else if (strcmp(argv[i], "--dir") == 0 && i + 1 < argc) {
dir = argv[++i];
} else {
fprintf(stderr, "bb: usage: bb serve [--port N] [--bind ADDR] [--dir DIR]\n");
return 1;
}
}
char *root = realpath(dir, NULL);
struct stat st;
if (!root || stat(root, &st) != 0 || !S_ISDIR(st.st_mode)) {
fprintf(stderr, "bb: '%s' not found -- run `bb build` first\n", dir);
free(root);
return 1;
}
struct addrinfo hints, *res;
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
int gai = getaddrinfo(bind_addr, port, &hints, &res);
if (gai != 0) {
fprintf(stderr, "bb: cannot resolve '%s': %s\n", bind_addr, gai_strerror(gai));
free(root);
return 1;
}
int lfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
if (lfd < 0) {
perror("bb: socket");
freeaddrinfo(res); free(root);
return 1;
}
int one = 1;
setsockopt(lfd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof one);
if (bind(lfd, res->ai_addr, res->ai_addrlen) != 0 || listen(lfd, 16) != 0) {
fprintf(stderr, "bb: cannot listen on %s:%s: %s\n", bind_addr, port, strerror(errno));
close(lfd); freeaddrinfo(res); free(root);
return 1;
}
freeaddrinfo(res);
signal(SIGPIPE, SIG_IGN);
struct sigaction sa;
memset(&sa, 0, sizeof sa);
sa.sa_handler = SIG_IGN;
sa.sa_flags = SA_NOCLDWAIT | SA_RESTART;
sigaction(SIGCHLD, &sa, NULL);
fprintf(stderr, "bb: serving %s at http://%s:%s/ (Ctrl-C to stop)\n",
dir, bind_addr, port);
for (;;) {
int cfd = accept(lfd, NULL, NULL);
if (cfd < 0) {
if (errno == EINTR) continue;
perror("bb: accept");
break;
}
pid_t pid = fork();
if (pid == 0) {
close(lfd);
handle(cfd, root);
close(cfd);
_exit(0);
}
close(cfd);
}
close(lfd);
free(root);
return 1;
}
+76
View File
@@ -0,0 +1,76 @@
/* strbuf.c — growable string buffer */
#include "bb.h"
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <stdio.h>
void sb_init(strbuf *sb) {
sb->data = NULL;
sb->len = 0;
sb->cap = 0;
}
void sb_free(strbuf *sb) {
free(sb->data);
sb->data = NULL;
sb->len = 0;
sb->cap = 0;
}
static int sb_reserve(strbuf *sb, size_t extra) {
if (sb->len + extra + 1 <= sb->cap) return 0;
size_t ncap = sb->cap ? sb->cap : 64;
while (ncap < sb->len + extra + 1) ncap *= 2;
char *nd = realloc(sb->data, ncap);
if (!nd) return -1;
sb->data = nd;
sb->cap = ncap;
return 0;
}
void sb_append_n(strbuf *sb, const char *s, size_t n) {
if (sb_reserve(sb, n) != 0) return;
memcpy(sb->data + sb->len, s, n);
sb->len += n;
sb->data[sb->len] = '\0';
}
void sb_append(strbuf *sb, const char *s) {
if (!s) return;
sb_append_n(sb, s, strlen(s));
}
void sb_append_char(strbuf *sb, char c) {
if (sb_reserve(sb, 1) != 0) return;
sb->data[sb->len++] = c;
sb->data[sb->len] = '\0';
}
void sb_printf(strbuf *sb, const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
va_list ap2;
va_copy(ap2, ap);
int n = vsnprintf(NULL, 0, fmt, ap);
va_end(ap);
if (n < 0) { va_end(ap2); return; }
if (sb_reserve(sb, (size_t)n) != 0) { va_end(ap2); return; }
vsnprintf(sb->data + sb->len, (size_t)n + 1, fmt, ap2);
va_end(ap2);
sb->len += (size_t)n;
}
void html_escape(strbuf *out, const char *s) {
if (!s) return;
for (; *s; s++) {
switch (*s) {
case '&': sb_append(out, "&amp;"); break;
case '<': sb_append(out, "&lt;"); break;
case '>': sb_append(out, "&gt;"); break;
case '"': sb_append(out, "&quot;"); break;
case '\'': sb_append(out, "&#39;"); break;
default: sb_append_char(out, *s); break;
}
}
}
+83
View File
@@ -0,0 +1,83 @@
/* template.c — /bin/sh templating engine.
*
* Modeled on Konrad's tmpl(1) (https://github.com/lotherk/tmpl): run a sh
* script with env vars, capture stdout, stream it to the destination.
*
* Memory-efficient: stdout is streamed straight to the destination FILE*
* (the dist output file), never buffered in RAM. Only one line at a time
* is held in memory.
*
* Content is passed via a temp file (CONTENT_FILE) to avoid shell-quoting
* issues with large HTML blobs.
*
* The render shell is compiled in via BB_SHELL (default /bin/sh) and can be
* overridden at build time: make SHELL=/bin/bash.
*
* The environment is passed to the child via execve(2) — never setenv(3) —
* so frontmatter vars (FM_*) from one page cannot leak into the next.
*/
#include "bb.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#ifndef BB_SHELL
#define BB_SHELL "/bin/sh"
#endif
int template_run(const char *script_path, char *const envp[], FILE *out) {
/* find BB_LIB in envp (we never setenv, so getenv won't see it) */
const char *lib = NULL;
for (size_t i = 0; envp && envp[i]; i++) {
if (strncmp(envp[i], "BB_LIB=", 7) == 0) { lib = envp[i] + 7; break; }
}
/* argv: <shell> -c '. "$BB_LIB"; . "$1"' <shell> <script> */
const char *cmd;
if (lib && lib[0]) {
cmd = ". \"$BB_LIB\"; . \"$1\"";
} else {
cmd = ". \"$1\"";
}
char *argv[6] = {0};
argv[0] = (char *)BB_SHELL;
argv[1] = (char *)"-c";
argv[2] = (char *)cmd;
argv[3] = (char *)BB_SHELL;
argv[4] = (char *)script_path;
argv[5] = NULL; /* execve requires a NULL-terminated argv */
int pfd[2];
if (pipe(pfd) != 0) return -1;
pid_t pid = fork();
if (pid < 0) { close(pfd[0]); close(pfd[1]); return -1; }
if (pid == 0) {
/* child: stdout -> pipe */
close(pfd[0]);
dup2(pfd[1], STDOUT_FILENO);
close(pfd[1]);
execve(BB_SHELL, argv, envp);
_exit(127);
}
/* parent: stream pipe -> out, one line at a time */
close(pfd[1]);
FILE *fp = fdopen(pfd[0], "r");
if (!fp) { close(pfd[0]); return -1; }
char *line = NULL;
size_t line_len = 0;
ssize_t n;
while ((n = getline(&line, &line_len, fp)) != -1) {
fwrite(line, 1, (size_t)n, out);
}
free(line);
fclose(fp);
int status;
if (waitpid(pid, &status, 0) == -1) return -1;
if (!WIFEXITED(status)) return -1;
return WEXITSTATUS(status) == 0 ? 0 : -1;
}
+301
View File
@@ -0,0 +1,301 @@
/* theme.c — design tokens, contrast enforcement and CSS templating.
*
* A theme is a flat config of design tokens (see man bb, THEME FORMAT).
* Importers (scripts/import-*.sh) only have to map a source palette onto
* those tokens; this file makes the result usable:
* - missing tokens are derived from the ones present,
* - foreground colours are nudged until they reach WCAG AA contrast
* (4.5:1) against the surface they sit on, keeping their hue,
* - the structural tokens the CSS template needs (header bar, bevels)
* are computed.
* Pure C99, no libm. */
#include "bb.h"
#include <stdlib.h>
#include <string.h>
#define MIN_CONTRAST 4.5 /* WCAG AA, normal text */
#define MIN_CONTRAST_LARGE 3.0 /* WCAG AA, large text (headings) */
typedef struct { int r, g, b; } rgb;
static const rgb WHITE = { 255, 255, 255 };
static const rgb BLACK = { 0, 0, 0 };
static int hexdigit(int c) {
if (c >= '0' && c <= '9') return c - '0';
if (c >= 'a' && c <= 'f') return c - 'a' + 10;
if (c >= 'A' && c <= 'F') return c - 'A' + 10;
return -1;
}
/* Accepts #rgb and #rrggbb (surrounding blanks ignored). */
static int parse_color(const char *s, rgb *out) {
if (!s) return 0;
while (*s == ' ' || *s == '\t') s++;
if (*s != '#') return 0;
s++;
size_t n = 0;
while (hexdigit((unsigned char)s[n]) >= 0) n++;
const char *e = s + n;
while (*e == ' ' || *e == '\t') e++;
if (*e) return 0;
int v[6];
if (n == 3) {
for (int i = 0; i < 3; i++) v[2 * i] = v[2 * i + 1] = hexdigit((unsigned char)s[i]);
} else if (n == 6) {
for (int i = 0; i < 6; i++) v[i] = hexdigit((unsigned char)s[i]);
} else {
return 0;
}
out->r = v[0] * 16 + v[1];
out->g = v[2] * 16 + v[3];
out->b = v[4] * 16 + v[5];
return 1;
}
static void fmt_color(char buf[8], rgb c) {
static const char hx[] = "0123456789abcdef";
buf[0] = '#';
buf[1] = hx[c.r >> 4]; buf[2] = hx[c.r & 15];
buf[3] = hx[c.g >> 4]; buf[4] = hx[c.g & 15];
buf[5] = hx[c.b >> 4]; buf[6] = hx[c.b & 15];
buf[7] = '\0';
}
/* x^2.4 for x in [0,1] without libm: x^(12/5) = fifth root of x^12. */
static double pow24(double x) {
if (x <= 0.0) return 0.0;
double y = 1.0;
for (int i = 0; i < 12; i++) y *= x;
double r = 1.0;
for (int i = 0; i < 60; i++) {
double r2 = r * r, r4 = r2 * r2;
r -= (r4 * r - y) / (5.0 * r4);
}
return r;
}
static double chan(int v) {
double x = v / 255.0;
return x <= 0.04045 ? x / 12.92 : pow24((x + 0.055) / 1.055);
}
static double luminance(rgb c) {
return 0.2126 * chan(c.r) + 0.7152 * chan(c.g) + 0.0722 * chan(c.b);
}
static double contrast(rgb a, rgb b) {
double la = luminance(a), lb = luminance(b);
if (la < lb) { double t = la; la = lb; lb = t; }
return (la + 0.05) / (lb + 0.05);
}
double theme_contrast(const char *fg, const char *bg) {
rgb a, b;
if (!parse_color(fg, &a) || !parse_color(bg, &b)) return -1.0;
return contrast(a, b);
}
static int clamp255(double v) {
return v < 0.0 ? 0 : v > 255.0 ? 255 : (int)(v + 0.5);
}
/* a*(1-t) + b*t */
static rgb mix(rgb a, rgb b, double t) {
rgb o;
o.r = clamp255(a.r + (b.r - a.r) * t);
o.g = clamp255(a.g + (b.g - a.g) * t);
o.b = clamp255(a.b + (b.b - a.b) * t);
return o;
}
/* Black or white, whichever reads better on `bg`. */
static rgb best_on(rgb bg) {
return contrast(WHITE, bg) >= contrast(BLACK, bg) ? WHITE : BLACK;
}
/* Move `fg` towards black or white (whichever contrasts more with `bg`) in
* 1% steps until it reaches `target`. Keeps as much of the hue as possible.
* If even the extreme falls short (mid-tone surface) the extreme is used. */
static rgb ensure(rgb fg, rgb bg, double target) {
if (contrast(fg, bg) >= target) return fg;
rgb dir = best_on(bg);
for (int p = 1; p <= 100; p++) {
rgb c = mix(fg, dir, p / 100.0);
if (contrast(c, bg) >= target) return c;
}
return dir;
}
static int same(rgb a, rgb b) { return a.r == b.r && a.g == b.g && a.b == b.b; }
static int tget(const config *t, const char *key, rgb *out) {
return parse_color(config_get(t, key), out);
}
static void tset(config *t, const char *key, rgb c) {
char buf[8];
fmt_color(buf, c);
config_set(t, key, buf);
}
/* Record a contrast correction (only these are reported). */
static void note_fix(strbuf *rep, const char *key, rgb from, rgb to, rgb surface) {
if (!rep) return;
char a[8], b[8];
fmt_color(a, from); fmt_color(b, to);
sb_printf(rep, " %-13s %s -> %s (contrast %.2f -> %.2f)\n",
key, a, b, contrast(from, surface), contrast(to, surface));
}
/* Foreground token: read it (or derive `fallback`), make it readable on
* `surface`, store it, return it. */
static rgb fg_token(config *t, const char *key, rgb fallback, rgb surface,
double target, strbuf *rep) {
rgb c;
int have = tget(t, key, &c);
if (!have) c = fallback;
rgb fixed = ensure(c, surface, target);
if (!same(fixed, c)) note_fix(rep, key, c, fixed, surface);
tset(t, key, fixed);
return fixed;
}
/* Surface/decoration token: read it or derive `fallback`, store, return. */
static rgb plain_token(config *t, const char *key, rgb fallback) {
rgb c;
if (!tget(t, key, &c)) c = fallback;
tset(t, key, c);
return c;
}
void theme_normalize(config *t, strbuf *rep) {
rgb bg, text, link, link_raw, heading, border, codebg, light, dark;
rgb tbg, tfg, visited;
if (!tget(t, "bg", &bg)) bg = WHITE;
tset(t, "bg", bg);
text = fg_token(t, "text", best_on(bg), bg, MIN_CONTRAST, rep);
if (!tget(t, "link", &link_raw)) link_raw = text;
link = fg_token(t, "link", text, bg, MIN_CONTRAST, rep);
heading = fg_token(t, "heading", text, bg, MIN_CONTRAST_LARGE, rep);
fg_token(t, "muted", mix(text, bg, 0.35), bg, MIN_CONTRAST, rep);
visited = fg_token(t, "link.visited", mix(link, text, 0.5), bg, MIN_CONTRAST, rep);
(void)visited;
border = plain_token(t, "border", mix(bg, text, 0.30));
/* code background: the text colour must stay readable on it */
codebg = plain_token(t, "codebg", mix(bg, text, 0.07));
{
rgb c = codebg;
for (int i = 0; i < 30 && contrast(text, c) < MIN_CONTRAST; i++)
c = mix(c, bg, 0.1);
if (contrast(text, c) < MIN_CONTRAST) c = bg; /* text is readable on bg */
if (!same(c, codebg)) {
note_fix(rep, "codebg", codebg, c, text);
codebg = c;
tset(t, "codebg", codebg);
}
}
/* bevel edges */
light = plain_token(t, "light", mix(bg, WHITE, luminance(bg) < 0.05 ? 0.30 : 0.55));
dark = plain_token(t, "dark", mix(bg, BLACK, 0.50));
/* title bar (used by frame = bevel) and selection */
tbg = plain_token(t, "title.bg", link_raw);
tfg = fg_token(t, "title.fg", best_on(tbg), tbg, MIN_CONTRAST, rep);
{
rgb sbg = plain_token(t, "select.bg", tbg);
fg_token(t, "select.fg", best_on(sbg), sbg, MIN_CONTRAST, rep);
}
/* structure + computed tokens for the CSS template */
{
const char *fr = config_get(t, "frame");
int bevel = fr && strcmp(fr, "bevel") == 0;
const char *fw = config_get(t, "frame.width");
char width[32];
snprintf(width, sizeof width, "%s", fw && fw[0] ? fw : (bevel ? "2px" : "1px"));
config_set(t, "frame", bevel ? "bevel" : "flat");
config_set(t, "frame.width", width);
char sl[8], sd[8], stx[8], sh[8], sk[8], sb[8], stbg[8], stfg[8];
char edges[40];
fmt_color(sl, light); fmt_color(sd, dark); fmt_color(stx, text);
fmt_color(sh, heading); fmt_color(sk, link); fmt_color(sb, border);
fmt_color(stbg, tbg); fmt_color(stfg, tfg);
if (bevel) {
config_set(t, "hdr.bg", stbg);
config_set(t, "hdr.text", stfg);
config_set(t, "hdr.h1", stfg);
config_set(t, "hdr.link", stfg);
config_set(t, "hdr.bw", width);
snprintf(edges, sizeof edges, "%s %s %s %s", sl, sd, sd, sl);
config_set(t, "hdr.bc", edges);
config_set(t, "hdr.pad", "0.2em 1em");
config_set(t, "pre.bw", width);
snprintf(edges, sizeof edges, "%s %s %s %s", sd, sl, sl, sd);
config_set(t, "pre.bc", edges);
} else {
config_set(t, "hdr.bg", "transparent");
config_set(t, "hdr.text", stx);
config_set(t, "hdr.h1", sh);
config_set(t, "hdr.link", sk);
config_set(t, "hdr.bw", "0 0 1px 0");
config_set(t, "hdr.bc", sb);
config_set(t, "hdr.pad", "0");
config_set(t, "pre.bw", "0");
config_set(t, "pre.bc", sb);
}
}
}
/* Substitute {{key}} in `template_css` with values from cfg. Unknown keys
* expand to nothing. */
void theme_render(strbuf *out, const char *template_css, const config *cfg) {
if (!template_css) return;
const char *p = template_css;
while (*p) {
if (p[0] == '{' && p[1] == '{') {
const char *end = strstr(p + 2, "}}");
if (end) {
size_t klen = (size_t)(end - (p + 2));
char *key = malloc(klen + 1);
if (key) {
memcpy(key, p + 2, klen);
key[klen] = '\0';
const char *val = config_get(cfg, key);
if (val) sb_append(out, val);
free(key);
}
p = end + 2;
continue;
}
}
sb_append_char(out, *p);
p++;
}
}
int theme_check_file(const char *path, const char *name, FILE *out) {
config t; config_init(&t);
if (config_load(&t, path) != 0) {
fprintf(out, "%s: cannot read %s\n", name, path);
config_free(&t);
return 1;
}
strbuf rep; sb_init(&rep);
theme_normalize(&t, &rep);
int n = 0;
if (rep.data) for (const char *c = rep.data; *c; c++) if (*c == '\n') n++;
if (n == 0) fprintf(out, "%-22s ok\n", name);
else fprintf(out, "%-22s %d fix%s\n%s", name, n, n == 1 ? "" : "es", rep.data);
sb_free(&rep);
config_free(&t);
return n;
}
+103
View File
@@ -0,0 +1,103 @@
/* md_test.c — unit tests for the Markdown parser (markdown_to_html).
* Compile: cc -std=c99 -Wall -Wextra -pedantic -I../src md_test.c ../src/markdown.c ../src/strbuf.c
* Run: ./md_test (exit 0 = all pass, 1 = failures)
*/
#include "bb.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
static int failures = 0;
static int total = 0;
/* Render markdown string to HTML string (malloc'd). */
static char *render(const char *md) {
FILE *in = tmpfile();
FILE *out = tmpfile();
if (!in || !out) { fprintf(stderr, "tmpfile failed\n"); exit(2); }
fputs(md, in);
rewind(in);
markdown_to_html(out, in, "");
fflush(out);
rewind(out);
/* read all of out */
size_t cap = 256, len = 0;
char *buf = malloc(cap);
if (!buf) exit(2);
int c;
while ((c = fgetc(out)) != EOF) {
if (len + 1 >= cap) { cap *= 2; buf = realloc(buf, cap); }
buf[len++] = (char)c;
}
buf[len] = '\0';
fclose(in);
fclose(out);
return buf;
}
static void check(const char *name, const char *md, const char *want) {
total++;
char *got = render(md);
if (strcmp(got, want) != 0) {
failures++;
printf("FAIL %s\n md: %s\n want: %s\n got: %s\n", name, md, want, got);
} else {
printf("ok %s\n", name);
}
free(got);
}
int main(void) {
/* headings */
check("h1", "# Hello\n", "<h1 id=\"hello\">Hello</h1>\n");
check("h3", "### Sub\n", "<h3 id=\"sub\">Sub</h3>\n");
check("h6", "###### deep\n", "<h6 id=\"deep\">deep</h6>\n");
check("h7-not", "####### not heading\n", "<p>####### not heading</p>\n");
/* paragraphs */
check("para", "hello world\n", "<p>hello world</p>\n");
check("para-multi", "line one\nline two\n", "<p>line one\nline two</p>\n");
check("para-blank", "one\n\ntwo\n", "<p>one</p>\n<p>two</p>\n");
/* emphasis / strong */
check("em", "*hi*\n", "<p><em>hi</em></p>\n");
check("strong", "**hi**\n", "<p><strong>hi</strong></p>\n");
check("em-strong", "**bold** and *it*\n", "<p><strong>bold</strong> and <em>it</em></p>\n");
/* inline code */
check("code", "`x`\n", "<p><code>x</code></p>\n");
check("code-esc", "`a<b&c`\n", "<p><code>a&lt;b&amp;c</code></p>\n");
/* links */
check("link", "[x](https://e.com)\n", "<p><a href=\"https://e.com\">x</a></p>\n");
check("link-esc", "[x](https://e.com?a=1&b=2)\n", "<p><a href=\"https://e.com?a=1&amp;b=2\">x</a></p>\n");
/* escaping */
check("escape-amp", "a & b\n", "<p>a &amp; b</p>\n");
check("escape-lt", "a < b\n", "<p>a &lt; b</p>\n");
check("escape-quote", "say \"hi\"\n", "<p>say &quot;hi&quot;</p>\n");
/* lists */
check("ul", "- one\n- two\n", "<ul>\n<li>one</li>\n<li>two</li>\n</ul>\n");
check("ol", "1. one\n2. two\n", "<ol>\n<li>one</li>\n<li>two</li>\n</ol>\n");
/* fenced code */
check("fence", "```\ncode\n```\n", "<pre><code>code\n</code></pre>\n");
check("fence-esc", "```\n<a>&\n```\n", "<pre><code>&lt;a&gt;&amp;\n</code></pre>\n");
/* blockquote */
check("blockquote", "> quote\n", "<blockquote>\n<p>quote</p>\n</blockquote>\n");
/* hr */
check("hr", "---\n", "<hr>\n");
/* table */
check("table", "| a | b |\n|---|---|\n| 1 | 2 |\n",
"<table>\n<thead>\n<tr>\n<th>a</th>\n<th>b</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>1</td>\n<td>2</td>\n</tr>\n</tbody>\n</table>\n");
/* frontmatter stripped */
check("frontmatter", "---\ntitle: X\n---\nbody\n", "<p>body</p>\n");
printf("\n%d/%d passed\n", total - failures, total);
return failures ? 1 : 0;
}
+24
View File
@@ -0,0 +1,24 @@
#!/bin/sh
# Smoke test for `bb serve`: status codes, HEAD, traversal, method check.
BB=${BB:-./bb}
PORT=${PORT:-18765}
T=$(mktemp -d) || exit 1
mkdir "$T/sub"; echo hi > "$T/index.html"; echo x > "$T/sub/index.html"
echo secret > "$T/../bb-serve-outside.txt"
$BB serve --port "$PORT" --dir "$T" 2>/dev/null & PID=$!
trap 'kill $PID 2>/dev/null; rm -rf "$T" "$T/../bb-serve-outside.txt"' EXIT
sleep 1
fail=0 n=0
chk() { n=$((n+1)); if [ "$2" = "$3" ]; then :; else echo "FAIL $1: got $2 want $3"; fail=$((fail+1)); fi; }
code() { curl -s -o /dev/null -w '%{http_code}' "$@"; }
U=http://127.0.0.1:$PORT
chk root "$(code $U/)" 200
chk subdir "$(code $U/sub/)" 200
chk redirect "$(code $U/sub)" 301
chk missing "$(code $U/nope)" 404
chk head "$(code -I $U/)" 200
chk dotdot "$(code --path-as-is $U/../bb-serve-outside.txt)" 400
chk encdotdot "$(code --path-as-is $U/%2e%2e/bb-serve-outside.txt)" 400
chk post "$(code -X POST $U/)" 405
echo "$((n-fail))/$n passed"
[ $fail -eq 0 ]
+150
View File
@@ -0,0 +1,150 @@
/* theme_test.c — contrast maths, token derivation and the shipped themes.
* Compile: cc -std=c99 -Wall -Wextra -pedantic -Isrc tests/theme_test.c \
* src/theme.c src/config.c src/strbuf.c -o tests/theme_test
* Run from the repo root: ./tests/theme_test */
#include "bb.h"
#include <dirent.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static int failures = 0, total = 0;
#define CHECK(cond, ...) do { total++; if (!(cond)) { failures++; \
printf("FAIL: "); printf(__VA_ARGS__); printf("\n"); } } while (0)
static double ratio(const config *t, const char *fg, const char *bg) {
return theme_contrast(config_get(t, fg), config_get(t, bg));
}
static void test_contrast_maths(void) {
CHECK(fabs(theme_contrast("#000000", "#ffffff") - 21.0) < 0.01, "black/white is 21:1");
CHECK(fabs(theme_contrast("#ffffff", "#000000") - 21.0) < 0.01, "contrast is symmetric");
CHECK(fabs(theme_contrast("#777", "#fff") - 4.48) < 0.02, "#777 on white is about 4.48 (got %.3f)",
theme_contrast("#777", "#fff"));
CHECK(fabs(theme_contrast("#808080", "#808080") - 1.0) < 1e-9, "same colour is 1:1");
CHECK(theme_contrast("red", "#fff") < 0, "named colours are rejected");
CHECK(theme_contrast("#12345", "#fff") < 0, "5-digit hex is rejected");
CHECK(theme_contrast("#gggggg", "#fff") < 0, "non-hex digits are rejected");
}
static void test_derivation(void) {
config t; config_init(&t);
config_add(&t, "bg", "#202020");
theme_normalize(&t, NULL);
const char *keys[] = { "bg", "text", "link", "link.visited", "heading", "muted",
"border", "codebg", "light", "dark", "title.bg", "title.fg", "select.bg",
"select.fg", "frame", "frame.width", "hdr.bg", "hdr.bw", "hdr.bc", "hdr.pad",
"pre.bw", "pre.bc", NULL };
for (int i = 0; keys[i]; i++)
CHECK(config_get(&t, keys[i]) != NULL, "bg-only theme gets token %s", keys[i]);
CHECK(ratio(&t, "text", "bg") >= 4.5, "derived text readable on dark bg");
CHECK(ratio(&t, "muted", "bg") >= 4.5, "derived muted readable");
config_free(&t);
config_init(&t);
theme_normalize(&t, NULL);
CHECK(strcmp(config_get(&t, "bg"), "#ffffff") == 0, "empty theme falls back to white bg");
CHECK(ratio(&t, "text", "bg") >= 4.5, "empty theme still readable");
config_free(&t);
}
static void test_fixes_unreadable(void) {
config t; config_init(&t);
config_add(&t, "bg", "#8e8e77");
config_add(&t, "text", "#000000");
config_add(&t, "link", "#7f6c6c"); /* 1.47:1 as imported once */
config_add(&t, "heading", "#82829b"); /* 1.12:1 */
strbuf rep; sb_init(&rep);
theme_normalize(&t, &rep);
CHECK(ratio(&t, "link", "bg") >= 4.5, "link fixed (%.2f)", ratio(&t, "link", "bg"));
CHECK(ratio(&t, "heading", "bg") >= 3.0, "heading fixed (%.2f)", ratio(&t, "heading", "bg"));
CHECK(rep.data && strstr(rep.data, "link") && strstr(rep.data, "heading"),
"report names the fixed tokens");
sb_free(&rep);
config_free(&t);
}
static void test_good_theme_untouched(void) {
config t; config_init(&t);
config_add(&t, "bg", "#ffffff");
config_add(&t, "text", "#111111");
config_add(&t, "link", "#0033aa");
config_add(&t, "heading", "#222222");
strbuf rep; sb_init(&rep);
theme_normalize(&t, &rep);
CHECK(strcmp(config_get(&t, "link"), "#0033aa") == 0, "readable link is left alone");
CHECK(strcmp(config_get(&t, "text"), "#111111") == 0, "readable text is left alone");
CHECK(rep.len == 0, "no report for a readable theme");
sb_free(&rep);
config_free(&t);
}
static void test_idempotent(void) {
config t; config_init(&t);
config_add(&t, "bg", "#c0c0c0"); config_add(&t, "link", "#c8c8d0");
config_add(&t, "frame", "bevel");
theme_normalize(&t, NULL);
char *first = strdup(config_get(&t, "link"));
strbuf rep; sb_init(&rep);
theme_normalize(&t, &rep);
CHECK(strcmp(first, config_get(&t, "link")) == 0, "second normalize changes nothing");
CHECK(rep.len == 0, "second normalize reports nothing");
CHECK(strcmp(config_get(&t, "frame"), "bevel") == 0, "frame stays bevel");
CHECK(strcmp(config_get(&t, "frame.width"), "2px") == 0, "bevel default width");
free(first); sb_free(&rep); config_free(&t);
}
static void test_render(void) {
config t; config_init(&t);
config_add(&t, "a", "1"); config_add(&t, "b.c", "two");
strbuf out; sb_init(&out);
theme_render(&out, "x{{a}}y{{b.c}}z{{missing}}!{{broken", &t);
CHECK(out.data && strcmp(out.data, "x1ytwoz!{{broken") == 0, "render: got '%s'", out.data ? out.data : "");
sb_free(&out); config_free(&t);
}
/* Every theme we ship must come out readable, distinguishable and complete. */
static void test_shipped_themes(void) {
DIR *d = opendir("share/themes");
if (!d) { printf("FAIL: cannot open share/themes (run from repo root)\n"); failures++; total++; return; }
struct dirent *e;
int n = 0;
while ((e = readdir(d))) {
size_t l = strlen(e->d_name);
if (l < 6 || strcmp(e->d_name + l - 5, ".conf") != 0) continue;
char path[512];
snprintf(path, sizeof path, "share/themes/%s", e->d_name);
config t; config_init(&t);
CHECK(config_load(&t, path) == 0, "%s loads", e->d_name);
CHECK(config_get(&t, "bg") != NULL, "%s defines bg", e->d_name);
theme_normalize(&t, NULL);
CHECK(ratio(&t, "text", "bg") >= 4.5, "%s text %.2f", e->d_name, ratio(&t, "text", "bg"));
CHECK(ratio(&t, "link", "bg") >= 4.5, "%s link %.2f", e->d_name, ratio(&t, "link", "bg"));
CHECK(ratio(&t, "link.visited", "bg") >= 4.5, "%s visited %.2f", e->d_name, ratio(&t, "link.visited", "bg"));
CHECK(ratio(&t, "heading", "bg") >= 3.0, "%s heading %.2f", e->d_name, ratio(&t, "heading", "bg"));
CHECK(ratio(&t, "muted", "bg") >= 4.5, "%s muted %.2f", e->d_name, ratio(&t, "muted", "bg"));
CHECK(ratio(&t, "text", "codebg") >= 4.5, "%s text on codebg %.2f", e->d_name, ratio(&t, "text", "codebg"));
CHECK(ratio(&t, "title.fg", "title.bg") >= 4.5, "%s title %.2f", e->d_name, ratio(&t, "title.fg", "title.bg"));
CHECK(ratio(&t, "select.fg", "select.bg") >= 4.5, "%s selection %.2f", e->d_name, ratio(&t, "select.fg", "select.bg"));
CHECK(ratio(&t, "text", "link") >= 1.25, "%s text/link distinct %.2f", e->d_name, ratio(&t, "text", "link"));
CHECK(ratio(&t, "bg", "codebg") >= 1.05, "%s bg/codebg distinct %.2f", e->d_name, ratio(&t, "bg", "codebg"));
config_free(&t);
n++;
}
closedir(d);
CHECK(n >= 70, "found %d shipped themes", n);
}
int main(void) {
test_contrast_maths();
test_derivation();
test_fixes_unreadable();
test_good_theme_untouched();
test_idempotent();
test_render();
test_shipped_themes();
printf("%d/%d passed\n", total - failures, total);
return failures ? 1 : 0;
}
+105
View File
@@ -0,0 +1,105 @@
/* tmpl_test.c — tests for the sh templating engine (template_run).
* Compile: cc -std=c99 -Wall -Wextra -pedantic -I../src tmpl_test.c ../src/template.c ../src/strbuf.c
* Run: ./tmpl_test (exit 0 = all pass, 1 = failures)
*/
#include "bb.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
static int failures = 0;
static int total = 0;
/* Run a layout script with given envp, capture stdout into malloc'd string. */
static char *run(const char *script, char *const envp[]) {
/* write script to a temp file */
FILE *sf = tmpfile();
if (!sf) { fprintf(stderr, "tmpfile failed\n"); exit(2); }
fputs(script, sf);
fflush(sf);
rewind(sf);
/* copy to a real path (tmpfile has no name; use /tmp) */
char path[64];
snprintf(path, sizeof path, "/tmp/bb-tmpl-%d.sh", (int)getpid());
FILE *pf = fopen(path, "w");
if (!pf) exit(2);
rewind(sf);
int c;
while ((c = fgetc(sf)) != EOF) fputc(c, pf);
fclose(pf);
fclose(sf);
FILE *out = tmpfile();
if (!out) exit(2);
int rc = template_run(path, envp, out);
fflush(out);
rewind(out);
size_t cap = 256, len = 0;
char *buf = malloc(cap);
if (!buf) exit(2);
while ((c = fgetc(out)) != EOF) {
if (len + 1 >= cap) { cap *= 2; buf = realloc(buf, cap); }
buf[len++] = (char)c;
}
buf[len] = '\0';
fclose(out);
unlink(path);
if (rc != 0) { free(buf); return strdup("__RC_ERR__"); }
return buf;
}
static void check(const char *name, const char *script, char *const envp[], const char *want) {
total++;
char *got = run(script, envp);
if (strcmp(got, want) != 0) {
failures++;
printf("FAIL %s\n want: %s\n got: %s\n", name, want, got);
} else {
printf("ok %s\n", name);
}
free(got);
}
int main(void) {
char *e1[] = { "SITE_TITLE=My Site", NULL };
check("env-var", "echo \"title=$SITE_TITLE\"", e1, "title=My Site\n");
char *e2[] = { "FM_TITLE=Hello", NULL };
check("fm-var", "echo \"fm=$FM_TITLE\"", e2, "fm=Hello\n");
/* FM_* must not leak between runs (no setenv) */
char *e3[] = { "FM_A=one", NULL };
char *e4[] = { "FM_B=two", NULL };
check("no-leak", "echo \"a=$FM_A\"", e3, "a=one\n");
check("no-leak2", "echo \"b=$FM_B\"", e4, "b=two\n");
/* bb.sh sourced transparently: bb_content helper exists */
FILE *cf = tmpfile();
fputs("<p>hi</p>\n", cf);
fflush(cf);
rewind(cf);
char cpath[64];
snprintf(cpath, sizeof cpath, "/tmp/bb-content-%d.html", (int)getpid());
FILE *cpf = fopen(cpath, "w");
int cc;
while ((cc = fgetc(cf)) != EOF) fputc(cc, cpf);
fclose(cpf);
fclose(cf);
char *e5[] = { "CONTENT_FILE=/tmp/bb-content-0.html", "BB_LIB=share/bb/lib/bb.sh", NULL };
e5[0] = malloc(96);
snprintf(e5[0], 96, "CONTENT_FILE=%s", cpath);
check("bb_content", "bb_content", e5, "<p>hi</p>\n");
free(e5[0]);
unlink(cpath);
/* shell features: loop */
char *e6[] = { NULL };
check("loop", "for x in a b; do echo \"x=$x\"; done", e6, "x=a\nx=b\n");
/* shell features: conditional */
check("conditional", "if [ -n \"$NAV\" ]; then echo yes; else echo no; fi", e6, "no\n");
printf("\n%d/%d passed\n", total - failures, total);
return failures ? 1 : 0;
}