New editor written in Python dialect
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

339 lines
12 KiB

1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
  1. from browser import document
  2. from model.tags import export_tag
  3. from model.translation import Translation
  4. def export_to_xml(model):
  5. xml_document = export_entry(model.entry)
  6. serializer = __new__(XMLSerializer())
  7. return serializer.serializeToString(xml_document)
  8. def export_entry(entry):
  9. parser = __new__(DOMParser())
  10. doc = parser.parseFromString("<entry />", "text/xml")
  11. entry_xml = doc.firstChild
  12. # create head
  13. head = doc.createElement("head")
  14. entry_xml.appendChild(head)
  15. status = doc.createElement("status")
  16. status.textContent = entry.status
  17. head.appendChild(status)
  18. headword = doc.createElement("headword")
  19. headword_lemma = doc.createElement("lemma")
  20. # headword_lemma = entry.original_xml.querySelector("head headword lemma")
  21. headword_lemma.textContent = entry.headword
  22. if entry.headword_type is not None:
  23. headword_lemma.setAttribute("type", entry.headword_type)
  24. if entry.headword_audio is not None:
  25. headword_lemma.setAttribute("audio", entry.headword_audio)
  26. headword.appendChild(headword_lemma)
  27. head.appendChild(headword)
  28. homonymy = doc.createElement("homonymy")
  29. headword.appendChild(homonymy)
  30. for hFeature in entry.homonymy:
  31. feature = doc.createElement("homonymyFeature")
  32. feature.textContent = hFeature.value
  33. # Can't use hFeature.name, because Python has name reserver and so it becomes py_name in JS
  34. feature.setAttribute("name", hFeature["name"])
  35. homonymy.appendChild(feature)
  36. # if({}) works uncorrectly in transcrypt
  37. if len(entry.lexical_unit) > 0 and len(entry.lexical_unit['lexemes']) > 0:
  38. lexunit = doc.createElement("lexicalUnit")
  39. lexunit.setAttribute("id", entry.lexical_unit["id"])
  40. lexunit.setAttribute("type", entry.lexical_unit['type'])
  41. for lexeme in entry.lexical_unit["lexemes"]:
  42. lexeme_xml = doc.createElement("lexeme")
  43. lexeme_xml.setAttribute("lexical_unit_lexeme_id", lexeme["id"])
  44. lexeme_xml.textContent = lexeme["text"]
  45. if len(entry.lexical_unit["lexemes"] > 1):
  46. component = doc.createElement('component')
  47. component.appendChild(lexeme_xml)
  48. lexunit.appendChild(component)
  49. else:
  50. lexunit.appendChild(lexeme_xml)
  51. head.appendChild(lexunit)
  52. grammar = doc.createElement("grammar")
  53. grammar_category = doc.createElement("category")
  54. grammar_category.textContent = entry.grammar
  55. grammar.appendChild(grammar_category)
  56. head.appendChild(grammar)
  57. if len(entry.measure) > 0:
  58. measure_list = doc.createElement("measureList")
  59. measure = doc.createElement("measure")
  60. measure.setAttribute("source", entry.measure["source"])
  61. measure.setAttribute("type", entry.measure["type"])
  62. measure.textContent = entry.measure["text"]
  63. measure_list.appendChild(measure)
  64. head.appendChild(measure_list)
  65. variants = doc.createElement("variantList")
  66. head.appendChild(variants)
  67. for v in entry.variants:
  68. variant = doc.createElement("variant")
  69. variant.textContent = v
  70. variants.appendChild(variant)
  71. relist = doc.createElement("relatedEntryList")
  72. head.appendChild(relist)
  73. for re in entry.related_entries:
  74. relateEntry = doc.createElement("relatedEntry")
  75. relateEntry.textContent = re
  76. relist.appendChild(relateEntry)
  77. head.appendChild(_export_label_list(doc, entry.labels))
  78. comment = doc.createElement("comment")
  79. comment.textContent = entry.comment
  80. head.appendChild(comment)
  81. # now lets do body
  82. body = doc.createElement("body")
  83. entry_xml.appendChild(body)
  84. sense_list = doc.createElement("senseList")
  85. body.appendChild(sense_list)
  86. for sense in entry.senses:
  87. sense_list.appendChild(export_sense(doc, sense))
  88. return doc
  89. def export_sense(doc, sense):
  90. sense_xml = doc.createElement("sense")
  91. sense_xml.appendChild(_export_label_list(doc, sense.labels))
  92. if sense.id is not None:
  93. sense_xml.setAttribute("id", sense.id)
  94. definition_list = doc.createElement("definitionList")
  95. sense_xml.appendChild(definition_list)
  96. for definition in sense.definitions:
  97. definition_xml = doc.createElement("definition")
  98. definition_xml.textContent = definition["value"]
  99. definition_xml.setAttribute("type", definition["type"])
  100. definition_list.appendChild(definition_xml)
  101. translation_container_list = doc.createElement("translationContainerList")
  102. export_translation_list(doc, sense, translation_container_list)
  103. sense_xml.appendChild(translation_container_list)
  104. example_container_list = doc.createElement("exampleContainerList")
  105. sense_xml.appendChild(example_container_list)
  106. for example in sense.examples:
  107. example_container = example.export(doc)
  108. translation_container_list = doc.createElement("translationContainerList")
  109. export_translation_list(doc, example, translation_container_list)
  110. example_container.appendChild(translation_container_list)
  111. example_container_list.appendChild(example_container)
  112. return sense_xml
  113. def export_translation_list(doc, py_parent, xml_parent):
  114. for cidx, cluster in enumerate(py_parent.translations):
  115. if len(cluster) > 0:
  116. for translation in cluster:
  117. translation_container = export_translation(doc, translation)
  118. translation_container.setAttribute("cluster", str(cidx + 1))
  119. xml_parent.appendChild(translation_container)
  120. def export_translation(doc, translation):
  121. translation_xml = doc.createElement("translationContainer")
  122. translation_xml.appendChild(_export_label_list(doc, translation.tags))
  123. actual_t = doc.createElement("translation")
  124. actual_t.textContent = translation.translation
  125. actual_t.setAttribute("targetLang", translation.targetLang)
  126. if translation.audio:
  127. actual_t.setAttribute("audio", translation.audio)
  128. if translation.source:
  129. actual_t.setAttribute("source", translation.source)
  130. translation_xml.appendChild(actual_t)
  131. if len(translation.explanationList) > 0 :
  132. explanationList = _export_explanation_list(doc, translation.explanationList)
  133. translation_xml.appendChild(explanationList)
  134. return translation_xml
  135. def _export_explanation_list(doc, lst):
  136. result = doc.createElement('explanationList')
  137. for explanation in lst:
  138. result.appendChild(explanation.export(doc))
  139. return result
  140. def _export_label_list(doc, lst):
  141. result = doc.createElement("labelList")
  142. for key, value in lst:
  143. key, value = export_tag(key, value)
  144. label_el = doc.createElement("label")
  145. label_el.textContent = value
  146. label_el.setAttribute('type', key)
  147. result.appendChild(label_el)
  148. return result
  149. def _original_xml_query_selector(selector, entry, doc, parent_selector = selector.rsplit(' ', 1)[0]):
  150. query = entry.original_xml.querySelector(selector)
  151. if query is None:
  152. query = doc.createElement(selector.rsplit(' ', 1)[1])
  153. entry.original_xml.querySelector(parent_selector).appendChild(query)
  154. return query
  155. def export_example_to_entry_xml(example, other_examples = None):
  156. parser = __new__(DOMParser())
  157. doc = parser.parseFromString("<entry />", "text/xml")
  158. entry_xml = doc.firstChild
  159. head = doc.createElement("head")
  160. entry_xml.appendChild(head)
  161. status = doc.createElement("status")
  162. head.appendChild(status)
  163. headword = doc.createElement("headword")
  164. head.appendChild(headword)
  165. lemma = doc.createElement("lemma")
  166. lemma.textContent = " ".join(comp.text for comp in example.components)
  167. lemma.setAttribute("type", "compound")
  168. if example.inner.other_attributes['audio'] is not None:
  169. lemma.setAttribute('audio', example.inner.other_attributes['audio'])
  170. headword.appendChild(lemma)
  171. homonymy = doc.createElement("homonymy")
  172. headword.appendChild(homonymy)
  173. lexical_unit = doc.createElement("lexicalUnit")
  174. lexical_unit.setAttribute("type", "MWE")
  175. head.appendChild(lexical_unit)
  176. if example.inner.other_attributes['structure_id'] != None and len(example.components) <= 3:
  177. lexical_unit.setAttribute("id", example.inner.other_attributes['structure_id'])
  178. for comp in example.components:
  179. lexeme = doc.createElement("lexeme")
  180. lexeme.textContent = comp.text
  181. comp_xml = doc.createElement("component")
  182. lexical_unit.appendChild(comp_xml)
  183. comp_xml.appendChild(lexeme)
  184. grammar = doc.createElement("grammar")
  185. category = doc.createElement("category")
  186. grammar.appendChild(category)
  187. head.appendChild(grammar)
  188. variant_list = doc.createElement("variantList")
  189. head.appendChild(variant_list)
  190. related_entry_list = doc.createElement("relatedEntryList")
  191. head.appendChild(related_entry_list)
  192. label_list = doc.createElement("labelList")
  193. head.appendChild(label_list)
  194. comment = doc.createElement("comment")
  195. head.appendChild(comment)
  196. body = doc.createElement("body")
  197. entry_xml.appendChild(body)
  198. sense_list = doc.createElement("senseList")
  199. body.appendChild(sense_list)
  200. sense = doc.createElement("sense")
  201. sense_list.appendChild(sense)
  202. sense_label_list = doc.createElement("labelList")
  203. sense.appendChild(sense_label_list)
  204. first_translation = example.translations[0][0] if len(example.translations) > 0 and len(example.translations[0]) > 0 else Translation()
  205. first_translation_is_valid = False
  206. translation_label_list = doc.createElement("labelList")
  207. # Add labels to sense if label value isn't kontrastivno or približek else keep them in translation
  208. for key, value in first_translation.tags:
  209. key, value = export_tag(key, value)
  210. label_el = doc.createElement("label")
  211. label_list = translation_label_list if value == "kontrastivno" or value == "približek" else sense_label_list
  212. label_el.textContent = value
  213. label_el.setAttribute('type', key)
  214. label_list.appendChild(label_el)
  215. # Set definition as explanation if explanation in slo
  216. definition_list = doc.createElement("definitionList")
  217. sense.appendChild(definition_list)
  218. for explanation in first_translation.explanationList:
  219. if explanation.language == "slo":
  220. definition = doc.createElement("definition")
  221. definition.setAttribute("type", "indicator")
  222. definition.textContent = explanation.value
  223. definition_list.appendChild(definition)
  224. first_translation.explanationList.remove(explanation)
  225. translation_container_list = doc.createElement("translationContainerList")
  226. sense.appendChild(translation_container_list)
  227. translation_container = doc.createElement("translationContainer")
  228. if len(translation_label_list) > 0:
  229. translation_container.appendChild(translation_label_list)
  230. if first_translation.translation is not "":
  231. translation = doc.createElement("translation")
  232. translation_container.appendChild(translation)
  233. translation.textContent = first_translation.translation
  234. translation.setAttribute("targetLang", first_translation.targetLang)
  235. if first_translation.audio:
  236. translation.setAttribute("audio", first_translation.audio)
  237. if first_translation.source:
  238. translation.setAttribute("source", first_translation.source)
  239. first_translation_is_valid = True
  240. if len(first_translation.explanationList) > 0 :
  241. explanation_list = _export_explanation_list(doc, first_translation.explanationList)
  242. translation_container.appendChild(explanation_list)
  243. first_translation_is_valid = True
  244. if first_translation_is_valid:
  245. translation_container_list.appendChild(translation_container)
  246. example.translations[0] = example.translations[0][1:] if len(example.translations) > 0 and len(example.translations[0]) > 0 else example.translations[0]
  247. export_translation_list(doc, example, translation_container_list)
  248. if other_examples is not None:
  249. example_container_list = doc.createElement("exampleContainerList")
  250. sense.appendChild(example_container_list)
  251. for example in other_examples:
  252. example_container = example.export(doc)
  253. translation_container_list = doc.createElement("translationContainerList")
  254. export_translation_list(doc, example, translation_container_list)
  255. example_container.appendChild(translation_container_list)
  256. example_container_list.appendChild(example_container)
  257. return doc