Permalink
Browse files

Initial commit

  • Loading branch information...
mathiasbynens committed May 4, 2014
0 parents commit 239f7dc517aa2607abb4d1e6da4ba3ca66b4e33f
Showing with 1,113 additions and 0 deletions.
  1. +2 −0 .gitattributes
  2. +17 −0 .gitignore
  3. +18 −0 .travis.yml
  4. +75 −0 Gruntfile.js
  5. +20 −0 LICENSE-MIT.txt
  6. +147 −0 README.md
  7. +110 −0 bin/quoted-printable
  8. +16 −0 bower.json
  9. +19 −0 component.json
  10. +58 −0 man/quoted-printable.1
  11. +58 −0 package.json
  12. +155 −0 quoted-printable.js
  13. +30 −0 scripts/export-data.js
  14. +154 −0 src/quoted-printable.js
  15. +35 −0 tests/index.html
  16. +199 −0 tests/tests.js
@@ -0,0 +1,2 @@
# Automatically normalize line endings for all text-based files
* text=auto
@@ -0,0 +1,17 @@
# Coverage report
coverage

# Installed npm modules
node_modules

# Folder view configuration files
.DS_Store
Desktop.ini

# Thumbnail cache files
._*
Thumbs.db

# Files that might appear on external disks
.Spotlight-V100
.Trashes
@@ -0,0 +1,18 @@
language: node_js
node_js:
- "0.10"
before_script:
- "npm install -g grunt-cli"
# Narwhal uses a hardcoded path to openjdk v6, so use that version
- "sudo apt-get update -qq"
- "sudo apt-get install -qq openjdk-6-jre"
- "PACKAGE=rhino1_7R3; wget http://ftp.mozilla.org/pub/mozilla.org/js/$PACKAGE.zip && sudo unzip $PACKAGE -d /opt/ && rm $PACKAGE.zip"
- "PACKAGE=rhino1_7R3; echo -e '#!/bin/sh\\njava -jar /opt/'$PACKAGE'/js.jar $@' | sudo tee /usr/local/bin/rhino && sudo chmod +x /usr/local/bin/rhino"
- "PACKAGE=ringojs-0.9; wget http://ringojs.org/downloads/$PACKAGE.zip && sudo unzip $PACKAGE -d /opt/ && rm $PACKAGE.zip"
- "PACKAGE=ringojs-0.9; sudo ln -s /opt/$PACKAGE/bin/ringo /usr/local/bin/ringo && sudo chmod +x /usr/local/bin/ringo"
- "PACKAGE=v0.3.2; wget https://github.com/280north/narwhal/archive/$PACKAGE.zip && sudo unzip $PACKAGE -d /opt/ && rm $PACKAGE.zip"
- "PACKAGE=narwhal-0.3.2; sudo ln -s /opt/$PACKAGE/bin/narwhal /usr/local/bin/narwhal && sudo chmod +x /usr/local/bin/narwhal"
# If the enviroment stores rt.jar in a different directory, find it and symlink the directory
- "PREFIX=/usr/lib/jvm; if [ ! -d $PREFIX/java-6-openjdk ]; then for d in $PREFIX/java-6-openjdk-*; do if [ -e $d/jre/lib/rt.jar ]; then sudo ln -s $d $PREFIX/java-6-openjdk; break; fi; done; fi"
script:
"grunt ci"
@@ -0,0 +1,75 @@
module.exports = function(grunt) {

grunt.initConfig({
'shell': {
'options': {
'stdout': true,
'stderr': true,
'failOnError': true
},
'cover': {
'command': 'istanbul cover --report "html" --verbose --dir "coverage" "tests/tests.js"'
},
'test-narwhal': {
'command': 'echo "Testing in Narwhal..."; export NARWHAL_OPTIMIZATION=-1; narwhal "tests/tests.js"'
},
'test-phantomjs': {
'command': 'echo "Testing in PhantomJS..."; phantomjs "tests/tests.js"'
},
// Rhino 1.7R4 has a bug that makes it impossible to test in.
// https://bugzilla.mozilla.org/show_bug.cgi?id=775566
// To test, use Rhino 1.7R3, or wait (heh) for the 1.7R5 release.
'test-rhino': {
'command': 'echo "Testing in Rhino..."; rhino -opt -1 "tests.js"',
'options': {
'execOptions': {
'cwd': 'tests'
}
}
},
'test-ringo': {
'command': 'echo "Testing in Ringo..."; ringo -o -1 "tests/tests.js"'
},
'test-node': {
'command': 'echo "Testing in Node..."; node "tests/tests.js"'
},
'test-browser': {
'command': 'echo "Testing in a browser..."; open "tests/index.html"'
}
},
'template': {
'build': {
'options': {
'data': function() {
return require('./scripts/export-data.js');
}
},
'files': {
'quoted-printable.js': ['src/quoted-printable.js']
}
}
}
});

grunt.loadNpmTasks('grunt-shell');
grunt.loadNpmTasks('grunt-template');

grunt.registerTask('cover', 'shell:cover');
grunt.registerTask('ci', [
'shell:test-narwhal',
'shell:test-phantomjs',
'shell:test-rhino',
'shell:test-ringo',
'shell:test-node',
]);
grunt.registerTask('test', [
'ci',
'shell:test-browser'
]);

grunt.registerTask('default', [
'template',
'shell:test-node'
]);

};
@@ -0,0 +1,20 @@
Copyright Mathias Bynens <http://mathiasbynens.be/>

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.
147 README.md
@@ -0,0 +1,147 @@
# quoted-printable [![Build status](https://travis-ci.org/mathiasbynens/quoted-printable.svg?branch=master)](https://travis-ci.org/mathiasbynens/quoted-printable) [![Dependency status](https://gemnasium.com/mathiasbynens/quoted-printable.svg)](https://gemnasium.com/mathiasbynens/quoted-printable)

_quoted-printable_ is a JavaScript implementation of [the `Quoted-Printable` content transfer encoding as defined by RFC 2045](http://tools.ietf.org/html/rfc2045#section-6.7), using UTF-8 for any non-ASCII symbols. It can be used to encode plaintext to its `Quoted-Printable` encoding, or the other way around (i.e. decoding). [Here’s an online demo.](http://mothereff.in/quoted-printable)

## Installation

Via [npm](http://npmjs.org/):

```bash
npm install quoted-printable
```

Via [Bower](http://bower.io/):

```bash
bower install quoted-printable
```

Via [Component](https://github.com/component/component):

```bash
component install mathiasbynens/quoted-printable
```

In a browser:

```html
<script src="quoted-printable.js"></script>
```

In [Narwhal](http://narwhaljs.org/), [Node.js](http://nodejs.org/), and [RingoJS](http://ringojs.org/):

```js
var quotedPrintable = require('quoted-printable');
```

In [Rhino](http://www.mozilla.org/rhino/):

```js
load('quoted-printable.js');
```

Using an AMD loader like [RequireJS](http://requirejs.org/):

```js
require(
{
'paths': {
'quoted-printable': 'path/to/quoted-printable'
}
},
['quoted-printable'],
function(quotedPrintable) {
console.log(quotedPrintable);
}
);
```

## API

### `quotedPrintable.version`

A string representing the semantic version number.

### `quotedPrintable.encode(text)`

This function takes a string of text (the `text` parameter) and `Quoted-Printable`-encodes it, using UTF-8 to encode any non-ASCII symbols.

```js
quotedPrintable.encode('foo=bar');
// → 'foo=3Dbar'
quotedPrintable.encode('Iñtërnâtiônàlizætiøn☃💩');
// → 'I=C3=B1t=C3=ABrn=C3=A2ti=C3=B4n=C3=A0liz=C3=A6ti=C3=B8n=E2=98=83=F0=9F=92=\r\n=A9'
```

### `quoted-printable.decode(text, options)`

This function takes a string of text (the `text` parameter) and `Quoted-Printable`-decodes it, using UTF-8 to decode any non-ASCII octets.

```js
quoted-printable.decode('foo=3Dbar');
// → 'foo=bar'
quoted-printable.decode('I=C3=B1t=C3=ABrn=C3=A2ti=C3=B4n=C3=A0liz=C3=A6ti=C3=B8n=E2=98=83=F0=9F=92=\r\n=A9');
// → 'Iñtërnâtiônàlizætiøn☃💩'
```

### Using the `quoted-printable` binary

To use the `quoted-printable` binary in your shell, simply install _quoted-printable_ globally using npm:

```bash
npm install -g quoted-printable
```

After that, you’ll be able to use `quoted-printable` on the command line:

```bash
$ quoted-printable --encode 'foo=bar'
foo=3Dbar
$ quoted-printable --decode 'foo=3Dbar'
foo=bar
```

Read a local text file, `Quoted-Printable`-encode it, and save the result to a new file:

```bash
$ quoted-printable --encode < foo.txt > foo-quoted-printable.txt
```

Or do the same with an online text file:

```bash
$ curl -sL 'http://mths.be/brh' | quoted-printable --encode > quoted-printable.txt
```

Or, the opposite — read a local file containing a `Quoted-Printable`-encoded message, decode it back to plain text, and save the result to a new file:

```bash
$ quoted-printable --decode < quoted-printable.txt > original.txt
```

See `quoted-printable --help` for the full list of options.

## Support

_quoted-printable_ is designed to work in at least Node.js v0.10.0, Narwhal 0.3.2, RingoJS 0.8-0.9, PhantomJS 1.9.0, Rhino 1.7RC4, as well as old and modern versions of Chrome, Firefox, Safari, Opera, and Internet Explorer.

## Unit tests & code coverage

After cloning this repository, run `npm install` to install the dependencies needed for development and testing. You may want to install Istanbul _globally_ using `npm install istanbul -g`.

Once that’s done, you can run the unit tests in Node using `npm test` or `node tests/tests.js`. To run the tests in Rhino, Ringo, Narwhal, and web browsers as well, use `grunt test`.

To generate the code coverage report, use `grunt cover`.

## Author

| [![twitter/mathias](https://gravatar.com/avatar/24e08a9ea84deb17ae121074d0f17125?s=70)](https://twitter.com/mathias "Follow @mathias on Twitter") |
|---|
| [Mathias Bynens](http://mathiasbynens.be/) |

## License

_quoted-printable_ is available under the [MIT](http://mths.be/mit) license.
@@ -0,0 +1,110 @@
#!/usr/bin/env node
(function() {

var fs = require('fs');
var quotedPrintable = require('../quoted-printable.js');
var strings = process.argv.splice(2);
var stdin = process.stdin;
var data;
var timeout;
var action;
var options = {};
var log = console.log;

var main = function() {
var option = strings[0];
var count = 0;

if (/^(?:-h|--help|undefined)$/.test(option)) {
log(
'quoted-printable v%s - http://mths.be/quoted-printable',
quotedPrintable.version
);
log([
'\nUsage:\n',
'\tquoted-printable [-e | --encode] string',
'\tquoted-printable [-d | --decode] string',
'\tquoted-printable [-v | --version]',
'\tquoted-printable [-h | --help]',
'\nExamples:\n',
'\tquoted-printable --encode \'foo = bar ©\'',
'\techo \'foo =3D bar =C2=A9\' | quoted-printable --decode',
].join('\n'));
return process.exit(1);
}

if (/^(?:-v|--version)$/.test(option)) {
log('v%s', quotedPrintable.version);
return process.exit(1);
}

strings.forEach(function(string) {
// Process options
if (string == '-e' || string == '--encode') {
action = 'encode';
return;
}
if (string == '-d' || string == '--decode') {
action = 'decode';
return;
}
// Process string(s)
var result;
if (!action) {
log('Error: quoted-printable requires at least one option and a string argument.');
log('Try `quoted-printable --help` for more information.');
return process.exit(1);
}
try {
result = quotedPrintable[action](string, options);
log(result);
count++;
} catch (exception) {
log(exception.message + '\n');
log('Error: failed to %s.', action);
log('If you think this is a bug in quoted-printable, please report it:');
log('https://github.com/mathiasbynens/quoted-printable/issues/new');
log('\nStack trace using quoted-printable@%s:\n', quotedPrintable.version);
log(exception.stack);
return process.exit(1);
}
});
if (!count) {
log('Error: quoted-printable requires a string argument.');
log('Try `quoted-printable --help` for more information.');
return process.exit(1);
}
// Return with exit status 0 outside of the `forEach` loop, in case
// multiple strings were passed in.
return process.exit(0);
};

if (stdin.isTTY) {
// handle shell arguments
main();
} else {
// Either the script is called from within a non-TTY context, or `stdin`
// content is being piped in.
if (!process.stdout.isTTY) {
// The script was called from a non-TTY context. This is a rather uncommon
// use case we don’t actively support. However, we don’t want the script
// to wait forever in such cases, so…
timeout = setTimeout(function() {
// …if no piped data arrived after a whole minute, handle shell
// arguments instead.
main();
}, 60000);
}
data = '';
stdin.on('data', function(chunk) {
clearTimeout(timeout);
data += chunk;
});
stdin.on('end', function() {
strings.push(data.trim());
main();
});
stdin.resume();
}

}());
Oops, something went wrong.

0 comments on commit 239f7dc

Please sign in to comment.