0.0.4-alpha • Published 13 years ago
tag-parser v0.0.4-alpha
tag-parser.js
An NPM for parsing text markup
TagParser
First include the module:
var TagParser = require('tag-parser');then, instantiate the object:
var parser = new TagParser([environments]);where environments may contain:
- name :
- sentinels : a list of the following mixed types:- an array of 2 strings, the first being the 'open' sentinel, the second, 'close'.
- an object containing 'opener' && 'closer'
- a single string that is both the opener and closer
 
- onParse : a callback allowing you to make custom modifications of an existing tag
parse with:
var parseTree = parser.parse(text);HTMLParser
First include the module:
var HTMLParser = require('tag-parser/html');then, instantiate the object:
var parser = new HTMLParser();parse with:
var parseTree = parser.parse('<html><head><title>Awesome!</title></head><body onload="callReady()"><h1>Congrats</h1><p>It worked.</p><!--a comment--></body></html>');which will produce:
{
    type: 'tag',
    text: 'html',
    name: 'html',
    attributes: {},
    children: [
        {
            type: 'tag',
            text: 'head',
            name: 'head',
            attributes: {},
            children: [
                {
                    type: 'tag',
                    text: 'title',
                    name: 'title',
                    attributes: {},
                    children: [
                        'Awesome!'
                    ]
                }
            ]
        },
        {
            type: 'tag',
            text: 'body onload="callReady()"',
            name: 'body',
            attributes: {
                onload : 'callReady()'
            },
            children: [
                {
                    type: 'tag',
                    text: 'h1',
                    name: 'h1',
                    attributes: {},
                    children: [
                        'Congrats!'
                    ]
                },
                {
                    type: 'tag',
                    text: 'p',
                    name: 'p',
                    attributes: {},
                    children: [
                        'It worked.'
                    ]
                },
                {
                    type: 'comment',
                    text: 'a comment',
                    children: [
                        'a comment'
                    ]
                }
            ]
        }
    ]
}Testing
Run the tests at the project root with:
mochaEnjoy,
-Abbey Hawk Sparrow