Skip to content

Commit 78cd13f

Browse files
author
Malte Legenhausen
committed
Closed #4
1 parent e75cf92 commit 78cd13f

File tree

3 files changed

+52
-6
lines changed

3 files changed

+52
-6
lines changed

README.md

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,26 @@ console.log(text);
4545

4646
You can configure the behaviour of html-to-text with the following options:
4747

48-
* `tables` allows to select certain tables by the `class` attribute from the HTML document. This is necessary because the majority of HTML E-Mails uses a table based layout. So you have to define which tables should be treaded as `table`. All other tables are ignored.
49-
* `wordwrap` defines after how many chars a line break should follow in `p` elements.
50-
48+
* `tables` allows to select certain tables by the `class` attribute from the HTML document. This is necessary because the majority of HTML E-Mails uses a table based layout. So you have to define which tables should be treaded as `table`. All other tables are ignored. Default: `[]`
49+
* `wordwrap` defines after how many chars a line break should follow in `p` elements. Default: `80`
50+
51+
## Command Line Interface
52+
53+
It is possible to use html-to-text as command line interface. This allows an easy validation of your generated text and the integration in other systems that does not run on node.js.
54+
55+
`html-to-text` uses `stdin` and `stdout` for data in and output. So you can use `html-to-html` the following way:
56+
57+
```
58+
cat examples/test.html | html-to-text > test.txt
59+
```
60+
61+
There also all options available as described above. You can use them like this:
62+
63+
```
64+
cat examples/test.html | html-to-text --tables=invoice,address --wordwrap=100 > test.txt
65+
```
66+
67+
The `tables` option has to be declared as comma separated list without whitespaces.
5168

5269
## Example
5370

bin/cli.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/usr/bin/env node
2+
var optimist = require('optimist');
3+
4+
var htmlToText = require('../lib/html-to-text');
5+
6+
var argv = optimist.default('tables', '').default('wordwrap', 80).argv;
7+
var text = '';
8+
9+
process.title = 'html-to-text';
10+
11+
process.stdin.resume();
12+
process.stdin.setEncoding('utf8');
13+
process.stdin.on('data', function data(data) {
14+
text += data;
15+
});
16+
17+
process.stdout.setEncoding('utf8');
18+
process.stdin.on('end', function end() {
19+
text = htmlToText.fromString(text, {
20+
tables: argv.tables.split(','),
21+
wordwrap: argv.wordwrap
22+
});
23+
process.stdout.write(text + '\n');
24+
});

package.json

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "html-to-text",
33
"version": "0.0.3",
4-
"description": "Simple html to text converter",
4+
"description": "Simple html to plain text converter",
55
"main": "index.js",
66
"scripts": {
77

@@ -11,16 +11,21 @@
1111
"dependencies": {
1212
"htmlparser": "1.x.x",
1313
"underscore": "1.x.x",
14-
"underscore.string": "2.x.x"
14+
"underscore.string": "2.x.x",
15+
"optimist": "0.x.x"
1516
},
1617
"keywords": [
17-
"html",
18+
"html",
1819
"node",
1920
"text",
2021
"mail",
22+
"plain",
2123
"converter"
2224
],
2325
"engines": {
2426
"node": "*"
27+
},
28+
"bin": {
29+
"html-to-text": "./bin/cli.js"
2530
}
2631
}

0 commit comments

Comments
 (0)