Skip to content

Commit

Permalink
refactor, use consistent variable names
Browse files Browse the repository at this point in the history
  • Loading branch information
chjj committed Jan 30, 2012
1 parent ac4ca9d commit c2d1e31
Showing 1 changed file with 65 additions and 67 deletions.
132 changes: 65 additions & 67 deletions lib/marked.js
Expand Up @@ -70,20 +70,20 @@ block.paragraph = (function() {
* Block Lexer
*/

block.lexer = function(str) {
block.lexer = function(src) {
var tokens = [];

tokens.links = {};

str = str
src = src
.replace(/\r\n|\r/g, '\n')
.replace(/\t/g, ' ');

return block.token(str, tokens, true);
return block.token(src, tokens, true);
};

block.token = function(str, tokens, top) {
var str = str.replace(/^ +$/gm, '')
block.token = function(src, tokens, top) {
var src = src.replace(/^ +$/gm, '')
, next
, loose
, cap
Expand All @@ -92,10 +92,10 @@ block.token = function(str, tokens, top) {
, i
, l;

while (str) {
while (src) {
// newline
if (cap = block.newline.exec(str)) {
str = str.substring(cap[0].length);
if (cap = block.newline.exec(src)) {
src = src.substring(cap[0].length);
if (cap[0].length > 1) {
tokens.push({
type: 'space'
Expand All @@ -104,8 +104,8 @@ block.token = function(str, tokens, top) {
}

// code
if (cap = block.code.exec(str)) {
str = str.substring(cap[0].length);
if (cap = block.code.exec(src)) {
src = src.substring(cap[0].length);
cap = cap[0].replace(/^ {4}/gm, '');
tokens.push({
type: 'code',
Expand All @@ -115,8 +115,8 @@ block.token = function(str, tokens, top) {
}

// gfm_code
if (cap = block.gfm_code.exec(str)) {
str = str.substring(cap[0].length);
if (cap = block.gfm_code.exec(src)) {
src = src.substring(cap[0].length);
tokens.push({
type: 'code',
lang: cap[1],
Expand All @@ -126,8 +126,8 @@ block.token = function(str, tokens, top) {
}

// heading
if (cap = block.heading.exec(str)) {
str = str.substring(cap[0].length);
if (cap = block.heading.exec(src)) {
src = src.substring(cap[0].length);
tokens.push({
type: 'heading',
depth: cap[1].length,
Expand All @@ -137,8 +137,8 @@ block.token = function(str, tokens, top) {
}

// lheading
if (cap = block.lheading.exec(str)) {
str = str.substring(cap[0].length);
if (cap = block.lheading.exec(src)) {
src = src.substring(cap[0].length);
tokens.push({
type: 'heading',
depth: cap[2] === '=' ? 1 : 2,
Expand All @@ -148,17 +148,17 @@ block.token = function(str, tokens, top) {
}

// hr
if (cap = block.hr.exec(str)) {
str = str.substring(cap[0].length);
if (cap = block.hr.exec(src)) {
src = src.substring(cap[0].length);
tokens.push({
type: 'hr'
});
continue;
}

// blockquote
if (cap = block.blockquote.exec(str)) {
str = str.substring(cap[0].length);
if (cap = block.blockquote.exec(src)) {
src = src.substring(cap[0].length);
tokens.push({
type: 'blockquote_start'
});
Expand All @@ -177,8 +177,8 @@ block.token = function(str, tokens, top) {
}

// list
if (cap = block.list.exec(str)) {
str = str.substring(cap[0].length);
if (cap = block.list.exec(src)) {
src = src.substring(cap[0].length);

tokens.push({
type: 'list_start',
Expand Down Expand Up @@ -240,8 +240,8 @@ block.token = function(str, tokens, top) {
}

// html
if (cap = block.html.exec(str)) {
str = str.substring(cap[0].length);
if (cap = block.html.exec(src)) {
src = src.substring(cap[0].length);
tokens.push({
type: 'html',
text: cap[0]
Expand All @@ -250,8 +250,8 @@ block.token = function(str, tokens, top) {
}

// def
if (top && (cap = block.def.exec(str))) {
str = str.substring(cap[0].length);
if (top && (cap = block.def.exec(src))) {
src = src.substring(cap[0].length);
tokens.links[cap[1].toLowerCase()] = {
href: cap[2],
title: cap[3]
Expand All @@ -260,8 +260,8 @@ block.token = function(str, tokens, top) {
}

// top-level paragraph
if (top && (cap = block.paragraph.exec(str))) {
str = str.substring(cap[0].length);
if (top && (cap = block.paragraph.exec(src))) {
src = src.substring(cap[0].length);
tokens.push({
type: 'paragraph',
text: cap[0]
Expand All @@ -270,9 +270,9 @@ block.token = function(str, tokens, top) {
}

// text
if (cap = block.text.exec(str)) {
if (cap = block.text.exec(src)) {
// Top-level should never reach here.
str = str.substring(cap[0].length);
src = src.substring(cap[0].length);
tokens.push({
type: 'text',
text: cap[0]
Expand Down Expand Up @@ -307,25 +307,25 @@ var inline = {
* Inline Lexer
*/

inline.lexer = function(str) {
inline.lexer = function(src) {
var out = ''
, links = tokens.links
, link
, text
, href
, cap;

while (str) {
while (src) {
// escape
if (cap = inline.escape.exec(str)) {
str = str.substring(cap[0].length);
if (cap = inline.escape.exec(src)) {
src = src.substring(cap[0].length);
out += cap[1];
continue;
}

// autolink
if (cap = inline.autolink.exec(str)) {
str = str.substring(cap[0].length);
if (cap = inline.autolink.exec(src)) {
src = src.substring(cap[0].length);
if (cap[2] === '@') {
text = cap[1][6] === ':'
? mangle(cap[1].substring(7))
Expand All @@ -344,8 +344,8 @@ inline.lexer = function(str) {
}

// gfm_autolink
if (cap = inline.gfm_autolink.exec(str)) {
str = str.substring(cap[0].length);
if (cap = inline.gfm_autolink.exec(src)) {
src = src.substring(cap[0].length);
text = escape(cap[1]);
href = text;
out += '<a href="'
Expand All @@ -357,19 +357,19 @@ inline.lexer = function(str) {
}

// tag
if (cap = inline.tag.exec(str)) {
str = str.substring(cap[0].length);
if (cap = inline.tag.exec(src)) {
src = src.substring(cap[0].length);
out += cap[0];
continue;
}

// link
if (cap = inline.link.exec(str)) {
str = str.substring(cap[0].length);
if (cap = inline.link.exec(src)) {
src = src.substring(cap[0].length);
text = /^\s*<?([^\s]*?)>?(?:\s+"([^\n]+)")?\s*$/.exec(cap[2]);
if (!text) {
out += cap[0][0];
str = cap[0].substring(1) + str;
src = cap[0].substring(1) + src;
continue;
}
out += outputLink(cap, {
Expand All @@ -380,57 +380,57 @@ inline.lexer = function(str) {
}

// reflink, nolink
if ((cap = inline.reflink.exec(str))
|| (cap = inline.nolink.exec(str))) {
str = str.substring(cap[0].length);
if ((cap = inline.reflink.exec(src))
|| (cap = inline.nolink.exec(src))) {
src = src.substring(cap[0].length);
link = (cap[2] || cap[1]).replace(/\s+/g, ' ');
link = links[link.toLowerCase()];
if (!link || !link.href) {
out += cap[0][0];
str = cap[0].substring(1) + str;
src = cap[0].substring(1) + src;
continue;
}
out += outputLink(cap, link);
continue;
}

// strong
if (cap = inline.strong.exec(str)) {
str = str.substring(cap[0].length);
if (cap = inline.strong.exec(src)) {
src = src.substring(cap[0].length);
out += '<strong>'
+ inline.lexer(cap[2] || cap[1])
+ '</strong>';
continue;
}

// em
if (cap = inline.em.exec(str)) {
str = str.substring(cap[0].length);
if (cap = inline.em.exec(src)) {
src = src.substring(cap[0].length);
out += '<em>'
+ inline.lexer(cap[2] || cap[1])
+ '</em>';
continue;
}

// code
if (cap = inline.code.exec(str)) {
str = str.substring(cap[0].length);
if (cap = inline.code.exec(src)) {
src = src.substring(cap[0].length);
out += '<code>'
+ escape(cap[2], true)
+ '</code>';
continue;
}

// br
if (cap = inline.br.exec(str)) {
str = str.substring(cap[0].length);
if (cap = inline.br.exec(src)) {
src = src.substring(cap[0].length);
out += '<br>';
continue;
}

// text
if (cap = inline.text.exec(str)) {
str = str.substring(cap[0].length);
if (cap = inline.text.exec(src)) {
src = src.substring(cap[0].length);
out += escape(cap[0]);
continue;
}
Expand Down Expand Up @@ -605,25 +605,23 @@ var parse = function(src) {
* Helpers
*/

var escape = function(html, dbl) {
var escape = function(html, encode) {
return html
.replace(!dbl
? /&(?!#?\w+;)/g
: /&/g, '&amp;')
.replace(!encode ? /&(?!#?\w+;)/g : /&/g, '&amp;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;')
.replace(/'/g, '&apos;');
};

var mangle = function(str) {
var mangle = function(text) {
var out = ''
, ch
, l = text.length
, i = 0
, l = str.length;
, ch;

for (; i < l; i++) {
ch = str.charCodeAt(i);
ch = text.charCodeAt(i);
if (Math.random() > 0.5) {
ch = 'x' + ch.toString(16);
}
Expand All @@ -646,8 +644,8 @@ function tag() {
* Expose
*/

var marked = function(str) {
return parse(block.lexer(str));
var marked = function(src) {
return parse(block.lexer(src));
};

marked.parser = parse;
Expand Down

0 comments on commit c2d1e31

Please sign in to comment.