Permalink
Browse files

Shouldn't have to instantiate a method with bbcode

The BBCode parser should be truly functional in that calling it twice
should have no side effects. Thus, it will parse the input and spit out
the text as requested and we only need to export parse.

Also, move the tests and update versions/readme/etc.

Signed-off-by: Nick Campbell <nicholas.j.campbell@gmail.com>
  • Loading branch information...
1 parent 3139536 commit 5f4bdccddd7b47810049ebeb54aa220987db3f97 @ncb000gt committed Aug 18, 2012
Showing with 30 additions and 46 deletions.
  1. +2 −1 .gitignore
  2. +9 −0 .travis.yml
  3. +10 −0 Makefile
  4. +5 −1 README.md
  5. +1 −6 lib/bbcode.js
  6. +2 −3 package.json
  7. +1 −35 tests.js → tests/parse.js
View
3 .gitignore
@@ -1 +1,2 @@
-*.swp
+*.sw[a-z]
+node_modules
View
9 .travis.yml
@@ -0,0 +1,9 @@
+language: node_js
+node_js:
+ - 0.4
+ - 0.6
+ - 0.7
+ - 0.8
+notifications:
+ email:
+ on_success: never
View
10 Makefile
@@ -0,0 +1,10 @@
+TESTS = tests/*.js
+
+all: test
+
+test:
+ npm install .
+ @./node_modules/nodeunit/bin/nodeunit \
+ $(TESTS)
+
+.PHONY: test
View
6 README.md
@@ -7,9 +7,13 @@ This project aims to provide a robust BBCode Parser for NodeJS. Originally based
Usage
=============
+Install with NPM.
npm install bbcode
- var bbcode = new (require('bbcode'))({});
+
+Write some code.
+
+ var bbcode = require('bbcode');
bbcode.parse('[b]text[/b]', function(content) { ... }
View
7 lib/bbcode.js
@@ -61,11 +61,6 @@ var uri_re = /^[-;\/\?:@&=\+\$,_\.!~\*'\(\)%0-9a-z]{1,512}$/i;
// main regular expression: CRLF, [tag=option], [tag] or [/tag]
var postfmt_re = /([\r\n])|(?:\[([a-z]{1,16})(?:=([^\x00-\x1F"'\(\)<>\[\]]{1,256}))?\])|(?:\[\/([a-z]{1,16})\])/ig;
-function BBCode(opts) {
-}
-
-module.exports = BBCode;
-
// stack frame object
function taginfo_t(bbtag, etag)
{
@@ -229,7 +224,7 @@ function textToHtmlCB(mstr, m1, m2, m3, m4, offset, string)
//
// post must be HTML-encoded
//
-BBCode.prototype.parse = function(post, cb) {
+exports.parse = function(post, cb) {
var result = '', endtags, tag;
// convert CRLF to <br> by default
View
5 package.json
@@ -2,9 +2,8 @@
"name": "bbcode",
"description": "A BBCode Parser for NodeJS",
"main": "./lib/bbcode",
- "version": "0.0.2",
+ "version": "0.1.0",
"author": "Nick Campbell (http://github.com/ncb000gt)",
- "engines": { "node": ">= 0.1.100" },
"repository": {
"type": "git",
"url": "http://github.com/ncb000gt/node-bbcode.git"
@@ -15,7 +14,7 @@
}
],
"bugs": {
- "web" : "http://github.com/ncb000gt/node-bbcode/issues"
+ "url" : "http://github.com/ncb000gt/node-bbcode/issues"
},
"dependencies": {
"nodeunit": ">=0.3.1"
View
36 tests.js → tests/parse.js
@@ -1,35 +1,31 @@
-var bbcode_lib = require('./lib/bbcode'),
+var bbcode = require('../lib/bbcode'),
testCase = require('nodeunit').testCase;
module.exports = testCase(
{
'test bold': function(assert) {
assert.expect(1);
- var bbcode = new bbcode_lib({});
bbcode.parse('[b]Bold[/b]', function(data) {
assert.equals('<b>Bold</b>', data);
assert.done();
});
},
'test italics': function(assert) {
assert.expect(1);
- var bbcode = new bbcode_lib({});
bbcode.parse('[i]italics[/i]', function(data) {
assert.equals('<i>italics</i>', data);
assert.done();
});
},
'test underline': function(assert) {
assert.expect(1);
- var bbcode = new bbcode_lib({});
bbcode.parse('[u]underline[/u]', function(data) {
assert.equals('<u>underline</u>', data);
assert.done();
});
},
'test samp': function(assert) {
assert.expect(1);
- var bbcode = new bbcode_lib({});
bbcode.parse('[samp]samp[/samp]', function(data) {
assert.equals('<samp>samp</samp>', data);
assert.done();
@@ -38,15 +34,13 @@ module.exports = testCase(
'test code': function(assert) {
assert.expect(1);
- var bbcode = new bbcode_lib({});
bbcode.parse('[code]Code[/code]', function(data) {
assert.equals('<pre><code>Code</code></pre>', data);
assert.done();
});
},
'test pre': function(assert) {
assert.expect(1);
- var bbcode = new bbcode_lib({});
bbcode.parse('[pre]Pre[/pre]', function(data) {
assert.equals('<pre>Pre</pre>', data);
assert.done();
@@ -56,143 +50,125 @@ module.exports = testCase(
// test all colors: since they are regexed
'test color-black': function(assert) {
assert.expect(1);
- var bbcode = new bbcode_lib({});
bbcode.parse('[color=black]Black[/color]', function(data) {
assert.equals('<span style="color: black">Black</span>', data);
assert.done();
});
},
'test color-silver': function(assert) {
assert.expect(1);
- var bbcode = new bbcode_lib({});
bbcode.parse('[color=silver]silver[/color]', function(data) {
assert.equals('<span style="color: silver">silver</span>', data);
assert.done();
});
},
'test color-gray': function(assert) {
assert.expect(1);
- var bbcode = new bbcode_lib({});
bbcode.parse('[color=gray]gray[/color]', function(data) {
assert.equals('<span style="color: gray">gray</span>', data);
assert.done();
});
},
'test color-white': function(assert) {
assert.expect(1);
- var bbcode = new bbcode_lib({});
bbcode.parse('[color=white]white[/color]', function(data) {
assert.equals('<span style="color: white">white</span>', data);
assert.done();
});
},
'test color-maroon': function(assert) {
assert.expect(1);
- var bbcode = new bbcode_lib({});
bbcode.parse('[color=maroon]maroon[/color]', function(data) {
assert.equals('<span style="color: maroon">maroon</span>', data);
assert.done();
});
},
'test color-red': function(assert) {
assert.expect(1);
- var bbcode = new bbcode_lib({});
bbcode.parse('[color=red]red[/color]', function(data) {
assert.equals('<span style="color: red">red</span>', data);
assert.done();
});
},
'test color-purple': function(assert) {
assert.expect(1);
- var bbcode = new bbcode_lib({});
bbcode.parse('[color=purple]purple[/color]', function(data) {
assert.equals('<span style="color: purple">purple</span>', data);
assert.done();
});
},
'test color-fuchsia': function(assert) {
assert.expect(1);
- var bbcode = new bbcode_lib({});
bbcode.parse('[color=fuchsia]fuchsia[/color]', function(data) {
assert.equals('<span style="color: fuchsia">fuchsia</span>', data);
assert.done();
});
},
'test color-green': function(assert) {
assert.expect(1);
- var bbcode = new bbcode_lib({});
bbcode.parse('[color=green]Green[/color]', function(data) {
assert.equals('<span style="color: green">Green</span>', data);
assert.done();
});
},
'test color-lime': function(assert) {
assert.expect(1);
- var bbcode = new bbcode_lib({});
bbcode.parse('[color=lime]lime[/color]', function(data) {
assert.equals('<span style="color: lime">lime</span>', data);
assert.done();
});
},
'test color-olive': function(assert) {
assert.expect(1);
- var bbcode = new bbcode_lib({});
bbcode.parse('[color=olive]olive[/color]', function(data) {
assert.equals('<span style="color: olive">olive</span>', data);
assert.done();
});
},
'test color-yellow': function(assert) {
assert.expect(1);
- var bbcode = new bbcode_lib({});
bbcode.parse('[color=yellow]yellow[/color]', function(data) {
assert.equals('<span style="color: yellow">yellow</span>', data);
assert.done();
});
},
'test color-navy': function(assert) {
assert.expect(1);
- var bbcode = new bbcode_lib({});
bbcode.parse('[color=navy]navy[/color]', function(data) {
assert.equals('<span style="color: navy">navy</span>', data);
assert.done();
});
},
'test color-blue': function(assert) {
assert.expect(1);
- var bbcode = new bbcode_lib({});
bbcode.parse('[color=blue]blue[/color]', function(data) {
assert.equals('<span style="color: blue">blue</span>', data);
assert.done();
});
},
'test color-teal': function(assert) {
assert.expect(1);
- var bbcode = new bbcode_lib({});
bbcode.parse('[color=teal]teal[/color]', function(data) {
assert.equals('<span style="color: teal">teal</span>', data);
assert.done();
});
},
'test color-aqua': function(assert) {
assert.expect(1);
- var bbcode = new bbcode_lib({});
bbcode.parse('[color=aqua]aqua[/color]', function(data) {
assert.equals('<span style="color: aqua">aqua</span>', data);
assert.done();
});
},
'test color-#fff': function(assert) {
assert.expect(1);
- var bbcode = new bbcode_lib({});
bbcode.parse('[color=#fff]#fff[/color]', function(data) {
assert.equals('<span style="color: #fff">#fff</span>', data);
assert.done();
});
},
'test color-#ffffff': function(assert) {
assert.expect(1);
- var bbcode = new bbcode_lib({});
bbcode.parse('[color=#ffffff]#ffffff[/color]', function(data) {
assert.equals('<span style="color: #ffffff">#ffffff</span>', data);
assert.done();
@@ -201,23 +177,20 @@ module.exports = testCase(
'test size': function(assert) {
assert.expect(1);
- var bbcode = new bbcode_lib({});
bbcode.parse('[size=1.5]size[/size]', function(data) {
assert.equals('<span style="font-size: 1.5em">size</span>', data);
assert.done();
});
},
'test size-min': function(assert) {
assert.expect(1);
- var bbcode = new bbcode_lib({});
bbcode.parse('[size=0.5]min[/size]', function(data) {
assert.equals('<span style="font-size: 0.7em">min</span>', data);
assert.done();
});
},
'test size-max': function(assert) {
assert.expect(1);
- var bbcode = new bbcode_lib({});
bbcode.parse('[size=4]max[/size]', function(data) {
assert.equals('<span style="font-size: 3em">max</span>', data);
assert.done();
@@ -226,7 +199,6 @@ module.exports = testCase(
'test strikethrough': function(assert) {
assert.expect(1);
- var bbcode = new bbcode_lib({});
bbcode.parse('[s]strikethrough[/s]', function(data) {
assert.equals('<span style="text-decoration: line-through">strikethrough</span>', data);
assert.done();
@@ -235,23 +207,20 @@ module.exports = testCase(
'test url': function(assert) {
assert.expect(1);
- var bbcode = new bbcode_lib({});
bbcode.parse('[url=http://example.com]url[/url]', function(data) {
assert.equals('<a href="http://example.com">url</a>', data);
assert.done();
});
},
'test img with =': function(assert) {
assert.expect(1);
- var bbcode = new bbcode_lib({});
bbcode.parse('[img=http://example.com/img.png][/img]', function(data) {
assert.equals('<img src="http://example.com/img.png" />', data);
assert.done();
});
},
'test img as content': function(assert) {
assert.expect(1);
- var bbcode = new bbcode_lib({});
bbcode.parse('[img]http://example.com/img.png[/img]', function(data) {
assert.equals('<img src="http://example.com/img.png" />', data);
assert.done();
@@ -260,15 +229,13 @@ module.exports = testCase(
'test quote': function(assert) {
assert.expect(1);
- var bbcode = new bbcode_lib({});
bbcode.parse('[q=person]quoted[/q]', function(data) {
assert.equals('<q cite="person">quoted</q>', data);
assert.done();
});
},
'test blockquote': function(assert) {
assert.expect(1);
- var bbcode = new bbcode_lib({});
bbcode.parse('[blockquote=person]quoted[/blockquote]', function(data) {
assert.equals('<blockquote cite="person">quoted</blockquote>', data);
assert.done();
@@ -277,7 +244,6 @@ module.exports = testCase(
'test broken': function(assert) {
assert.expect(1);
- var bbcode = new bbcode_lib({});
bbcode.parse('[b]test', function(data) {
assert.equals('<b>test</b>', data);
assert.done();

0 comments on commit 5f4bdcc

Please sign in to comment.