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.
lexonomy_custom_editor/src/view/utils.py

84 lines
2.9 KiB

from export import export_to_xml
from model.example_clusters import ExampleClusters
from browser import window
from lib.snabbdom import h
import message
from math import log10
def show_toggle_cluster_buttons(sense, example):
cls = example.get_cluster()
if cls is None:
return []
base_tag = "input.cluster-list-button"
result = []
for opt in ExampleClusters.get_list(sense, example):
tag = base_tag
if opt == cls:
tag += ".cluster-button-checked"
attrs = {"value": str(opt + 1), "type": "button"}
style = {"width": str(int(log10(opt + 1)) + 1) + "em"}
result.append(h(tag,
{"attrs": attrs, "style": style,
"on": {"click": message.msg(message.ExampleClusterEdit, example, opt)}}, []))
result.append(h(base_tag,
{"attrs": {"value": "+", "type": "button"},
"style": {"width": "1em"},
"on": {"click": message.msg(message.ExampleClusterAdd, example)}},
[]))
return result
def example_sense(example, entry):
for sense in entry.senses:
for ex in sense.examples:
if ex == example:
return sense
def clean_label(label):
return label.replace("-- ", "")
def check_export(model):
exported_string = prettify_xml(export_to_xml(model))
model.import_xml(exported_string)
exported_string_2 = prettify_xml(export_to_xml(model))
linenum = 1
for line1, line2 in zip(exported_string.split("\n"), exported_string_2.split("\n")):
if line1 != line2:
window.console.log("Error on line {}".format(linenum))
window.console.log(exported_string)
window.console.log(exported_string_2)
window.console.log("Error on line {}".format(linenum))
break
linenum += 1
window.console.log("OK")
def prettify_xml(source_xml):
xml_doc = __new__(DOMParser()).parseFromString(source_xml, 'application/xml');
xslt_doc = __new__(DOMParser()).parseFromString("".join([
'<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform">',
' <xsl:strip-space elements="*"/>',
' <xsl:template match="para[content-style][not(text())]">',
' <xsl:value-of select="normalize-space(.)"/>',
' </xsl:template>',
' <xsl:template match="node()|@*">',
' <xsl:copy><xsl:apply-templates select="node()|@*"/></xsl:copy>',
' </xsl:template>',
' <xsl:output indent="yes"/>',
'</xsl:stylesheet>']), 'application/xml')
xslt_processor = __new__(XSLTProcessor())
xslt_processor.importStylesheet(xslt_doc)
result_doc = xslt_processor.transformToDocument(xml_doc)
result_xml = __new__(XMLSerializer()).serializeToString(result_doc)
return result_xml