[Pkg-javascript-commits] [node-jade] 01/72: Initial code

Jelmer Vernooij jelmer at moszumanska.debian.org
Sun Jul 3 18:03:24 UTC 2016


This is an automated email from the git hooks/post-receive script.

jelmer pushed a commit to annotated tag upstream/1.0.0
in repository node-jade.

commit a6c22a5115c49c3fd85d828c9a73b53f0783f395
Author: Forbes Lindesay <forbes at lindesay.co.uk>
Date:   Tue Nov 18 17:18:14 2014 +0000

    Initial code
---
 .gitignore   | 13 ++++++++++
 .travis.yml  |  3 +++
 LICENSE      | 19 ++++++++++++++
 README.md    | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 index.js     | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 package.json | 19 ++++++++++++++
 6 files changed, 207 insertions(+)

diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..b83202d
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,13 @@
+lib-cov
+*.seed
+*.log
+*.csv
+*.dat
+*.out
+*.pid
+*.gz
+pids
+logs
+results
+npm-debug.log
+node_modules
diff --git a/.travis.yml b/.travis.yml
new file mode 100644
index 0000000..6e5919d
--- /dev/null
+++ b/.travis.yml
@@ -0,0 +1,3 @@
+language: node_js
+node_js:
+  - "0.10"
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..27cc9f3
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,19 @@
+Copyright (c) 2014 Forbes Lindesay
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
\ No newline at end of file
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..e495ad9
--- /dev/null
+++ b/README.md
@@ -0,0 +1,72 @@
+# jstransformer
+
+Normalize the API of any jstransformer
+
+[![Build Status](https://img.shields.io/travis/jstransformers/jstransformer/master.svg)](https://travis-ci.org/jstransformers/jstransformer)
+[![Dependency Status](https://img.shields.io/gemnasium/jstransformers/jstransformer.svg)](https://gemnasium.com/jstransformers/jstransformer)
+[![NPM version](https://img.shields.io/npm/v/jstransformer.svg)](https://www.npmjs.org/package/jstransformer)
+
+## Installation
+
+    npm install jstransformer
+
+## Usage
+
+```js
+var transformer = require('jstransformer');
+var marked = transformer(require('jstransformer-marked'));
+
+var options = {};
+var res = marked.render('Some **markdown**', options);
+// => {body: 'Some <strong>markdown</strong>', dependencies: []}
+```
+
+This gives the same API regardless of the jstransformer passed in.
+
+## API
+
+A transformer, once normalised using this module, will implement the following methods.  Note that if the underlying transformer cannot be used to implement the functionality, it may ultimately just throw an error.
+
+### `.render(str, options) => {body: String, dependencies: Array.<String>}`
+
+_requires the underlying transform to implement `.render`_
+
+Transform a string and return an object where:
+
+ - `body` represents the result as a string
+ - `dependencies` is an array of files that were read in as part of the render process (or an empty array if there were no dependencies)
+
+### `.renderAsync(str, options, callback)` / `.renderAsync(str, options) => Promise({body: String, dependencies: Array.<String>})`
+
+_requires the underlying transform to implement `.renderAsync` or `.render`_
+
+Transform a string asynchronously and return an object where:
+
+ - `body` represents the result as a string
+ - `dependencies` is an array of files that were read in as part of the render process (or an empty array if there were no dependencies)
+
+If a callback is provided, it is called with the error followed by the result, otherwise a Promise is returned.
+
+### `.renderFile(filename, options) => {body: String, dependencies: Array.<String>}`
+
+_requires the underlying transform to implement `.renderFile` or `.render`_
+
+Transform a file and return an object where:
+
+ - `body` represents the result as a string
+ - `dependencies` is an array of files that were read in as part of the render process (or an empty array if there were no dependencies).  This does not include the initial file being rendered.
+
+### `.renderFileAsync(filename, options, callback)` / `.renderFileAsync(filename, options) => Promise({body: String, dependencies: Array.<String>})`
+
+_requires the underlying transform to implement `.renderFileAsync`, `.renderFile`, `.renderAsync` or `.render`_
+
+Transform a file and return an object where:
+
+ - `body` represents the result as a string
+ - `dependencies` is an array of files that were read in as part of the render process (or an empty array if there were no dependencies).  This does not include the initial file being rendered.
+
+If a callback is provided, it is called with the error followed by the result, otherwise a Promise is returned.
+
+## License
+
+  MIT
diff --git a/index.js b/index.js
new file mode 100644
index 0000000..3edddb6
--- /dev/null
+++ b/index.js
@@ -0,0 +1,81 @@
+'use strict';
+
+var fs = require('fs');
+var assert = require('assert');
+var Promise = require('promise');
+var readFile = Promise.denodeify(fs.readFile);
+
+module.exports = function (transformer) {
+  return new Transformer(transformer);
+};
+
+function normalize(result) {
+  if (typeof result === 'string') {
+    return {body: result, dependencies: []};
+  } else if (result && typeof result === 'object' && typeof result.body === 'string') {
+    if ('dependencies' in result) {
+      if (!Array.isArray(result.dependencies)) {
+        throw new Error('Result should have a dependencies property that is an array');
+      }
+    } else {
+      result.dependencies = [];
+    }
+    return result;
+  } else {
+    throw new Error('Invalid result object from transform.');
+  }
+}
+function normalizeAsync(result, cb) {
+  return Promise.resolve(result).then(normalize).nodeify(cb);
+}
+
+function Transformer(tr) {
+  assert(tr, 'Transformer must be an object');
+  assert(typeof tr.name === 'string', 'Transformer must have a name');
+  assert(typeof tr.outputFormat === 'string', 'Transformer must have an output format');
+  assert(['render', 'renderAsync', 'renderFile', 'renderFileAsync'].some(function (method) {
+    return typeof tr[method] === 'function';
+  }), 'Transformer must implement at least one of the potential methods.');
+  this._tr = tr;
+  this.name = this._tr.name;
+  this.outputFormat = this._tr.outputFormat;
+}
+Transformer.prototype.render = function (str, options) {
+  if (typeof this._tr.render === 'function') {
+    return normalize(this._tr.render(str, options));
+  } else if (typeof this._tr.renderAsync === 'function') {
+    throw new Error('This transform does not support synchronous rendering');
+  } else {
+    throw new Error('This transform does not support rendering plain strings');
+  }
+};
+Transformer.prototype.renderAsync = function (str, options, cb) {
+  if (typeof this._tr.renderAsync === 'function') {
+    return normalizeAsync(this._tr.renderAsync(str, options), cb);
+  } else if (typeof this._tr.render === 'function') {
+    return normalizeAsync(this._tr.render(str, options), cb);
+  } else {
+    throw new Error('This transform does not support rendering of plain strings');
+  }
+};
+
+Transformer.prototype.renderFile = function (filename, options) {
+  if (typeof this._tr.renderFile === 'function') {
+    return normalize(this._tr.renderFile(filename, options));
+  } else if (typeof this._tr.render === 'function') {
+    return normalize(this._tr.render(fs.readFileSync(filename, 'utf8'), options));
+  } else {
+    throw new Error('This transform does not support synchronous rendering');
+  }
+};
+Transformer.prototype.renderFileAsync = function (filename, options, cb) {
+  if (typeof this._tr.renderFileAsync === 'function') {
+    return normalizeAsync(this._tr.renderFileAsync(filename, options), cb);
+  } else if (typeof this._tr.renderFile === 'function') {
+    return normalizeAsync(this._tr.renderFile(filename, options), cb);
+  } else {
+    return readFile(filename, 'utf8').then(function (str) {
+      return this.renderAsync(str, options);
+    }.bind(this)).then(normalize).nodeify(cb);
+  }
+};
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..920dca6
--- /dev/null
+++ b/package.json
@@ -0,0 +1,19 @@
+{
+  "name": "jstransformer",
+  "version": "0.0.0",
+  "description": "Normalize the API of any jstransformer",
+  "keywords": [],
+  "dependencies": {
+    "promise": "^6.0.1"
+  },
+  "devDependencies": {},
+  "scripts": {
+    "test": "node test"
+  },
+  "repository": {
+    "type": "git",
+    "url": "https://github.com/jstransformers/jstransformer.git"
+  },
+  "author": "ForbesLindesay",
+  "license": "MIT"
+}

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-jade.git



More information about the Pkg-javascript-commits mailing list