npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@arijs/stream-xml-parser

v0.2.10

Published

## Testing

Downloads

254

Readme

@AriJS / StreamXMLParser

Testing

$ npm run test -- notPrettyXmlStream

$ npm run test -- notPrettyXml

$ npm run test -- testHtml

$ npm run test -- selfClose

$ npm run test -- treeBuilder

Snabbdom

Parse a HTML string to a tree of VNodes and use Snabbdom to patch a DOM element

// Note below that you need to provide functions
// to decode and encode html entities
// (ex: < to &lt;, " to &quot;, etc)

function elGetHtml (el) {
	var t = String(el.tagName).toLowerCase();
	var a = '';
	var ea = el.attributes;
	var eac = ea.length;
	for (var i = 0; i < eac; i++) {
		var eai = ea[i];
		// ↓ provide your encode function ↓
		var ean = htmlEntitiesEncode(eai.name);
		var eav = htmlEntitiesEncode(eai.value);
		a += ' '+ean+'='+JSON.stringify(eav);
	}
	var sc = StreamXMLParser.htmlVoidTagMap[t];
	return {
		open: '<'+t+a+(sc?'/':'')+'>',
		close: sc ? '' : '</'+t+'>'
	};
}

function htmlToVnodes (html) {
	var tb = new StreamXMLParser.TreeBuilder({
		element: StreamXMLParser.elementSnabbdom()
	});
	var xp = new StreamXMLParser.XMLParser({
		event: tb.parserEvent.bind(tb),
		// ↓ provide your decode function ↓
		decodeString: htmlEntitiesDecode
	});
	xp.end(html);
	return tb.root.tag.children;
}

function patchElHtml (el, html) {
	var patch = snabbdom.init([
		require('snabbdom/modules/attrs').default
	]);
	var outer = elGetHtml(el);
	html = outer.open+html+outer.close;
	html = htmlToVnodes(html);
	el = snabbdom.toVNode(el);
	patch(el, html[0]);
}

// How to use:
patchElHtml(your_element, your_html_string);

HTML to PDF with Leerraum.js and jsPDF

Leerraum.js

jsPDF

var mmPerInch = 25.4;
var pointsPerInch = 72;
var pointsPerMm = pointsPerInch / mmPerInch;
var mmPerPoints = mmPerInch / pointsPerInch;
var pageWidth = 210; // A4 paper
var pageHeight = 297;

function renderNode(doc, node, lineX, lineY) {
	var span = node.span;
	var font = span.fontFamily.split('\t');
	doc.setFont(font[0]);
	doc.setFontStyle(font[1] || "normal");
	doc.setFontSize(span.fontSize);
	doc.text(node.text, node.x * mmPerPoints + lineX, lineY);
}

function htmlToParagraphs(html, rootStyle) {
	var ht = new StreamXMLParser.HTMLTypeset();
	var tb = new StreamXMLParser.TreeBuilder(ht.treeEvent);
	var xp = new StreamXMLParser.XMLParser({
		event: tb.parserEvent
		// ↓ provide your decode function ↓
		decodeString: htmlEntitiesDecode
	});
	Object.assign(ht.rootStyle, rootStyle);
	xp.end(html);
	return ht;
}

function renderHtmlColumnsToPdf(html, rootStyle)
	var ht = new StreamXMLParser.HTMLTypeset();
	var rootStyle = ht.rootStyle;
	rootStyle.fontSize = 8;
	rootStyle.lineLeading = 10;
	rootStyle.paragraphLeading = 6;
	var tb = new StreamXMLParser.TreeBuilder(ht.treeEvent);
	var xp = new StreamXMLParser.XMLParser({
		event: tb.parserEvent
		// ↓ provide your decode function ↓
		decodeString: htmlEntitiesDecode
	});
	xp.end(this.sumarioExecutivo);
	ht = ht.getResult();
	var measures = Leerraum.jsPdfDocMeasures(doc);
	var colNum = 3;
	var colGap = 2;
	var colTop = 10;
	var colLeft = 10;
	var colWidth = 62;
	var colHeight = pageHeight - (colTop * 2);
	var firstPageOffset = 12;
	var lineHeight = rootStyle.lineLeading * mmPerPoints;
	var parMargin = rootStyle.paragraphLeading * mmPerPoints;
	var parsLines = Leerraum.paragraphsToTextNodes(measures, ht.result, function() {
		return colWidth * pointsPerMm;
	});
	var getPageHeight = function (page) {
		// page number starting from zero on the first page,
		// content has less space because of space reserved
		// for a title on the page.
		// This function can return a custom height for each page.
		return page ? colHeight : colHeight - firstPageOffset;
	};
	var getPageTop = function (page) {
		return colTop + (page ? 0 : firstPageOffset);
	};
	var pagesCols = Leerraum.columns.paragraphsToPageColumns(parsLines, getPageHeight, colNum, lineHeight, parMargin);
	var pageCols;
	var pageIndex = 0;
	while (pageCols = pagesCols.shift()) {
		var colPars = null;
		var x = colLeft;
		var y = getPageTop(pageIndex);
		while (colPars = pageCols.shift()) {
			var parLines;
			while (parLines = colPars.shift()) {
				var lineWords;
				while (lineWords = parLines.shift()) {
					var word;
					while (word = lineWords.shift()) {
						renderNode(doc, word, x, y + 1);
					}
					y += lineHeight;
				}
				y += parMargin;
			}
			x += colWidth + colGap;
			y = getPageTop(pageIndex);
		}
		if (pagesCols.length) {
			doc.addPage('a4', 'p');
			this.createPDFBackground(doc, cs);
			pageIndex++;
		}
	}
}

License

MIT.