Lindenii Project Forge
Login

go-htmpl

Simple HTML templating engine
Commit info
ID
a6a1c6a3ea5c6c3cb3939fc3ea027ffdf1bba17f
Author
Runxi Yu <me@runxiyu.org>
Author date
Wed, 19 Mar 2025 12:45:35 +0800
Committer
Runxi Yu <me@runxiyu.org>
Committer date
Wed, 19 Mar 2025 12:45:35 +0800
Actions
Remove the runtime module as it's unnecessary for Go
The htmpl module provides runtime functions for the HTML template engine.

Functions herein are expected to be used by code generated by the same version
of htmplgen.

Many functions take an [[io::handle]], which represents the destination where
templates shall be rendered to.
use io;
use memio;
use strings;

// Writes a string to the destination.
export fn write(h: io::handle, s: str) (size | io::error) = {
	return io::write(h, strings::toutf8(s))?;
};

// Writes a string to the destination after HTML-escaping it.
export fn write_escape_html(h: io::handle, s: str) (size | io::error | nomem) = {
	let buf = memio::dynamic();
	defer io::close(&buf)!; // TODO: Might not want to abort for syscall interruptions

	let si = strings::iter(s);
	for (true) {
		match (strings::next(&si)) {
		case done =>
			break;
		case let c: rune =>
			switch (c) {
				case '<' =>
					memio::concat(&buf, "&lt;")?;
				case '>' =>
					memio::concat(&buf, "&gt;")?;
				case '&' =>
					memio::concat(&buf, "&amp;")?;
				case '"' =>
					memio::concat(&buf, "&quot;")?;
				case '\'' =>
					memio::concat(&buf, "&apos;")?;
				case =>
					memio::appendrune(&buf, c)?;
			};
		};
	};

	return io::write(h, memio::buffer(&buf))?;
};