web search done right
Refactor askama templating to use function rather then components (#41)
Oliver Bøvingadcaa5ddfbb7
diff --git a/frontend/src/askama.ts b/frontend/src/askama.tsindex 8604df1c..417fec81 100644--- a/frontend/src/askama.ts+++ b/frontend/src/askama.ts@@ -1 +1,25 @@-export const askama = (s: TemplateStringsArray) => `{{ ${s} }}`;+type Component = (() => any) | any;++const prepare = (c: Component | null) => (typeof c == "function" ? c() : c);++const match = (expr: string, cases: Record<string, Component>) =>+ [+ `{% match ${expr} %}`,+ Object.entries(cases).map(([c, v]) => [`{% when ${c} %}`, prepare(v)]),+ `{% endmatch %}`,+ ].flat(100);+type ArrayPattern<S> = S extends `${infer _} in ${infer _}` ? S : never;+const for_ = <S extends string>(pat: ArrayPattern<S>, f: Component) =>+ [`{% for ${pat} %}`, prepare(f), `{% endfor %}`].flat(100);+const if_ = (pat: string, then: Component, else_: null | Component = null) =>+ [+ `{% if ${pat} %}`,+ prepare(then),+ else_ && [`{% else %}`, prepare(else_)],+ `{% endif %}`,+ ].flat(100);++export const askama = Object.assign(+ (s: TemplateStringsArray) => `{{ ${s.raw.join("")} }}`,+ { match, for_, if_ }+);diff --git a/frontend/src/components/AskamaElse.astro b/frontend/src/components/AskamaElse.astrodeleted file mode 100644index 4bc622b5..00000000--- a/frontend/src/components/AskamaElse.astro+++ /dev/null@@ -1,8 +0,0 @@-----export interface Props {}------<Fragment>- <Fragment set:html={`{% else %}`} />- <slot />-</Fragment>diff --git a/frontend/src/components/AskamaFor.astro b/frontend/src/components/AskamaFor.astrodeleted file mode 100644index 768a3bb2..00000000--- a/frontend/src/components/AskamaFor.astro+++ /dev/null@@ -1,17 +0,0 @@-----export interface Props {- elem: string;- in: string;-}--const { props } = Astro;--const header = `{% for ${props.elem} in ${props.in} %} `;-const footer = `{% endfor %} `;------<Fragment>- <Fragment set:html={header} />- <slot />- <Fragment set:html={footer} />-</Fragment>diff --git a/frontend/src/components/AskamaIf.astro b/frontend/src/components/AskamaIf.astrodeleted file mode 100644index 001da079..00000000--- a/frontend/src/components/AskamaIf.astro+++ /dev/null@@ -1,13 +0,0 @@-----export interface Props {- expr: string;-}--const { expr } = Astro.props;------<Fragment>- <Fragment set:html={`{% if ${expr} %}`} />- <slot />- <Fragment set:html={"{% endif %}"} />-</Fragment>diff --git a/frontend/src/components/AskamaMatch.astro b/frontend/src/components/AskamaMatch.astrodeleted file mode 100644index d8de9317..00000000--- a/frontend/src/components/AskamaMatch.astro+++ /dev/null@@ -1,13 +0,0 @@-----export interface Props {- expr: string;-}--const { expr } = Astro.props;------<Fragment>- <Fragment set:html={`{% match ${expr} %}`} />- <slot />- <Fragment set:html={"{% endmatch %}"} />-</Fragment>diff --git a/frontend/src/components/AskamaWhen.astro b/frontend/src/components/AskamaWhen.astrodeleted file mode 100644index 9c3b0fa0..00000000--- a/frontend/src/components/AskamaWhen.astro+++ /dev/null@@ -1,17 +0,0 @@-----export interface Props {- pat: string;- with?: string;-}--const { props } = Astro;------<Fragment>- <Fragment- set:html={`{% when ${props.pat}${- props.with ? ` with ${props.with}` : ""- } %}`}- />- <slot />-</Fragment>diff --git a/frontend/src/components/Entity.astro b/frontend/src/components/Entity.astroindex dbee5eba..3b7cdbfa 100644--- a/frontend/src/components/Entity.astro+++ b/frontend/src/components/Entity.astro@@ -1,8 +1,4 @@----import AskamaFor from "./AskamaFor.astro";-import AskamaIf from "./AskamaIf.astro";-import AskamaMatch from "./AskamaMatch.astro";-import AskamaWhen from "./AskamaWhen.astro";import { askama } from "../askama";export interface Props {@@ -12,8 +8,8 @@ export interface Props {<div class="flex grow justify-center pt-3"><div class="max-w-md flex flex-col items-center">- <AskamaMatch expr="entity.image">- <AskamaWhen pat="Some(image)">+ {+ askama.if_("let Some(image) = entity.image", () => (<div class="w-lg mb-3"><a href="https://en.wikipedia.org/wiki/{{ entity.title|urlencode }}"><img@@ -22,9 +18,8 @@ export interface Props {/></a></div>- </AskamaWhen>- <AskamaWhen pat="None" />- </AskamaMatch>+ ))+ }<div class="text-xl mb-1"><a href="https://en.wikipedia.org/wiki/{{ entity.title }}">{askama`entity.title`}@@ -40,51 +35,48 @@ export interface Props {></span></div>- <AskamaIf expr="!entity.info.is_empty()">- <div class="flex flex-col text-sm mt-5 w-full px-4">- <div- class="border-separate border-spacing-x-4 gap-x-4 grid grid-cols-[auto_1fr] border-spacing-y-1.5"- >- <AskamaFor elem="(key, value)" in="entity.info">- <div class="text-gray-400">- {askama`key`}- </div>- <div>- {askama`value`}- </div>- </AskamaFor>+ {+ askama.if_("!entity.info.is_empty()", () => (+ <div class="mt-5 flex w-full flex-col px-4 text-sm">+ <div class="border-spacing-x-4 border-spacing-y-1.5 grid border-separate grid-cols-[auto_1fr] gap-x-4">+ {askama.for_("(key, value) in entity.info", () => (+ <>+ <div class="text-gray-400">{askama`key`}</div>+ <div>{askama`value`}</div>+ </>+ ))}+ </div></div>- </div>- </AskamaIf>- <AskamaIf expr="!entity.related_entities.is_empty()">- <div class="flex flex-col mt-5 text-gray-500 w-full">- <div class="font-light">Related Searches</div>- <div class="flex">- <AskamaFor elem="entity" in="entity.related_entities">- <div class="flex flex-col items-center p-4">- <AskamaMatch expr="entity.image">- <AskamaWhen pat="Some(image)">- <div class="h-20 w-20 mb-3">+ ))+ }+ {+ askama.if_("!entity.related_entities.is_empty()", () => (+ <div class="mt-5 flex w-full flex-col text-gray-500">+ <div class="font-light">Related Searches</div>+ <div class="flex">+ {askama.for_("entity in entity.related_entities", () => (+ <div class="flex flex-col items-center p-4">+ {askama.if_("let Some(image) = entity.image", () => (+ <div class="mb-3 h-20 w-20"><a href="/search?q={{ entity.title|urlencode }}"><img- class="h-full object-cover w-full rounded-full"+ class="h-full w-full rounded-full object-cover"src="/entity/image/{{ image|urlencode }}"/></a></div>- </AskamaWhen>- <AskamaWhen pat="None" />- </AskamaMatch>+ ))}- <div class="text-center">- <a href="/search?q={{ entity.title|urlencode }}">- {askama`entity.title`}- </a>+ <div class="text-center">+ <a href="/search?q={{ entity.title|urlencode }}">+ {askama`entity.title`}+ </a>+ </div></div>- </div>- </AskamaFor>+ ))}+ </div></div>- </div>- </AskamaIf>+ ))+ }</div></div>diff --git a/frontend/src/pages/search.astro b/frontend/src/pages/search.astroindex 89f41961..fb456f73 100644--- a/frontend/src/pages/search.astro+++ b/frontend/src/pages/search.astro@@ -1,11 +1,6 @@---import Layout from "../layouts/Layout.astro";import Searchbar from "../components/Searchbar.astro";-import AskamaFor from "../components/AskamaFor.astro";-import AskamaMatch from "../components/AskamaMatch.astro";-import AskamaWhen from "../components/AskamaWhen.astro";-import AskamaIf from "../components/AskamaIf.astro";-import AskamaElse from "../components/AskamaElse.astro";import Navbar from "../components/Navbar.astro";import Footer from "../components/Footer.astro";import Entity from "../components/Entity.astro";@@ -44,110 +39,108 @@ import { askama } from "../askama";name="gl"class="p-0 m-0 hover:cursor-pointer">- <AskamaFor elem="region_selection" in="all_regions">- <AskamaMatch expr="region_selection">- <AskamaWhen pat="RegionSelection::Selected" with="(region)">- <option value="{{ region.gl() }}" selected>- {askama`region.name()`}- </option>- </AskamaWhen>- <AskamaWhen pat="RegionSelection::Unselected" with="(region)">- <option value="{{ region.gl() }}">- {askama`region.name()`}- </option>- </AskamaWhen>- </AskamaMatch>- </AskamaFor>+ {+ askama.for_(+ "region_selection in all_regions",+ askama.match("region_selection", {+ "RegionSelection::Selected(region)": (+ <option value="{{ region.gl() }}" selected>+ {askama`region.name()`}+ </option>+ ),+ "RegionSelection::Unselected(region)": (+ <option value="{{ region.gl() }}">+ {askama`region.name()`}+ </option>+ ),+ })+ )+ }</select></div></div><div class="flex flex-col pt-4 pb-8">- <AskamaMatch expr="spell_correction">- <AskamaWhen pat="Some" with="(correction)">+ {+ askama.if_("let Some(correction) = spell_correction", () => (<div class="pl-3 pb-3">- Did you mean: <a- class="italic title-link font-bold"+ Did you mean:{" "}+ <a+ class="title-link font-bold italic"href="/search?q={{ correction|urlencode }}">{askama`correction`}</a></div>- </AskamaWhen>- <AskamaWhen pat="None" />- </AskamaMatch>+ ))+ }<div class="search-result flex flex-col space-y-3">- <AskamaFor elem="item" in="search_result">- <div class="flex max-w-3xl w-full p-3">- <div class="flex flex-col w-full">- <div class="text-sm mb-1 flex">- <div class="flex items-center mr-2">- <img- src="/favicons/{{ item.domain|urlencode }}"- width="13px"- />+ {+ askama.for_("item in search_result", () => (+ <div class="flex w-full max-w-3xl p-3">+ <div class="flex w-full flex-col">+ <div class="mb-1 flex text-sm">+ <div class="mr-2 flex items-center">+ <img+ src="/favicons/{{ item.domain|urlencode }}"+ width="13px"+ />+ </div>+ <div>+ <a+ class="text-gray-600 hover:no-underline"+ href="{{ item.url }}"+ >+ {askama`item.pretty_url`}+ </a>+ </div></div>- <div>- <a- class="hover:no-underline text-gray-600"- href="{{ item.url }}"- >{askama`item.pretty_url`}+ <div class="mb-1 text-lg font-medium">+ <a class="title-link" href="{{ item.url }}">+ {askama`item.title`}</a></div>+ <div class="text-sm">+ {askama.if_("let Some(val) = item.last_updated", () => (+ <>+ <span class="text-gray-500">{askama`val`}</span> —+ </>+ ))}+ {askama`item.snippet`}+ </div></div>- <div class="text-lg font-medium mb-1">- <a class="title-link" href="{{ item.url }}"- >{askama`item.title`}- </a>- </div>- <div class="text-sm">- <AskamaMatch expr="item.last_updated">- <AskamaWhen pat="Some" with="(val)">- <span class="text-gray-500">- {askama`val`}- </span> —- </AskamaWhen>- <AskamaWhen pat="None" />- </AskamaMatch>- {askama`item.snippet`}- </div>- </div>- <div class="flex h-full items-center">- <div class="h-20 w-20 pl-2 pt-1 pb-1">- <AskamaMatch expr="item.primary_image_uuid">- <AskamaWhen pat="Some" with="(val)">+ <div class="flex h-full items-center">+ <div class="h-20 w-20 pl-2 pt-1 pb-1">+ {askama.if_(+ "let Some(val) = item.primary_image_uuid",<a href="{{ item.url|urlencode }}"><img- class="h-full w-full object-cover rounded-full"+ class="h-full w-full rounded-full object-cover"src="/image/{{ val|urlencode }}"/></a>- </AskamaWhen>- <AskamaWhen pat="None" />- </AskamaMatch>+ )}+ </div></div></div>- </div>- </AskamaFor>+ ))+ }</div><div class="flex w-full justify-center items-center pr-24 mt-5">- <AskamaIf expr="current_page == 1">- <div class="prev-page gray-arrow"></div>-- <AskamaElse>- <AskamaMatch expr="prev_page_url">- <AskamaWhen pat="Some" with="(url)">- <a href="{{ url }}">- <div class="prev-page cuely-color-arrow"></div>- </a>- </AskamaWhen>- <AskamaWhen pat="None">- <div class="prev-page gray-arrow"></div>- </AskamaWhen>- </AskamaMatch>- </AskamaElse>- </AskamaIf>+ {+ askama.if_(+ "current_page == 1",+ <div class="prev-page gray-arrow" />,+ askama.if_(+ "let Some(url) = prev_page_url",+ <a href="{{ url }}">+ <div class="prev-page cuely-color-arrow" />+ </a>,+ <div class="prev-page gray-arrow" />+ )+ )+ }<div class="mr-2 ml-2">Page {askama`current_page`}</div>@@ -158,12 +151,7 @@ import { askama } from "../askama";</div></div>- <AskamaMatch expr="entity">- <AskamaWhen pat="Some" with="(entity)">- <Entity entity="entity" />- </AskamaWhen>- <AskamaWhen pat="None" />- </AskamaMatch>+ {askama.if_("let Some(entity) = entity", <Entity entity="entity" />)}</div></main>