Skip to content

Commit

Permalink
initial
Browse files Browse the repository at this point in the history
  • Loading branch information
junosuarez committed Apr 29, 2013
0 parents commit 404310a
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
7 changes: 7 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright © 2013 Agile Diagnosis, Inc.

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.
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# html2plaintext
convert html-formatted text to plaintext

- strip tags
- treat whitespace like a browser
- decode html entities

## usage

var h2p = require('html2plaintext')

h2p('<p>Hello,\n &amp; <em>how are you?</em></p>')
// => "Hello, & how are you?"

## installation

$ npm install html2plaintext

## running the tests

From project root:

$ npm install
$ npm test

## contributors

jden <jason@denizac.org>

## license

MIT (c) 2013 Agile Diagnosis <hello@agilediagnosis.com>
37 changes: 37 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
var plumb = require('plumb')
var decode = require('htmldec')

var html2plaintext = plumb(
collapseWhitespace,
linebreaks,
stripTags,
decode,
trim
)

function stripTags (str) {
return str.replace(/<[^<]+>/g, '')
}

function collapseWhitespace (val) {
val = val.replace(/\s+/g, ' ')
return val
}

function linebreaks (str) {
return str.replace(/<\s?(p|br)[^<]*>/gi, function (x, tag) {
switch(tag.toLowerCase()){
case 'p':
return '\n\n'
case 'br':
return '\n'
}
return x
})
}

function trim (str) {
return str.trim()
}

module.exports = html2plaintext
30 changes: 30 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "html2plaintext",
"version": "0.1.0",
"description": "convert html-formatted text to plaintext",
"main": "index.js",
"directories": {
"test": "test"
},
"scripts": {
"test": "node node_modules/mocha/bin/mocha"
},
"repository": "git@github.com:AgileDiagnosis/html2plaintext.git",
"keywords": [
"html",
"plain",
"text"
],
"contributors": ["jden <jason@denizac.org>"],
"author": "Agile Diagnosis <hello@agilediagnosis.com>",
"license": "MIT",
"devDependencies": {
"heredoc": "~1.1.0",
"chai": "~1.5.0",
"mocha": "~1.9.0"
},
"dependencies": {
"htmldec": "0.0.1",
"plumb": "~0.1.0"
}
}
43 changes: 43 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
var chai = require('chai')
chai.should()
var heredoc = require('heredoc')
var h2p = require('../index')

describe('html2plaintext', function () {
it('strips html tags', function () {

h2p('<b>HELLO <blink>world</blink></b>')
.should.equal('HELLO world')

})
it('treats whitespace like a browser - trim', function () {
h2p('<b>Hey there, Jon</b> \n')
.should.equal('Hey there, Jon')
})
it('treats whitespace like a browser - collapse repeated space', function () {

h2p('<h1>It is aa maaaa zzzzz ing!</h1>')
.should.equal('It is aa maaaa zzzzz ing!')

})
it('ignores linebreaks', function () {
h2p('hi\n\n\n\n<b>foo</b>\n')
.should.equal('hi foo')
})
it('ignores single linebreaks', function () {
h2p('hi\nii')
.should.equal('hi ii')
})
it('inserts 2 linebreaks after </p>', function () {
h2p('<p>it was the best of times</p><p>it was the worst of times</p>')
.should.equal('it was the best of times\n\nit was the worst of times')
})
it('inserts 1 linebreak <br>', function () {
h2p('hello<br/>goodbye')
.should.equal('hello\ngoodbye')
})
it('decodes html entities', function () {
h2p('&lt;3 &amp; &lt;/3')
.should.equal('<3 & </3')
})
})

0 comments on commit 404310a

Please sign in to comment.