From 37698ee7e096a2c511d710e49a160f330ba70aaf Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Wed, 7 Aug 2013 06:10:50 -0500 Subject: [PATCH] list-like lines = lists in gfm. fixes #120. --- lib/marked.js | 4 +- test/browser/index.js | 2 +- test/browser/test.js | 38 ++++++--- test/index.js | 80 +++++++------------ .../{gfm_break.html => gfm_break.breaks.html} | 0 .../{gfm_break.text => gfm_break.breaks.text} | 0 test/new/text.smartypants.html | 3 +- ...aphs.html => toplevel_paragraphs.gfm.html} | 4 +- ...aphs.text => toplevel_paragraphs.gfm.text} | 0 .../{gfm_break.html => gfm_break.breaks.html} | 0 .../{gfm_break.text => gfm_break.breaks.text} | 0 ...aragraphs_with_list_like_lines.nogfm.html} | 0 ...aragraphs_with_list_like_lines.nogfm.text} | 0 test/tests/text.smartypants.html | 3 +- ...aphs.html => toplevel_paragraphs.gfm.html} | 4 +- ...aphs.text => toplevel_paragraphs.gfm.text} | 0 16 files changed, 66 insertions(+), 72 deletions(-) rename test/new/{gfm_break.html => gfm_break.breaks.html} (100%) rename test/new/{gfm_break.text => gfm_break.breaks.text} (100%) rename test/new/{toplevel_paragraphs.html => toplevel_paragraphs.gfm.html} (90%) rename test/new/{toplevel_paragraphs.text => toplevel_paragraphs.gfm.text} (100%) rename test/tests/{gfm_break.html => gfm_break.breaks.html} (100%) rename test/tests/{gfm_break.text => gfm_break.breaks.text} (100%) rename test/tests/{hard_wrapped_paragraphs_with_list_like_lines.html => hard_wrapped_paragraphs_with_list_like_lines.nogfm.html} (100%) rename test/tests/{hard_wrapped_paragraphs_with_list_like_lines.text => hard_wrapped_paragraphs_with_list_like_lines.nogfm.text} (100%) rename test/tests/{toplevel_paragraphs.html => toplevel_paragraphs.gfm.html} (90%) rename test/tests/{toplevel_paragraphs.text => toplevel_paragraphs.gfm.text} (100%) diff --git a/lib/marked.js b/lib/marked.js index e4caaad995..57ae68915d 100644 --- a/lib/marked.js +++ b/lib/marked.js @@ -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') + '|') (); /** diff --git a/test/browser/index.js b/test/browser/index.js index b9904b7759..d5d7ee698c 100644 --- a/test/browser/index.js +++ b/test/browser/index.js @@ -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); diff --git a/test/browser/test.js b/test/browser/test.js index 0d1a90301f..cef9e376fa 100644 --- a/test/browser/test.js +++ b/test/browser/test.js @@ -15,22 +15,34 @@ console.log = function(text) { document.body.innerHTML += '
' + escape(text) + '
'; }; -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; diff --git a/test/index.js b/test/index.js index 4e45d9d5a8..bb74983908 100644 --- a/test/index.js +++ b/test/index.js @@ -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 */ @@ -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 @@ -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; @@ -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; } @@ -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) @@ -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))); }); @@ -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; diff --git a/test/new/gfm_break.html b/test/new/gfm_break.breaks.html similarity index 100% rename from test/new/gfm_break.html rename to test/new/gfm_break.breaks.html diff --git a/test/new/gfm_break.text b/test/new/gfm_break.breaks.text similarity index 100% rename from test/new/gfm_break.text rename to test/new/gfm_break.breaks.text diff --git a/test/new/text.smartypants.html b/test/new/text.smartypants.html index f8f3ea35eb..22997c4c12 100644 --- a/test/new/text.smartypants.html +++ b/test/new/text.smartypants.html @@ -2,4 +2,5 @@

“It’s a more ‘challenging’ smartypants test…”

-

‘And,’ as a bonus — “one
multiline” test!

+

‘And,’ as a bonus — “one +multiline” test!

diff --git a/test/new/toplevel_paragraphs.html b/test/new/toplevel_paragraphs.gfm.html similarity index 90% rename from test/new/toplevel_paragraphs.html rename to test/new/toplevel_paragraphs.gfm.html index 97d715e26d..970c6f19f6 100644 --- a/test/new/toplevel_paragraphs.html +++ b/test/new/toplevel_paragraphs.gfm.html @@ -17,8 +17,8 @@

how are you

hello world

how are you

-

hello world -* how are you

+

hello world

+

hello world

how are you
diff --git a/test/new/toplevel_paragraphs.text b/test/new/toplevel_paragraphs.gfm.text similarity index 100% rename from test/new/toplevel_paragraphs.text rename to test/new/toplevel_paragraphs.gfm.text diff --git a/test/tests/gfm_break.html b/test/tests/gfm_break.breaks.html similarity index 100% rename from test/tests/gfm_break.html rename to test/tests/gfm_break.breaks.html diff --git a/test/tests/gfm_break.text b/test/tests/gfm_break.breaks.text similarity index 100% rename from test/tests/gfm_break.text rename to test/tests/gfm_break.breaks.text diff --git a/test/tests/hard_wrapped_paragraphs_with_list_like_lines.html b/test/tests/hard_wrapped_paragraphs_with_list_like_lines.nogfm.html similarity index 100% rename from test/tests/hard_wrapped_paragraphs_with_list_like_lines.html rename to test/tests/hard_wrapped_paragraphs_with_list_like_lines.nogfm.html diff --git a/test/tests/hard_wrapped_paragraphs_with_list_like_lines.text b/test/tests/hard_wrapped_paragraphs_with_list_like_lines.nogfm.text similarity index 100% rename from test/tests/hard_wrapped_paragraphs_with_list_like_lines.text rename to test/tests/hard_wrapped_paragraphs_with_list_like_lines.nogfm.text diff --git a/test/tests/text.smartypants.html b/test/tests/text.smartypants.html index f8f3ea35eb..22997c4c12 100644 --- a/test/tests/text.smartypants.html +++ b/test/tests/text.smartypants.html @@ -2,4 +2,5 @@

“It’s a more ‘challenging’ smartypants test…”

-

‘And,’ as a bonus — “one
multiline” test!

+

‘And,’ as a bonus — “one +multiline” test!

diff --git a/test/tests/toplevel_paragraphs.html b/test/tests/toplevel_paragraphs.gfm.html similarity index 90% rename from test/tests/toplevel_paragraphs.html rename to test/tests/toplevel_paragraphs.gfm.html index 97d715e26d..970c6f19f6 100644 --- a/test/tests/toplevel_paragraphs.html +++ b/test/tests/toplevel_paragraphs.gfm.html @@ -17,8 +17,8 @@

how are you

hello world

how are you

-

hello world -* how are you

+

hello world

+

hello world

how are you
diff --git a/test/tests/toplevel_paragraphs.text b/test/tests/toplevel_paragraphs.gfm.text similarity index 100% rename from test/tests/toplevel_paragraphs.text rename to test/tests/toplevel_paragraphs.gfm.text