From 4f9e6abf812b416ede8f1b0b55029d94c1bb910b Mon Sep 17 00:00:00 2001 From: tkQubo Date: Sat, 5 Sep 2015 00:39:09 +0900 Subject: [PATCH] Add gulp-cheerio --- gulp-cheerio/gulp-cheerio-tests.ts | 93 ++++++++++++++++++++++++++++++ gulp-cheerio/gulp-cheerio.d.ts | 34 +++++++++++ 2 files changed, 127 insertions(+) create mode 100644 gulp-cheerio/gulp-cheerio-tests.ts create mode 100644 gulp-cheerio/gulp-cheerio.d.ts diff --git a/gulp-cheerio/gulp-cheerio-tests.ts b/gulp-cheerio/gulp-cheerio-tests.ts new file mode 100644 index 000000000..8a32c57d2 --- /dev/null +++ b/gulp-cheerio/gulp-cheerio-tests.ts @@ -0,0 +1,93 @@ +/// +/// +/// +/// + +import cheerio = require('gulp-cheerio'); +import gulp = require('gulp'); +import Vinyl = require('vinyl'); + +// +// There are two ways to use gulp-cheerio: synchronous and asynchronous. See the following usage examples: +// + +gulp.task('sync', function () { + return gulp + .src(['src/*.html']) + .pipe(cheerio(function ($: CheerioStatic, file: Vinyl) { + // Each file will be run through cheerio and each corresponding `$` will be passed here. + // `file` is the gulp file object + // Make all h1 tags uppercase + $('h1').each(function () { + var h1 = $(this); + h1.text(h1.text().toUpperCase()); + }); + })) + .pipe(gulp.dest('dist/')); +}); +gulp.task('async', function () { + return gulp + .src(['src/*.html']) + .pipe(cheerio(function ($: CheerioStatic, file: Vinyl, done: Function) { + // The only difference here is the inclusion of a `done` parameter. + // Call `done` when everything is finished. `done` accepts an error if applicable. + done(); + })) + .pipe(gulp.dest('dist/')); +}); +//TODO + + +// +// Additional options can be passed by passing an object as the main argument with your function as the run option: +// + + +gulp.task('sync', function () { + return gulp + .src(['src/*.html']) + .pipe(cheerio({ + run: function ($: CheerioStatic, file: Vinyl) { + // Each file will be run through cheerio and each corresponding `$` will be passed here. + // `file` is the gulp file object + // Make all h1 tags uppercase + $('h1').each(function () { + var h1 = $(this); + h1.text(h1.text().toUpperCase()); + }); + } + })) + .pipe(gulp.dest('dist/')); +}); + +gulp.task('async', function () { + return gulp + .src(['src/*.html']) + .pipe(cheerio({ + run: function ($: CheerioStatic, file: Vinyl, done: Function) { + // The only difference here is the inclusion of a `done` parameter. + // Call `done` when everything is finished. `done` accepts an error if applicable. + done(); + } + })) + .pipe(gulp.dest('dist/')); +}); + +cheerio({ + run: function () {}, + parserOptions: { + // Options here + } +}); + + +cheerio({ + run: function () {}, + parserOptions: { + xmlMode: true + } +}); + +cheerio({ + cheerio: require('../cheerio/cheerio.d.ts') as CheerioStatic // special version of `cheerio` +}); diff --git a/gulp-cheerio/gulp-cheerio.d.ts b/gulp-cheerio/gulp-cheerio.d.ts new file mode 100644 index 000000000..3a558ae5b --- /dev/null +++ b/gulp-cheerio/gulp-cheerio.d.ts @@ -0,0 +1,34 @@ +// Type definitions for gulp-cheerio +// Project: https://github.com/KenPowers/gulp-cheerio +// Definitions by: Qubo +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +/// +/// +/// + +declare module "gulp-cheerio" { + import Vinyl = require('vinyl'); + + namespace cheerio { + interface Cheerio { + (callback: Callback): NodeJS.ReadWriteStream; + (option: Option): NodeJS.ReadWriteStream; + } + + interface Callback { + ($: CheerioStatic, file: Vinyl, done?: Function): any; + } + + interface Option { + run?: Callback; + parserOptions?: CheerioOptionsInterface; + cheerio?: CheerioStatic; + } + } + + var cheerio: cheerio.Cheerio; + + export = cheerio; +} +