Do reference parsing and orthography translation on backend for public view

This commit is contained in:
Robbie Antenesse 2019-07-07 00:00:32 -06:00 committed by Robbie Antenesse
parent d36eec52fa
commit 850b042d6b
3 changed files with 35 additions and 67 deletions

View File

@ -4,7 +4,6 @@ import { getWordsStats, getHomonymnNumber } from './utilities';
import { getMatchingSearchWords, highlightSearchTerm, getSearchFilters, getSearchTerm } from './search'; import { getMatchingSearchWords, highlightSearchTerm, getSearchFilters, getSearchTerm } from './search';
import { showSection } from './displayToggles'; import { showSection } from './displayToggles';
import { setupSearchFilters, setupInfoModal } from './setupListeners'; import { setupSearchFilters, setupInfoModal } from './setupListeners';
import { parseReferences, translateOrthography } from './wordManagement';
import { renderAd } from '../ads'; import { renderAd } from '../ads';
import { sortWords } from './wordManagement'; import { sortWords } from './wordManagement';
@ -35,7 +34,7 @@ export function renderName() {
} }
export function renderDescription() { export function renderDescription() {
const descriptionHTML = md(removeTags(window.currentDictionary.description)); const descriptionHTML = md(window.currentDictionary.description);
document.getElementById('detailsPanel').innerHTML = '<div class="content">' + descriptionHTML + '</div>'; document.getElementById('detailsPanel').innerHTML = '<div class="content">' + descriptionHTML + '</div>';
} }
@ -161,7 +160,7 @@ export function renderWords() {
pronunciation: removeTags(originalWord.pronunciation), pronunciation: removeTags(originalWord.pronunciation),
partOfSpeech: removeTags(originalWord.partOfSpeech), partOfSpeech: removeTags(originalWord.partOfSpeech),
definition: removeTags(originalWord.definition), definition: removeTags(originalWord.definition),
details: parseReferences(removeTags(originalWord.details)), details: originalWord.details,
wordId: originalWord.wordId, wordId: originalWord.wordId,
}); });
@ -170,11 +169,9 @@ export function renderWords() {
wordsHTML += renderAd(displayIndex); wordsHTML += renderAd(displayIndex);
let wordNameDisplay = translateOrthography(word.name);
wordsHTML += `<article class="entry" id="${word.wordId}"> wordsHTML += `<article class="entry" id="${word.wordId}">
<header> <header>
<h4 class="word"><span class="orthographic-translation">${wordNameDisplay}</span>${homonymnNumber > 0 ? ' <sub>' + homonymnNumber.toString() + '</sub>' : ''}</h4> <h4 class="word"><span class="orthographic-translation">${word.name}</span>${homonymnNumber > 0 ? ' <sub>' + homonymnNumber.toString() + '</sub>' : ''}</h4>
<span class="pronunciation">${word.pronunciation}</span> <span class="pronunciation">${word.pronunciation}</span>
<span class="part-of-speech">${word.partOfSpeech}</span> <span class="part-of-speech">${word.partOfSpeech}</span>
<a href="${shareLink}" target="_blank" class="small button word-option-button" title="Link to Word">&#10150;</a> <a href="${shareLink}" target="_blank" class="small button word-option-button" title="Link to Word">&#10150;</a>

View File

@ -1,4 +1,3 @@
import { wordExists, getHomonymnIndexes } from "./utilities";
import removeDiacritics from "../StackOverflow/removeDiacritics"; import removeDiacritics from "../StackOverflow/removeDiacritics";
export function sortWords() { export function sortWords() {
@ -10,56 +9,3 @@ export function sortWords() {
return removeDiacritics(wordA[sortBy]).toLowerCase() > removeDiacritics(wordB[sortBy]).toLowerCase() ? 1 : -1; return removeDiacritics(wordA[sortBy]).toLowerCase() > removeDiacritics(wordB[sortBy]).toLowerCase() ? 1 : -1;
}); });
} }
export function translateOrthography(word) {
window.currentDictionary.details.orthography.translations.forEach(translation => {
translation = translation.split('=').map(value => value.trim());
if (translation.length > 1 && translation[0] !== '' && translation[1] !== '') {
word = word.replace(new RegExp(translation[0], 'g'), translation[1]);
}
});
return word;
}
export function parseReferences(detailsMarkdown) {
const references = detailsMarkdown.match(/\{\{.+?\}\}/g);
if (references && Array.isArray(references)) {
new Set(references).forEach(reference => {
let wordToFind = reference.replace(/\{\{|\}\}/g, '');
let homonymn = 0;
if (wordToFind.includes(':')) {
const separator = wordToFind.indexOf(':');
homonymn = wordToFind.substr(separator + 1);
wordToFind = wordToFind.substring(0, separator);
if (homonymn && homonymn.trim()
&& !isNaN(parseInt(homonymn.trim())) && parseInt(homonymn.trim()) > 0) {
homonymn = parseInt(homonymn.trim());
} else {
homonymn = false;
}
}
let existingWordId = false;
const homonymnIndexes = getHomonymnIndexes({ name: wordToFind, wordId: -1 });
if (homonymn !== false && homonymn > 0) {
if (typeof homonymnIndexes[homonymn - 1] !== 'undefined') {
existingWordId = window.currentDictionary.words[homonymnIndexes[homonymn - 1]].wordId;
}
} else if (homonymn !== false) {
existingWordId = wordExists(wordToFind, true);
}
if (existingWordId !== false) {
if (homonymn < 1 && homonymnIndexes.length > 0) {
homonymn = 1;
}
const homonymnSubHTML = homonymn > 0 ? '<sub>' + homonymn.toString() + '</sub>' : '';
const wordMarkdownLink = `<span class="word-reference">[<span class="orthographic-translation">${translateOrthography(wordToFind)}</span>${homonymnSubHTML}](#${existingWordId})</span>`;
detailsMarkdown = detailsMarkdown.replace(new RegExp(reference, 'g'), wordMarkdownLink);
}
});
}
return detailsMarkdown;
}

View File

@ -101,7 +101,7 @@ VALUES ($new_id, ?, ?, ?, ?, ?)";
'externalID' => $result['id'], 'externalID' => $result['id'],
'name' => $result['name'], 'name' => $result['name'],
'specification' => $result['specification'], 'specification' => $result['specification'],
'description' => $result['description'], 'description' => $this->parseReferences(strip_tags($result['description']), $result['id']),
'createdBy' => $result['public_name'], 'createdBy' => $result['public_name'],
'partsOfSpeech' => explode(',', $partsOfSpeech), 'partsOfSpeech' => explode(',', $partsOfSpeech),
'alphabeticalOrder' => array(), 'alphabeticalOrder' => array(),
@ -147,11 +147,11 @@ VALUES ($new_id, ?, ?, ?, ?, ?)";
if ($results) { if ($results) {
return array_map(function ($row) use ($dictionary) { return array_map(function ($row) use ($dictionary) {
return array( return array(
'name' => $row['name'], 'name' => $this->translateOrthography($row['name'], $dictionary),
'pronunciation' => $row['pronunciation'], 'pronunciation' => $row['pronunciation'],
'partOfSpeech' => $row['part_of_speech'], 'partOfSpeech' => $row['part_of_speech'],
'definition' => $row['definition'], 'definition' => $row['definition'],
'details' => $this->parseReferences($row['details'], $dictionary), 'details' => $this->parseReferences(strip_tags($row['details']), $dictionary),
'lastUpdated' => is_null($row['last_updated']) ? intval($row['created_on']) : intval($row['last_updated']), 'lastUpdated' => is_null($row['last_updated']) ? intval($row['created_on']) : intval($row['last_updated']),
'createdOn' => intval($row['created_on']), 'createdOn' => intval($row['created_on']),
'wordId' => intval($row['word_id']), 'wordId' => intval($row['word_id']),
@ -168,11 +168,11 @@ VALUES ($new_id, ?, ?, ?, ?, ?)";
$result = $this->db->query($query, array($dictionary, $word))->fetch(); $result = $this->db->query($query, array($dictionary, $word))->fetch();
if ($result) { if ($result) {
return array( return array(
'name' => $result['name'], 'name' => $this->translateOrthography($result['name'], $dictionary),
'pronunciation' => $result['pronunciation'], 'pronunciation' => $result['pronunciation'],
'partOfSpeech' => $result['part_of_speech'], 'partOfSpeech' => $result['part_of_speech'],
'definition' => $result['definition'], 'definition' => $result['definition'],
'details' => $this->parseReferences($result['details'], $dictionary), 'details' => $this->parseReferences(strip_tags($result['details']), $dictionary),
'lastUpdated' => is_null($result['last_updated']) ? intval($result['created_on']) : intval($result['last_updated']), 'lastUpdated' => is_null($result['last_updated']) ? intval($result['created_on']) : intval($result['last_updated']),
'createdOn' => intval($result['created_on']), 'createdOn' => intval($result['created_on']),
'wordId' => intval($result['word_id']), 'wordId' => intval($result['word_id']),
@ -219,8 +219,9 @@ VALUES ($new_id, ?, ?, ?, ?, ?)";
} }
$homonymn_sub_html = $homonymn > 0 ? '<sub>' . $homonymn . '</sub>' : ''; $homonymn_sub_html = $homonymn > 0 ? '<sub>' . $homonymn . '</sub>' : '';
$site_root = substr($_SERVER['REQUEST_URI'], 0, strpos($_SERVER['REQUEST_URI'], $dictionary_id)); $site_root = substr($_SERVER['REQUEST_URI'], 0, strpos($_SERVER['REQUEST_URI'], $dictionary_id));
$markdown_link = '<a href="' . $site_root . $dictionary_id . '/' . $target_id .'" target="_blank" title="Link to Reference">' $markdown_link = '<span class="word-reference"><a href="' . $site_root . $dictionary_id . '/' . $target_id .'" target="_blank" title="Link to Reference">'
. $word_to_find . $homonymn_sub_html . '</a>'; . '<span class="orthographic-translation">' . $this->translateOrthography($word_to_find, $dictionary_id) . '</span>' . $homonymn_sub_html
. '</a></span>';
$details = str_replace($reference, $markdown_link, $details); $details = str_replace($reference, $markdown_link, $details);
} }
} }
@ -243,6 +244,30 @@ VALUES ($new_id, ?, ?, ?, ?, ?)";
return array(); return array();
} }
private function translateOrthography($word, $dictionary) {
if (!isset($this->translations)) {
$this->translations = $this->getTranslations($dictionary);
}
foreach($this->translations as $translation) {
$translation = array_map('trim', explode('=', $translation));
if (count($translation) > 1 && $translation[0] !== '' && $translation[1] !== '') {
$word = str_replace($translation[0], $translation[1], $word);
}
};
return $word;
}
private function getTranslations($dictionary) {
if (is_numeric($dictionary)) {
$query = "SELECT translations FROM dictionary_linguistics WHERE dictionary=?";
$result = $this->db->query($query, array($dictionary))->fetch();
if ($result) {
return explode(PHP_EOL, $result['translations']);
}
}
return array();
}
public function getDetails ($user, $dictionary) { public function getDetails ($user, $dictionary) {
$query = "SELECT * FROM dictionaries JOIN dictionary_linguistics ON dictionary = id WHERE user=$user AND id=$dictionary"; $query = "SELECT * FROM dictionaries JOIN dictionary_linguistics ON dictionary = id WHERE user=$user AND id=$dictionary";
$result = $this->db->query($query)->fetch(); $result = $this->db->query($query)->fetch();