jsonml-stringify
v1.0.1
Published
Convert jsonml arrays to html strings
Downloads
10
Readme
jsonml-stringify
Convert jsonml arrays to html strings
Example
var Stringify = require("jsonml-stringify/stringify")
var stringify = Stringify([
require("jsonml-stringify/plugins/loose")
])
var assert = require("assert")
var html = stringify(["html", [
["head", [
["meta", { charset: "utf-8" }],
["title", "Process dashboard"],
["link", { rel: "stylesheet", href: "/less/main"}]
]],
["body", { class: "main" }, [
["script", { src: "/browserify/main" }]
]]
]])
assert.equal(html,
"<html>\n" +
" <head>\n" +
" <meta charset=\"utf-8\"></meta>\n" +
" <title>\n" +
" Process dashboard\n" +
" </title>\n" +
" <link rel=\"stylesheet\" href=\"/less/main\"></link>\n" +
" </head>\n" +
" <body class=\"main\">\n" +
" <script src=\"/browserify/main\"></script>\n" +
" </body>\n" +
"</html>")
stringify raw html entities
var Stringify = require("jsonml-stringify/stringify")
var stringify = Stringify([
require("jsonml-stringify/plugins/loose"),
require("jsonml-stringify/plugins/raw")
])
var assert = require("assert")
var html = stringify(["div", { raw: "foo©" }])
assert.equal(html, "<div>\n foo©\n</div>")
stringify fragments
var Stringify = require("jsonml-stringify/stringify")
var stringify = Stringify([
require("jsonml-stringify/plugins/loose"),
require("jsonml-stringify/plugins/fragment")
])
var assert = require("assert")
var html = stringify(["div", [
{ fragment: [
["div", "one"],
["div", "two"]
] },
["div", "three"]
]])
assert.equal(html, "<div>\n" +
" <div>\n" +
" one\n" +
" </div>\n" +
" <div>\n" +
" two\n" +
" </div>\n\n" +
" <div>\n" +
" three\n" +
"</div>\n" +
"</div>")
Loose JSONML definition
(*
JsonML is both loosely and strictly defined.
A plugin is an object literal with either a single key / value
pair or a key 'type' and some properties
Loose:
- null
- undefined
- plugin
- text content
- [ tagName ]
- [ tagName , properties ]
- [ tagName , text content ]
- [ tagName , children ]
- [ tagName , plugin ]
- [ tagName , properties , text content ]
- [ tagName , properties , children ]
- [ tagname , properties , plugin ]
- [ '#text' , text content ]
- [ '#text' , properties , text content ]
*)
type JsonMLPlugin := Object | Function
type JsonMLProperties :=
Object<String, String | Boolean | JsonMLPlugin>
type LooseJsonML :=
null |
undefined |
JsonMLPlugin |
String |
[ String ] |
[ String , JsonMLProperties ] |
[ String , String ] |
[ String , Array<LooseJsonML> ] |
[ String , JsonMLPlugin ] |
[ "#text" , String ] |
[ String , JsonMLProperties , String ] |
[ String , JsonMLProperties , Array<LooseJsonML> ] |
[ String , JsonMLProperties , JsonMLPlugin ] |
[ "#text" , JsonMLProperties , String ]
Plugin definition
type Plugin := {
stringify: (JsonML, JsonMLOptions) => String,
dom: (JsonML, JsonMLOptions) => DOMElement,
merge: (JsonML, JsonMLMergeOptions) => void,
type: String,
normalize: (JsonML, JsonMLOptions) => JsonML,
renderProperty: (DOMElement, value: Any, key: String, JsonMLOptions),
stringifyProperty: (value: Any, key: String, JsonMLOptions) => String,
mergeProperty: (DOMElement, value: Any, key: String, JsonMLMergeOptions),
setProperty: (value: Any, key: String),
getProperty: (value: Any, key: String) => String
}
Strict definition & functions
(*
Strict:
- null
- plugin
- [ tagName , properties , children ]
- [ '#text' , properties , text content ]
- [ '#text' , properties , plugin ]
*)
type JsonMLPlugin := Object | Function
type JsonMLProperties :=
Object<String, String | Boolean | JsonMLPlugin>
type JsonML :=
null |
JsonMLPlugin |
[ String , JsonMLProperties , Array<JsonML> ] |
[ "#text" , JsonMLProperties , String | JsonMLPlugin ]
type JsonMLOptions := {
parent: JsonML,
parents: Array<JsonML>,
plugins: Array<Plugin>
}
type JsonMLMergeOptions := JsonMLOptions & {
elements: Array<DOMElement | DOMTextNode>,
root: DOMElement
}
stringify-recur := (JsonML, JsonMLOptions) => String
dom-recur := (JsonML, JsonMLOptions) => DOMElement
merge-recur := (JsonML, JsonMLMergeOptions)
Installation
npm install jsonml-stringify
Contributors
- Raynos