much cleaner way to handle "last component in multiword example does not have a space"
This commit is contained in:
parent
9f116f3452
commit
4cc3c2cdc6
|
@ -1,6 +1,11 @@
|
||||||
from model.data import Data
|
from model.data import Data
|
||||||
|
|
||||||
|
|
||||||
class ComponentLexeme(Data):
|
class ComponentLexeme(Data):
|
||||||
|
# special value for last component that evaluates to True by default but is not
|
||||||
|
# exported to true
|
||||||
|
LAST_COMPONENT_SPACE = "last component does not have space"
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.other_attributes = {}
|
self.other_attributes = {}
|
||||||
self.text = ""
|
self.text = ""
|
||||||
|
@ -25,7 +30,7 @@ class ComponentLexeme(Data):
|
||||||
def isValid(self):
|
def isValid(self):
|
||||||
return len(self.text) > 0
|
return len(self.text) > 0
|
||||||
|
|
||||||
def export(self, doc, is_multiword, last_component):
|
def export(self, doc):
|
||||||
if self.role is None:
|
if self.role is None:
|
||||||
return doc.createTextNode(self.text)
|
return doc.createTextNode(self.text)
|
||||||
|
|
||||||
|
@ -33,9 +38,8 @@ class ComponentLexeme(Data):
|
||||||
result.setAttribute("role", self.role)
|
result.setAttribute("role", self.role)
|
||||||
result.textContent = self.text
|
result.textContent = self.text
|
||||||
|
|
||||||
# no space is only allowed in in multiword examples that are not last component
|
if self.no_space and self.no_space != self.LAST_COMPONENT_SPACE:
|
||||||
if self.no_space is True and is_multiword and not last_component:
|
result.setAttribute("space", "false")
|
||||||
result.setAttribute("space", "false")
|
|
||||||
|
|
||||||
for key, value in self.other_attributes.items():
|
for key, value in self.other_attributes.items():
|
||||||
result.setAttribute(key, value)
|
result.setAttribute(key, value)
|
||||||
|
|
|
@ -17,6 +17,12 @@ class Example(Data):
|
||||||
self.components = []
|
self.components = []
|
||||||
self.edited = False
|
self.edited = False
|
||||||
|
|
||||||
|
# removes space from last component if multiword example
|
||||||
|
def check_multiword_components(self):
|
||||||
|
if self.is_multiword():
|
||||||
|
self.components[len(self.components) - 1].no_space = ComponentLexeme.LAST_COMPONENT_SPACE
|
||||||
|
|
||||||
|
|
||||||
def import_xml(self, example_xml):
|
def import_xml(self, example_xml):
|
||||||
self.translations = from_container_list(example_xml.querySelectorAll("translationContainer"))
|
self.translations = from_container_list(example_xml.querySelectorAll("translationContainer"))
|
||||||
|
|
||||||
|
@ -38,14 +44,17 @@ class Example(Data):
|
||||||
if comp.isValid():
|
if comp.isValid():
|
||||||
self.components.append(comp)
|
self.components.append(comp)
|
||||||
|
|
||||||
|
self.check_multiword_components()
|
||||||
|
|
||||||
def export(self, doc):
|
def export(self, doc):
|
||||||
|
self.check_multiword_components()
|
||||||
|
|
||||||
result = doc.createElement("exampleContainer")
|
result = doc.createElement("exampleContainer")
|
||||||
|
|
||||||
inner = self.inner.export(doc, self.edited)
|
inner = self.inner.export(doc, self.edited)
|
||||||
# TODO: bad quick fix
|
# TODO: bad quick fix
|
||||||
for idx, comp in enumerate(self.components):
|
for comp in self.components:
|
||||||
inner.appendChild(comp.export(
|
inner.appendChild(comp.export(doc))
|
||||||
doc, self.is_multiword(), idx == len(self.components) - 1))
|
|
||||||
|
|
||||||
result.appendChild(inner)
|
result.appendChild(inner)
|
||||||
return result
|
return result
|
||||||
|
|
Loading…
Reference in New Issue
Block a user