Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
* New Block and Span node types used for extended syntax
* Support classes and attributes on nodes
  • Loading branch information
joehewitt committed Nov 3, 2011
1 parent 7fb0077 commit a49a0ac
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 25 deletions.
8 changes: 7 additions & 1 deletion lib/NodeHandler.js
Expand Up @@ -111,11 +111,17 @@ NodeHandler.prototype = {
var nodes = [];
if (ids) {
var idns = ids.split(',');
var lastNode;
for (var i = 0; i < idns.length; ++i) {
var id = idns[i];
var node = this.nodes[id];
if (node) {
nodes.push(node);
if (node instanceof NodeTypes.Text && lastNode instanceof NodeTypes.Text) {
lastNode.text += node.text;
} else {
nodes.push(node);
lastNode = node;
}
}
}
}
Expand Down
12 changes: 12 additions & 0 deletions lib/NodeTransformer.js
Expand Up @@ -12,6 +12,10 @@ NodeTransformer.prototype = {
node.visit(this);
if (node instanceof NodeTypes.NodeSet) {
return this.nodeSet(node);
} else if (node instanceof NodeTypes.Block) {
return this.block(node);
} else if (node instanceof NodeTypes.Span) {
return this.span(node);
} else if (node instanceof NodeTypes.Header) {
return this.header(node);
} else if (node instanceof NodeTypes.Paragraph) {
Expand Down Expand Up @@ -57,6 +61,14 @@ NodeTransformer.prototype = {
return node;
},

block: function(node) {
return node;
},

span: function(node) {
return node;
},

header: function(node) {
return node;
},
Expand Down
124 changes: 100 additions & 24 deletions lib/nodes.js
Expand Up @@ -3,19 +3,52 @@ var _ = require('underscore');

// *************************************************************************************************

function openTag(tagName, className) {
return '<' + tagName + (className ? ' class="' + className + '"' : '');
}

// *************************************************************************************************

function Node() {

}

Node.prototype = {
visit: function(visitor) {
},

setAttribute: function(name, value) {
if (!this.attributes) {
this.attributes = {};
}
this.attributes[name] = value;
},

addClass: function(name) {
if (!this.classes) {
this.classes = [];
}
this.classes.push(name);
},

openTag: function(tag, leaveOpen) {
var classNode = this;
var attrs = '';
if (this.content && this.content.nodes) {
var lastNode = this.content.nodes[this.content.nodes.length-1];
if (lastNode && lastNode.classes) {
classNode = lastNode;
}
}

if (classNode.classes) {
attrs += ' class="' + classNode.classes.join(' ') + '"';
} else if (classNode.className) {
attrs += ' class="' + classNode.className + '"';
}

if (classNode.attributes) {
for (var name in classNode.attributes) {
attrs += ' ' + name + '="' + classNode.attributes[name] + '"';
}
}

return '<' + tag + attrs + (leaveOpen ? '' : '>');
}
};

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Expand All @@ -40,6 +73,46 @@ NodeSet.prototype = subclass(Node, {

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

function Block(content) {
this.content = content;
}

Block.prototype = subclass(Node, {
visit: function(visitor) {
this.content = visitor.visit(this.content);
},

toHTML: function() {
return this.openTag('div') + (this.content ? this.content.toHTML() : '')+ '</div>';
},

toMarkdown: function() {
return this.content.toMarkdown();
}
});

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

function Span(content) {
this.content = content;
}

Span.prototype = subclass(Node, {
visit: function(visitor) {
this.content = visitor.visit(this.content);
},

toHTML: function() {
return this.openTag('span') + (this.content ? this.content.toHTML() : '')+ '</span>';
},

toMarkdown: function() {
return this.content.toMarkdown();
}
});

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

function Header(level, content) {
this.level = level;
this.content = content;
Expand All @@ -52,7 +125,7 @@ Header.prototype = subclass(Node, {

toHTML: function() {
var tag = 'h' + this.level;
return '<' + tag + '>' + this.content.toHTML() + '</' + tag + '>';
return this.openTag(tag) + this.content.toHTML() + '</' + tag + '>';
},

toMarkdown: function() {
Expand All @@ -67,7 +140,7 @@ function HRule() {

HRule.prototype = subclass(Node, {
toHTML: function() {
return '<hr>';
return this.openTag('hr');
},

toMarkdown: function() {
Expand All @@ -82,7 +155,7 @@ function LineBreak() {

LineBreak.prototype = subclass(Node, {
toHTML: function() {
return '<br>';
return this.openTag('br');
},

toMarkdown: function() {
Expand All @@ -102,7 +175,7 @@ Paragraph.prototype = subclass(Node, {
},

toHTML: function() {
return '<p>' + this.content.toHTML() + '</p>';
return this.openTag('p') + this.content.toHTML() + '</p>';
},

toMarkdown: function() {
Expand All @@ -122,7 +195,7 @@ Blockquote.prototype = subclass(Node, {
},

toHTML: function() {
return '<blockquote>' + this.content.toHTML() + '</blockquote>';
return this.openTag('blockquote') + this.content.toHTML() + '</blockquote>';
},

toMarkdown: function() {
Expand All @@ -139,7 +212,7 @@ function BlockCode(lang, text) {

BlockCode.prototype = subclass(Node, {
toHTML: function() {
return '<pre><code>' + escapeHTML(this.text) + '</code></pre>';
return this.openTag('pre') + '<code>' + escapeHTML(this.text) + '</code></pre>';
},

toMarkdown: function(indent) {
Expand Down Expand Up @@ -179,7 +252,7 @@ List.prototype = subclass(Node, {

toHTML: function() {
var tag = this.ordered ? 'ol' : 'ul';
return '<' + tag + '>' + this.items.toHTML() + '</' + tag + '>';
return this.openTag(tag) + this.items.toHTML() + '</' + tag + '>';
},

toMarkdown: function(indent) {
Expand All @@ -204,7 +277,7 @@ ListItem.prototype = subclass(Node, {
},

toHTML: function() {
return '<li>' + this.content.toHTML() + '</li>';
return this.openTag('li') + this.content.toHTML() + '</li>';
},

toMarkdown: function(indent) {
Expand All @@ -227,7 +300,7 @@ Table.prototype = subclass(Node, {
},

toHTML: function() {
return '<table><th>' + this.header.toHTML() + '</th>'
return this.openTag('table') + '<th>' + this.header.toHTML() + '</th>'
+ '<tbody>' + this.body.toHTML() + '</tbody></table>';
},

Expand Down Expand Up @@ -255,7 +328,7 @@ TableRow.prototype = subclass(Node, {
},

toHTML: function() {
return '<tr>' + this.cells.toHTML() + '</tr>';
return this.openTag('tr') + this.cells.toHTML() + '</tr>';
},

toMarkdown: function(indent) {
Expand All @@ -278,7 +351,7 @@ TableCell.prototype = subclass(Node, {
},

toHTML: function() {
return '<td>' + this.content.toHTML() + '</td>';
return this.openTag('td') + this.content.toHTML() + '</td>';
},

toMarkdown: function(indent) {
Expand All @@ -300,11 +373,11 @@ Emphasis.prototype = subclass(Node, {

toHTML: function() {
if (this.depth == 1) {
return '<em>' + this.content.toHTML() + '</em>';
return this.openTag('em') + this.content.toHTML() + '</em>';
} else if (this.depth == 2) {
return '<strong>' + this.content.toHTML() + '</strong>';
return this.openTag('strong') + this.content.toHTML() + '</strong>';
} else if (this.depth == 3) {
return '<strong><em>' + this.content.toHTML() + '</em></strong>';
return this.openTag('strong') + '<em>' + this.content.toHTML() + '</em></strong>';
}
},

Expand All @@ -325,7 +398,7 @@ Strikethrough.prototype = subclass(Node, {
},

toHTML: function() {
return '<strike>' + this.content.toHTML() + '</strike>';
return this.openTag('strike') + this.content.toHTML() + '</strike>';
},

toMarkdown: function() {
Expand All @@ -341,7 +414,7 @@ function CodeSpan(text) {

CodeSpan.prototype = subclass(Node, {
toHTML: function() {
return '<code>' + escapeHTML(this.text) + '</code>';
return this.openTag('code') + escapeHTML(this.text) + '</code>';
},

toMarkdown: function(indent) {
Expand All @@ -363,7 +436,7 @@ Link.prototype = subclass(Node, {
},

toHTML: function() {
return openTag('a', this.className)
return this.openTag('a', true)
+ ' href="' + this.url + '">' + (this.content ? this.content.toHTML() : '') + '</a>';
},

Expand All @@ -385,7 +458,7 @@ Image.prototype = subclass(Node, {
toHTML: function() {
var w = this.width == undefined ? '' : (' width="' + this.width + '"');
var h = this.height == undefined ? '' : (' height="' + this.height + '"');
return openTag('img', this.className) + ' src="' + this.url + '"' + w + h + '>';
return this.openTag('img', true) + ' src="' + this.url + '"' + w + h + '>';
},

toMarkdown: function() {
Expand Down Expand Up @@ -513,6 +586,8 @@ function escapeHTML(text) {

exports.Node = Node;
exports.NodeSet = NodeSet;
exports.Block = Block;
exports.Span = Span;
exports.Paragraph = Paragraph;
exports.Header = Header;
exports.HRule = HRule;
Expand All @@ -534,3 +609,4 @@ exports.Text = Text;
exports.Script = Script;
exports.Embed = Embed;
exports.Raw = Raw;

0 comments on commit a49a0ac

Please sign in to comment.