From da30e65c2959568ac1d99721d56ec653642df99e Mon Sep 17 00:00:00 2001 From: David Jablonski Date: Mon, 20 Apr 2015 22:47:15 +0200 Subject: [PATCH] adding support for tab-separated translations Former-commit-id: a51c49dad09ddee57401b0510392ab2e4af64c21 --- yomi_base/preferences.py | 2 +- yomi_base/reader.py | 3 ++- yomi_base/reader_util.py | 17 +++++++++++++++-- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/yomi_base/preferences.py b/yomi_base/preferences.py index 7a1c76c..0c97298 100644 --- a/yomi_base/preferences.py +++ b/yomi_base/preferences.py @@ -105,7 +105,7 @@ class DialogPreferences(QtGui.QDialog, gen.preferences_ui.Ui_DialogPreferences): self.comboBoxModel.blockSignals(False) allowedTags = { - 'vocab': ['expression', 'reading', 'glossary', 'sentence'], + 'vocab': ['expression', 'reading', 'glossary', 'sentence','translation'], 'kanji': ['character', 'onyomi', 'kunyomi', 'glossary'], }[name] diff --git a/yomi_base/reader.py b/yomi_base/reader.py index 2fd1c69..8e56778 100644 --- a/yomi_base/reader.py +++ b/yomi_base/reader.py @@ -507,9 +507,10 @@ class MainWindowReader(QtGui.QMainWindow, gen.reader_ui.Ui_MainWindowReader): lengthMatched = 0 if self.dockVocab.isVisible(): self.state.vocabDefs, lengthMatched = self.language.findTerm(contentSampleFlat) - sentence = reader_util.findSentence(content, samplePosStart) + sentence, translation = reader_util.findSentence(content, samplePosStart) for definition in self.state.vocabDefs: definition['sentence'] = sentence + definition['translation'] = translation self.updateVocabDefs() if self.dockKanji.isVisible(): diff --git a/yomi_base/reader_util.py b/yomi_base/reader_util.py index 014263f..4788d12 100644 --- a/yomi_base/reader_util.py +++ b/yomi_base/reader_util.py @@ -78,8 +78,21 @@ def findSentence(content, position): quoteStack.pop() elif c in quotesFwd: quoteStack.insert(0, quotesFwd[c]) - - return content[start:end].strip() + cend = len(content) + translation_start = 0 + for i in xrange(end, cend): + if content[i] == '\t': + translation_start = i+1 + break + translation_end = cend + for i in xrange(translation_start, cend): + if content[i] == '\n': + translation_end = i + break + translation = '' + if translation_start > 0: + translation = content[translation_start:translation_end].strip() + return content[start:end].strip(), translation def formatFields(fields, markup):