better paragraphs

This commit is contained in:
Christopher Jeffrey 2011-08-18 18:59:42 -05:00
parent 76d7ee9bc4
commit dbb20c0e40

View File

@ -10,7 +10,7 @@
*/ */
var block = { var block = {
newline: /^\n/, newline: /^\n+/,
block: /^ {4,}[^\n]*(?:\n {4,}[^\n]*)*/, block: /^ {4,}[^\n]*(?:\n {4,}[^\n]*)*/,
heading: /^ *(#{1,6}) *([^\n#]*) *#*/, heading: /^ *(#{1,6}) *([^\n#]*) *#*/,
lheading: /^([^\n]+)\n *(=|-){3,}/, lheading: /^([^\n]+)\n *(=|-){3,}/,
@ -63,10 +63,10 @@ block.lexer = function(str) {
tokens.links = links; tokens.links = links;
return block.token(str, tokens, 0); return block.token(str, tokens);
}; };
block.token = function(str, tokens, line) { block.token = function(str, tokens) {
var rules = block var rules = block
, keys = block.keys , keys = block.keys
, len = keys.length , len = keys.length
@ -87,43 +87,42 @@ block.token = function(str, tokens, line) {
while (scan()) { while (scan()) {
switch (key) { switch (key) {
case 'newline': case 'newline':
line++; if (cap[0].length > 1) {
tokens.push({
type: 'space'
});
}
break; break;
case 'hr': case 'hr':
tokens.push({ tokens.push({
type: 'hr', type: 'hr'
line: line
}); });
break; break;
case 'lheading': case 'lheading':
tokens.push({ tokens.push({
type: 'heading', type: 'heading',
depth: cap[2] === '=' ? 1 : 2, depth: cap[2] === '=' ? 1 : 2,
text: cap[1], text: cap[1]
line: line
}); });
break; break;
case 'heading': case 'heading':
tokens.push({ tokens.push({
type: 'heading', type: 'heading',
depth: cap[1].length, depth: cap[1].length,
text: cap[2], text: cap[2]
line: line
}); });
break; break;
case 'block': case 'block':
cap = cap[0].replace(/^ {4}/gm, ''); cap = cap[0].replace(/^ {4}/gm, '');
tokens.push({ tokens.push({
type: 'block', type: 'block',
text: cap, text: cap
line: line
}); });
break; break;
case 'list': case 'list':
tokens.push({ tokens.push({
type: 'list_start', type: 'list_start',
ordered: isFinite(cap[2]), ordered: isFinite(cap[2])
line: line
}); });
// get each top-level // get each top-level
// item in the list // item in the list
@ -142,38 +141,32 @@ block.token = function(str, tokens, line) {
item = item.replace(space, ''); item = item.replace(space, '');
} }
tokens.push({ tokens.push({
type: 'list_item_start', type: 'list_item_start'
line: line
}); });
block.token(item, tokens, line); block.token(item, tokens);
tokens.push({ tokens.push({
type: 'list_item_end', type: 'list_item_end'
line: line
}); });
}); });
tokens.push({ tokens.push({
type: 'list_end', type: 'list_end'
line: line
}); });
break; break;
case 'html': case 'html':
case 'text': case 'text':
tokens.push({ tokens.push({
type: key, type: key,
text: cap[0], text: cap[0]
line: line
}); });
break; break;
case 'blockquote': case 'blockquote':
tokens.push({ tokens.push({
type: 'blockquote_start', type: 'blockquote_start'
line: line
}); });
cap = cap[0].replace(/^ *>/gm, ''); cap = cap[0].replace(/^ *>/gm, '');
block.token(cap, tokens, line); block.token(cap, tokens);
tokens.push({ tokens.push({
type: 'blockquote_end', type: 'blockquote_end'
line: line
}); });
break; break;
} }
@ -357,6 +350,8 @@ var next = function() {
var tok = function() { var tok = function() {
switch (token.type) { switch (token.type) {
case 'space':
return '';
case 'hr': case 'hr':
return '<hr>'; return '<hr>';
case 'heading': case 'heading':
@ -400,11 +395,9 @@ var tok = function() {
return inline.lexer(token.text); return inline.lexer(token.text);
case 'text': case 'text':
var body = [ token.text ] var body = [ token.text ]
, last = token.line
, top; , top;
while ((top = tokens[tokens.length-1]) while ((top = tokens[tokens.length-1])
&& top.type === 'text' && top.type === 'text') {
&& top.line === ++last) {
body.push(next().text); body.push(next().text);
} }
return '<p>' return '<p>'