hare

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

commit 59e3dfbef782f8b8640a119bc2aecf64a645a72c
parent 69c15619b04c2c992110ac93e5ae370ba12c1be9
Author: Drew DeVault <sir@cmpwn.com>
Date:   Mon, 19 Apr 2021 11:42:58 -0400

format::html: new module

Diffstat:
Aformat/html/docs.ha | 2++
Aformat/html/escape.ha | 40++++++++++++++++++++++++++++++++++++++++
Mscripts/gen-stdlib | 8++++++++
Mstdlib.mk | 28++++++++++++++++++++++++++++
4 files changed, 78 insertions(+), 0 deletions(-)

diff --git a/format/html/docs.ha b/format/html/docs.ha @@ -0,0 +1,2 @@ +// 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 @@ -0,0 +1,40 @@ +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(in: str, out: *io::stream) (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("hello world!", sink); + assert(strio::string(sink) == "hello world!"); + + let sink = strio::dynamic(); + defer io::close(sink); + escape("\"hello world!\"", sink); + assert(strio::string(sink) == "&quot;hello world!&quot;"); + + let sink = strio::dynamic(); + defer io::close(sink); + escape("<hello & 'world'!>", sink); + assert(strio::string(sink) == "&lt;hello &amp; &apos;world&apos;!&gt;"); +}; diff --git a/scripts/gen-stdlib b/scripts/gen-stdlib @@ -267,6 +267,13 @@ format_elf() { gen_ssa format::elf } +format_html() { + gen_srcs format::html \ + escape.ha \ + docs.ha + gen_ssa format::html encoding::utf8 io strings strio +} + gensrcs_format_xml() { gen_srcs format::xml \ types.ha \ @@ -674,6 +681,7 @@ endian errors fmt format_elf +format_html format_xml fs fs_mem diff --git a/stdlib.mk b/stdlib.mk @@ -123,6 +123,9 @@ hare_stdlib_deps+=$(stdlib_fmt) stdlib_format_elf=$(HARECACHE)/format/elf/format_elf.o hare_stdlib_deps+=$(stdlib_format_elf) +stdlib_format_html=$(HARECACHE)/format/html/format_html.o +hare_stdlib_deps+=$(stdlib_format_html) + stdlib_format_xml=$(HARECACHE)/format/xml/format_xml.o hare_stdlib_deps+=$(stdlib_format_xml) @@ -427,6 +430,17 @@ $(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 \ + $(STDLIB)/format/html/docs.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 \ @@ -977,6 +991,9 @@ hare_testlib_deps+=$(testlib_fmt) testlib_format_elf=$(TESTCACHE)/format/elf/format_elf.o hare_testlib_deps+=$(testlib_format_elf) +testlib_format_html=$(TESTCACHE)/format/html/format_html.o +hare_testlib_deps+=$(testlib_format_html) + testlib_format_xml=$(TESTCACHE)/format/xml/format_xml.o hare_testlib_deps+=$(testlib_format_xml) @@ -1286,6 +1303,17 @@ $(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 \ + $(STDLIB)/format/html/docs.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 \