hare

[hare] The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

commit 2741bda906333f9c999821b5ed881f0aa1948490
parent d2927d61156153709a4a9fb28b7b1d922b269491
Author: Umar Getagazov <umar@handlerug.me>
Date:   Thu, 26 May 2022 16:32:20 +0000

haredoc: fix lists parsed as text in decl docs

Trims the first space in every line of a comment for every declaration
(" - list item" versus "- list item") so that comments are parsed
correctly.

Signed-off-by: Umar Getagazov <umar@handlerug.me>

Diffstat:
Mcmd/haredoc/html.ha | 4+++-
Mcmd/haredoc/util.ha | 16++++++++++++++++
2 files changed, 19 insertions(+), 1 deletion(-)

diff --git a/cmd/haredoc/html.ha b/cmd/haredoc/html.ha @@ -318,7 +318,9 @@ fn details(ctx: *context, decl: ast::decl) (void | error) = { fmt::fprintln(ctx.out, "</pre>")?; if (len(decl.docs) != 0) { - const buf = strings::toutf8(decl.docs); + const trimmed = trim_comment(decl.docs); + defer free(trimmed); + const buf = strings::toutf8(trimmed); markup_html(ctx, &bufio::fixed(buf, io::mode::READ))?; } else { fmt::fprintln(ctx.out, "</details>")?; diff --git a/cmd/haredoc/util.ha b/cmd/haredoc/util.ha @@ -3,6 +3,7 @@ use fmt; use io; use strings; +use strio; // Forked from [[hare::unparse]]. fn newline(out: io::handle, indent: size) (size | io::error) = { @@ -16,3 +17,18 @@ fn newline(out: io::handle, indent: size) (size | io::error) = { fn multiline_comment(s: str) bool = strings::byteindex(s, '\n') as size != len(s) - 1; + +fn trim_comment(s: str) str = { + let trimmed = strio::dynamic(); + let tok = strings::tokenize(s, "\n"); + for (true) { + const line = match (strings::next_token(&tok)) { + case void => + break; + case let line: str => + yield line; + }; + strio::concat(&trimmed, strings::trimprefix(line, " "), "\n")!; + }; + return strings::dup(strio::string(&trimmed)); +};