diff --git a/Gruntfile.js b/Gruntfile.js index 8534ad2..6a2e0e7 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -1,3 +1,4 @@ +var converter = require('./index.js'); module.exports = function(grunt) { grunt.initConfig({ @@ -18,7 +19,11 @@ module.exports = function(grunt) { grunt.loadNpmTasks('grunt-contrib-watch'); grunt.registerTask('default', ['watch']); grunt.registerTask('convert-tabs-to-html', function(){ - console.log('hi'); + var done = this.async(); + converter(function(result){ + console.log(result); + done(); + }); }); }; diff --git a/index.js b/index.js index 7f6550a..72f95f1 100755 --- a/index.js +++ b/index.js @@ -1,69 +1,74 @@ -#!/usr/bin/env node fs = require('fs'); +module.exports = function(callback){ -var insertTabs = function(num_tabs) { - var result = ''; - for(var i = 0; i < num_tabs; i++){ - result += '\t'; + var insertTabs = function(num_tabs) { + var result = ''; + for(var i = 0; i < num_tabs; i++){ + result += '\t'; + } + return result; } - return result; -} -var emptyTabStack = function(tabStack){ - var result = ''; - while(tabStack.length > 0){ - result += tabStack.pop(); + var emptyTabStack = function(tabStack){ + var result = ''; + while(tabStack.length > 0){ + result += tabStack.pop(); + } + return result; } - return result; -} -var popTabStack = function(tabStack, current_line_num_tabs, previous_line_num_tabs){ - var result = ''; - for(var i = previous_line_num_tabs; i > current_line_num_tabs; i--){ - result += tabStack.pop(); + var popTabStack = function(tabStack, current_line_num_tabs, previous_line_num_tabs){ + var result = ''; + for(var i = previous_line_num_tabs; i > current_line_num_tabs; i--){ + result += tabStack.pop(); + } + return result; } - return result; -} -var parseFile = function(data, callback){ - var previous_line = -1; - var lines = data.split('\n'); - var html = ''; - var tabStack = []; - lines.forEach(function(value, index){ - if(value !== ''){ - var split_value = value.split('\t'); - var num_tabs = split_value.length - 1; - if(num_tabs > previous_line){ - if(num_tabs > 0){ - html += '\n' + insertTabs(num_tabs * 2 - 1) +'
  • \n'; + var parseFile = function(data, callback){ + var previous_line = -1; + var lines = data.split('\n'); + var html = ''; + var tabStack = []; + lines.forEach(function(value, index){ + if(value !== ''){ + var split_value = value.split('\t'); + var num_tabs = split_value.length - 1; + if(num_tabs > previous_line){ + if(num_tabs > 0){ + html += '\n' + insertTabs(num_tabs * 2 - 1) +'
  • \n'; + } + html += insertTabs(num_tabs * 2) + ''; + if(num_tabs > 0){ + html_push_string += '\n' + insertTabs(num_tabs * 2 - 1) + '
  • '; + } + tabStack.push(html_push_string); } - html += insertTabs(num_tabs * 2) + ''; - if(num_tabs > 0){ - html_push_string += '\n' + insertTabs(num_tabs * 2 - 1) + ''; + else if(num_tabs < previous_line){ + html += popTabStack(tabStack, num_tabs, previous_line); } - tabStack.push(html_push_string); + html += '\n' + insertTabs(num_tabs * 2 + 1) + + '
  • ' + split_value[split_value.length - 1] + '
  • '; + previous_line = num_tabs; } - else if(num_tabs < previous_line){ - html += popTabStack(tabStack, num_tabs, previous_line); - } - html += '\n' + insertTabs(num_tabs * 2 + 1) + - '
  • ' + split_value[split_value.length - 1] + '
  • '; - previous_line = num_tabs; - } - }); - html += emptyTabStack(tabStack); - callback(html); -} - -fs.readFile(process.argv[2], 'utf8', function(err, data){ - if(err){ - console.log(err); - } else { - parseFile(data, function(html){ - console.log(''); - console.log(html); - console.log(''); }); + html += emptyTabStack(tabStack); + callback(html); } -}); + //fs.readFile(process.argv[2], 'utf8', function(err, data){ + fs.readFile('test.txt', 'utf8', function(err, data){ + if(err){ + console.log(err); + } else { + parseFile(data, function(html){ + //console.log(''); + //console.log(html); + //console.log(''); + var result = ''; + result += html; + result += ''; + callback(result); + }); + } + }); +};