Projekt

Obecné

Profil

Stáhnout (3.89 KB) Statistiky
| Větev: | Revize:
1
'use strict';
2

    
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');
8
var regexCache = {};
9
var cache = {};
10

    
11
/**
12
 * Create a new instance of `Snapdragon` with the given `options`.
13
 *
14
 * ```js
15
 * var snapdragon = new Snapdragon();
16
 * ```
17
 *
18
 * @param {Object} `options`
19
 * @api public
20
 */
21

    
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);
27

    
28
  Object.defineProperty(this, 'compilers', {
29
    get: function() {
30
      return this.compiler.compilers;
31
    }
32
  });
33

    
34
  Object.defineProperty(this, 'parsers', {
35
    get: function() {
36
      return this.parser.parsers;
37
    }
38
  });
39

    
40
  Object.defineProperty(this, 'regex', {
41
    get: function() {
42
      return this.parser.regex;
43
    }
44
  });
45
}
46

    
47
/**
48
 * Inherit Base
49
 */
50

    
51
Base.extend(Snapdragon);
52

    
53
/**
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.
58
 *
59
 * ```js
60
 * snapdragon
61
 *   .capture('slash', /^\//)
62
 *   .capture('dot', function() {
63
 *     var pos = this.position();
64
 *     var m = this.match(/^\./);
65
 *     if (!m) return;
66
 *     return pos({
67
 *       type: 'dot',
68
 *       val: m[0]
69
 *     });
70
 *   });
71
 * ```
72
 * @param {String} `type`
73
 * @param {RegExp|Function} `regex`
74
 * @return {Object} Returns the parser instance for chaining
75
 * @api public
76
 */
77

    
78
Snapdragon.prototype.capture = function() {
79
  return this.parser.capture.apply(this.parser, arguments);
80
};
81

    
82
/**
83
 * Register a plugin `fn`.
84
 *
85
 * ```js
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
91
 * });
92
 * ```
93
 * @param {Object} `fn`
94
 * @api public
95
 */
96

    
97
Snapdragon.prototype.use = function(fn) {
98
  fn.call(this, this);
99
  return this;
100
};
101

    
102
/**
103
 * Parse the given `str`.
104
 *
105
 * ```js
106
 * var snapdragon = new Snapdgragon([options]);
107
 * // register parsers
108
 * snapdragon.parser.use(function() {});
109
 *
110
 * // parse
111
 * var ast = snapdragon.parse('foo/bar');
112
 * console.log(ast);
113
 * ```
114
 * @param {String} `str`
115
 * @param {Object} `options` Set `options.sourcemap` to true to enable source maps.
116
 * @return {Object} Returns an AST.
117
 * @api public
118
 */
119

    
120
Snapdragon.prototype.parse = function(str, options) {
121
  this.options = utils.extend({}, this.options, options);
122
  var parsed = this.parser.parse(str, this.options);
123

    
124
  // add non-enumerable parser reference
125
  define(parsed, 'parser', this.parser);
126
  return parsed;
127
};
128

    
129
/**
130
 * Compile the given `AST`.
131
 *
132
 * ```js
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() {});
140
 *
141
 * // parse
142
 * var ast = snapdragon.parse('foo/bar');
143
 *
144
 * // compile
145
 * var res = snapdragon.compile(ast);
146
 * console.log(res.output);
147
 * ```
148
 * @param {Object} `ast`
149
 * @param {Object} `options`
150
 * @return {Object} Returns an object with an `output` property with the rendered string.
151
 * @api public
152
 */
153

    
154
Snapdragon.prototype.compile = function(ast, options) {
155
  this.options = utils.extend({}, this.options, options);
156
  var compiled = this.compiler.compile(ast, this.options);
157

    
158
  // add non-enumerable compiler reference
159
  define(compiled, 'compiler', this.compiler);
160
  return compiled;
161
};
162

    
163
/**
164
 * Expose `Snapdragon`
165
 */
166

    
167
module.exports = Snapdragon;
168

    
169
/**
170
 * Expose `Parser` and `Compiler`
171
 */
172

    
173
module.exports.Compiler = Compiler;
174
module.exports.Parser = Parser;
(3-3/4)