feat: Separate highlightedKanji extraction & expose it
This commit is contained in:
parent
cbaac88644
commit
8c1613187a
|
@ -77,26 +77,16 @@ 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) {
|
||||
foreach ($note->getHighlightedKanji() as $highlighedKanji) {
|
||||
$term = new Term();
|
||||
$term->kanji = mb_trim($match[2]);
|
||||
$term->kanji = $highlighedKanji;
|
||||
$term->definitionEn = null;
|
||||
$term->definitionJp = null;
|
||||
$note->terms[] = $term;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Set to null whatever is null
|
||||
$readings = array_map(
|
||||
|
@ -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 [
|
||||
|
|
Loading…
Reference in New Issue