From 99fedadd81cdaad8557eb7ceaec81e5e0d417dbb Mon Sep 17 00:00:00 2001 From: Dendy Faist Date: Sat, 6 Sep 2025 11:49:51 +0200 Subject: [PATCH] feat: Add terms to kanji view --- src/Controller/KanjiController.php | 2 ++ src/Service/AnkiService.php | 21 +++++++++++++++++++++ src/Twig/Extension/AppExtension.php | 11 +++++++++++ templates/kanji/view.html.twig | 28 ++++++++++++++++++---------- 4 files changed, 52 insertions(+), 10 deletions(-) diff --git a/src/Controller/KanjiController.php b/src/Controller/KanjiController.php index 15b50fb..071bea9 100644 --- a/src/Controller/KanjiController.php +++ b/src/Controller/KanjiController.php @@ -124,6 +124,7 @@ class KanjiController extends AbstractController "{$this->getParameter('kernel.project_dir')}/data/kanken-links.json", ), true); + $terms = $this->anki->searchTerms($charStr); $ebookRef = require "$this->varBasepath/ebook-ref.php"; @@ -131,6 +132,7 @@ class KanjiController extends AbstractController 'char' => $charStr, 'codepoint' => $codepoint, 'info' => $charInfo, + 'terms' => $terms, 'ref' => $ebookRef[$charStr] ?? [], 'jiten_href' => key_exists($charStr, $jiten) ? "https://kanji.jitenon.jp/kanji{$jiten[$charStr]}" diff --git a/src/Service/AnkiService.php b/src/Service/AnkiService.php index 365d621..ea0763c 100644 --- a/src/Service/AnkiService.php +++ b/src/Service/AnkiService.php @@ -5,6 +5,7 @@ namespace App\Service; use App\Entity\Note; use App\Entity\SentenceListeningNote; use App\Entity\SentenceNote; +use App\Entity\Term; use App\Entity\UnicodeNote; use App\Utils\Japanese; use Symfony\Contracts\HttpClient\HttpClientInterface; @@ -203,4 +204,24 @@ class AnkiService return $ret; } + + + /** @return Term[] */ + public function searchTerms(string $query): array + { + $ret = []; + + $ids = $this->request('findNotes', ['query' => "VocabKanji:*$query*"]); + $notes = $this->getNotes($ids); + + foreach ($notes as $note) { + if (!$note instanceof SentenceNote) continue; + + foreach ($note->getTerms() as $term) { + $ret[$term->getKanji()] = $term; + } + } + + return $ret; + } } diff --git a/src/Twig/Extension/AppExtension.php b/src/Twig/Extension/AppExtension.php index c73f93d..eab01da 100644 --- a/src/Twig/Extension/AppExtension.php +++ b/src/Twig/Extension/AppExtension.php @@ -3,6 +3,7 @@ namespace App\Twig\Extension; use App\Twig\Runtime\AppExtensionRuntime; +use App\Utils\Japanese; use Twig\Extension\AbstractExtension; use Twig\TwigFilter; use Twig\TwigFunction; @@ -16,6 +17,7 @@ class AppExtension extends AbstractExtension // parameter: ['is_safe' => ['html']] // Reference: https://twig.symfony.com/doc/3.x/advanced.html#automatic-escaping new TwigFilter('basename', basename(...)), + new TwigFilter('ruby', self::ankiRubyToHtml(...)), ]; } @@ -25,4 +27,13 @@ class AppExtension extends AbstractExtension //new TwigFunction('function_name', [AppExtensionRuntime::class, 'doSomething']), ]; } + + public static function ankiRubyToHtml(string $str): string + { + return preg_replace( + '/ ?([^ ><\[\n]+)\[([^\[]+)\]/', + '$1($2)', + $str + ); + } } diff --git a/templates/kanji/view.html.twig b/templates/kanji/view.html.twig index f7bc95b..64323b7 100644 --- a/templates/kanji/view.html.twig +++ b/templates/kanji/view.html.twig @@ -1,21 +1,20 @@
+
+
+ {{ codepoint }} +
{{ char }}
- -
-

Lists

-
    - {% for list in info|keys %} -
  • {{ list }}
  • - {% endfor %} -
-
+

Links

    @@ -92,6 +91,15 @@
+
+

Terms

+ {% for term in terms|slice(0,5) %} +
+
{{ term.toAnkiVocabDef|ruby|raw }}
+
+ {% endfor %} +
+

Appeareances

{% for refname, refcount in ref %}
{{ refname|basename }}: {{ refcount }}