Permalink
Browse files

added wrap line function

  • Loading branch information...
1 parent b4378b3 commit c1b83b86a85020cbab73a46d1af6253fdcfdc3f3 @treasonx committed Nov 11, 2012
Showing with 30 additions and 10 deletions.
  1. +30 −10 tasks/markdown.js
View
40 tasks/markdown.js
@@ -8,20 +8,37 @@
module.exports = function(grunt) {
'use strict';
-
- var path = require('path');
+
+ var path = require('path');
var markdown = require('marked');
var hljs = require('highlight.js');
var _ = require('lodash');
grunt.registerHelper('markdown', function(src, options, template) {
var html = null;
+ var codeLines = options.codeLines;
+ var shouldWrap = codeLines && codeLines.before && codeLines.after;
+
+ function wrapLines(code) {
+ var out = [];
+ var before = codeLines.before;
+ var after = codeLines.after;
+ code = code.split('\n');
+ code.forEach(function(line) {
+ out.push(before+line+after);
+ });
+ return out.join('\n');
+ }
if(typeof options.highlight === 'string') {
if(options.highlight === 'auto') {
options.highlight = function(code) {
- return hljs.highlightAuto(code).value;
+ var out = hljs.highlightAuto(code).value;
+ if(shouldWrap) {
+ out = wrapLines(out);
+ }
+ return out;
};
} else if (options.highlight === 'manual') {
options.highlight = function(code, lang) {
@@ -31,15 +48,18 @@ module.exports = function(grunt) {
} catch(e) {
out = hljs.highlightAuto(code).value;
}
+ if(shouldWrap) {
+ out = wrapLines(out);
+ }
return out;
};
}
-
+
}
markdown.setOptions(options);
- grunt.verbose.write('Marking down...');
+ grunt.verbose.write('Marking down...');
html = markdown(src);
@@ -51,21 +71,21 @@ module.exports = function(grunt) {
var destPath = this.data.dest;
var options = this.data.options || {};
var extension = this.data.extenstion || 'html';
- var templateFn = this.data.template || path.join(__dirname, 'template.html');
+ var templateFn = this.data.template || path.join(__dirname, 'template.html');
var template = grunt.file.read(templateFn);
grunt.file.expandFiles(this.data.files).forEach(function(filepath) {
-
+
var file = grunt.file.read(filepath);
var html = grunt.helper('markdown', file, options, template);
var ext = path.extname(filepath);
var dest = path.join(destPath, path.basename(filepath, ext) +'.'+ extension);
grunt.file.write(dest, html);
-
+
});
-
+
});
-
+
};

0 comments on commit c1b83b8

Please sign in to comment.