hare

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

commit 7b9b1e8ed87e03f05d80d6498081a6fe5b7931b4
parent cae30bc92ee301851539ee6190b9c1bb530122c5
Author: Drew DeVault <sir@cmpwn.com>
Date:   Sun, 22 Aug 2021 12:25:45 +0200

format::html: remove module

Signed-off-by: Drew DeVault <sir@cmpwn.com>

Diffstat:
Mcmd/haredoc/html.ha | 39+++++++++++++++++++++++++++++----------
Dformat/html/README | 2--
Dformat/html/escape.ha | 40----------------------------------------
Mscripts/gen-stdlib | 7-------
Mstdlib.mk | 28----------------------------
5 files changed, 29 insertions(+), 87 deletions(-)

diff --git a/cmd/haredoc/html.ha b/cmd/haredoc/html.ha @@ -1,7 +1,7 @@ // Note: ast::ident should never have to be escaped use bufio; +use encoding::utf8; use fmt; -use format::html; use hare::ast; use hare::lex; use hare::module; @@ -12,6 +12,25 @@ use path; use strings; use strio; +// Prints a string to an output stream, escaping any of HTML's reserved +// characters. +fn html_escape(out: *io::stream, in: str) (size | io::error) = { + let z = 0z; + let iter = strings::iter(in); + for (true) match (strings::next(&iter)) { + void => break, + rn: rune => z += io::write(out, switch (rn) { + '&' => strings::toutf8("&amp;"), + '<' => strings::toutf8("&lt;"), + '>' => strings::toutf8("&gt;"), + '"' => strings::toutf8("&quot;"), + '\'' => strings::toutf8("&apos;"), + * => utf8::encoderune(rn), + })?, + }; + return z; +}; + // Formats output as HTML fn emit_html(ctx: *context) (void | error) = { const decls = ctx.summary; @@ -63,9 +82,9 @@ fn emit_html(ctx: *context) (void | error) = { defer free(path); fmt::printf("<li><a href='")?; - html::escape(os::stdout, path)?; + html_escape(os::stdout, path)?; fmt::printf("'>")?; - html::escape(os::stdout, dir)?; + html_escape(os::stdout, dir)?; fmt::printfln("</a></li>")?; }; fmt::println("</ul>")?; @@ -270,19 +289,19 @@ fn markup_html(ctx: *context, in: *io::stream) (void | io::error) = { tx: text => if (strings::has_prefix(tx, "https://")) { // Temporary hack fmt::print("<a rel='nofollow noopener' href='")?; - html::escape(os::stdout, tx)?; + html_escape(os::stdout, tx)?; fmt::print("'>")?; - html::escape(os::stdout, tx)?; + html_escape(os::stdout, tx)?; fmt::print("</a>")?; free(tx); } else { - html::escape(os::stdout, tx)?; + html_escape(os::stdout, tx)?; free(tx); }, re: reference => htmlref(ctx, re)?, sa: sample => { fmt::print("<pre class='sample'>")?; - html::escape(os::stdout, sa)?; + html_escape(os::stdout, sa)?; fmt::print("</pre>")?; free(sa); }, @@ -290,7 +309,7 @@ fn markup_html(ctx: *context, in: *io::stream) (void | io::error) = { fmt::println("<ul>")?; for (let i = 0z; i < len(li); i += 1) { fmt::println("<li>")?; - html::escape(os::stdout, li[i])?; + html_escape(os::stdout, li[i])?; fmt::println("</li>")?; }; fmt::println("</ul>")?; @@ -496,7 +515,7 @@ fn type_html( defer io::close(buf); unparse::_type(buf, indent, _type)?; if (brief) { - return html::escape(out, strio::string(buf))?; + return html_escape(out, strio::string(buf))?; }; // TODO: More detailed formatter which can find aliases nested deeper in @@ -590,7 +609,7 @@ fn type_html( }, t: ast::struct_type => z += struct_union_html(out, indent, _type, brief)?, t: ast::union_type => z += struct_union_html(out, indent, _type, brief)?, - * => z += html::escape(out, strio::string(buf))?, + * => z += html_escape(out, strio::string(buf))?, }; z; diff --git a/format/html/README b/format/html/README @@ -1,2 +0,0 @@ -This module provides an HTML parser. Or rather, it will provide one. Right now -all it does is escape HTML strings for safe printing. diff --git a/format/html/escape.ha b/format/html/escape.ha @@ -1,40 +0,0 @@ -use encoding::utf8; -use io; -use strings; -use strio; - -// Prints a string to an output stream, escaping any of HTML's reserved -// characters. -export fn escape(out: *io::stream, in: str) (size | io::error) = { - let z = 0z; - let iter = strings::iter(in); - for (true) match (strings::next(&iter)) { - void => break, - rn: rune => z += io::write(out, switch (rn) { - '&' => strings::toutf8("&amp;"), - '<' => strings::toutf8("&lt;"), - '>' => strings::toutf8("&gt;"), - '"' => strings::toutf8("&quot;"), - '\'' => strings::toutf8("&apos;"), - * => utf8::encoderune(rn), - })?, - }; - return z; -}; - -@test fn escape() void = { - let sink = strio::dynamic(); - defer io::close(sink); - escape(sink, "hello world!")!; - assert(strio::string(sink) == "hello world!"); - - let sink = strio::dynamic(); - defer io::close(sink); - escape(sink, "\"hello world!\"")!; - assert(strio::string(sink) == "&quot;hello world!&quot;"); - - let sink = strio::dynamic(); - defer io::close(sink); - escape(sink, "<hello & 'world'!>")!; - assert(strio::string(sink) == "&lt;hello &amp; &apos;world&apos;!&gt;"); -}; diff --git a/scripts/gen-stdlib b/scripts/gen-stdlib @@ -297,12 +297,6 @@ format_elf() { gen_ssa format::elf } -format_html() { - gen_srcs format::html \ - escape.ha - gen_ssa format::html encoding::utf8 io strings strio -} - gensrcs_format_xml() { gen_srcs format::xml \ types.ha \ @@ -871,7 +865,6 @@ endian errors fmt format::elf -format::html format::xml fs fs::mem diff --git a/stdlib.mk b/stdlib.mk @@ -157,10 +157,6 @@ hare_stdlib_deps+=$(stdlib_fmt) stdlib_format_elf=$(HARECACHE)/format/elf/format_elf.o hare_stdlib_deps+=$(stdlib_format_elf) -# gen_lib format::html -stdlib_format_html=$(HARECACHE)/format/html/format_html.o -hare_stdlib_deps+=$(stdlib_format_html) - # gen_lib format::xml stdlib_format_xml=$(HARECACHE)/format/xml/format_xml.o hare_stdlib_deps+=$(stdlib_format_xml) @@ -582,16 +578,6 @@ $(HARECACHE)/format/elf/format_elf.ssa: $(stdlib_format_elf_srcs) $(stdlib_rt) @HARECACHE=$(HARECACHE) $(HAREC) $(HAREFLAGS) -o $@ -Nformat::elf \ -t$(HARECACHE)/format/elf/format_elf.td $(stdlib_format_elf_srcs) -# format::html -stdlib_format_html_srcs= \ - $(STDLIB)/format/html/escape.ha - -$(HARECACHE)/format/html/format_html.ssa: $(stdlib_format_html_srcs) $(stdlib_rt) $(stdlib_encoding_utf8) $(stdlib_io) $(stdlib_strings) $(stdlib_strio) - @printf 'HAREC \t$@\n' - @mkdir -p $(HARECACHE)/format/html - @HARECACHE=$(HARECACHE) $(HAREC) $(HAREFLAGS) -o $@ -Nformat::html \ - -t$(HARECACHE)/format/html/format_html.td $(stdlib_format_html_srcs) - # format::xml stdlib_format_xml_srcs= \ $(STDLIB)/format/xml/types.ha \ @@ -1377,10 +1363,6 @@ hare_testlib_deps+=$(testlib_fmt) testlib_format_elf=$(TESTCACHE)/format/elf/format_elf.o hare_testlib_deps+=$(testlib_format_elf) -# gen_lib format::html -testlib_format_html=$(TESTCACHE)/format/html/format_html.o -hare_testlib_deps+=$(testlib_format_html) - # gen_lib format::xml testlib_format_xml=$(TESTCACHE)/format/xml/format_xml.o hare_testlib_deps+=$(testlib_format_xml) @@ -1809,16 +1791,6 @@ $(TESTCACHE)/format/elf/format_elf.ssa: $(testlib_format_elf_srcs) $(testlib_rt) @HARECACHE=$(TESTCACHE) $(HAREC) $(TESTHAREFLAGS) -o $@ -Nformat::elf \ -t$(TESTCACHE)/format/elf/format_elf.td $(testlib_format_elf_srcs) -# format::html -testlib_format_html_srcs= \ - $(STDLIB)/format/html/escape.ha - -$(TESTCACHE)/format/html/format_html.ssa: $(testlib_format_html_srcs) $(testlib_rt) $(testlib_encoding_utf8) $(testlib_io) $(testlib_strings) $(testlib_strio) - @printf 'HAREC \t$@\n' - @mkdir -p $(TESTCACHE)/format/html - @HARECACHE=$(TESTCACHE) $(HAREC) $(TESTHAREFLAGS) -o $@ -Nformat::html \ - -t$(TESTCACHE)/format/html/format_html.td $(testlib_format_html_srcs) - # format::xml testlib_format_xml_srcs= \ $(STDLIB)/format/xml/types.ha \