diff --git a/Backend-libXML/Parser.py b/Backend-libXML/Parser.py index 56b0d8b..0357cd8 100644 --- a/Backend-libXML/Parser.py +++ b/Backend-libXML/Parser.py @@ -1,7 +1,28 @@ -from lxml import etree +from lxml import etree, html from io import BytesIO +def convertHTML(source: str, sourceFrom: str): + htmlParser = html.HTMLParser(remove_comments=True, remove_blank_text=True) + xmlParser = etree.XMLParser(remove_comments=True, remove_blank_text=True) + + if sourceFrom == "xml": + xmldoc = etree.parse(BytesIO(source.encode("utf-8")), xmlParser) + return html.tostring(xmldoc, method="html", pretty_print=True).decode() + elif sourceFrom == "html": + htmldoc = html.parse(BytesIO(source.encode("utf-8")), htmlParser) + return etree.tostring(htmldoc, method="xml", pretty_print=True, doctype="", xml_declaration=True, encoding="utf-8").decode() + else: + return + + +def formatHTML(source: str, prettify: bool) -> str: + parser = html.HTMLParser(remove_blank_text=True, remove_comments=True, remove_pis=True) + htmlDoc = html.parse(BytesIO(source.encode("utf-8")),parser=parser) + if not prettify: + return html.tostring(htmlDoc).decode().replace("\n", "").replace("> ", ">") + return etree.tostring(htmlDoc, encoding='unicode', pretty_print=True) + def formatXML(source: str, prettify: bool) -> str: """Method used to format XML diff --git a/Backend-libXML/main.py b/Backend-libXML/main.py index 9155395..abb78f9 100644 --- a/Backend-libXML/main.py +++ b/Backend-libXML/main.py @@ -40,6 +40,12 @@ def process_xml(request: request, type: str) -> str: response_json['result'] = Parser.formatXML(data, True) elif (type == "minimize"): response_json['result'] = Parser.formatXML(data, False) + elif (type == "prettifyHtml"): + response_json['result'] = Parser.formatHTML(data, True) + elif (type == "minimizeHtml"): + response_json['result'] = Parser.formatHTML(data, False) + elif (type == "convertHTML"): + response_json['result'] = Parser.convertHTML(data, process) else: raise ValueError("Valid operation types are: xsd, xslt, xpath") @@ -79,5 +85,17 @@ def prettify(): def minimize(): return process_xml(request, "minimize") +@app.route("/html/prettify",methods=["POST"]) +def prettifyHtml(): + return process_xml(request, "prettifyHtml") + +@app.route("/html/minimize",methods=["POST"]) +def minimizeHtml(): + return process_xml(request, "minimizeHtml") + +@app.route("/html/convert",methods=["POST"]) +def XMLToHTMLConvertion(): + return process_xml(request, "convertHTML") + if __name__ == "__main__": app.run() \ No newline at end of file diff --git a/Frontend/src/assets/sampleHtml.html b/Frontend/src/assets/sampleHtml.html new file mode 100644 index 0000000..676a15d --- /dev/null +++ b/Frontend/src/assets/sampleHtml.html @@ -0,0 +1,14 @@ + + + + + + + Example Page + + +

Hello World!

+

That's paragraph

+
+ + \ No newline at end of file diff --git a/Frontend/src/components/common/InsertTemplateComponent.vue b/Frontend/src/components/common/InsertTemplateComponent.vue index d80c240..6548764 100644 --- a/Frontend/src/components/common/InsertTemplateComponent.vue +++ b/Frontend/src/components/common/InsertTemplateComponent.vue @@ -4,7 +4,7 @@ import sampleXML from "@/assets/sampleXml.xml?raw" import sampleXSLT from "@/assets/sampleXslt.xml?raw" import sampleXSD from "@/assets/sampleXsd.xml?raw" import sampleXQuery from "@/assets/sampleXQuery.xquery?raw" - +import sampleHTML from "@assets/sampleHtml.html?raw" const props = defineProps( { @@ -32,7 +32,9 @@ function setDefault() { case "xquery": emit(emitName, sampleXQuery) break; - + case "html": + emit(emitName,sampleHTML); + break; default: emit(emitName, sampleXML) break; diff --git a/Frontend/src/components/formatter/HtmlButtonFormatterComponent.vue b/Frontend/src/components/formatter/HtmlButtonFormatterComponent.vue new file mode 100644 index 0000000..3a64814 --- /dev/null +++ b/Frontend/src/components/formatter/HtmlButtonFormatterComponent.vue @@ -0,0 +1,54 @@ + + + \ No newline at end of file diff --git a/Frontend/src/router/index.ts b/Frontend/src/router/index.ts index 99858a0..a4af1bd 100644 --- a/Frontend/src/router/index.ts +++ b/Frontend/src/router/index.ts @@ -5,6 +5,7 @@ const restMock = import("@views/RestMockView.vue") const jsonFormatter = import("@views/JsonFormatterView.vue") const xmlFormatter = import("@views/XmlFormatterView.vue") +const HtmlFormatterView = import("@views/HtmlFormatterView.vue") const xsltTool = import("@views/XSLTView.vue") const xsdTool = import("@views/XSDView.vue") @@ -27,6 +28,11 @@ const routes = [ name: 'jsonFormatter', component: () => jsonFormatter }, + { + path: '/format/html', + name: 'htmlFormatter', + component: () => HtmlFormatterView + }, { path: '/xml/xpath', name: 'xpath', diff --git a/Frontend/src/views/HtmlFormatterView.vue b/Frontend/src/views/HtmlFormatterView.vue new file mode 100644 index 0000000..4e16a32 --- /dev/null +++ b/Frontend/src/views/HtmlFormatterView.vue @@ -0,0 +1,34 @@ + + + \ No newline at end of file