Permalink
Browse files

change from markdown.js to wmd

  • Loading branch information...
1 parent ded739a commit fe0ac923d746c5c8229b036f1448b4efc9844752 @caolan committed Nov 13, 2010
Showing with 46 additions and 79 deletions.
  1. +3 −0 .gitmodules
  2. +1 −0 deps/wmd
  3. +12 −40 lib/petrify.js
  4. +30 −39 test/test-petrify.js
View
3 .gitmodules
@@ -13,3 +13,6 @@
[submodule "deps/async"]
path = deps/async
url = git://github.com/caolan/async.git
+[submodule "deps/wmd"]
+ path = deps/wmd
+ url = git://github.com/caolan/wmd.git
1 deps/wmd
@@ -0,0 +1 @@
+Subproject commit 8baf69ed9dcbebd9cdb7f24c72fa425b90389628
View
52 lib/petrify.js
@@ -6,7 +6,7 @@ var fs = require('fs'),
behaviours = require('../deps/behaviours'),
jsontemplate = require('../deps/json-template/lib/json-template'),
child_process = require('child_process'),
- markdown = require('../deps/markdown-js/lib/markdown');
+ wmd = require('../deps/wmd');
// iterates over a directory of files where the filename matches a regexp
@@ -174,45 +174,17 @@ exports.emit = function(output_dir, urlpath, data, callback){
// parses markdown files exposing the metadata, html, jsonml, first h1 heading
// and filename as properties
exports.readFile = function(filename, data){
- var dialect = markdown.Markdown.dialects.Maruku;
- var jsonml = markdown.parse(data, dialect);
- var result = {};
- if(jsonml.length > 1 && !(jsonml[1] instanceof Array)){
- result.meta = jsonml[1];
- }
- result.heading = (function(){
- for(var i=0; i<jsonml.length; i++){
- var x = jsonml[i];
- if(x instanceof Array){
- if(x[0] == 'header' && x[1].level == 1){
- return x[2];
- }
- }
- }
- })();
- result.jsonml = jsonml;
- result.html = markdown.toHTML(jsonml);
-
- var found_header = false;
- result.html_no_heading = markdown.toHTML(jsonml.filter(function(x){
- if(x instanceof Array){
- if(x[0] == 'header' && x[1].level == 1 && !found_header){
- found_header = true;
- return false;
- }
- }
- return true;
- }));
- result.first_paragraph = (function(){
- for(var i=0; i<result.jsonml.length; i++){
- var x = result.jsonml[i];
- if(x instanceof Array && x[0] == 'para'){
- return markdown.toHTML(['markdown', x]);
- }
- }
- return '';
- })();
-
+ var result = wmd(data, {
+ preprocessors: [
+ wmd.preprocessors.metadata,
+ wmd.preprocessors.underscores
+ ],
+ postprocessors: [
+ wmd.postprocessors.heading,
+ wmd.postprocessors.first_paragraph,
+ wmd.postprocessors.html_no_heading
+ ]
+ });
result.filename = path.basename(filename);
return result;
};
View
69 test/test-petrify.js
@@ -21,21 +21,16 @@ var ensureEmptyDir = function(callback){
};
exports.testReadFileMarkdown = function(test){
- test.expect(7);
+ test.expect(6);
var filename = __dirname + '/fixtures/data/file1.md';
var data = 'key1: value1\nkey2: value2\n\n# Test\n\n* one\n* two';
var r = petrify.readFile(filename, data);
test.equals(r.filename, 'file1.md');
- test.same(r.meta, {key1: 'value1', key2: 'value2'});
- test.same(r.jsonml, ["markdown",
- {"key1":"value1","key2":"value2"},
- ["header",{"level":1},"Test"],
- ["bulletlist",["listitem", "one"],["listitem","two"]]
- ]);
+ test.same(r.metadata, {key1: 'value1', key2: 'value2'});
test.equals(r.heading, 'Test');
- test.equals(r.html, '<h1>Test</h1>\n\n<ul><li>one</li><li>two</li></ul>');
- test.equals(r.html_no_heading, '<ul><li>one</li><li>two</li></ul>');
- test.equals(r.first_paragraph, '');
+ test.equals(r.html, '<h1>Test</h1>\n\n<ul>\n<li>one</li>\n<li>two</li>\n</ul>');
+ test.equals(r.html_no_heading, '\n\n<ul>\n<li>one</li>\n<li>two</li>\n</ul>');
+ test.equals(r.first_paragraph, null);
test.done();
};
@@ -45,12 +40,12 @@ exports.testReadFirstParagraph = function(test){
var data = 'key1: value1\nkey2: value2\n\n' +
'# Test\n\nfirst paragraph\n\nsecond paragraph';
var r = petrify.readFile(filename, data);
- test.equals(r.first_paragraph, '<p>first paragraph</p>');
+ test.equals(r.first_paragraph, 'first paragraph');
test.done();
};
exports.testLoadData = function(test){
- test.expect(7);
+ test.expect(18);
var loadData = petrify.loadData(__dirname + '/fixtures/data');
loadData.addListener('load', function(filename, completed, total){
test.ok(filename == 'file1.md' || filename == 'file2.md');
@@ -67,33 +62,29 @@ exports.testLoadData = function(test){
}
return 0;
});
- test.same(data, [
- {
- filename: 'file1.md',
- meta: {key1:'value1', key2:'value2'},
- jsonml: ["markdown",
- {"key1":"value1","key2":"value2"},
- ["header",{"level":1},"Test"],
- ["bulletlist",["listitem", "one"],["listitem","two"]]
- ],
- heading: 'Test',
- html:'<h1>Test</h1>\n\n<ul><li>one</li><li>two</li></ul>',
- html_no_heading:'<ul><li>one</li><li>two</li></ul>',
- first_paragraph:''
- },
- {
- filename:'file2.md',
- meta: {key:'value'},
- jsonml: ["markdown",
- {"key":"value"},
- ["header",{"level":1},"Test 2"]
- ],
- heading: 'Test 2',
- html:'<h1>Test 2</h1>',
- html_no_heading:'',
- first_paragraph: ''
- }
- ]);
+
+ // file1.md
+ test.equal(data[0].filename, 'file1.md');
+ test.same(data[0].metadata, {key1:'value1',key2:'value2'});
+ test.equal(data[0].heading, 'Test');
+ test.equal(data[0].first_paragraph, null);
+ test.equal(
+ data[0].html,
+ '<h1>Test</h1>\n\n<ul>\n<li>one</li>\n<li>two</li>\n</ul>'
+ );
+ test.equal(
+ data[0].html_no_heading,
+ '\n\n<ul>\n<li>one</li>\n<li>two</li>\n</ul>'
+ );
+
+ // file2.md
+ test.equal(data[1].filename, 'file2.md');
+ test.same(data[1].metadata, {key:'value'});
+ test.equal(data[1].heading, 'Test 2');
+ test.equal(data[1].first_paragraph, null);
+ test.equal(data[1].html, '<h1>Test 2</h1>');
+ test.equal(data[1].html_no_heading, '');
+
test.done();
});
};

0 comments on commit fe0ac92

Please sign in to comment.