def __addXmlText(xmlWriteSettings:XMLWriteSettings, htext:HText, w:HWriter): c_d = xmlWriteSettings.colorSchema["d"] if xmlWriteSettings.colorSchema else "" c_tn = xmlWriteSettings.colorSchema["tn"] if xmlWriteSettings.colorSchema else "" c_an = xmlWriteSettings.colorSchema["an"] if xmlWriteSettings.colorSchema else "" c_av = xmlWriteSettings.colorSchema["av"] if xmlWriteSettings.colorSchema else "" c_r = xmlWriteSettings.colorSchema["r"] if xmlWriteSettings.colorSchema else "" c_t = xmlWriteSettings.colorSchema["t"] if xmlWriteSettings.colorSchema else "" text = htext.text if not text: return if xmlWriteSettings.textEncoding == EnumXMLTextOutputEncoding.AlwaysAsIs: w.write( c_t, text.replace("\n", "\n" + c_t), c_r ) elif xmlWriteSettings.textEncoding == EnumXMLTextOutputEncoding.EncodeReservedCharsAsEntities: sb = [ c_t ] for c in text: if c == "\n": sb.append("\n" + c_t) elif c == "\"": sb.append(""") elif c == "&": sb.append("&") elif c == "<": sb.append("<") elif c == ">": sb.append(">") else: sb.append(c) sb.append(c_r) w.write(*sb) elif xmlWriteSettings.textEncoding == EnumXMLTextOutputEncoding.OnReservedCharsOutputTextAsCData: if ((text.find("&") >= 0) or (text.find("\"") >= 0) or (text.find(">") >= 0) or (text.find("<") >= 0)): w.write( c_t, "<![CDATA[" ) if text.find("<![CDATA[") >= 0: raise Exception("Text may not contain \"<![CDATA[\"! Recursive CDATA-definitions are not allowed!") w.write( text.replace("\n", "\n" + c_t), "]]>", c_r) else: w.write( c_t, text.replace("\n", "\n" + c_t), c_r ) else: raise ImplementationErrorException("(None)" if xmlWriteSettings is None else str(xmlWriteSettings.textEncoding))
def __addXmlSimple(xmlWriteSettings:XMLWriteSettings, e:HElement, bParentIsMixedContent:bool, w:HWriter): if not e.children: HToolkit_Write_XML.__writeXmlOpeningClosingTag(xmlWriteSettings, e, w) w.writeLn() else: if e.children.hasTexts: HToolkit_Write_XML.__writeXmlOpeningTag(xmlWriteSettings, e, w) for eChild in e.children: if isinstance(eChild, HElement): HToolkit_Write_XML.__addXmlSimple(xmlWriteSettings, eChild, True, w) elif isinstance(eChild, HText): HToolkit_Write_XML.__addXmlText(xmlWriteSettings, eChild, w) else: raise ImplementationErrorException("(None)" if eChild is None else str(type(eChild))) HToolkit_Write_XML.__writeXmlClosingTag(xmlWriteSettings, e, w) if not bParentIsMixedContent: w.writeLn() else: HToolkit_Write_XML.__writeXmlOpeningTag(xmlWriteSettings, e, w) w.writeLn() for eChild in e.children: if isinstance(eChild, HElement): HToolkit_Write_XML.__addXmlSimple(xmlWriteSettings, eChild, False, w) else: raise ImplementationErrorException("(None)" if eChild is None else str(type(eChild))) HToolkit_Write_XML.__writeXmlClosingTag(xmlWriteSettings, e, w) w.writeLn()
def _serialize(self, w: HWriter): if self.texts1: for text in self.texts1: w.lineBreak() w.write(text) w.lineBreak() for text in self.texts2: w.write(text)
def __writeXmlClosingTag(xmlWriteSettings:XMLWriteSettings, e:HElement, w:HWriter): c_d = xmlWriteSettings.colorSchema["d"] if xmlWriteSettings.colorSchema else "" c_tn = xmlWriteSettings.colorSchema["tn"] if xmlWriteSettings.colorSchema else "" c_an = xmlWriteSettings.colorSchema["an"] if xmlWriteSettings.colorSchema else "" c_av = xmlWriteSettings.colorSchema["av"] if xmlWriteSettings.colorSchema else "" c_r = xmlWriteSettings.colorSchema["r"] if xmlWriteSettings.colorSchema else "" w.write( c_d, "</", c_tn, e.name, c_d, ">", c_r )
def __addXmlAttributeValue(xmlWriteSettings:XMLWriteSettings, text:str, w:HWriter): c_d = xmlWriteSettings.colorSchema["d"] if xmlWriteSettings.colorSchema else "" c_tn = xmlWriteSettings.colorSchema["tn"] if xmlWriteSettings.colorSchema else "" c_an = xmlWriteSettings.colorSchema["an"] if xmlWriteSettings.colorSchema else "" c_av = xmlWriteSettings.colorSchema["av"] if xmlWriteSettings.colorSchema else "" c_r = xmlWriteSettings.colorSchema["r"] if xmlWriteSettings.colorSchema else "" c_t = xmlWriteSettings.colorSchema["t"] if xmlWriteSettings.colorSchema else "" if not text: return if xmlWriteSettings.attributeEncoding == EnumXMLTextOutputEncoding.AlwaysAsIs: w.write( c_av, text, c_r, ) elif xmlWriteSettings.attributeEncoding == EnumXMLTextOutputEncoding.EncodeReservedCharsAsEntities: sb = [ c_av ] for c in text: if c == "\"": sb.append(""") elif c == "&": sb.append("&") elif c == "<": sb.append("<") elif c == ">": sb.append(">") else: sb.append(c) sb.append(c_r) w.write(*sb) elif xmlWriteSettings.attributeEncoding == EnumXMLTextOutputEncoding.OnReservedCharsOutputTextAsCData: raise Exception("Attributes are not allowed to contain CData!") else: raise ImplementationErrorException("(None)" if xmlWriteSettings is None else str(xmlWriteSettings.attributeEncoding))
def __writeXmlOpeningClosingTag(xmlWriteSettings:XMLWriteSettings, e:HElement, w:HWriter): c_d = xmlWriteSettings.colorSchema["d"] if xmlWriteSettings.colorSchema else "" c_tn = xmlWriteSettings.colorSchema["tn"] if xmlWriteSettings.colorSchema else "" c_an = xmlWriteSettings.colorSchema["an"] if xmlWriteSettings.colorSchema else "" c_av = xmlWriteSettings.colorSchema["av"] if xmlWriteSettings.colorSchema else "" c_r = xmlWriteSettings.colorSchema["r"] if xmlWriteSettings.colorSchema else "" w.write( c_d, "<", c_tn, e.name, c_r ) if len(e.attributes) > 0: for a in e.attributes: w.write( " ", c_an, a.name, c_d, "=", "\"", c_r ) if ((a.value is not None) and (len(a.value) > 0)): HToolkit_Write_XML.__addXmlAttributeValue(xmlWriteSettings, a.value, w) w.write( c_d, "\"", c_r ) w.write( c_d, "/>", c_r )
def __addPlainTextSimple(e: HElement, w: HWriter): for ea in e.children: if isinstance(ea, HText): w.writeLn(ea.text) elif isinstance(ea, HElement): HToolkit_Write_PlainText.__addPlainTextSimple(ea, w)
def __writeHElement(e: HElement, w: HWriter): w.writeLn("<<<" + e.name + ">>>") if e.attributes: w.incrementIndent() for a in sorted(e.attributes, key=lambda x: x.name): HToolkit_Write_Dump.__writeAttribute(a, w) w.decrementIndent() if e.children: w.incrementIndent() for ea in e.children: if isinstance(ea, HText): w.writeLn(HToolkit_Write_Dump.__toText(ea.text)) elif isinstance(ea, HElement): HToolkit_Write_Dump.__writeHElement(ea, w) w.decrementIndent()
def __writeAttribute(a: HAttribute, w: HWriter): if a.value: w.writeLn("--- " + a.name + "=" + HToolkit_Write_Dump.__toText(a.value)) else: w.writeLn("--- " + a.name)
def _serialize(self, w: HWriter): if self._proto.bLineBreakOuter: w.lineBreak() w.write(self._openingTagData()) if self._proto.bHasClosingTag: w.incrementIndent() if self._proto.bLineBreakInner: if self.children: w.lineBreak() for child in self.children: if isinstance(child, (int, float, str)): w.write(htmlEscape(str(child))) else: child._serialize(w) w.lineBreak() else: for child in self.children: if isinstance(child, (int, float, str)): w.write(htmlEscape(str(child))) else: child._serialize(w) w.decrementIndent() w.write(self._closingTagData()) else: if len(self.children) > 0: raise Exception("HTML tag \"" + self.name + "\" is not allowed to have child elements!") if self._proto.bLineBreakOuter: w.lineBreak()
def __str__(self): w = HWriter() self._serialize(w) return str(w)
def __addXmlPretty(xmlWriteSettings:XMLWriteSettings, e:HElement, bForceInline:bool, w:HWriter): if bForceInline: HToolkit_Write_XML.__writeXmlOpeningTag(xmlWriteSettings, e, w) for eChild in e.children: if isinstance(eChild, HElement): HToolkit_Write_XML.__addXmlPretty(xmlWriteSettings, "", eChild, True, w) else: if isinstance(eChild, HText): HToolkit_Write_XML.__addXmlText(xmlWriteSettings, eChild, w) else: raise ImplementationErrorException("(None)" if eChild is None else str(type(eChild))) HToolkit_Write_XML.__writeXmlClosingTag(xmlWriteSettings, e, w) return if len(e.children) == 0: HToolkit_Write_XML.__writeXmlOpeningClosingTag(xmlWriteSettings, e, w) w.writeLn() else: if e.hasOnlyTexts: HToolkit_Write_XML.__writeXmlOpeningTag(xmlWriteSettings, e, w) for eChild in e.children: if isinstance(eChild, HText): HToolkit_Write_XML.__addXmlText(xmlWriteSettings, eChild, w) else: raise ImplementationErrorException("(None)" if eChild is None else str(type(eChild))) HToolkit_Write_XML.__writeXmlClosingTag(xmlWriteSettings, e, w) w.writeLn() else: HToolkit_Write_XML.__writeXmlOpeningTag(xmlWriteSettings, e, w) w.writeLn() w.incrementIndent() for eChild in e.children: if isinstance(eChild, HElement): if bForceInline: HToolkit_Write_XML.__addXmlPretty(xmlWriteSettings, eChild, True, w) else: bForceInlineNew = False if xmlWriteSettings.checkInlineOverride is None else xmlWriteSettings.checkInlineOverride(e, eChild) if bForceInlineNew: HToolkit_Write_XML.__addXmlPretty(xmlWriteSettings, eChild, bForceInlineNew, w) w.writeLn() else: HToolkit_Write_XML.__addXmlPretty(xmlWriteSettings, eChild, False, w) else: if isinstance(eChild, HText): HToolkit_Write_XML.__addXmlText(xmlWriteSettings, eChild, w) w.writeLn() else: raise ImplementationErrorException("(None)" if eChild is None else str(type(eChild))) w.decrementIndent() HToolkit_Write_XML.__writeXmlClosingTag(xmlWriteSettings, e, w) w.writeLn()
def _writeAsXML(root:HElement, xmlWriteSettings:XMLWriteSettings, specialElements2:list, specialElements:list, w:HWriter): if xmlWriteSettings.writeXmlHeader: w.write(xmlWriteSettings.colorSchema["m"] if xmlWriteSettings.colorSchema else "", "<?xml version=\"1.0\" encoding=\"UTF-8\"?>", Console.RESET) if xmlWriteSettings.printStyle != EnumXMLPrintStyle.SingleLine: w.writeLn() if specialElements2 is not None: for specialElement in specialElements2: w.write(xmlWriteSettings.colorSchema["s"] if xmlWriteSettings.colorSchema else "", specialElement, Console.RESET) if xmlWriteSettings.printStyle != EnumXMLPrintStyle.SingleLine: w.writeLn() if specialElements is not None: for specialElement in specialElements: HToolkit_Write_XML.__writeXmlSpecialTag(specialElement, w) if xmlWriteSettings.printStyle != EnumXMLPrintStyle.SingleLine: w.writeLn() if xmlWriteSettings.printStyle == EnumXMLPrintStyle.Pretty: if xmlWriteSettings.checkInlineOverride is not None: if xmlWriteSettings.checkInlineOverride(None, root): HToolkit_Write_XML.__addXmlPretty(xmlWriteSettings, root, True, w) else: HToolkit_Write_XML.__addXmlPretty(xmlWriteSettings, root, False, w) else: HToolkit_Write_XML.__addXmlPretty(xmlWriteSettings, root, False, w) elif xmlWriteSettings.printStyle == EnumXMLPrintStyle.Simple: HToolkit_Write_XML.__addXmlSimple(xmlWriteSettings, root, False, w) elif xmlWriteSettings.printStyle == EnumXMLPrintStyle.SingleLine: HToolkit_Write_XML.__addXmlSingleLine(xmlWriteSettings, root, w) w.writeLn() else: raise ImplementationErrorException("(None)" if xmlWriteSettings is None else str(xmlWriteSettings.printStyle))
def _serialize(self, w: HWriter): w.write(self._openingTagData()) if self._proto.bHasClosingTag: w.incrementIndent() bRequireExtraCharsetTag = not self.__hasMetaTagWithCharset() if self.children or bRequireExtraCharsetTag: w.lineBreak() if bRequireExtraCharsetTag: w.writeLn("<meta charset=\"UTF-8\">") for child in self.children: if isinstance(child, (int, float, str)): w.write(htmlEscape(str(child))) else: child._serialize(w) w.lineBreak() w.decrementIndent() w.write(self._closingTagData()) else: if len(self.children) > 0: raise Exception("HTML tag \"" + self.name + "\" is not allowed to have child elements!") w.lineBreak()
def _serialize(self, w: HWriter): if self.texts1: w.lineBreak() w.write("<!-- ") if self.texts1: for text in self.texts1: w.lineBreak() w.writeLn(text.replace("-->", "-->")) for text in self.texts2: w.write(text.replace("-->", "-->")) if self.texts1: w.lineBreak() w.write("-->") if self.texts1: w.lineBreak()
def _serialize(self, w: HWriter): if self.texts: w.lineBreak() w.writeLn("<style type=\"text/css\">") w.incrementIndent() for text in self.texts: w.writeLn(text) w.decrementIndent() w.writeLn("</style>")