3 var Base = require('base');
4 var define = require('define-property');
5 var Compiler = require('./lib/compiler');
6 var Parser = require('./lib/parser');
7 var utils = require('./lib/utils');
12 * Create a new instance of `Snapdragon` with the given `options`.
15 * var snapdragon = new Snapdragon();
18 * @param {Object} `options`
22 function Snapdragon(options) {
23 Base.call(this, null, options);
24 this.options = utils.extend({source: 'string'}, this.options);
25 this.compiler = new Compiler(this.options);
26 this.parser = new Parser(this.options);
28 Object.defineProperty(this, 'compilers', {
30 return this.compiler.compilers;
34 Object.defineProperty(this, 'parsers', {
36 return this.parser.parsers;
40 Object.defineProperty(this, 'regex', {
42 return this.parser.regex;
51 Base.extend(Snapdragon);
54 * Add a parser to `snapdragon.parsers` for capturing the given `type` using
55 * the specified regex or parser function. A function is useful if you need
56 * to customize how the token is created and/or have access to the parser
57 * instance to check options, etc.
61 * .capture('slash', /^\//)
62 * .capture('dot', function() {
63 * var pos = this.position();
64 * var m = this.match(/^\./);
72 * @param {String} `type`
73 * @param {RegExp|Function} `regex`
74 * @return {Object} Returns the parser instance for chaining
78 Snapdragon.prototype.capture = function() {
79 return this.parser.capture.apply(this.parser, arguments);
83 * Register a plugin `fn`.
86 * var snapdragon = new Snapdgragon([options]);
87 * snapdragon.use(function() {
88 * console.log(this); //<= snapdragon instance
89 * console.log(this.parser); //<= parser instance
90 * console.log(this.compiler); //<= compiler instance
93 * @param {Object} `fn`
97 Snapdragon.prototype.use = function(fn) {
103 * Parse the given `str`.
106 * var snapdragon = new Snapdgragon([options]);
107 * // register parsers
108 * snapdragon.parser.use(function() {});
111 * var ast = snapdragon.parse('foo/bar');
114 * @param {String} `str`
115 * @param {Object} `options` Set `options.sourcemap` to true to enable source maps.
116 * @return {Object} Returns an AST.
120 Snapdragon.prototype.parse = function(str, options) {
121 this.options = utils.extend({}, this.options, options);
122 var parsed = this.parser.parse(str, this.options);
124 // add non-enumerable parser reference
125 define(parsed, 'parser', this.parser);
130 * Compile the given `AST`.
133 * var snapdragon = new Snapdgragon([options]);
134 * // register plugins
135 * snapdragon.use(function() {});
136 * // register parser plugins
137 * snapdragon.parser.use(function() {});
138 * // register compiler plugins
139 * snapdragon.compiler.use(function() {});
142 * var ast = snapdragon.parse('foo/bar');
145 * var res = snapdragon.compile(ast);
146 * console.log(res.output);
148 * @param {Object} `ast`
149 * @param {Object} `options`
150 * @return {Object} Returns an object with an `output` property with the rendered string.
154 Snapdragon.prototype.compile = function(ast, options) {
155 this.options = utils.extend({}, this.options, options);
156 var compiled = this.compiler.compile(ast, this.options);
158 // add non-enumerable compiler reference
159 define(compiled, 'compiler', this.compiler);
164 * Expose `Snapdragon`
167 module.exports = Snapdragon;
170 * Expose `Parser` and `Compiler`
173 module.exports.Compiler = Compiler;
174 module.exports.Parser = Parser;