foreseen

A text-based gemini browser with prefetching.
git clone git://git.laack.co/foreseen.git
Log | Files | Refs | README | LICENSE

commit 1008db9716e448b0f6d08e8b5eb219695927c144
parent d81e7ad12a94b4f03355515a75e091d902251a1c
Author: Andrew Laack <andrew@laack.co>
Date:   Sun, 27 Sep 2026 19:54:30 -0500

Improving gemtext spec compliance

Diffstat:
MREADME | 3+++
Msrc/utils.cpp | 16+++++++++++-----
Mtests/pure-test.cpp | 61+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 75 insertions(+), 5 deletions(-)

diff --git a/README b/README @@ -63,6 +63,9 @@ Pages fetched with an identity are never cached, and prefetching never sends req One of the main reasons this browser feels so much faster than other gemini protocol browsers is because of its optional prefetch caching. This is enabled by default. When you visit a site without an identity, a thread will be created that fetches the first 10 links on the page. This enables ~ instantaneous visitation of those links. Prefetch caching is only enabled when you aren't using an identity for a domain. + Per the gemtext specification, "clients must not automatically make any network connections as part of displaying links", so this mode of + operation is considered non-compliant with the gemtext specification. I don't believe a file format specification should be prescriptive + about such things. History caching: diff --git a/src/utils.cpp b/src/utils.cpp @@ -69,7 +69,10 @@ std::string stripLeadingWhiteSpace(std::string& input) { } bool isWhiteSpace(std::string& line, int idx) { - return line[idx] == ' ' || line[idx] == '\t'; + if(idx < line.size() && idx >= 0) { + return line[idx] == ' ' || line[idx] == '\t'; + } + return false; } std::string truncateAfter(std::string input, char truncate) { @@ -110,20 +113,23 @@ Line* lineToLine(std::string input, std::optional<uri> prior, int linkCount, boo } if(!isPreformatted) { - if(input.substr(0,2) == "=>") { + if(input.substr(0,2) == "=>" && isWhiteSpace(input,2)) { Link* ln = new Link{input, prior, linkCount}; return ln; } - if(input.substr(0,1) == "#") { + if(input.substr(0,1) == "#" && isWhiteSpace(input,1) || + input.substr(0,2) == "##" && isWhiteSpace(input,2) || + input.substr(0,3) == "###" && isWhiteSpace(input,3)) { + Heading* hd = new Heading{input}; return hd; } - if(input.substr(0,1) == ">") { + if(input.substr(0,1) == ">" && isWhiteSpace(input,1)) { Quote* qt= new Quote{input}; return qt; } - if(input.substr(0,1) == "*") { + if(input.substr(0,1) == "*" && isWhiteSpace(input,1)) { ListItem* li= new ListItem{input}; return li; } diff --git a/tests/pure-test.cpp b/tests/pure-test.cpp @@ -197,3 +197,64 @@ TEST_CASE("Test line parsing handles whitespace correctly") { } } } + +TEST_CASE("Proper whitespace compliance") { + auto res1 = lineToLine("", std::nullopt, -1, false); + REQUIRE(res1->type() == PLAINTEXT); + + auto res2 = lineToLine("", std::nullopt, -1, true); + REQUIRE(res2->type() == PREFORMATTED); + + auto res3 = lineToLine("=>gemini://laack.co", std::nullopt, 1, false); + REQUIRE(res3->type() == PLAINTEXT); + + auto res4 = lineToLine("```", std::nullopt, 1, false); + REQUIRE(res4->type() == FORMAT_SWITCH); + + auto res5 = lineToLine("#H1 Heading", std::nullopt, 1, false); + REQUIRE(res5->type() == PLAINTEXT); + + auto res6 = lineToLine("##H2 Heading", std::nullopt, 1, false); + REQUIRE(res6->type() == PLAINTEXT); + + auto res7 = lineToLine("###H3 Heading", std::nullopt, 1, false); + REQUIRE(res7->type() == PLAINTEXT); + + auto res8 = lineToLine(">test quote", std::nullopt, 1, false); + REQUIRE(res8->type() == PLAINTEXT); + + auto res9 = lineToLine("*test li", std::nullopt, 1, false); + REQUIRE(res9->type() == PLAINTEXT); + + +} + + +TEST_CASE("Crash test") { + auto res1 = lineToLine("", std::nullopt, -1, false); + REQUIRE(res1->type() == PLAINTEXT); + + auto res2 = lineToLine("", std::nullopt, -1, true); + REQUIRE(res2->type() == PREFORMATTED); + + auto res3 = lineToLine("=>", std::nullopt, 1, false); + REQUIRE(res3->type() == PLAINTEXT); + + auto res4 = lineToLine("```", std::nullopt, 1, false); + REQUIRE(res4->type() == FORMAT_SWITCH); + + auto res5 = lineToLine("#", std::nullopt, 1, false); + REQUIRE(res5->type() == PLAINTEXT); + + auto res6 = lineToLine("##", std::nullopt, 1, false); + REQUIRE(res6->type() == PLAINTEXT); + + auto res7 = lineToLine("###", std::nullopt, 1, false); + REQUIRE(res7->type() == PLAINTEXT); + + auto res8 = lineToLine(">", std::nullopt, 1, false); + REQUIRE(res8->type() == PLAINTEXT); + + auto res9 = lineToLine("*", std::nullopt, 1, false); + REQUIRE(res9->type() == PLAINTEXT); +}