mt-export-example-as-entry - v4.1 #9

Merged
matic_t merged 6 commits from mt-export-example-as-entry into master 2020-10-12 08:35:03 +00:00
5 changed files with 158 additions and 5 deletions
Showing only changes of commit eea25b9968 - Show all commits

View File

@ -190,3 +190,137 @@ def _export_label_list(doc, lst):
label_el.setAttribute('type', key)
result.appendChild(label_el)
return result
def _original_xml_query_selector(selector, entry, doc, parent_selector = selector.rsplit(' ', 1)[0]):
query = entry.original_xml.querySelector(selector)
if query is None:
query = doc.createElement(selector.rsplit(' ', 1)[1])
entry.original_xml.querySelector(parent_selector).appendChild(query)
return query
def export_example_to_entry_xml(example):
parser = __new__(DOMParser())
doc = parser.parseFromString("<entry />", "text/xml")
entry_xml = doc.firstChild
head = doc.createElement("head")
entry_xml.appendChild(head)
status = doc.createElement("status")
head.appendChild(status)
headword = doc.createElement("headword")
head.appendChild(headword)
lemma = doc.createElement("lemma")
lemma.textContent = " ".join(comp.text for comp in example.components)
lemma.setAttribute("type", "compound")
headword.appendChild(lemma)
homonymy = doc.createElement("homonymy")
headword.appendChild(homonymy)
lexical_unit = doc.createElement("lexicalUnit")
lexical_unit.setAttribute("type", "MWE")
head.appendChild(lexical_unit)
if example.inner.other_attributes['structure_id'] != None:
lexical_unit.setAttribute("structure_id", example.inner.other_attributes['structure_id'])
for comp in example.components:
comp_xml = doc.createElement("component")
lexeme = doc.createElement("lexeme")
lexeme.textContent = comp.text
comp_xml.appendChild(lexeme)
lexical_unit.appendChild(comp_xml)
grammar = doc.createElement("grammar")
category = doc.createElement("category")
grammar.appendChild(category)
head.appendChild(grammar)
variant_list = doc.createElement("variantList")
head.appendChild(variant_list)
related_entry_list = doc.createElement("relatedEntryList")
head.appendChild(related_entry_list)
label_list = doc.createElement("labelList")
head.appendChild(label_list)
comment = doc.createElement("comment")
head.appendChild(comment)
body = doc.createElement("body")
entry_xml.appendChild(body)
sense_list = doc.createElement("senseList")
body.appendChild(sense_list)
sense = doc.createElement("sense")
sense_list.appendChild(sense)
sense_label_list = doc.createElement("labelList")
sense.appendChild(sense_label_list)
first_translation = example.translations[0][0]
translation_label_list = doc.createElement("labelList")
# Add labels to sense if label value isn't kontrastivno or približek else keep them in translation
for key, value in first_translation.tags:
key, value = export_tag(key, value)
label_el = doc.createElement("label")
label_list = translation_label_list if value == "kontrastivno" or value == "približek" else sense_label_list
# if idx >= 0:
# label_el = first_translation.original_xml.querySelectorAll("labelList label")[idx].cloneNode(True)
label_el.textContent = value
label_el.setAttribute('type', key)
label_list.appendChild(label_el)
# Set definition as explanation if explanation in slo
definition_list = doc.createElement("definitionList")
sense.appendChild(definition_list)
for explanation in first_translation.explanationList:
if explanation.language == "slo":
definition = doc.createElement("definition")
definition.setAttribute("type", "indicator")
definition.textContent = explanation.value
definition_list.appendChild(definition)
first_translation.explanationList.remove(explanation)
translation_container_list = doc.createElement("translationContainerList")
sense.appendChild(translation_container_list)
translation_container = doc.createElement("translationContainer")
translation_container_list.appendChild(translation_container)
if len(translation_label_list) > 0:
translation_container.appendChild(translation_label_list)
# translation = first_translation.original_xml.querySelector("translation").cloneNode(True) if first_translation.original_xml != None else doc.createElement("translation")
translation = doc.createElement("translation")
translation_container.appendChild(translation)
translation.textContent = first_translation.translation
translation.setAttribute("targetLang", first_translation.targetLang)
if first_translation.audio:
translation.setAttribute("audio", first_translation.audio)
if first_translation.source:
translation.setAttribute("source", first_translation.source)
if len(first_translation.explanationList) > 0 :
_export_explanation_list(doc, first_translation.explanationList, translation)
example.translations[0] = example.translations[0][1:]
export_translation_list(doc, example, translation_container_list)
return doc
# for comp in example.components:
# if comp.role == "collocate":
# self.headword = comp.role
# break
# self.headword = example.

View File

@ -4,7 +4,7 @@ from message.show_messages import ShowEntryLabelsEdit, ShowEditTranslation, Show
from message.simple_edits import EditSenseLabel, EditSenseDefinition, EditComment, AddSenseLabel, AddSense, AddExampleTranslation, DoChosenExamples, AddToLabelList, AddToGenericList, EditVariants, EditHomonymy, EditRelatedEntries, EditEntryLabels, ExampleClusterEdit, ExampleClusterAdd
from message.show_menu import ShowTranslationMenu, ShowSenseMenu, ShowExampleMenu
from message.sense_edit import SenseMoveUp, SenseMoveDown, SenseBin, AddMultiwordExample
from message.example_edit import ExampleMoveUp, ExampleMoveDown, ExampleBin, ExampleRoleChange, ExampleComponentSpace, ExampleComponentAdd, ExampleComponentRemove, EditExampleText, ToggleExamples, ToggleClusters
from message.example_edit import ExampleAsNewEntry, ExampleMoveUp, ExampleMoveDown, ExampleBin, ExampleRoleChange, ExampleComponentSpace, ExampleComponentAdd, ExampleComponentRemove, EditExampleText, ToggleExamples, ToggleClusters
from message.delete_messages import DeleteComment, DeleteVariants, DeleteHomonymy, DeleteRelatedEntries, DeleteEntryLabels
from message.ske_messages import ShowSkeModal, SearchInSkeModal, SkeInsert

View File

@ -3,6 +3,10 @@ from message.simple_messages import DataChgClickMessage, ClickMessage, NoReset
from message.message import Message
from model.example import Example, ComponentLexeme
from model.sense import Sense
from model.entry import Entry
from model.model import Model
import lib.screenful as screenful
from export import export_to_xml, export_example_to_entry_xml
@ -13,6 +17,17 @@ def _get_example_idx(example, model):
return (sense, eidx)
class ExampleAsNewEntry(ClickMessage):
def update_model(self, model):
example = self.get_arg(0, Example)
entry_xml = export_example_to_entry_xml(example)
new_entry = Entry()
new_entry.import_xml(entry_xml)
new_model = Model()
new_model.entry = new_entry
screenful.screenful().Editor['new'](None, export_to_xml(new_model))
class ExampleMoveUp(DataChgClickMessage):
def update_model(self, model):
example = self.get_arg(0, Example)
@ -60,7 +75,7 @@ class EditExampleText(Message):
if example.newly_created:
example.newly_created = False
sense.examples.append(example)
idx = 0
for txt in document.getElementsByClassName("example-component-text"):
example.components[idx].text = txt.value

View File

@ -37,7 +37,6 @@ class Sense(Data):
def merge_labels(self):
return ", ".join(val for _, val in self.labels)
def view(self, model, sense_num):
examples = [example.view(model, self) for example in self.examples]

View File

@ -98,11 +98,16 @@ class View:
elif type(menu_target) is Example:
example = menu_target
sense = example_sense(example, entry)
return h("span.popup-menu", { "style": style }, [
dom_children = [
h("button.shyButton", { "on": {"click": msg(ShowExampleEdit, example, sense)}}, ""),
h("button.shyButton", { "on": {"click": msg(ExampleMoveUp, example)}}, ""),
h("button.shyButton", { "on": {"click": msg(ExampleMoveDown, example)}}, ""),
h("button.shyButton", { "on": {"click": msg(ExampleBin, example)}}, "🗑")])
h("button.shyButton", { "on": {"click": msg(ExampleBin, example)}}, "🗑")]
if example.is_multiword():
dom_children.insert(1, h("button.shyButton", { "on": {"click": msg(ExampleAsNewEntry, example, sense)}}, "As new Entry"))
return h("span.popup-menu", { "style": style }, dom_children)
else:
console.log("Should not be heree!!")