mirror of
https://codeberg.org/tacerus/teddit.git
synced 2024-11-26 17:19:27 +01:00
2.2 KiB
2.2 KiB
pug-attrs
Generate code for Pug attributes
Installation
npm install pug-attrs
Usage
var compileAttrs = require('pug-attrs');
compileAttrs(attrs, options)
Compile attrs
to a JavaScript string that evaluates to
the attributes in the desired format.
options
MUST include the following properties:
terse
: whether or not to use HTML5-style terse boolean attributesruntime
: callback that takes a runtime function name and returns the source code that will evaluate to that function at runtimeformat
: output format; must behtml
orobject
attrs
is an array of attributes, with each attribute
having the form of { name, val, mustEscape }
.
val
represents a JavaScript string that evaluates to the
value of the attribute, either statically or dynamically.
var compileAttrs = require('pug-attrs');
var pugRuntime = require('pug-runtime');
function getBaz () { return 'baz<>'; }
var attrs = [
name: 'foo', val: '"bar"', mustEscape: true },
{name: 'baz', val: 'getBaz()', mustEscape: true },
{name: 'quux', val: true, mustEscape: false}
{;
]var result, finalResult;
// HTML MODE
= compileAttrs(attrs, {
result terse: true,
format: 'html',
runtime: function (name) { return 'pugRuntime.' + name; }
;
})//=> '" foo=\\"bar\\"" + pugRuntime.attr("baz", getBaz(), true, true) + " quux"'
= Function('pugRuntime, getBaz',
finalResult 'return (' + result + ');'
;
)finalResult(pugRuntime, getBaz);
// => ' foo="bar" baz="baz<>" quux'
// OBJECT MODE
= compileAttrs(attrs, {
result terse: true,
format: 'object',
runtime: function (name) { return 'pugRuntime.' + name; }
;
})//=> '{"foo": "bar","baz": pugRuntime.escape(getBaz()),"quux": true}'
= Function('pugRuntime, getBaz',
finalResult 'return (' + result + ');'
;
)finalResult(pugRuntime, getBaz);
//=> { foo: 'bar', baz: 'baz<>', quux: true }
License
MIT