diff --git a/src/Controller/KanjiController.php b/src/Controller/KanjiController.php index 9d02417..8836b46 100644 --- a/src/Controller/KanjiController.php +++ b/src/Controller/KanjiController.php @@ -23,21 +23,39 @@ class KanjiController extends AbstractController return "kanji/$path.html.twig"; } + /** @return array> */ + 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 diff --git a/src/Service/AnkiService.php b/src/Service/AnkiService.php index 9e6a1ae..365d621 100644 --- a/src/Service/AnkiService.php +++ b/src/Service/AnkiService.php @@ -141,14 +141,14 @@ class AnkiService $this->request('guiBrowse', ['query' => 'nid:' . $note->getId()]); } - /** @return list */ + /** @return array */ 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; diff --git a/src/Service/CharListService.php b/src/Service/CharListService.php index 3414aec..11f61c4 100644 --- a/src/Service/CharListService.php +++ b/src/Service/CharListService.php @@ -13,7 +13,7 @@ class CharListService /** * TODO: Document the structure being returned... maybe * - * @return array + * @return array|array>> */ function getList(string $name): array { diff --git a/templates/kanji/grid.html.twig b/templates/kanji/grid.html.twig index 4a32c2e..f2155d2 100644 --- a/templates/kanji/grid.html.twig +++ b/templates/kanji/grid.html.twig @@ -3,15 +3,21 @@ {% block title %}Kanji Index{% endblock %} {% block body %} + +

{{ block('title') }}

{% for char in characters %}