Cleanup of helper functions for card generation
Former-commit-id: 61748bf8d76245d7a11ed2377bc8375bd6ece256
This commit is contained in:
parent
c4da3eb9c4
commit
cabd0a8506
@ -224,11 +224,11 @@ class MainWindowReader(QtGui.QMainWindow, gen.reader_ui.Ui_MainWindowReader):
|
|||||||
|
|
||||||
|
|
||||||
def onActionCopyDefinition(self):
|
def onActionCopyDefinition(self):
|
||||||
reader_util.copyDefinitions(self.state.definitions[:1])
|
reader_util.copyVocabDefs(self.state.definitions[:1])
|
||||||
|
|
||||||
|
|
||||||
def onActionCopyAllDefinitions(self):
|
def onActionCopyAllDefinitions(self):
|
||||||
reader_util.copyDefinitions(self.state.definitions)
|
reader_util.copyVocabDefs(self.state.definitions)
|
||||||
|
|
||||||
|
|
||||||
def onActionCopySentence(self):
|
def onActionCopySentence(self):
|
||||||
@ -251,23 +251,14 @@ class MainWindowReader(QtGui.QMainWindow, gen.reader_ui.Ui_MainWindowReader):
|
|||||||
command, index = unicode(url.toString()).split(':')
|
command, index = unicode(url.toString()).split(':')
|
||||||
definition = self.state.definitions[int(index)]
|
definition = self.state.definitions[int(index)]
|
||||||
|
|
||||||
if command == 'addExpression':
|
if command == 'addVocabExp':
|
||||||
markup = reader_util.buildFactMarkupExpression(
|
markup = reader_util.markupVocabExp(definition)
|
||||||
definition['expression'],
|
|
||||||
definition['reading'],
|
|
||||||
definition['glossary'],
|
|
||||||
definition['sentence']
|
|
||||||
)
|
|
||||||
self.ankiAddFact('vocab', markup)
|
self.ankiAddFact('vocab', markup)
|
||||||
if command == 'addReading':
|
if command == 'addVocabReading':
|
||||||
markup = reader_util.buildFactMarkupReading(
|
markup = reader_util.markupVocabReading(definition)
|
||||||
definition['reading'],
|
|
||||||
definition['glossary'],
|
|
||||||
definition['sentence']
|
|
||||||
)
|
|
||||||
self.ankiAddFact('vocab', markup)
|
self.ankiAddFact('vocab', markup)
|
||||||
elif command == 'copyDefinition':
|
elif command == 'copyVocabDef':
|
||||||
reader_util.copyDefinitions([definition])
|
reader_util.copyVocabDefs([definition])
|
||||||
|
|
||||||
|
|
||||||
def onDefinitionSearchReturn(self):
|
def onDefinitionSearchReturn(self):
|
||||||
@ -329,7 +320,7 @@ class MainWindowReader(QtGui.QMainWindow, gen.reader_ui.Ui_MainWindowReader):
|
|||||||
|
|
||||||
content, encoding = reader_util.decodeContent(content)
|
content, encoding = reader_util.decodeContent(content)
|
||||||
if self.preferences['stripReadings']:
|
if self.preferences['stripReadings']:
|
||||||
content = reader_util.stripContentReadings(content)
|
content = reader_util.stripReadings(content)
|
||||||
|
|
||||||
self.textContent.setPlainText(content)
|
self.textContent.setPlainText(content)
|
||||||
if self.state.scanPosition > 0:
|
if self.state.scanPosition > 0:
|
||||||
@ -437,7 +428,7 @@ class MainWindowReader(QtGui.QMainWindow, gen.reader_ui.Ui_MainWindowReader):
|
|||||||
if profile is None:
|
if profile is None:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
fields = reader_util.replaceMarkupInFields(profile['fields'], markup)
|
fields = reader_util.formatFields(profile['fields'], markup)
|
||||||
tagsSplit = reader_util.splitTags(unicode(self.comboTags.currentText()))
|
tagsSplit = reader_util.splitTags(unicode(self.comboTags.currentText()))
|
||||||
tagsJoined = ' '.join(tagsSplit)
|
tagsJoined = ' '.join(tagsSplit)
|
||||||
|
|
||||||
@ -474,7 +465,7 @@ class MainWindowReader(QtGui.QMainWindow, gen.reader_ui.Ui_MainWindowReader):
|
|||||||
if profile is None:
|
if profile is None:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
fields = reader_util.replaceMarkupInFields(profile['fields'], markup)
|
fields = reader_util.formatFields(profile['fields'], markup)
|
||||||
return self.anki.canAddNote(profile['deck'], profile['model'], fields)
|
return self.anki.canAddNote(profile['deck'], profile['model'], fields)
|
||||||
|
|
||||||
|
|
||||||
@ -571,7 +562,7 @@ class MainWindowReader(QtGui.QMainWindow, gen.reader_ui.Ui_MainWindowReader):
|
|||||||
|
|
||||||
|
|
||||||
def updateDefinitions(self):
|
def updateDefinitions(self):
|
||||||
html = reader_util.buildDefinitionsHtml(self.state.definitions, self.ankiIsFactValid, 'vocab')
|
html = reader_util.buildVocabDefs(self.state.definitions, self.ankiIsFactValid)
|
||||||
self.textDefinitions.setHtml(html)
|
self.textDefinitions.setHtml(html)
|
||||||
|
|
||||||
|
|
||||||
|
@ -34,7 +34,7 @@ def decodeContent(content):
|
|||||||
return content.decode(encoding, 'replace'), encoding
|
return content.decode(encoding, 'replace'), encoding
|
||||||
|
|
||||||
|
|
||||||
def stripContentReadings(content):
|
def stripReadings(content):
|
||||||
return re.sub(u'《[^》]+》', unicode(), content)
|
return re.sub(u'《[^》]+》', unicode(), content)
|
||||||
|
|
||||||
|
|
||||||
@ -80,7 +80,7 @@ def findSentence(content, position):
|
|||||||
return content[start:end].strip()
|
return content[start:end].strip()
|
||||||
|
|
||||||
|
|
||||||
def replaceMarkupInFields(fields, markup):
|
def formatFields(fields, markup):
|
||||||
result = dict()
|
result = dict()
|
||||||
for field, value in fields.items():
|
for field, value in fields.items():
|
||||||
result[field] = value.format(**markup)
|
result[field] = value.format(**markup)
|
||||||
@ -88,29 +88,29 @@ def replaceMarkupInFields(fields, markup):
|
|||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
def buildFactMarkupExpression(expression, reading, glossary, sentence=None):
|
|
||||||
return {
|
|
||||||
'expression': expression,
|
|
||||||
'reading': reading,
|
|
||||||
'glossary': glossary,
|
|
||||||
'sentence': sentence
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
def buildFactMarkupReading(reading, glossary, sentence=None):
|
|
||||||
return {
|
|
||||||
'expression': reading,
|
|
||||||
'reading': unicode(),
|
|
||||||
'glossary': glossary,
|
|
||||||
'sentence': sentence
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
def splitTags(tags):
|
def splitTags(tags):
|
||||||
return filter(lambda tag: tag.strip(), re.split('[;,\s]', tags))
|
return filter(lambda tag: tag.strip(), re.split('[;,\s]', tags))
|
||||||
|
|
||||||
|
|
||||||
def copyDefinitions(definitions):
|
def markupVocabExp(definition):
|
||||||
|
return {
|
||||||
|
'expression': definition['expression'],
|
||||||
|
'reading': definition['reading'],
|
||||||
|
'glossary': definition['glossary'],
|
||||||
|
'sentence': definition.get('sentence')
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def markupVocabReading(definition):
|
||||||
|
return {
|
||||||
|
'expression': definition['reading'],
|
||||||
|
'reading': unicode(),
|
||||||
|
'glossary': definition['glossary'],
|
||||||
|
'sentence': definition.get('sentence')
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def copyVocabDefs(definitions):
|
||||||
text = unicode()
|
text = unicode()
|
||||||
|
|
||||||
for definition in definitions:
|
for definition in definitions:
|
||||||
@ -122,34 +122,34 @@ def copyDefinitions(definitions):
|
|||||||
QtGui.QApplication.clipboard().setText(text)
|
QtGui.QApplication.clipboard().setText(text)
|
||||||
|
|
||||||
|
|
||||||
def buildDefinitionHtml(definition, factIndex, factQuery, profile):
|
def buildVocabDef(definition, factIndex, factQuery):
|
||||||
reading = unicode()
|
reading = unicode()
|
||||||
if definition['reading']:
|
if definition['reading']:
|
||||||
reading = u'[{0}]'.format(definition['reading'])
|
reading = u'[{0}]'.format(definition['reading'])
|
||||||
|
|
||||||
conjugations = unicode()
|
rules = unicode()
|
||||||
if len(definition['rules']) > 0:
|
if len(definition['rules']) > 0:
|
||||||
conjugations = u' • '.join(definition['rules'])
|
rules = ' • '.join(definition['rules'])
|
||||||
conjugations = '<span class = "conjugations"><{0}><br/></span>'.format(conjugations)
|
rules = '<span class = "rules"><{0}><br/></span>'.format(rules)
|
||||||
|
|
||||||
links = '<a href = "copyDefinition:{0}"><img src = "://img/img/icon_copy_definition.png" align = "right"/></a>'.format(factIndex)
|
links = '<a href = "copyVocabDef:{0}"><img src = "://img/img/icon_copy_definition.png" align = "right"/></a>'.format(factIndex)
|
||||||
if factQuery:
|
if factQuery:
|
||||||
if factQuery(profile, buildFactMarkupExpression(definition['expression'], definition['reading'], definition['glossary'])):
|
if factQuery('vocab', markupVocabExp(definition)):
|
||||||
links += '<a href = "addExpression:{0}"><img src = "://img/img/icon_add_expression.png" align = "right"/></a>'.format(factIndex)
|
links += '<a href = "addVocabExp:{0}"><img src = "://img/img/icon_add_expression.png" align = "right"/></a>'.format(factIndex)
|
||||||
if factQuery(profile, buildFactMarkupReading(definition['reading'], definition['glossary'])):
|
if factQuery('vocab', markupVocabReading(definition)):
|
||||||
links += '<a href = "addReading:{0}"><img src = "://img/img/icon_add_reading.png" align = "right"/></a>'.format(factIndex)
|
links += '<a href = "addVocabReading:{0}"><img src = "://img/img/icon_add_reading.png" align = "right"/></a>'.format(factIndex)
|
||||||
|
|
||||||
html = u"""
|
html = u"""
|
||||||
<span class = "links">{0}</span>
|
<span class = "links">{0}</span>
|
||||||
<span class = "expression">{1} {2}<br/></span>
|
<span class = "expression">{1} {2}<br/></span>
|
||||||
<span class = "glossary">{3}<br/></span>
|
<span class = "glossary">{3}<br/></span>
|
||||||
<span class = "conjugations">{4}</span>
|
<span class = "rules">{4}</span>
|
||||||
<br clear = "all"/>""".format(links, definition['expression'], reading, definition['glossary'], conjugations)
|
<br clear = "all"/>""".format(links, definition['expression'], reading, definition['glossary'], rules)
|
||||||
|
|
||||||
return html
|
return html
|
||||||
|
|
||||||
|
|
||||||
def buildDefinitionsHtml(definitions, factQuery, profile):
|
def buildVocabDefs(definitions, factQuery):
|
||||||
palette = QtGui.QApplication.palette()
|
palette = QtGui.QApplication.palette()
|
||||||
toolTipBg = palette.color(QtGui.QPalette.Window).name()
|
toolTipBg = palette.color(QtGui.QPalette.Window).name()
|
||||||
toolTipFg = palette.color(QtGui.QPalette.WindowText).name()
|
toolTipFg = palette.color(QtGui.QPalette.WindowText).name()
|
||||||
@ -162,7 +162,7 @@ def buildDefinitionsHtml(definitions, factQuery, profile):
|
|||||||
|
|
||||||
if len(definitions) > 0:
|
if len(definitions) > 0:
|
||||||
for i, definition in enumerate(definitions):
|
for i, definition in enumerate(definitions):
|
||||||
html += buildDefinitionHtml(definition, i, factQuery, profile)
|
html += buildVocabDef(definition, i, factQuery)
|
||||||
else:
|
else:
|
||||||
html += """
|
html += """
|
||||||
<p>No definitions to display.</p>
|
<p>No definitions to display.</p>
|
||||||
|
Loading…
Reference in New Issue
Block a user