| 1 |
// SPDX-License-Identifier: GPL-3.0-only |
| 2 |
// (c) Hare authors <https://harelang.org> |
| 3 |
|
| 4 |
// Note: ast::ident should never have to be escaped |
| 5 |
use encoding::utf8; |
| 6 |
use fmt; |
| 7 |
use hare::ast; |
| 8 |
use hare::lex; |
| 9 |
use hare::parse::doc; |
| 10 |
use hare::unparse; |
| 11 |
use io; |
| 12 |
use memio; |
| 13 |
use net::ip; |
| 14 |
use net::uri; |
| 15 |
use os; |
| 16 |
use path; |
| 17 |
use strings; |
| 18 |
|
| 19 |
// Prints a string to an output handle, escaping any of HTML's reserved |
| 20 |
// characters. |
| 21 |
fn html_escape(out: io::handle, in: str) (size | io::error) = { |
| 22 |
let z = 0z; |
| 23 |
let iter = strings::iter(in); |
| 24 |
for (let rn => strings::next(&iter)) { |
| 25 |
z += fmt::fprint(out, switch (rn) { |
| 26 |
case '&' => |
| 27 |
yield "&"; |
| 28 |
case '<' => |
| 29 |
yield "<"; |
| 30 |
case '>' => |
| 31 |
yield ">"; |
| 32 |
case '"' => |
| 33 |
yield """; |
| 34 |
case '\'' => |
| 35 |
yield "'"; |
| 36 |
case => |
| 37 |
yield strings::fromutf8(utf8::encoderune(rn))!; |
| 38 |
})?; |
| 39 |
}; |
| 40 |
return z; |
| 41 |
}; |
| 42 |
|
| 43 |
@test fn html_escape() void = { |
| 44 |
let sink = memio::dynamic(); |
| 45 |
defer io::close(&sink)!; |
| 46 |
html_escape(&sink, "hello world!")!; |
| 47 |
assert(memio::string(&sink)! == "hello world!"); |
| 48 |
|
| 49 |
let sink = memio::dynamic(); |
| 50 |
defer io::close(&sink)!; |
| 51 |
html_escape(&sink, "\"hello world!\"")!; |
| 52 |
assert(memio::string(&sink)! == ""hello world!""); |
| 53 |
|
| 54 |
let sink = memio::dynamic(); |
| 55 |
defer io::close(&sink)!; |
| 56 |
html_escape(&sink, "<hello & 'world'!>")!; |
| 57 |
assert(memio::string(&sink)! == "<hello & 'world'!>"); |
| 58 |
}; |
| 59 |
|
| 60 |
// Formats output as HTML |
| 61 |
export fn emit_html(ctx: *context) (void | error) = { |
| 62 |
const decls = ctx.summary; |
| 63 |
const ident = unparse::identstr(ctx.ident); |
| 64 |
defer free(ident); |
| 65 |
|
| 66 |
if (ctx.template) { |
| 67 |
head(ctx.ident)?; |
| 68 |
}; |
| 69 |
|
| 70 |
if (len(ident) == 0) { |
| 71 |
fmt::fprintf(ctx.out, "<h2>The Hare standard library <span class='heading-extra'>")?; |
| 72 |
} else { |
| 73 |
fmt::fprintf(ctx.out, "<h2><span class='heading-body'>{}</span><span class='heading-extra'>", ident)?; |
| 74 |
}; |
| 75 |
for (let tag .. ctx.tags) { |
| 76 |
fmt::fprintf(ctx.out, "+{} ", tag)?; |
| 77 |
}; |
| 78 |
fmt::fprintln(ctx.out, "</span></h2>")?; |
| 79 |
|
| 80 |
match (ctx.readme) { |
| 81 |
case void => void; |
| 82 |
case let f: io::file => |
| 83 |
fmt::fprintln(ctx.out, "<div class='readme'>")?; |
| 84 |
markup_html(ctx, f, lex::location { |
| 85 |
path = "README", // XXX: this is meh |
| 86 |
line = 1, |
| 87 |
col = 1, |
| 88 |
off = 0, |
| 89 |
})?; |
| 90 |
fmt::fprintln(ctx.out, "</div>")?; |
| 91 |
}; |
| 92 |
|
| 93 |
let identpath = strings::join("/", ctx.ident...)!; |
| 94 |
defer free(identpath); |
| 95 |
|
| 96 |
if (len(ctx.submods) != 0) { |
| 97 |
if (len(ctx.ident) == 0) { |
| 98 |
fmt::fprintln(ctx.out, "<h3>Modules</h3>")?; |
| 99 |
} else { |
| 100 |
fmt::fprintln(ctx.out, "<h3>Submodules</h3>")?; |
| 101 |
}; |
| 102 |
fmt::fprintln(ctx.out, "<ul class='submodules'>")?; |
| 103 |
for (let submodule .. ctx.submods) { |
| 104 |
fmt::fprintf(ctx.out, "<li><a href='")?; |
| 105 |
html_escape(ctx.out, submodule.name)?; |
| 106 |
fmt::fprintf(ctx.out, "'>")?; |
| 107 |
html_escape(ctx.out, submodule.name)?; |
| 108 |
if (submodule.desc != "") { |
| 109 |
fmt::fprintfln(ctx.out, "</a>: ")?; |
| 110 |
html_escape(ctx.out, submodule.desc)?; |
| 111 |
fmt::fprintfln(ctx.out, "</li>")?; |
| 112 |
} else { |
| 113 |
fmt::fprintfln(ctx.out, "</a></li>")?; |
| 114 |
}; |
| 115 |
}; |
| 116 |
fmt::fprintln(ctx.out, "</ul>")?; |
| 117 |
}; |
| 118 |
|
| 119 |
if (len(decls.types) == 0 |
| 120 |
&& len(decls.errors) == 0 |
| 121 |
&& len(decls.constants) == 0 |
| 122 |
&& len(decls.globals) == 0 |
| 123 |
&& len(decls.funcs) == 0) { |
| 124 |
return; |
| 125 |
}; |
| 126 |
|
| 127 |
fmt::fprintln(ctx.out, "<h3>Index</h3>")?; |
| 128 |
tocentries(ctx.out, decls.types, "Types", "types")?; |
| 129 |
tocentries(ctx.out, decls.errors, "Errors", "Errors")?; |
| 130 |
tocentries(ctx.out, decls.constants, "Constants", "constants")?; |
| 131 |
tocentries(ctx.out, decls.globals, "Globals", "globals")?; |
| 132 |
tocentries(ctx.out, decls.funcs, "Functions", "functions")?; |
| 133 |
|
| 134 |
if (len(decls.types) != 0) { |
| 135 |
fmt::fprintln(ctx.out, "<h3>Types</h3>")?; |
| 136 |
for (let t &.. decls.types) { |
| 137 |
details(ctx, t)?; |
| 138 |
}; |
| 139 |
}; |
| 140 |
|
| 141 |
if (len(decls.errors) != 0) { |
| 142 |
fmt::fprintln(ctx.out, "<h3>Errors</h3>")?; |
| 143 |
for (let e &.. decls.errors) { |
| 144 |
details(ctx, e)?; |
| 145 |
}; |
| 146 |
}; |
| 147 |
|
| 148 |
if (len(decls.constants) != 0) { |
| 149 |
fmt::fprintln(ctx.out, "<h3>Constants</h3>")?; |
| 150 |
for (let c &.. decls.constants) { |
| 151 |
details(ctx, c)?; |
| 152 |
}; |
| 153 |
}; |
| 154 |
|
| 155 |
if (len(decls.globals) != 0) { |
| 156 |
fmt::fprintln(ctx.out, "<h3>Globals</h3>")?; |
| 157 |
for (let g &.. decls.globals) { |
| 158 |
details(ctx, g)?; |
| 159 |
}; |
| 160 |
}; |
| 161 |
|
| 162 |
if (len(decls.funcs) != 0) { |
| 163 |
fmt::fprintln(ctx.out, "<h3>Functions</h3>")?; |
| 164 |
for (let f &.. decls.funcs) { |
| 165 |
details(ctx, f)?; |
| 166 |
}; |
| 167 |
}; |
| 168 |
}; |
| 169 |
|
| 170 |
fn tocentries( |
| 171 |
out: io::handle, |
| 172 |
decls: []ast::decl, |
| 173 |
name: str, |
| 174 |
lname: str, |
| 175 |
) (void | error) = { |
| 176 |
if (len(decls) == 0) { |
| 177 |
return; |
| 178 |
}; |
| 179 |
fmt::fprintfln(out, "<h4>{}</h4>", name)?; |
| 180 |
fmt::fprintln(out, "<pre>")?; |
| 181 |
let undoc = false; |
| 182 |
for (let i = 0z; i < len(decls); i += 1) { |
| 183 |
if (!undoc && decls[i].docs == "") { |
| 184 |
fmt::fprintfln( |
| 185 |
out, |
| 186 |
"{}<span class='comment'>// Undocumented {}:</span>", |
| 187 |
if (i == 0) "" else "\n", |
| 188 |
lname)?; |
| 189 |
undoc = true; |
| 190 |
}; |
| 191 |
|
| 192 |
match (&decls[i].decl) { |
| 193 |
case let d: *ast::decl_func => |
| 194 |
free(d.symbol); |
| 195 |
d.symbol = ""; |
| 196 |
case => void; |
| 197 |
}; |
| 198 |
|
| 199 |
const decl = &decls[i]; |
| 200 |
match (decl.decl) { |
| 201 |
case let t: ast::decl_type => |
| 202 |
// Print summarized types in the TOC |
| 203 |
fmt::fprintf(out, "<span class='keyword'>type</span> ")?; |
| 204 |
fmt::fprintf(out, "<a href='#")?; |
| 205 |
unparse::ident(out, decl_ident(decl))?; |
| 206 |
fmt::fprintf(out, "'>")?; |
| 207 |
unparse::ident(out, decl_ident(decl))?; |
| 208 |
fmt::fprintf(out, "</a>;")?; |
| 209 |
case => |
| 210 |
unparse::decl(out, &syn_centry, &decls[i])?; |
| 211 |
}; |
| 212 |
|
| 213 |
fmt::fprintln(out)?; |
| 214 |
}; |
| 215 |
fmt::fprint(out, "</pre>")?; |
| 216 |
return; |
| 217 |
}; |
| 218 |
|
| 219 |
fn details(ctx: *context, decl: *ast::decl) (void | error) = { |
| 220 |
fmt::fprintln(ctx.out, "<section class='member'>")?; |
| 221 |
fmt::fprint(ctx.out, "<h4 id='")?; |
| 222 |
unparse::ident(ctx.out, decl_ident(decl))?; |
| 223 |
fmt::fprint(ctx.out, "'><span class='heading-body'>")?; |
| 224 |
fmt::fprintf(ctx.out, "{} ", match (decl.decl) { |
| 225 |
case ast::decl_func => |
| 226 |
yield "fn"; |
| 227 |
case ast::decl_type => |
| 228 |
yield "type"; |
| 229 |
case ast::decl_const => |
| 230 |
yield "def"; |
| 231 |
case ast::decl_global => |
| 232 |
yield "let"; |
| 233 |
case ast::assert_expr => abort(); |
| 234 |
})?; |
| 235 |
unparse::ident(ctx.out, decl_ident(decl))?; |
| 236 |
// TODO: Add source URL |
| 237 |
fmt::fprint(ctx.out, "</span><span class='heading-extra'><a href='#")?; |
| 238 |
unparse::ident(ctx.out, decl_ident(decl))?; |
| 239 |
fmt::fprint(ctx.out, "'>[permalink]</a>")?; |
| 240 |
match (os::getenv("HAREDOC_HTML_SRC")) { |
| 241 |
case let fmt: str => |
| 242 |
const url = fmt::asprintf(fmt, |
| 243 |
cwdpath(decl.loc.start.path), |
| 244 |
decl.loc.start.line)!; |
| 245 |
defer free(url); |
| 246 |
fmt::fprint(ctx.out, " <a href='")?; |
| 247 |
html_escape(ctx.out, url)?; |
| 248 |
fmt::fprint(ctx.out, "'>[source]</a>")?; |
| 249 |
case void => yield; |
| 250 |
}; |
| 251 |
|
| 252 |
fmt::fprint(ctx.out, "</span>")?; |
| 253 |
fmt::fprintln(ctx.out, "</h4>")?; |
| 254 |
|
| 255 |
if (len(decl.docs) == 0) { |
| 256 |
fmt::fprintln(ctx.out, "<details>")?; |
| 257 |
fmt::fprintln(ctx.out, "<summary>Show undocumented member</summary>")?; |
| 258 |
}; |
| 259 |
|
| 260 |
fmt::fprintln(ctx.out, "<pre class='decl'>")?; |
| 261 |
unparse::decl(ctx.out, &syn_html, decl)?; |
| 262 |
fmt::fprintln(ctx.out, "</pre>")?; |
| 263 |
|
| 264 |
if (len(decl.docs) != 0) { |
| 265 |
const trimmed = trim_comment(decl.docs); |
| 266 |
defer free(trimmed); |
| 267 |
const buf = strings::toutf8(trimmed); |
| 268 |
markup_html(ctx, &memio::fixed(buf), decl.loc.start)?; |
| 269 |
} else { |
| 270 |
fmt::fprintln(ctx.out, "</details>")?; |
| 271 |
}; |
| 272 |
|
| 273 |
fmt::fprintln(ctx.out, "</section>")?; |
| 274 |
return; |
| 275 |
}; |
| 276 |
|
| 277 |
// Returns the relative path from the current module to a target module path. |
| 278 |
// The caller is responsible for freeing the allocated string. |
| 279 |
fn relpath(from: ast::ident, to: []str) str = { |
| 280 |
let common = 0z; // Length of common prefix. |
| 281 |
for (common < len(from) && common < len(to); common += 1) { |
| 282 |
if (from[common] != to[common]) { |
| 283 |
break; |
| 284 |
}; |
| 285 |
}; |
| 286 |
|
| 287 |
let parts: []str = []; |
| 288 |
for (let i = common; i < len(from); i += 1) { |
| 289 |
append(parts, "..")!; |
| 290 |
}; |
| 291 |
for (let i = common; i < len(to); i += 1) { |
| 292 |
append(parts, to[i])!; |
| 293 |
}; |
| 294 |
|
| 295 |
if (len(parts) == 0) { |
| 296 |
return strings::dup(".")!; |
| 297 |
}; |
| 298 |
|
| 299 |
return strings::join("/", parts...)!; |
| 300 |
}; |
| 301 |
|
| 302 |
@test fn relpath_sibling() void = { |
| 303 |
const from1: []str = ["strings"]; |
| 304 |
const to1: []str = ["strconv"]; |
| 305 |
const result1 = relpath(from1, to1); |
| 306 |
defer free(result1); |
| 307 |
assert(result1 == "../strconv"); |
| 308 |
}; |
| 309 |
|
| 310 |
@test fn relpath_shared_prefix() void = { |
| 311 |
const from2: []str = ["fakeone"]; |
| 312 |
const to2: []str = ["faketwo"]; |
| 313 |
const result2 = relpath(from2, to2); |
| 314 |
defer free(result2); |
| 315 |
assert(result2 == "../faketwo"); |
| 316 |
}; |
| 317 |
|
| 318 |
@test fn relpath_nested_to_top() void = { |
| 319 |
const from3: []str = ["encoding", "base64"]; |
| 320 |
const to3: []str = ["strings"]; |
| 321 |
const result3 = relpath(from3, to3); |
| 322 |
defer free(result3); |
| 323 |
assert(result3 == "../../strings"); |
| 324 |
}; |
| 325 |
|
| 326 |
@test fn relpath_top_to_nested() void = { |
| 327 |
const from4: []str = ["strings"]; |
| 328 |
const to4: []str = ["encoding", "base64"]; |
| 329 |
const result4 = relpath(from4, to4); |
| 330 |
defer free(result4); |
| 331 |
assert(result4 == "../encoding/base64"); |
| 332 |
}; |
| 333 |
|
| 334 |
fn html_decl_ref(ctx: *context, ref: ast::ident) (void | error) = { |
| 335 |
const ik = |
| 336 |
match (resolve(ctx, ref)?) { |
| 337 |
case let ik: (ast::ident, symkind) => |
| 338 |
yield ik; |
| 339 |
case void => |
| 340 |
const ident = unparse::identstr(ref); |
| 341 |
fmt::errorfln("Warning: Unresolved reference: {}", ident)?; |
| 342 |
fmt::fprintf(ctx.out, "<a href='#' " |
| 343 |
"class='ref invalid' " |
| 344 |
"title='This reference could not be found'>{}</a>", |
| 345 |
ident)?; |
| 346 |
free(ident); |
| 347 |
return; |
| 348 |
}; |
| 349 |
|
| 350 |
// TODO: The reference is not necessarily in the stdlib |
| 351 |
const kind = ik.1, id = ik.0; |
| 352 |
const ident = unparse::identstr(id); |
| 353 |
switch (kind) { |
| 354 |
case symkind::LOCAL => |
| 355 |
fmt::fprintf(ctx.out, "<a href='#{0}' class='ref'>{0}</a>", ident)?; |
| 356 |
case symkind::MODULE => |
| 357 |
let rpath = relpath(ctx.ident, id); |
| 358 |
defer free(rpath); |
| 359 |
fmt::fprintf(ctx.out, "<a href='{}' class='ref'>{}::</a>", |
| 360 |
rpath, ident)?; |
| 361 |
case symkind::SYMBOL => |
| 362 |
let rpath = relpath(ctx.ident, id[..len(id) - 1]); |
| 363 |
defer free(rpath); |
| 364 |
fmt::fprintf(ctx.out, "<a href='{}#{}' class='ref'>{}</a>", |
| 365 |
rpath, id[len(id) - 1], ident)?; |
| 366 |
case symkind::ENUM_LOCAL => |
| 367 |
fmt::fprintf(ctx.out, "<a href='#{}' class='ref'>{}</a>", |
| 368 |
id[len(id) - 2], ident)?; |
| 369 |
case symkind::ENUM_REMOTE => |
| 370 |
let rpath = relpath(ctx.ident, id[..len(id) - 2]); |
| 371 |
defer free(rpath); |
| 372 |
fmt::fprintf(ctx.out, "<a href='{}#{}' class='ref'>{}</a>", |
| 373 |
rpath, id[len(id) - 2], ident)?; |
| 374 |
}; |
| 375 |
free(ident); |
| 376 |
}; |
| 377 |
|
| 378 |
fn html_mod_ref(ctx: *context, ref: ast::ident) (void | error) = { |
| 379 |
const ident = unparse::identstr(ref); |
| 380 |
defer free(ident); |
| 381 |
let rpath = relpath(ctx.ident, ref); |
| 382 |
defer free(rpath); |
| 383 |
fmt::fprintf(ctx.out, "<a href='{}' class='ref'>{}::</a>", |
| 384 |
rpath, ident)?; |
| 385 |
}; |
| 386 |
|
| 387 |
|
| 388 |
fn html_paragraph(ctx: *context, p: doc::paragraph) (void | error) = { |
| 389 |
for (let elem .. p) { |
| 390 |
match (elem) { |
| 391 |
case let s: str => |
| 392 |
match (uri::parse(s)) { |
| 393 |
case let uri: uri::uri => |
| 394 |
defer uri::finish(&uri); |
| 395 |
if (uri.host is ip::addr || len(uri.host as str) > 0) { |
| 396 |
fmt::fprint(ctx.out, "<a rel='nofollow noopener' href='")?; |
| 397 |
uri::fmt(ctx.out, &uri)?; |
| 398 |
fmt::fprint(ctx.out, "'>")?; |
| 399 |
html_escape(ctx.out, s)?; |
| 400 |
fmt::fprint(ctx.out, "</a>")?; |
| 401 |
} else { |
| 402 |
html_escape(ctx.out, s)?; |
| 403 |
}; |
| 404 |
case uri::invalid => |
| 405 |
html_escape(ctx.out, s)?; |
| 406 |
}; |
| 407 |
case let d: doc::decl_ref => |
| 408 |
html_decl_ref(ctx, d)?; |
| 409 |
case let m: doc::mod_ref => |
| 410 |
html_mod_ref(ctx, m)?; |
| 411 |
}; |
| 412 |
}; |
| 413 |
}; |
| 414 |
|
| 415 |
fn markup_html( |
| 416 |
ctx: *context, |
| 417 |
in: io::handle, |
| 418 |
loc: lex::location, |
| 419 |
) (void | error) = { |
| 420 |
const doc = match (doc::parse(in, loc)) { |
| 421 |
case let doc: doc::doc => |
| 422 |
yield doc; |
| 423 |
case let err: lex::syntax => |
| 424 |
const err = lex::strerror(err); |
| 425 |
fmt::errorln("Warning:", err)?; |
| 426 |
fmt::fprint(ctx.out, "<p class='ref invalid'>Can't parse docs: ")?; |
| 427 |
html_escape(ctx.out, err)?; |
| 428 |
fmt::fprintln(ctx.out)?; |
| 429 |
return; |
| 430 |
}; |
| 431 |
defer doc::freeall(doc); |
| 432 |
|
| 433 |
for (let elem .. doc) { |
| 434 |
match (elem) { |
| 435 |
case let p: doc::paragraph => |
| 436 |
fmt::fprint(ctx.out, "<p>")?; |
| 437 |
html_paragraph(ctx, p)?; |
| 438 |
fmt::fprintln(ctx.out)?; |
| 439 |
case let l: doc::list => |
| 440 |
fmt::fprintln(ctx.out, "<ul>")?; |
| 441 |
for (let entry .. l) { |
| 442 |
fmt::fprint(ctx.out, "<li>")?; |
| 443 |
html_paragraph(ctx, entry)?; |
| 444 |
fmt::fprintln(ctx.out)?; |
| 445 |
}; |
| 446 |
fmt::fprintln(ctx.out, "</ul>")?; |
| 447 |
case let c: doc::code_sample => |
| 448 |
fmt::fprint(ctx.out, "<pre class='sample'>")?; |
| 449 |
html_escape(ctx.out, c)?; |
| 450 |
fmt::fprintln(ctx.out, "</pre>")?; |
| 451 |
}; |
| 452 |
}; |
| 453 |
}; |
| 454 |
|
| 455 |
fn syn_centry( |
| 456 |
ctx: *unparse::context, |
| 457 |
s: str, |
| 458 |
kind: unparse::synkind, |
| 459 |
) (size | io::error) = { |
| 460 |
let z = 0z; |
| 461 |
switch (kind) { |
| 462 |
case unparse::synkind::CONSTANT, |
| 463 |
unparse::synkind::FUNCTION, |
| 464 |
unparse::synkind::GLOBAL, |
| 465 |
unparse::synkind::TYPEDEF => |
| 466 |
z += fmt::fprint(ctx.out, "<a href='#")?; |
| 467 |
z += html_escape(ctx.out, s)?; |
| 468 |
z += fmt::fprint(ctx.out, "'>")?; |
| 469 |
z += html_escape(ctx.out, s)?; |
| 470 |
z += fmt::fprint(ctx.out, "</a>")?; |
| 471 |
ctx.linelen += len(s); |
| 472 |
return z; |
| 473 |
case => |
| 474 |
return syn_html(ctx, s, kind); |
| 475 |
}; |
| 476 |
}; |
| 477 |
|
| 478 |
fn syn_html( |
| 479 |
ctx: *unparse::context, |
| 480 |
s: str, |
| 481 |
kind: unparse::synkind, |
| 482 |
) (size | io::error) = { |
| 483 |
let z = 0z; |
| 484 |
const span = switch (kind) { |
| 485 |
case unparse::synkind::COMMENT => |
| 486 |
const stack = ctx.stack as *unparse::stack; |
| 487 |
if (stack.cur is *ast::decl) { |
| 488 |
// doc comment is unparsed separately later |
| 489 |
return 0z; |
| 490 |
}; |
| 491 |
z += fmt::fprint(ctx.out, "<span class='comment'>")?; |
| 492 |
yield true; |
| 493 |
case unparse::synkind::KEYWORD => |
| 494 |
z += fmt::fprint(ctx.out, "<span class='keyword'>")?; |
| 495 |
yield true; |
| 496 |
case unparse::synkind::TYPE => |
| 497 |
z += fmt::fprint(ctx.out, "<span class='type'>")?; |
| 498 |
yield true; |
| 499 |
case => |
| 500 |
yield false; |
| 501 |
}; |
| 502 |
|
| 503 |
z += html_escape(ctx.out, s)?; |
| 504 |
ctx.linelen += len(s); |
| 505 |
|
| 506 |
if (span) { |
| 507 |
z += fmt::fprint(ctx.out, "</span>")?; |
| 508 |
}; |
| 509 |
return z; |
| 510 |
}; |
| 511 |
|
| 512 |
fn breadcrumb(ident: ast::ident) str = { |
| 513 |
if (len(ident) == 0) { |
| 514 |
return ""; |
| 515 |
}; |
| 516 |
let buf = memio::dynamic(); |
| 517 |
let root_parts: []str = []; |
| 518 |
for (let i = 0z; i < len(ident); i += 1) { |
| 519 |
append(root_parts, "..")!; |
| 520 |
}; |
| 521 |
let root_path = strings::join("/", root_parts...)!; |
| 522 |
defer free(root_path); |
| 523 |
fmt::fprintf(&buf, "<a href='{}'>stdlib</a> » ", root_path)!; |
| 524 |
|
| 525 |
for (let i = 0z; i < len(ident) - 1; i += 1) { |
| 526 |
let rpath = relpath(ident, ident[..i+1]); |
| 527 |
defer free(rpath); |
| 528 |
fmt::fprintf(&buf, "<a href='{}'>{}</a>::", rpath, ident[i])!; |
| 529 |
}; |
| 530 |
fmt::fprint(&buf, ident[len(ident) - 1])!; |
| 531 |
return memio::string(&buf)!; |
| 532 |
}; |
| 533 |
|
| 534 |
const harriet_b64 = "iVBORw0KGgoAAAANSUhEUgAAAQAAAAEAAQMAAABmvDolAAAABlBMVEUAAAD///+l2Z/dAAAK40lEQVRo3u3ZX2xb1R0H8O/NzWIXXGw0xILa1QE6Wk0gMspIESU3WSf2sD/wODFtpFC1Q1Ob0AJpacm5pYVUAxHENK2IUiONaQ/TBIjRFKXNvSHbijSDeaGja5vr/ovHlmIHQ66de+/57iF27Gv7um8TD/glUvzROb9z7jnnnp9/4GU++Ap8iYEeJ6EFA9k9SSlGgkFRFiizs8HgPKWQ33ZFIEgZjiYNSwsECTpxaViJQKDRSUnDSgUBKcjN0mAmEJAclAbtIOCRhiMNOkHAIVl0DRaDQJ6k5xr0gkCGpOuRbhDIkvzUWwi2IbBI8smF4TYEr5C0nzTIIGCQ5N1NgEbaPGaUZD2QgvKw0QxYzviJkSbAZXH8RPQVozSceuDROzw3ciYYFOkdPhE9YxhBwOGlwydGThtkqjHIk/98fOT06wtz3hBMnfh85HTWCAI2p6a+ME7zWCCQU3MfaUkRDBzL/mg0Sa8JcE4Mz/DY4rKui+HTY/cPz9AIBHJm6onhGVbWfS2Yn7F+uXfGYBD4wnGtGXVmLBjwsf5jTYHzpHdUvTDmBYGMw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XkBRNZXgJzk7B59+bYfjouZFOc/eVAHYuH1vi7yKmLusrHBS2c4/5/vmUA7enyb92ALsFvt9C6+YnXMf9iDcASoasHFughwce+A4DtjFz42gchN1UCSbjuU48MDXXTeenyFiWtaWxTf+WBe1Qn1gz8ORBXnjjvu+FAHdGWv/5XUgfg+uTEykX+8bTSnA1AmfaO4qgdxTF1QzOOb2kZzaQAIVQNTAlAOXlInRnY/txJpAFCrQI4EoPxll/ryN9cl0ToBILykugVXjQHKd3/zoLZ07brV6AEQifsv3jrQsnlV34qlHdcsQw+A1hpgAh33bOu7xnsVoRvuaQDSQF9ywOwUb6DtBgDlFbe4HtJAZP/GyevFm0BLKwD4Uhg9WgCWHvj++o7Nb4aBlXWAhQFgyXVt2LRV+RMQ2wfAly2avx8A2te0tGzdqBLAPsRUzR/kNHD1bcAHSdhHAACqUQ3+jVbgxptiiCTx26M9PQCW1CRBLvBgayewBPvWnTYbAJq4R9GBPdBv9kwsbovF7a+aiAA9APSbb+kB4E+rcypNlD+RJX2PhDFY04UEAHQCQCT8RC68WKAozaQOFwAGVCAGbBtoDWk1LZh7dQA/ARCLoBPoqgEXoOrlGJZMdgJd9T+qL4Lw5FqgvjyR6yx9H8O7nQtJTPX7oh2YXRynuXi8+LrIl/sIm8CVhXjtPOjKCwCANvQAWBatbcEk3ygBLJ5w/nv1qy2ofKxa4CLqjFS+v7Nxqait/L268/N4I7Cp9H1L4s7F3NgHZjoA4KbtaqXM41tyiAMApgejlV+Ka/KLtLq8e9806ZlqQLFJ04xsk4IXECIzx11EgytiBUCp/OofWFMbaQ4KVRW1WpCGIuaDg6waXLYBSFdin2v0uCcqOyhqNAkSomllMK01Lx2evUxt8enLFB8roeXizae6Os2qBwXEm9U302heANUvUyEd/n9Vac3mwFW+qlZ/WcH/ADT9vVqjZ2RdAAAAAElFTkSuQmCC"; |
| 535 |
|
| 536 |
fn head(ident: ast::ident) (void | error) = { |
| 537 |
const id = unparse::identstr(ident); |
| 538 |
defer free(id); |
| 539 |
|
| 540 |
let breadcrumb = breadcrumb(ident); |
| 541 |
defer free(breadcrumb); |
| 542 |
|
| 543 |
const title = |
| 544 |
if (len(id) == 0) |
| 545 |
fmt::asprintf("Hare documentation")! |
| 546 |
else |
| 547 |
fmt::asprintf("{} — Hare documentation", id)!; |
| 548 |
defer free(title); |
| 549 |
|
| 550 |
// TODO: Move bits to +embed? |
| 551 |
fmt::printfln("<!doctype html> |
| 552 |
<html lang='en'> |
| 553 |
<meta charset='utf-8' /> |
| 554 |
<meta name='viewport' content='width=device-width, initial-scale=1' /> |
| 555 |
<title>{}</title> |
| 556 |
<link rel='icon' type='image/png' href='data:image/png;base64,{}'>", title, harriet_b64)?; |
| 557 |
fmt::println("<style> |
| 558 |
body { |
| 559 |
font-family: sans-serif; |
| 560 |
line-height: 1.3; |
| 561 |
margin: 0 auto; |
| 562 |
padding: 0 1rem; |
| 563 |
} |
| 564 |
|
| 565 |
nav:not(#TableOfContents) { |
| 566 |
max-width: calc(800px + 128px + 128px); |
| 567 |
margin: 1rem auto 0; |
| 568 |
display: grid; |
| 569 |
grid-template-rows: auto auto 1fr; |
| 570 |
grid-template-columns: auto 1fr; |
| 571 |
grid-template-areas: |
| 572 |
'logo header' |
| 573 |
'logo nav' |
| 574 |
'logo none'; |
| 575 |
} |
| 576 |
|
| 577 |
nav:not(#TableOfContents) img { |
| 578 |
grid-area: logo; |
| 579 |
} |
| 580 |
|
| 581 |
nav:not(#TableOfContents) h1 { |
| 582 |
grid-area: header; |
| 583 |
margin: 0; |
| 584 |
padding: 0; |
| 585 |
} |
| 586 |
|
| 587 |
nav:not(#TableOfContents) ul { |
| 588 |
grid-area: nav; |
| 589 |
margin: 0.5rem 0 0 0; |
| 590 |
padding: 0; |
| 591 |
list-style: none; |
| 592 |
display: flex; |
| 593 |
flex-direction: row; |
| 594 |
justify-content: left; |
| 595 |
flex-wrap: wrap; |
| 596 |
} |
| 597 |
|
| 598 |
nav:not(#TableOfContents) li:not(:first-child) { |
| 599 |
margin-left: 2rem; |
| 600 |
} |
| 601 |
|
| 602 |
#TableOfContents { |
| 603 |
font-size: 1.1rem; |
| 604 |
} |
| 605 |
|
| 606 |
main { |
| 607 |
padding: 0 128px; |
| 608 |
max-width: 800px; |
| 609 |
margin: 0 auto; |
| 610 |
|
| 611 |
} |
| 612 |
|
| 613 |
pre { |
| 614 |
background-color: #eee; |
| 615 |
padding: 0.25rem 1rem; |
| 616 |
margin: 0 -1rem 1rem; |
| 617 |
font-size: 1.2rem; |
| 618 |
max-width: calc(100% + 1rem); |
| 619 |
overflow-x: auto; |
| 620 |
} |
| 621 |
|
| 622 |
pre .keyword { |
| 623 |
color: #008; |
| 624 |
} |
| 625 |
|
| 626 |
pre .type { |
| 627 |
color: #44F; |
| 628 |
} |
| 629 |
|
| 630 |
ol { |
| 631 |
padding-left: 0; |
| 632 |
list-style: none; |
| 633 |
} |
| 634 |
|
| 635 |
ol li { |
| 636 |
padding-left: 0; |
| 637 |
} |
| 638 |
|
| 639 |
h2, h3, h4 { |
| 640 |
display: flex; |
| 641 |
} |
| 642 |
|
| 643 |
h3 { |
| 644 |
border-bottom: 1px solid #ccc; |
| 645 |
padding-bottom: 0.25rem; |
| 646 |
} |
| 647 |
|
| 648 |
.invalid { |
| 649 |
color: red; |
| 650 |
} |
| 651 |
|
| 652 |
.heading-body { |
| 653 |
word-wrap: anywhere; |
| 654 |
} |
| 655 |
|
| 656 |
.heading-extra { |
| 657 |
align-self: flex-end; |
| 658 |
flex-grow: 1; |
| 659 |
padding-left: 0.5rem; |
| 660 |
text-align: right; |
| 661 |
font-size: 0.8rem; |
| 662 |
color: #444; |
| 663 |
} |
| 664 |
|
| 665 |
h4:target + pre { |
| 666 |
background: #ddf; |
| 667 |
} |
| 668 |
|
| 669 |
details { |
| 670 |
background: #eee; |
| 671 |
margin: 1rem -1rem 1rem; |
| 672 |
} |
| 673 |
|
| 674 |
summary { |
| 675 |
cursor: pointer; |
| 676 |
padding: 0.5rem 1rem; |
| 677 |
} |
| 678 |
|
| 679 |
details pre { |
| 680 |
margin: 0; |
| 681 |
} |
| 682 |
|
| 683 |
.comment { |
| 684 |
color: #000; |
| 685 |
font-weight: bold; |
| 686 |
} |
| 687 |
|
| 688 |
@media(max-width: 1000px) { |
| 689 |
main { |
| 690 |
padding: 0; |
| 691 |
} |
| 692 |
} |
| 693 |
|
| 694 |
@media(prefers-color-scheme: dark) { |
| 695 |
body { |
| 696 |
background: #121415; |
| 697 |
color: #e1dfdc; |
| 698 |
} |
| 699 |
|
| 700 |
img.mascot { |
| 701 |
filter: invert(.92); |
| 702 |
} |
| 703 |
|
| 704 |
a { |
| 705 |
color: #78bef8; |
| 706 |
} |
| 707 |
|
| 708 |
a:visited { |
| 709 |
color: #48a7f5; |
| 710 |
} |
| 711 |
|
| 712 |
summary { |
| 713 |
background: #16191c; |
| 714 |
} |
| 715 |
|
| 716 |
h3 { |
| 717 |
border-bottom: solid #16191c; |
| 718 |
} |
| 719 |
|
| 720 |
h4:target + pre { |
| 721 |
background: #162329; |
| 722 |
} |
| 723 |
|
| 724 |
pre { |
| 725 |
background-color: #16191c; |
| 726 |
} |
| 727 |
|
| 728 |
pre .keyword { |
| 729 |
color: #69f; |
| 730 |
} |
| 731 |
|
| 732 |
pre .type { |
| 733 |
color: #3cf; |
| 734 |
} |
| 735 |
|
| 736 |
.comment { |
| 737 |
color: #fff; |
| 738 |
} |
| 739 |
|
| 740 |
.heading-extra { |
| 741 |
color: #9b9997; |
| 742 |
} |
| 743 |
} |
| 744 |
</style>")?; |
| 745 |
fmt::printfln("<nav> |
| 746 |
<img src='data:image/png;base64,{}' |
| 747 |
class='mascot' |
| 748 |
alt='An inked drawing of the Hare mascot, a fuzzy rabbit' |
| 749 |
width='128' height='128' /> |
| 750 |
<h1>Hare documentation</h1> |
| 751 |
<ul> |
| 752 |
<li> |
| 753 |
<a href='https://harelang.org'>Home</a> |
| 754 |
</li>", harriet_b64)?; |
| 755 |
fmt::printf("<li>{}</li>", breadcrumb)?; |
| 756 |
fmt::print("</ul> |
| 757 |
</nav> |
| 758 |
<main>")?; |
| 759 |
return; |
| 760 |
}; |