Refactor to externalise core API · remarkjs/remark@9892ec4 (original) (raw)

`@@ -12,324 +12,17 @@

`

12

12

` * Dependencies.

`

13

13

` */

`

14

14

``

15

``

`-

var Ware = require('ware');

`

16

``

`-

var VFile = require('vfile');

`

17

``

`-

var extend = require('extend.js');

`

18

``

`-

var parser = require('./lib/parse.js');

`

19

``

`-

var stringifier = require('./lib/stringify.js');

`

20

``

`-

var utilities = require('./lib/utilities.js');

`

``

15

`+

var unified = require('unified');

`

``

16

`+

var Parser = require('./lib/parse.js');

`

``

17

`+

var Compiler = require('./lib/stringify.js');

`

21

18

``

22

19

`/*

`

23

``

`-

`

``

20

`+

`

24

21

` */

`

25

22

``

26

``

`-

var Parser = parser.Parser;

`

27

``

`-

var parseProto = Parser.prototype;

`

28

``

`-

var Compiler = stringifier.Compiler;

`

29

``

`-

var compileProto = Compiler.prototype;

`

30

``

-

31

``

`-

/**

`

32

``

`-

`

33

``

`-

`

34

``

`-

`

35

``

`-

`

36

``

`-

`

37

``

`-

`

38

``

`-

`

39

``

`-

`

40

``

`-

*/

`

41

``

`-

function fail(exception) {

`

42

``

`-

if (exception) {

`

43

``

`-

throw exception;

`

44

``

`-

}

`

45

``

`-

}

`

46

``

-

47

``

`-

/**

`

48

``

`-

`

49

``

`-

`

50

``

`-

`

51

``

`-

*/

`

52

``

`-

function constructParser() {

`

53

``

`-

var customProto;

`

54

``

`-

var expressions;

`

55

``

`-

var key;

`

56

``

-

57

``

`-

/**

`

58

``

`-

`

59

``

`-

*/

`

60

``

`-

function CustomProto() {}

`

61

``

-

62

``

`-

CustomProto.prototype = parseProto;

`

63

``

-

64

``

`-

customProto = new CustomProto();

`

65

``

-

66

``

`-

/**

`

67

``

`-

`

68

``

`-

*/

`

69

``

`-

function CustomParser() {

`

70

``

`-

Parser.apply(this, arguments);

`

71

``

`-

}

`

72

``

-

73

``

`-

CustomParser.prototype = customProto;

`

74

``

-

75

``

`-

/*

`

76

``

`-

`

77

``

`-

`

78

``

`-

*/

`

79

``

-

80

``

`-

customProto.blockTokenizers = extend({}, parseProto.blockTokenizers);

`

81

``

`-

customProto.blockMethods = extend([], parseProto.blockMethods);

`

82

``

`-

customProto.inlineTokenizers = extend({}, parseProto.inlineTokenizers);

`

83

``

`-

customProto.inlineMethods = extend([], parseProto.inlineMethods);

`

84

``

-

85

``

`-

expressions = parseProto.expressions;

`

86

``

`-

customProto.expressions = {};

`

87

``

-

88

``

`-

for (key in expressions) {

`

89

``

`-

customProto.expressions[key] = extend({}, expressions[key]);

`

90

``

`-

}

`

91

``

-

92

``

`-

return CustomParser;

`

93

``

`-

}

`

94

``

-

95

``

`-

/**

`

96

``

`-

`

97

``

`-

`

98

``

`-

`

99

``

`-

*/

`

100

``

`-

function constructCompiler() {

`

101

``

`-

var customProto;

`

102

``

-

103

``

`-

/**

`

104

``

`-

`

105

``

`-

*/

`

106

``

`-

function CustomProto() {}

`

107

``

-

108

``

`-

CustomProto.prototype = compileProto;

`

109

``

-

110

``

`-

customProto = new CustomProto();

`

111

``

-

112

``

`-

/**

`

113

``

`-

`

114

``

`-

*/

`

115

``

`-

function CustomCompiler() {

`

116

``

`-

Compiler.apply(this, arguments);

`

117

``

`-

}

`

118

``

-

119

``

`-

CustomCompiler.prototype = customProto;

`

120

``

-

121

``

`-

return CustomCompiler;

`

122

``

`-

}

`

123

``

-

124

``

`-

/**

`

125

``

`-

`

126

``

`-

`

127

``

`-

`

128

``

`-

*/

`

129

``

`-

function MDAST() {

`

130

``

`-

var self = this;

`

131

``

-

132

``

`-

if (!(self instanceof MDAST)) {

`

133

``

`-

return new MDAST();

`

134

``

`-

}

`

135

``

-

136

``

`-

self.ware = new Ware();

`

137

``

`-

self.attachers = [];

`

138

``

-

139

``

`-

self.Parser = constructParser();

`

140

``

`-

self.Compiler = constructCompiler();

`

141

``

`-

}

`

142

``

-

143

``

`-

/**

`

144

``

`-

`

145

``

`-

`

146

``

`-

`

147

``

`-

`

148

``

`-

`

149

``

`-

`

150

``

`-

`

151

``

`-

*/

`

152

``

`-

function use(attach, options, fileSet) {

`

153

``

`-

var self = this;

`

154

``

`-

var index;

`

155

``

`-

var transformer;

`

156

``

-

157

``

`-

if (!(self instanceof MDAST)) {

`

158

``

`-

self = new MDAST();

`

159

``

`-

}

`

160

``

-

161

``

`-

/*

`

162

``

`-

`

163

``

`-

*/

`

164

``

-

165

``

`-

if ('length' in attach && typeof attach !== 'function') {

`

166

``

`-

index = -1;

`

167

``

-

168

``

`-

while (attach[++index]) {

`

169

``

`-

self.use(attach[index], options, fileSet);

`

170

``

`-

}

`

171

``

-

172

``

`-

return self;

`

173

``

`-

}

`

174

``

-

175

``

`-

/*

`

176

``

`-

`

177

``

`-

*/

`

178

``

-

179

``

`-

if (self.attachers.indexOf(attach) === -1) {

`

180

``

`-

transformer = attach(self, options, fileSet);

`

181

``

-

182

``

`-

self.attachers.push(attach);

`

183

``

-

184

``

`-

if (transformer) {

`

185

``

`-

self.ware.use(transformer);

`

186

``

`-

}

`

187

``

`-

}

`

188

``

-

189

``

`-

return self;

`

190

``

`-

}

`

191

``

-

192

``

`-

/**

`

193

``

`` -

``

194

``

`-

`

195

``

`-

`

196

``

`-

`

197

``

`-

`

198

``

`` -

``

199

``

`-

*/

`

200

``

`-

function run(ast, file, done) {

`

201

``

`-

var self = this;

`

202

``

-

203

``

`-

if (typeof file === 'function') {

`

204

``

`-

done = file;

`

205

``

`-

file = null;

`

206

``

`-

}

`

207

``

-

208

``

`-

file = new VFile(file);

`

209

``

-

210

``

`-

done = typeof done === 'function' ? done : fail;

`

211

``

-

212

``

`-

if (typeof ast !== 'object' && typeof ast.type !== 'string') {

`

213

``

`-

utilities.raise(ast, 'ast');

`

214

``

`-

}

`

215

``

-

216

``

`-

/*

`

217

``

`-

`

218

``

`-

*/

`

219

``

-

220

``

`-

if (self.ware) {

`

221

``

`-

self.ware.run(ast, file, done);

`

222

``

`-

} else {

`

223

``

`-

done(null, ast, file);

`

224

``

`-

}

`

225

``

-

226

``

`-

return ast;

`

227

``

`-

}

`

228

``

-

229

``

`-

/**

`

230

``

`` -

``

231

``

`-

*/

`

232

``

`-

function parse(value, options) {

`

233

``

`-

var file = new VFile(value);

`

234

``

`-

var ast = file.namespace('mdast').ast = parser.call(this, file, options);

`

235

``

-

236

``

`-

return ast;

`

237

``

`-

}

`

238

``

-

239

``

`-

/**

`

240

``

`` -

``

241

``

`-

*/

`

242

``

`-

function stringify(ast, file, options) {

`

243

``

`-

if (options === null || options === undefined) {

`

244

``

`-

options = file;

`

245

``

`-

file = null;

`

246

``

`-

}

`

247

``

-

248

``

`-

if (!file && ast && !ast.type) {

`

249

``

`-

file = ast;

`

250

``

`-

ast = null;

`

251

``

`-

}

`

252

``

-

253

``

`-

file = new VFile(file);

`

254

``

-

255

``

`-

if (!ast) {

`

256

``

`-

ast = file.namespace('mdast').ast || ast;

`

257

``

`-

}

`

258

``

-

259

``

`-

return stringifier.call(this, ast, file, options);

`

260

``

`-

}

`

261

``

-

262

``

`-

/**

`

263

``

`-

`

264

``

`-

`

265

``

`-

`

266

``

`-

`

267

``

`-

`

268

``

`-

`

269

``

`-

*/

`

270

``

`-

function process(value, options, done) {

`

271

``

`-

var file = new VFile(value);

`

272

``

`-

var self = this instanceof MDAST ? this : new MDAST();

`

273

``

`-

var result = null;

`

274

``

`-

var ast;

`

275

``

-

276

``

`-

if (typeof options === 'function') {

`

277

``

`-

done = options;

`

278

``

`-

options = null;

`

279

``

`-

}

`

280

``

-

281

``

`-

if (!options) {

`

282

``

`-

options = {};

`

283

``

`-

}

`

284

``

-

285

``

`-

/**

`

286

``

`` -

``

287

``

`-

`

288

``

`-

`

289

``

`-

*/

`

290

``

`-

function callback(exception) {

`

291

``

`-

if (exception) {

`

292

``

`-

(done || fail)(exception);

`

293

``

`-

} else {

`

294

``

`-

result = self.stringify(ast, file, options);

`

295

``

-

296

``

`-

if (done) {

`

297

``

`-

done(null, result, file);

`

298

``

`-

}

`

299

``

`-

}

`

300

``

`-

}

`

301

``

-

302

``

`-

ast = self.parse(file, options);

`

303

``

-

304

``

`-

self.run(ast, file, callback);

`

305

``

-

306

``

`-

return result;

`

307

``

`-

}

`

308

``

-

309

``

`-

/*

`

310

``

`-

`

311

``

`-

*/

`

312

``

-

313

``

`-

var proto = MDAST.prototype;

`

314

``

-

315

``

`-

proto.use = use;

`

316

``

`-

proto.parse = parse;

`

317

``

`-

proto.run = run;

`

318

``

`-

proto.stringify = stringify;

`

319

``

`-

proto.process = process;

`

320

``

-

321

``

`-

/*

`

322

``

`-

`

323

``

`-

*/

`

324

``

-

325

``

`-

MDAST.use = use;

`

326

``

`-

MDAST.parse = parse;

`

327

``

`-

MDAST.run = run;

`

328

``

`-

MDAST.stringify = stringify;

`

329

``

`-

MDAST.process = process;

`

330

``

-

331

``

`-

/*

`

332

``

`` -

``

333

``

`-

*/

`

334

``

-

335

``

`-

module.exports = MDAST;

`

``

23

`+

module.exports = unified({

`

``

24

`+

'name': 'mdast',

`

``

25

`+

'type': 'ast',

`

``

26

`+

'Parser': Parser,

`

``

27

`+

'Compiler': Compiler

`

``

28

`+

});

`