getting closer to full port of web app starter

This commit is contained in:
udo
2026-04-19 19:44:18 +00:00
parent d1167aec3b
commit af642c8167
131 changed files with 900 additions and 80 deletions
+51 -12
View File
@@ -5,6 +5,14 @@ bool str_contains(String haystack, String needle)
return replace(haystack, needle, "\x01") != haystack;
}
void render_hit(String ft, String name, String badge, String snippet)
{
<><div class="search-hit">
<a href="index.uce?p=<?= uri_encode(ft) ?>"><?= html_escape(name) ?><? if(badge != "") { ?><span class="badge"><?= badge ?></span><? } ?></a><?
if(snippet != "") { ?><span class="search-snippet"><?= html_escape(snippet) ?></span><? } ?>
</div></>
}
RENDER(Request& context)
{
String q = to_lower(trim(context.get["q"]));
@@ -14,7 +22,16 @@ RENDER(Request& context)
return;
}
u32 count = 0;
// Collect results into two buckets: title matches first, then content-only matches
StringList title_ft;
StringList title_name;
StringList title_badge;
StringList title_snippet;
StringList body_ft;
StringList body_name;
StringList body_badge;
StringList body_snippet;
for(auto file_name : ls("pages/"))
{
@@ -34,34 +51,56 @@ RENDER(Request& context)
}
String content = file_get_contents("pages/" + ft + ".txt");
bool name_match = str_contains(to_lower(name), q);
bool content_match = str_contains(to_lower(content), q);
if(str_contains(to_lower(name), q) || str_contains(to_lower(content), q))
if(!name_match && !content_match)
continue;
// Find first content line that contains the query for a snippet
String snippet = "";
if(!name_match || content_match)
{
// Find first content line that contains the query for a snippet
String snippet = "";
for(auto line : split(content, "\n"))
{
String t = trim(line);
if(t.length() > 4 && t.substr(0, 1) != ":" && str_contains(to_lower(t), q))
{
snippet = t;
if(snippet.length() > 100)
snippet = snippet.substr(0, 100) + "…";
if(snippet.length() > 120)
snippet = snippet.substr(0, 120) + "…";
break;
}
}
}
<><div class="func-item search-hit">
<a href="index.uce?p=<?= uri_encode(ft) ?>"><?= html_escape(name) ?><? if(badge != "") { ?><span class="badge"><?= badge ?></span><? } ?></a>
<? if(snippet != "") { ?><div class="search-snippet"><?= html_escape(snippet) ?></div><? } ?>
</div></>
count += 1;
if(name_match)
{
title_ft.push_back(ft);
title_name.push_back(name);
title_badge.push_back(badge);
title_snippet.push_back(snippet);
}
else
{
body_ft.push_back(ft);
body_name.push_back(name);
body_badge.push_back(badge);
body_snippet.push_back(snippet);
}
}
u32 count = title_ft.size() + body_ft.size();
if(count == 0)
{
<><p class="no-results">No results for "<strong><?= html_escape(q) ?></strong>"</p></>
return;
}
for(u32 i = 0; i < title_ft.size(); i++)
render_hit(title_ft[i], title_name[i], title_badge[i], title_snippet[i]);
for(u32 i = 0; i < body_ft.size(); i++)
render_hit(body_ft[i], body_name[i], body_badge[i], body_snippet[i]);
}