display by category

This commit is contained in:
Kay Faraday 2024-07-23 01:31:44 +00:00
parent 4a3ee534ca
commit e8ee2850aa
2 changed files with 28 additions and 18 deletions

View File

@ -13,6 +13,7 @@ use rocket::{get, routes, Request, Response, State};
use serde::Deserialize; use serde::Deserialize;
use std::io::Cursor; use std::io::Cursor;
use std::str::FromStr; use std::str::FromStr;
use std::collections::BTreeMap;
#[rocket::launch] #[rocket::launch]
fn rocket() -> _ { fn rocket() -> _ {
@ -54,6 +55,8 @@ impl<T: Template> Responder<'_, 'static> for Html<T> {
struct Emojo { struct Emojo {
shortcode: String, shortcode: String,
url: String, url: String,
#[serde(default)]
category: Option<String>,
static_url: String, static_url: String,
visible_in_picker: Option<bool>, visible_in_picker: Option<bool>,
} }
@ -63,7 +66,7 @@ struct Emojo {
struct Output { struct Output {
instance: String, instance: String,
show_animated: bool, show_animated: bool,
emojo: Vec<Emojo>, emojo: BTreeMap<String, Vec<Emojo>>,
} }
#[get("/<instance>?<show_all>&<show_animated>")] #[get("/<instance>?<show_all>&<show_animated>")]
@ -97,10 +100,16 @@ async fn instance(
emojo.retain(|x| x.visible_in_picker.unwrap_or(true)); emojo.retain(|x| x.visible_in_picker.unwrap_or(true));
} }
let mut emojo_by_category: BTreeMap<String, Vec<Emojo>> = BTreeMap::new();
for emoj in emojo {
let category = emoj.category.clone().unwrap_or("(No category)".to_string());
emojo_by_category.entry(category).or_default().push(emoj);
}
Ok(Html(Output { Ok(Html(Output {
instance, instance,
show_animated, show_animated,
emojo, emojo: emojo_by_category,
})) }))
} }

View File

@ -5,23 +5,24 @@
<b>{{ instance }}</b> emojo list<br> <b>{{ instance }}</b> emojo list<br>
click/touch to copy to clipboard click/touch to copy to clipboard
</p> </p>
<dl class="emojo"> {% for category in emojo.keys() %}
{% if show_animated %} <h2 id={{ category }}>{{ category }}</h2>
{% for emoj in emojo %} <dl class="emojo">
<div> {% for emoj in emojo[category] %}
<dt><img src="{{ emoj.url }}" alt=":{{ emoj.shortcode }}:" loading=lazy></dt> {% if show_animated %}
<dd>:{{ emoj.shortcode }}:</dd> <div>
</div> <dt><img src="{{ emoj.url }}" alt=":{{ emoj.shortcode }}:" loading=lazy></dt>
<dd>:{{ emoj.shortcode }}:</dd>
</div>
{% else %}
<div>
<dt><img src="{{ emoj.static_url }}" alt=":{{ emoj.shortcode }}:" loading=lazy></dt>
<dd>:{{ emoj.shortcode }}:</dd>
</div>
{% endif %}
{% endfor %} {% endfor %}
{% else %} </dl>
{% for emoj in emojo %} {% endfor %}
<div>
<dt><img src="{{ emoj.static_url }}" alt=":{{ emoj.shortcode }}:" loading=lazy></dt>
<dd>:{{ emoj.shortcode }}:</dd>
</div>
{% endfor %}
{% endif %}
</dl>
{% endblock body %} {% endblock body %}
{% block script %} {% block script %}
<script src="/static/copy.js"></script> <script src="/static/copy.js"></script>