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

whatxml

v1.4.0

Published

Construct XML (or (X)HTML) with LiveScript function calls

Downloads

20

Readme

whatxml

XML/HTML templating with LiveScript's cascade syntax.

"What XML?" None, ever again.

npm package Build status npm dependencies

x = whatxml \html
  .. \head
    .. \title ._ "My page"
    ..self-closing \link rel : \stylesheet type : \text/css href : \main.css
  .. \body
    .. \p ._ (.content)

console.log x.to-string { content : "Here's a paragraph." }

<html><head><title>My page</title><link rel="stylesheet" type="text/css" href="main.css" /></head><body><p>Here&#x27;s a paragraph.</p></body></html>

API summary

  • .. <string> [<attr-object>] adds a tag (with optional attributes)
  • ..self-closing <string> [<attr-object>] same, but a self-closing tag
  • .. <object> sets attributes
  • .._ <string> adds text
  • ..raw <string> adds text (without escaping it)
  • ..comment <string> adds a comment

to-string recursively renders that tag's tree.

Any of the setters can also take a function parameter which is called with the value passed to to-string. It is expected to return the value that should be inserted at that point. (See § Templating.)

API tutorial

Basics

Create a root tag, call it with a string to create child tags, with an object to add attributes or call _ to add text between the tags.

gandalf = whatxml \person      # Create a root tag.
  .. { profession : \wizard }  # Set an attribute.
  .. \name                     # Add a child node.
    .._ "Gandalf"              # Put text in it.
console.log gandalf.to-string!
<person profession="wizard"><name>Gandalf</name></person>

Handy shortcut: When creating a tag, pass attributes as an object.

t = whatxml \tower lean : "3.99"
  .. \place city : "Pisa", country : "Italy"
console.log t.to-string!
<tower lean="3.99"><place city="Pisa" country="Italy"></place></tower>

Add self-closing tags and comments.

x = whatxml \a
  ..self-closing \b
  ..comment "what"
console.log x.to-string!
<a><b /><!--what--></a>

You can have stand-alone attributes without a value by setting them to true. (It's invalid XML, but fine in HTML.)

whatxml \input { +selected }
  ..to-string! |> console.log
<input selected></input>

Setting an attribute to another value overwrites the previous value. Setting attributes to false, null or undefined removes that attribute, if present.

Text is escaped automatically, but you can bypass that with raw if you have ready-escaped text (e.g. from marked).

greeting = whatxml \p
  .._ "What's up <3"
console.log greeting.to-string!

x = whatxml \p
  ..raw "<em>I know this is &gt; properly escaped already</em>"
console.log x.to-string!
<p>What&#x27;s up &#x3C;3</p>
<p><em>I know this is &gt; properly escaped already</em></p>

You can also have multiple top-level tags:

x = whatxml!
  .. \a
  .. \b
console.log x.to-string!
<a></a><b></b>

Templating

To generate content based on data, you can pass a function to any setter call. When a tag's to-string is called, the functions passed to its setters before are called with its arguments to produce the final value.

link = whatxml \a href : (.href)
  .._ (.name.to-upper-case!)

console.log link.to-string name : \google    href : "https://google.com"
console.log link.to-string name : \runescape href : "http://runescape.com"
<a href="https://google.com">GOOGLE</a>
<a href="http://runescape.com">RUNESCAPE</a>

Limitations

Check your XML comments are valid by the XML spec: They may not contain two consecutive hyphens (--). Whatxml doesn't check for you.

CDATA-sections and XML declarations (<?xml version="1.0"?> and such) aren't explicitly supported, but you can happily add them using raw.

Related libraries

Whatxml aims to be a serious general-purpose XML/HTML templating engine for LiveScript's syntax.

Existing attempts have their flaws:

  • live-templ came closest to my goals, but objects in nested arrays cannot represent comments, raw text data or self-closing tags. It also has no templating.
  • create-xml-ls is based on nested objects, so it can't represent two tags with the same name on the same level of nesting…
  • htmls supports only the base HTML tag set. Templating code is stringly typed and compiled separately.