aboutsummaryrefslogtreecommitdiff
path: root/cordova/node_modules/sax/examples/pretty-print.js
diff options
context:
space:
mode:
authorKumar Priyansh <[email protected]>2019-01-19 12:37:14 +0530
committerKumar Priyansh <[email protected]>2019-01-19 12:37:14 +0530
commitdcdfc94cb39dfe2c39925a0145ffa45e2d061c30 (patch)
tree4f6379d955555b298c0e7b83a67e264240ee5614 /cordova/node_modules/sax/examples/pretty-print.js
parent76f7b3678d3f1ff99c3935a774d420453b0c3cb9 (diff)
downloadWeatherApp-dcdfc94cb39dfe2c39925a0145ffa45e2d061c30.tar.xz
WeatherApp-dcdfc94cb39dfe2c39925a0145ffa45e2d061c30.zip
Initial Upload via GIT
Diffstat (limited to 'cordova/node_modules/sax/examples/pretty-print.js')
-rwxr-xr-xcordova/node_modules/sax/examples/pretty-print.js74
1 files changed, 74 insertions, 0 deletions
diff --git a/cordova/node_modules/sax/examples/pretty-print.js b/cordova/node_modules/sax/examples/pretty-print.js
new file mode 100755
index 0000000..cd6aca9
--- /dev/null
+++ b/cordova/node_modules/sax/examples/pretty-print.js
@@ -0,0 +1,74 @@
+var sax = require("../lib/sax")
+ , printer = sax.createStream(false, {lowercasetags:true, trim:true})
+ , fs = require("fs")
+
+function entity (str) {
+ return str.replace('"', '&quot;')
+}
+
+printer.tabstop = 2
+printer.level = 0
+printer.indent = function () {
+ print("\n")
+ for (var i = this.level; i > 0; i --) {
+ for (var j = this.tabstop; j > 0; j --) {
+ print(" ")
+ }
+ }
+}
+printer.on("opentag", function (tag) {
+ this.indent()
+ this.level ++
+ print("<"+tag.name)
+ for (var i in tag.attributes) {
+ print(" "+i+"=\""+entity(tag.attributes[i])+"\"")
+ }
+ print(">")
+})
+
+printer.on("text", ontext)
+printer.on("doctype", ontext)
+function ontext (text) {
+ this.indent()
+ print(text)
+}
+
+printer.on("closetag", function (tag) {
+ this.level --
+ this.indent()
+ print("</"+tag+">")
+})
+
+printer.on("cdata", function (data) {
+ this.indent()
+ print("<![CDATA["+data+"]]>")
+})
+
+printer.on("comment", function (comment) {
+ this.indent()
+ print("<!--"+comment+"-->")
+})
+
+printer.on("error", function (error) {
+ console.error(error)
+ throw error
+})
+
+if (!process.argv[2]) {
+ throw new Error("Please provide an xml file to prettify\n"+
+ "TODO: read from stdin or take a file")
+}
+var xmlfile = require("path").join(process.cwd(), process.argv[2])
+var fstr = fs.createReadStream(xmlfile, { encoding: "utf8" })
+
+function print (c) {
+ if (!process.stdout.write(c)) {
+ fstr.pause()
+ }
+}
+
+process.stdout.on("drain", function () {
+ fstr.resume()
+})
+
+fstr.pipe(printer)