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

jsonml2html

v0.0.6

Published

Microlibrary that converts jsonml into html strings

Downloads

5

Readme

jsonml2html 0.0.6

Microlibrary that converts jsonml into html strings ci

Install

bower install jsonml2html
npm install jsonml2html

Simple conversion of jsonml to html

> jsonml2html.toString(["div", "hello world", ["img", {src: "smiley.png"}]]);
'<div>hello world<img src="smiley"></div>'

Implicit class and id

> jsonml2html.toString(["div#root.red", "hello world", ["img.cool.im", {src: "smiley.png"}]]);
'<div id="root" class="red">hello world<img class="cool im" src="smiley"></div>'

Inline style

> jsonml2html.toString(["div", {style: {background: "blue", fontSize: 16}}, "hello world"]);
'<div style="background:blue;font-size:16px">hello world</div>'

Automatic escaping

> jsonml2html.toString(["div", "<blåbærgrød>"]);
'<div>&#60;bl&#229;b&#230;rgr&#248;d&#62;</div>'

Raw html passthrough

> jsonml2html.toString(["script", ["rawhtml", "a<b"]]);
'<script>a<b</script>'

Notics empty tags must have empty string content to emit endtag:

> jsonml2html.toString(["i.fa.fa-book"]); // WRONG
<i class="fa fa-book">
> jsonml2html.toString(["i.fa.fa-book",""]); // RIGHT
<i class="fa fa-book"></i>

Literate source code

Globals

Define isNodeJs and runTest in such a way that they will be fully removed by uglifyjs -mc -d isNodeJs=false -d runTest=false

if typeof isNodeJs == "undefined" or typeof runTest == "undefined" then do ->
  root = if typeof global == "undefined" then window else global
  root.isNodeJs = (typeof window == "undefined") if typeof isNodeJs == "undefined"
  root.runTest = isNodeJs and process.argv[2] == "test" if typeof runTest == "undefined"

jsonml2html = if isNodeJs then exports else window.jsonml2html = {}

xmlEscape

xmlEscape = (str) -> String(str).replace RegExp("[\x00-\x1f\x80-\uffff&<>\"']", "g"), (c) -> "&##{c.charCodeAt 0};"

jsonml2html.xmlEscape = xmlEscape

obj2style

obj2style = (obj) ->
  (for key, val of obj
    key = key.replace /[A-Z]/g, (c) -> "-" + c.toLowerCase()
    val = "#{val}px" if typeof val == "number"
    "#{key}:#{val}"
  ).join ";"

jsonml2html.obj2style = obj2style

toString

toString = (arr) ->
  return "#{xmlEscape arr}" if !Array.isArray(arr)

raw html, useful for stuff which shouldn't be xmlescaped etc.

  return arr[1] if arr[0] == "rawhtml"

normalise jsonml, make sure it contains attributes

  arr = [arr[0], {}].concat arr.slice(1) if arr[1]?.constructor != Object
  attr = {}
  attr[key] = val for key, val of arr[1]

convert style objects to strings

  attr.style = obj2style attr.style if attr.style?.constructor == Object

shorthand for classes and ids

  tag = arr[0].replace /#([^.#]*)/, ((_, id) -> attr.id = id; "")
  tag = tag.replace /\.([^.#]*)/g, (_, cls) ->
    attr["class"] = if attr["class"] == undefined then cls else "#{attr["class"]} #{cls}"
    ""

create actual tag string

  result = "<#{tag}#{(" #{key}=\"#{xmlEscape val}\"" for key, val of attr).join ""}>"

add children and endtag, if there are children. <foo></foo> is done with ["foo", ""]

  result += "#{arr.slice(2).map(toString).join ""}</#{tag}>" if arr.length > 2
  return result
jsonml2html.toString = toString

Test / examples

if runTest then process.nextTick ->
  assert = require "assert"
  jsonml = ["div.main",
      style:
        background: "red"
        textSize: 12
    ["h1#theHead.foo.bar", "Blåbærgrød"],
    ["img",
      src: "foo"
      alt: 'the "quoted"'],
    ["script", ["rawhtml", "console.log(foo<bar)"]]]
      
  assert.equal jsonml2html.toString(jsonml),
    """<div style="background:red;text-size:12px" class="main"><h1 id="theHead" class="foo bar">Bl&#229;b&#230;rgr&#248;d</h1><img src="foo" alt="the &#34;quoted&#34;"><script>console.log(foo<bar)</script></div>"""

README.md autogenerated from jsonml2html.coffee solsort