Render etymology in public view

This commit is contained in:
Robbie Antenesse 2020-05-03 23:02:33 -06:00
parent 40feae7194
commit 4d3132e151
2 changed files with 61 additions and 39 deletions

View File

@ -182,6 +182,8 @@ export function renderWords() {
partOfSpeech: removeTags(originalWord.partOfSpeech),
definition: removeTags(originalWord.definition),
details: originalWord.details,
etymology: typeof originalWord.etymology === 'undefined' || originalWord.etymology.length < 1 ? null
: originalWord.etymology.join(', '),
wordId: originalWord.wordId,
});
@ -202,6 +204,11 @@ export function renderWords() {
<dd class="details">
${md(word.details)}
</dd>
${word.etymology === null ? '' : `<hr>
<dt>Etymology <small>(Root Word${originalWord.etymology.length !== 1 ? 's' : ''})</small></dt>
<dd class="etymology">
${md(word.etymology).replace(/<\/?p>/g, '')}
</dd>`}
</dl>
</article>`;
});

View File

@ -93,7 +93,11 @@ class PublicDictionary {
);
if (!is_null($row['etymology'])) {
$word['etymology'] = $row['etymology'];
if (strlen($row['etymology']) > 0) {
$word['etymology'] = array_map(function ($root) use($dictionary) {
return $this->getWordReferenceHTML(strip_tags($root), $dictionary);
}, explode(',', $row['etymology']));
}
}
return $word;
@ -125,7 +129,10 @@ class PublicDictionary {
private function getWordsAsEntered() {
if (!isset($this->original_words)) {
$query = "SELECT words.* FROM words JOIN dictionaries ON id = dictionary WHERE dictionary=? AND is_public=1";
$query = "SELECT words.*, words_advanced.etymology FROM words
LEFT JOIN words_advanced ON words_advanced.dictionary = words.dictionary AND words_advanced.word_id = words.word_id
JOIN dictionaries ON dictionaries.id = words.dictionary
WHERE words.dictionary=? AND is_public=1";
$this->original_words = $this->db->query($query, array($this->details['externalID']))->fetchAll();
}
return $this->original_words;
@ -189,6 +196,17 @@ class PublicDictionary {
if (preg_match_all('/\{\{.+?\}\}/', $details, $references) !== false) {
$references = array_unique($references[0]);
foreach($references as $reference) {
$reference_link = $this->getWordReferenceHTML($reference, $dictionary_id);
if ($reference_link !== $reference) {
$details = str_replace($reference, $markdown_link, $details);
}
}
}
return $details;
}
private function getWordReferenceHTML($reference, $dictionary_id) {
$word_to_find = preg_replace('/\{\{|\}\}/', '', $reference);
$homonymn = 0;
@ -221,16 +239,13 @@ class PublicDictionary {
}
$homonymn_sub_html = count($reference_ids) > 1 && $homonymn - 1 >= 0 ? '<sub>' . $homonymn . '</sub>' : '';
$site_root = substr($_SERVER['REQUEST_URI'], 0, strpos($_SERVER['REQUEST_URI'], $dictionary_id));
$markdown_link = '<span class="word-reference"><a href="' . $site_root . $dictionary_id . '/' . $target_id .'" target="_blank" title="Link to Reference">'
return '<span class="word-reference"><a href="' . $site_root . $dictionary_id . '/' . $target_id .'" target="_blank" title="Link to Reference">'
. '<span class="orthographic-translation">' . $this->translateOrthography($word_to_find, $dictionary_id) . '</span>' . $homonymn_sub_html
. '</a></span>';
$details = str_replace($reference, $markdown_link, $details);
}
}
}
}
return $details;
return $reference;
}
private function getWordIdsWithName($dictionary, $word_name) {