Skip to content

Commit

Permalink
gts and npm update, eslint
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxim-Mazurok committed Oct 26, 2020
1 parent 51fdfe7 commit 388a75a
Show file tree
Hide file tree
Showing 68 changed files with 6,347 additions and 3,878 deletions.
3 changes: 0 additions & 3 deletions .clang-format

This file was deleted.

1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build/
3 changes: 3 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "./node_modules/gts/"
}
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
11.10.0
12.19.0
3 changes: 3 additions & 0 deletions .prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
...require('gts/.prettierrc.json')
}
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
[![License](https://img.shields.io/badge/license-ISC-brightgreen.svg)](https://github.com/Maxim-Mazurok/sax-ts/blob/master/LICENSE.md)
[![NPM](https://img.shields.io/npm/v/sax-ts.svg)](https://www.npmjs.com/package/sax-ts)
[![DenoLib](https://denolib.com/badge?scope=Maxim-Mazurok&repo=sax-ts)](https://github.com/denolib)
[Deno Third Party Modules as `sax_ts`](https://deno.land/x/sax_ts)

A [SAX](https://en.wikipedia.org/wiki/Simple_API_for_XML)-style parser for XML
and HTML.
Expand Down Expand Up @@ -61,7 +62,7 @@ Pass the following arguments to the parser function. All are optional.

`strict` - Boolean. Disabled "forgiving" mode. Default: `false`.

`options` - Object bag of settings regarding string formatting. All default to
`options` - Object bag of settings regarding string formatting. All default to
`false`.

Settings supported:
Expand Down Expand Up @@ -172,7 +173,7 @@ If you pass `noscript: true`, then this behavior is suppressed.
## What This Is (probably) Not

- An HTML Parser - That's a fine goal, but this isn't it. It's just XML.
- A DOM Builder - You can use it to build an object model out of XML, but it
- A DOM Builder - You can use it to build an object model out of XML, but it
does not do that out of the box.
- XSLT - No DOM = no querying.
- 100% Compliant with (some other SAX implementation) - Most SAX
Expand Down
19 changes: 16 additions & 3 deletions build/src/sax.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion build/src/sax.js.map

Large diffs are not rendered by default.

46 changes: 25 additions & 21 deletions examples/example.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,32 @@
var fs = require('fs'),
/* eslint-disable prefer-const */
/* eslint-disable no-unused-vars */
let fs = require('fs'),
util = require('util'),
path = require('path'),
xml = fs.readFileSync(path.join(__dirname, 'test.xml'), 'utf8'),
sax = require('../build/src/sax.js'),
strict = new sax.SAXParser(true),
loose = new sax.SAXParser(false, {trim: true}),
inspector = function (ev) { return function (data) {
console.error('%s %s %j', this.line + ':' + this.column, ev, data)
}}
inspector = function (ev) {
return function (data) {
console.error('%s %s %j', this.line + ':' + this.column, ev, data);
};
};

sax.EVENTS.forEach(function (ev) {
loose['on' + ev] = inspector(ev)
})
loose.onend = function () {
console.error('end')
console.error(loose)
}

// do this in random bits at a time to verify that it works.
(function () {
if (xml) {
var c = Math.ceil(Math.random() * 1000)
loose.write(xml.substr(0, c))
xml = xml.substr(c)
process.nextTick(arguments.callee)
} else loose.close()
})()
sax.EVENTS.forEach(ev => {
loose['on' + ev] = inspector(ev);
});
loose.onend = (function () {
console.error('end');
console.error(loose);
})(
// do this in random bits at a time to verify that it works.
function () {
if (xml) {
const c = Math.ceil(Math.random() * 1000);
loose.write(xml.substr(0, c));
xml = xml.substr(c);
process.nextTick(arguments.callee);
} else loose.close();
}
)();
113 changes: 58 additions & 55 deletions examples/get-products.js
Original file line number Diff line number Diff line change
@@ -1,58 +1,61 @@
// pull out /GeneralSearchResponse/categories/category/items/product tags
// the rest we don't care about.

var sax = require('../src/sax.js')
var fs = require('fs')
var path = require('path')
var xmlFile = path.resolve(__dirname, 'shopping.xml')
var util = require('util')
var http = require('http')

fs.readFile(xmlFile, function (er, d) {
http.createServer(function (req, res) {
if (er) throw er
var xmlstr = d.toString('utf8')

var parser = new sax.SAXParser(true)
var products = []
var product = null
var currentTag = null

parser.onclosetag = function (tagName) {
if (tagName === 'product') {
products.push(product)
currentTag = product = null
return
}
if (currentTag && currentTag.parent) {
var p = currentTag.parent
delete currentTag.parent
currentTag = p
}
}

parser.onopentag = function (tag) {
if (tag.name !== 'product' && !product) return
if (tag.name === 'product') {
product = tag
}
tag.parent = currentTag
tag.children = []
tag.parent && tag.parent.children.push(tag)
currentTag = tag
}

parser.ontext = function (text) {
if (currentTag) currentTag.children.push(text)
}

parser.onend = function () {
var out = util.inspect(products, false, 3, true)
res.writeHead(200, {'content-type': 'application/json'})
res.end('{"ok":true}')
// res.end(JSON.stringify(products))
}

parser.write(xmlstr).end()
}).listen(1337)
})
const sax = require('../src/sax.ts');
const fs = require('fs');
const path = require('path');
const xmlFile = path.resolve(__dirname, 'shopping.xml');
const util = require('util');
const http = require('http');

fs.readFile(xmlFile, (er, d) => {
http
.createServer((req, res) => {
if (er) throw er;
const xmlstr = d.toString('utf8');

const parser = new sax.SAXParser(true);
const products = [];
let product = null;
let currentTag = null;

parser.onclosetag = function (tagName) {
if (tagName === 'product') {
products.push(product);
currentTag = product = null;
return;
}
if (currentTag && currentTag.parent) {
const p = currentTag.parent;
delete currentTag.parent;
currentTag = p;
}
};

parser.onopentag = function (tag) {
if (tag.name !== 'product' && !product) return;
if (tag.name === 'product') {
product = tag;
}
tag.parent = currentTag;
tag.children = [];
tag.parent && tag.parent.children.push(tag);
currentTag = tag;
};

parser.ontext = function (text) {
if (currentTag) currentTag.children.push(text);
};

parser.onend = function () {
// eslint-disable-next-line no-unused-vars
const out = util.inspect(products, false, 3, true);
res.writeHead(200, {'content-type': 'application/json'});
res.end('{"ok":true}');
// res.end(JSON.stringify(products))
};

parser.write(xmlstr).end();
})
.listen(1337);
});
10 changes: 6 additions & 4 deletions examples/hello-world.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
require('http').createServer(function (req, res) {
res.writeHead(200, {'content-type': 'application/json'})
res.end(JSON.stringify({ok: true}))
}).listen(1337)
require('http')
.createServer((req, res) => {
res.writeHead(200, {'content-type': 'application/json'});
res.end(JSON.stringify({ok: true}));
})
.listen(1337);
96 changes: 49 additions & 47 deletions examples/pretty-print.js
Original file line number Diff line number Diff line change
@@ -1,74 +1,76 @@
var sax = require('../build/src/sax.js'),
const sax = require('../build/src/sax.js'),
printer = sax.createStream(false, {lowercasetags: true, trim: true}),
fs = require('fs')
fs = require('fs');

function entity (str) {
return str.replace('"', '"')
function entity(str) {
return str.replace('"', '"');
}

printer.tabstop = 2
printer.level = 0
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(' ')
print('\n');
for (let i = this.level; i > 0; i--) {
for (let 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]) + '"')
this.indent();
this.level++;
print('<' + tag.name);
for (const i in tag.attributes) {
print(' ' + i + '="' + entity(tag.attributes[i]) + '"');
}
print('>')
})
print('>');
});

printer.on('text', ontext)
printer.on('doctype', ontext)
function ontext (text) {
this.indent()
print(text)
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 + '>')
})
this.level--;
this.indent();
print('</' + tag + '>');
});

printer.on('cdata', function (data) {
this.indent()
print('<![CDATA[' + data + ']]>')
})
this.indent();
print('<![CDATA[' + data + ']]>');
});

printer.on('comment', function (comment) {
this.indent()
print('<!--' + comment + '-->')
})
this.indent();
print('<!--' + comment + '-->');
});

printer.on('error', function (error) {
console.error(error)
throw error
})
printer.on('error', 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')
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' })
const xmlfile = require('path').join(process.cwd(), process.argv[2]);
const fstr = fs.createReadStream(xmlfile, {encoding: 'utf8'});

function print (c) {
function print(c) {
if (!process.stdout.write(c)) {
fstr.pause()
fstr.pause();
}
}

process.stdout.on('drain', function () {
fstr.resume()
})
process.stdout.on('drain', () => {
fstr.resume();
});

fstr.pipe(printer)
fstr.pipe(printer);
Loading

0 comments on commit 388a75a

Please sign in to comment.