feat: Separate highlightedKanji extraction & expose it

This commit is contained in:
Dendy 2025-02-09 15:40:40 +09:00
parent cbaac88644
commit 8c1613187a
1 changed files with 31 additions and 17 deletions

View File

@ -77,26 +77,16 @@ class Note
// Set VocabKanji field // Set VocabKanji field
$note->terms = Term::fromNoteFields($note->fields); $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)) { if (empty($note->terms)) {
// 1. Get all spans in the text foreach ($note->getHighlightedKanji() as $highlighedKanji) {
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 = new Term();
$term->kanji = mb_trim($match[2]); $term->kanji = $highlighedKanji;
$term->definitionEn = null; $term->definitionEn = null;
$term->definitionJp = null; $term->definitionJp = null;
$note->terms[] = $term; $note->terms[] = $term;
} }
} }
}
// Set to null whatever is null // Set to null whatever is null
$readings = array_map( $readings = array_map(
@ -116,6 +106,30 @@ class Note
return $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 public function toAnki(): array
{ {
return [ return [