From da30e65c2959568ac1d99721d56ec653642df99e Mon Sep 17 00:00:00 2001 From: David Jablonski Date: Mon, 20 Apr 2015 22:47:15 +0200 Subject: [PATCH 1/2] 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): From 89f2e8426b23243b3f3a5ff83347ab968aefd017 Mon Sep 17 00:00:00 2001 From: David Jablonski Date: Tue, 21 Apr 2015 06:47:16 +0200 Subject: [PATCH 2/2] use of python std functions Former-commit-id: b914354d3226c57eb0cb3ecf27a8037425c6cae9 --- yomi_base/reader_util.py | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/yomi_base/reader_util.py b/yomi_base/reader_util.py index 4788d12..68ad7d1 100644 --- a/yomi_base/reader_util.py +++ b/yomi_base/reader_util.py @@ -78,20 +78,13 @@ def findSentence(content, position): quoteStack.pop() elif c in quotesFwd: quoteStack.insert(0, quotesFwd[c]) - 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() + translationStart = content.find('\t',end) + if translationStart >= 0: + translationEnd = content.find('\n',translationStart) + if translationEnd == -1: + translationEnd = len(content) + translation = content[translationStart+1:translationEnd].strip() return content[start:end].strip(), translation @@ -121,6 +114,7 @@ def markupVocabExp(definition): 'reading': definition['reading'] or unicode(), 'glossary': definition['glossary'], 'sentence': definition.get('sentence'), + 'translation': definition.get('translation'), 'summary': summary } @@ -132,6 +126,7 @@ def markupVocabReading(definition): 'reading': unicode(), 'glossary': definition['glossary'], 'sentence': definition.get('sentence'), + 'translation': definition.get('translation'), 'summary': definition['reading'] }