diff --git a/src/Entity/Note.php b/src/Entity/Note.php index 1d9dc9b..d16c7d4 100644 --- a/src/Entity/Note.php +++ b/src/Entity/Note.php @@ -77,24 +77,14 @@ class Note // Set VocabKanji field $note->terms = Term::fromNoteFields($note->fields); - // If not defined, find them from the highlighted parts in the sentence + // If unable to, create them from the highlighted parts in the sentence if (empty($note->terms)) { - // 1. Get all spans in the text - preg_match_all( - self::HIGHLIGHT_PATTERN, - $note->fields['SentKanji'], - $matches, - PREG_SET_ORDER, - ); - // 2. Check the ones that match with the kanji color - foreach ($matches as $match) { - if ($match[1] === self::HIGHLIGHT_ATTR_KANJI) { - $term = new Term(); - $term->kanji = mb_trim($match[2]); - $term->definitionEn = null; - $term->definitionJp = null; - $note->terms[] = $term; - } + foreach ($note->getHighlightedKanji() as $highlighedKanji) { + $term = new Term(); + $term->kanji = $highlighedKanji; + $term->definitionEn = null; + $term->definitionJp = null; + $note->terms[] = $term; } } @@ -116,6 +106,30 @@ class Note return $note; } + /** Return an array of strings with the highlighted kanji in the SentKanji */ + public function getHighlightedKanji(): array + { + $ret = []; + $matches = []; + + // 1. Get all spans in the text + preg_match_all( + self::HIGHLIGHT_PATTERN, + $this->fields['SentKanji'], + $matches, + PREG_SET_ORDER, + ); + + // 2. Check the ones that match with the kanji color + foreach ($matches as $match) { + if ($match[1] === self::HIGHLIGHT_ATTR_KANJI) { + $ret[] = mb_trim($match[2]); + } + } + + return $ret; + } + public function toAnki(): array { return [