Skip to content

Commit

Permalink
list-like lines = lists in gfm. fixes #120.
Browse files Browse the repository at this point in the history
  • Loading branch information
chjj committed Aug 7, 2013
1 parent 81e2062 commit 37698ee
Show file tree
Hide file tree
Showing 16 changed files with 66 additions and 72 deletions.
4 changes: 3 additions & 1 deletion lib/marked.js
Expand Up @@ -75,7 +75,9 @@ block.gfm = merge({}, block.normal, {
});

block.gfm.paragraph = replace(block.paragraph)
('(?!', '(?!' + block.gfm.fences.source.replace('\\1', '\\2') + '|')
('(?!', '(?!'
+ block.gfm.fences.source.replace('\\1', '\\2') + '|'
+ block.list.source.replace('\\1', '\\3') + '|')
();

/**
Expand Down
2 changes: 1 addition & 1 deletion test/browser/index.js
Expand Up @@ -29,7 +29,7 @@ app.get('/test.js', function(req, res, next) {
, files = load();

test = test.replace('__TESTS__', JSON.stringify(files));
test = test.replace('__MAIN__', test.replacePre + '\n\n' + runTests + '');
test = test.replace('__MAIN__', runTests + '');

res.contentType('.js');
res.send(test);
Expand Down
38 changes: 25 additions & 13 deletions test/browser/test.js
Expand Up @@ -15,22 +15,34 @@ console.log = function(text) {
document.body.innerHTML += '<pre>' + escape(text) + '</pre>';
};

Object.keys = Object.keys || function(obj) {
var out = []
, key;

for (key in obj) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
out.push(key);
if (!Object.keys) {
Object.keys = function(obj) {
var out = []
, key;

for (key in obj) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
out.push(key);
}
}
}

return out;
};
return out;
};
}

String.prototype.trim = String.prototype.trim || function() {
return this.replace(/^\s+|\s+$/g, '');
};
if (!Array.prototype.forEach) {
Array.prototype.forEach = function(callback, context) {
for (var i = 0; i < this.length; i++) {
callback.call(context || null, this[i], i, obj);
}
};
}

if (!String.prototype.trim) {
String.prototype.trim = function() {
return this.replace(/^\s+|\s+$/g, '');
};
}

function load() {
return files;
Expand Down
80 changes: 29 additions & 51 deletions test/index.js
Expand Up @@ -51,37 +51,6 @@ function load() {
return files;
}

function stripPre() {
var keys = Object.keys(files)
, i = 0
, key;

for (; i < keys.length; i++) {
key = keys[i];
files[key].text = replacePre(files[key].text, true);
files[key].html = replacePre(files[key].html, true);
}
}

function replacePre(text, remove) {
var o = /(?:^|\n)#if +([^\s]+)\n([\s\S]*?)(?:\n#else\n([\s\S]*?))?\n#endif/g;
if (remove) {
return text.replace(o, '');
}
return text.replace(o, function(str, option, content, els) {
var set = true;
option = option.toLowerCase();
if (option[0] === '!') {
option = option.substring(1);
set = false;
}
if (!!marked.defaults[option] === set) {
return '\n' + content;
}
return '\n' + (els || '');
});
}

/**
* Test Runner
*/
Expand All @@ -97,12 +66,12 @@ function runTests(engine, options) {
, files = options.files || load()
, complete = 0
, failed = 0
, skipped = 0
, keys = Object.keys(files)
, i = 0
, len = keys.length
, filename
, file
, flags
, text
, html
, j
Expand All @@ -117,24 +86,33 @@ main:
filename = keys[i];
file = files[filename];

if ((~filename.indexOf('.gfm.') && !marked.defaults.gfm)
|| (~filename.indexOf('.tables.') && !marked.defaults.tables)
|| (~filename.indexOf('.breaks.') && !marked.defaults.breaks)
|| (~filename.indexOf('.pedantic.') && !marked.defaults.pedantic)
|| (~filename.indexOf('.sanitize.') && !marked.defaults.sanitize)
|| (~filename.indexOf('.smartlists.') && !marked.defaults.smartLists)
|| (~filename.indexOf('.smartypants.') && !marked.defaults.smartypants)) {
skipped++;
console.log('#%d. %s skipped.', i + 1, filename);
continue main;
if (marked._original) {
marked.defaults = marked._original;
delete marked._original;
}

text = replacePre(file.text);
html = replacePre(file.html);
flags = filename.split('.').slice(1, -1);
if (flags.length) {
marked._original = marked.defaults;
marked.defaults = {};
Object.keys(marked._original).forEach(function(key) {
marked.defaults[key] = marked._original[key];
});
flags.forEach(function(key) {
var val = true;
if (key.indexOf('no') === 0) {
key = key.substring(2);
val = false;
}
if (marked.defaults.hasOwnProperty(key)) {
marked.defaults[key] = val;
}
});
}

try {
text = engine(text).replace(/\s/g, '');
html = html.replace(/\s/g, '');
text = engine(file.text).replace(/\s/g, '');
html = file.html.replace(/\s/g, '');
} catch(e) {
console.log('%s failed.', filename);
throw e;
Expand Down Expand Up @@ -176,7 +154,6 @@ main:

console.log('%d/%d tests completed successfully.', complete, len);
if (failed) console.log('%d/%d tests failed.', failed, len);
if (skipped) console.log('%d/%d tests skipped.', skipped, len);

return !failed;
}
Expand Down Expand Up @@ -206,8 +183,6 @@ function bench(name, func) {
files['main.text'].text = files['main.text'].text.replace('* * *\n\n', '');
}

stripPre();

var start = Date.now()
, times = 1000
, keys = Object.keys(files)
Expand Down Expand Up @@ -355,7 +330,11 @@ function fix(options) {

// cp -r original tests
fs.readdirSync(path.resolve(__dirname, 'original')).forEach(function(file) {
fs.writeFileSync(path.resolve(__dirname, 'tests', file),
var nfile = file;
if (file.indexOf('hard_wrapped_paragraphs_with_list_like_lines.') === 0) {
nfile = file.replace(/\.(text|html)$/, '.nogfm.$1');
}
fs.writeFileSync(path.resolve(__dirname, 'tests', nfile),
fs.readFileSync(path.resolve(__dirname, 'original', file)));
});

Expand Down Expand Up @@ -547,7 +526,6 @@ if (!module.parent) {
} else {
exports = main;
exports.main = main;
exports.replacePre = replacePre;
exports.runTests = runTests;
exports.runBench = runBench;
exports.load = load;
Expand Down
File renamed without changes.
File renamed without changes.
3 changes: 2 additions & 1 deletion test/new/text.smartypants.html
Expand Up @@ -2,4 +2,5 @@

<p>“It’s a more ‘challenging’ smartypants test…”</p>

<p>‘And,’ as a bonus — “one<br>multiline” test!</p>
<p>‘And,’ as a bonus — “one
multiline” test!</p>
Expand Up @@ -17,8 +17,8 @@ <h1 id="how-are-you">how are you</h1>
<p>hello world</p>
<blockquote><p>how are you</p></blockquote>

<p>hello world
* how are you</p>
<p>hello world</p>
<ul><li>how are you</li></ul>

<p>hello world</p>
<div>how are you</div>
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
3 changes: 2 additions & 1 deletion test/tests/text.smartypants.html
Expand Up @@ -2,4 +2,5 @@

<p>“It’s a more ‘challenging’ smartypants test…”</p>

<p>‘And,’ as a bonus — “one<br>multiline” test!</p>
<p>‘And,’ as a bonus — “one
multiline” test!</p>
Expand Up @@ -17,8 +17,8 @@ <h1 id="how-are-you">how are you</h1>
<p>hello world</p>
<blockquote><p>how are you</p></blockquote>

<p>hello world
* how are you</p>
<p>hello world</p>
<ul><li>how are you</li></ul>

<p>hello world</p>
<div>how are you</div>
Expand Down
File renamed without changes.

0 comments on commit 37698ee

Please sign in to comment.