feat: Get char list info in a more programmatic way preparing for cache

This commit is contained in:
Dendy 2025-09-03 07:31:14 +02:00
parent fe68c07e2c
commit 11cae6aad1
4 changed files with 38 additions and 24 deletions

View File

@ -23,21 +23,39 @@ class KanjiController extends AbstractController
return "kanji/$path.html.twig";
}
/** @return array<string, array<string, 0>> */
private function getCharInfo(): array
{
$ret = [];
$lists = [
'sn' => $this->anki->getKnownSnKanjiCounts(),
'sln' => $this->anki->getKnownSlnKanjiCounts(),
'unicode' => $this->anki->getUnicodeKanji(),
'taiwan' => $this->charList->getList('taiwan')['chars'],
];
foreach ($lists as $listName => $list) {
foreach (array_keys($list) as $char) {
$ret[$char] ??= [];
$ret[$char][$listName] = 0;
}
}
return $ret;
}
#[Route('/{start}-{end}', name: 'index')]
public function index(
string $start,
string $end,
): Response {
$snKanji = array_keys($this->anki->getKnownSnKanjiCounts());
$slnKanji = array_keys($this->anki->getKnownSlnKanjiCounts());
$unicodeKanji = $this->anki->getUnicodeKanji();
$taiwan = $this->charList->getList('taiwan');
$jiten = json_decode(file_get_contents(
"{$this->getParameter('kernel.project_dir')}/data/kanken-links.json",
), true);
$charInfo = $this->getCharInfo();
$chars = [];
foreach (range(intval("{$start}0", 16), intval("{$end}f", 16)) as $codepoint) {
$charStr = mb_chr($codepoint, 'UTF-8');
@ -45,12 +63,7 @@ class KanjiController extends AbstractController
$chars[] = [
'str' => $charStr,
'codepoint' => dechex($codepoint),
'lists' => [
'sn' => in_array($charStr, $snKanji, true),
'sln' => in_array($charStr, $slnKanji, true),
'unicode' => in_array($charStr, $unicodeKanji, true),
'taiwan' => key_exists($charStr, $taiwan['chars']),
],
'lists' => $charInfo[$charStr] ?? [],
'jiten_href' => key_exists($charStr, $jiten)
? "https://kanji.jitenon.jp/kanji{$jiten[$charStr]}"
: null

View File

@ -141,14 +141,14 @@ class AnkiService
$this->request('guiBrowse', ['query' => 'nid:' . $note->getId()]);
}
/** @return list<string> */
/** @return array<string, 0> */
public function getUnicodeKanji(): array
{
$ret = [];
foreach ($this->getNotes($this->getAllUnicodeNoteIds()) as $note) {
assert($note instanceof UnicodeNote);
$ret[] = $note->getCharacter();
$ret[$note->getCharacter()] = 0;
}
return $ret;

View File

@ -13,7 +13,7 @@ class CharListService
/**
* TODO: Document the structure being returned... maybe
*
* @return array<mixed>
* @return array<string, string|array<string, 0>|array<string|array<string, 0>>>
*/
function getList(string $name): array
{

View File

@ -3,15 +3,21 @@
{% block title %}Kanji Index{% endblock %}
{% block body %}
<style>
.taiwan > .kanji-card { background-color: var(--bs-success); }
.sn > .kanji-card { background-color: var(--bs-danger); }
.sln > .kanji-card { background-color: var(--bs-warning); }
</style>
<div class="example-wrapper">
<h1 class="text-center p-2 mb-2">{{ block('title') }}</h1>
<div class="d-flex flex-wrap m-2">
{% for char in characters %}
<div style="width: calc(100% / 16);">
<div
class="border text-center rounded"
class="border text-center rounded {{ char.lists|keys|join(' ') }}"
style="
{% if char.lists.unicode %}
{% if char.lists.unicode is defined %}
border-color: green !important;
border-width: 3px !important;
{% endif %}
@ -19,12 +25,7 @@
overflow: hidden;
"
>
<div class="
border-bottom fs-4 pb-1
{{ char.lists.taiwan ? 'bg-primary text-white' : '' }}
{{ char.lists.sln ? 'bg-danger text-white' : '' }}
{{ char.lists.sn ? 'bg-warning text-black' : '' }}
">
<div class="kanji-card border-bottom fs-4 pb-1">
{% if char.jiten_href != null %}
<a
target="_blank"
@ -41,7 +42,7 @@
font-size: 0.5em;
padding-bottom: 2px;
">
{% if not char.lists.unicode %}
{% if char.lists.unicode is not defined %}
<a
target="_blank"
href="{{ path('app_kanji_register', {