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 "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')] #[Route('/{start}-{end}', name: 'index')]
public function index( public function index(
string $start, string $start,
string $end, string $end,
): Response { ): 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( $jiten = json_decode(file_get_contents(
"{$this->getParameter('kernel.project_dir')}/data/kanken-links.json", "{$this->getParameter('kernel.project_dir')}/data/kanken-links.json",
), true); ), true);
$charInfo = $this->getCharInfo();
$chars = []; $chars = [];
foreach (range(intval("{$start}0", 16), intval("{$end}f", 16)) as $codepoint) { foreach (range(intval("{$start}0", 16), intval("{$end}f", 16)) as $codepoint) {
$charStr = mb_chr($codepoint, 'UTF-8'); $charStr = mb_chr($codepoint, 'UTF-8');
@ -45,12 +63,7 @@ class KanjiController extends AbstractController
$chars[] = [ $chars[] = [
'str' => $charStr, 'str' => $charStr,
'codepoint' => dechex($codepoint), 'codepoint' => dechex($codepoint),
'lists' => [ 'lists' => $charInfo[$charStr] ?? [],
'sn' => in_array($charStr, $snKanji, true),
'sln' => in_array($charStr, $slnKanji, true),
'unicode' => in_array($charStr, $unicodeKanji, true),
'taiwan' => key_exists($charStr, $taiwan['chars']),
],
'jiten_href' => key_exists($charStr, $jiten) 'jiten_href' => key_exists($charStr, $jiten)
? "https://kanji.jitenon.jp/kanji{$jiten[$charStr]}" ? "https://kanji.jitenon.jp/kanji{$jiten[$charStr]}"
: null : null

View File

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

View File

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

View File

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