From 32ae409798c598d8f1d28de00834399fe4edcecf Mon Sep 17 00:00:00 2001 From: Aaron Forsander Date: Thu, 8 Aug 2013 12:45:13 -0500 Subject: [PATCH] Very rudamentary syntax highlighting. --- README.md | 4 +- index.js | 315 ++++++++++++++++++++++++++++----------------------- package.json | 3 +- 3 files changed, 176 insertions(+), 146 deletions(-) diff --git a/README.md b/README.md index 1c48b44..52aa175 100644 --- a/README.md +++ b/README.md @@ -18,4 +18,6 @@ Stop dealing with `--help`. Shove that shit in a markdown file. * red fish * blue fish -`var foo = 'bar';` +```javascript +var foo = 'bar'; +``` diff --git a/index.js b/index.js index 53d1bd7..a14a89a 100644 --- a/index.js +++ b/index.js @@ -1,161 +1,188 @@ var fs = require('fs'); -var fmt = require('util').format; +var util = require('util'); var marked = require('marked'); var colors = require('colors'); - -var Consoul = function (file) { - this.inBlockquote = false; - this.inList = false; - this.inListItem = false; - this.order = null; -}; - -Consoul.NEWLINE = process.platform === 'windows' ? '\r\n' : '\n'; - -Consoul.prototype.parse = function (src) { - // this.inline = new InlineLexer(src.links, this.options); - this.tokens = src.reverse(); - - var out = ''; - while (this.next()) { - out += this.tok(); - } - - return out; -}; - -Consoul.prototype.next = function() { - this.token = this.tokens.pop(); - return this.token; -}; - -Consoul.prototype.peek = function() { - return this.tokens[this.tokens.length-1] || 0; -}; - -Consoul.prototype.parseText = function() { - var body = this.token.text; - - while (this.peek().type === 'text') { - body += '\n' + this.next().text; +var highlight = require('highlight.js'); + +function Parser(options) { + this.tokens = []; + this.token = null; + this.options = options || marked.defaults; +} + +util.inherits(Parser, marked.Parser); + +Parser.prototype.tok = function () { + switch (this.token.type) { + case 'space': { + return ''; + } + + case 'hr': { + return new Array(80).join('-') + '\n\n'; + } + + case 'heading': { + var body = this.token.text; + + switch (this.token.depth) { + case 1: + body = body.toUpperCase(); + default: + body = body.underline; + } + + return body + '\n\n'; + } + + case 'code': { + return highlight.highlight(this.token.lang, this.token.text, function (type, text) { + switch (type) { + case 'keyword': + return text.cyan; + case 'string': + return text.red; + default: + return text; + } + }).value + '\n'; + + // if (this.options.highlight) { + // var code = this.options.highlight(this.token.text, this.token.lang); + // if (code != null && code !== this.token.text) { + // this.token.escaped = true; + // this.token.text = code; + // } + // } + + // if (!this.token.escaped) { + // this.token.text = escape(this.token.text, true); + // } + + // return '
'
+      //   + this.token.text
+      //   + '
\n'; + } + + case 'table': { + var body = '' + , heading + , i + , row + , cell + , j; + + // header + body += '\n\n'; + for (i = 0; i < this.token.header.length; i++) { + heading = this.inline.output(this.token.header[i]); + body += this.token.align[i] + ? '' + heading + '\n' + : '' + heading + '\n'; + } + body += '\n\n'; + + // body + body += '\n' + for (i = 0; i < this.token.cells.length; i++) { + row = this.token.cells[i]; + body += '\n'; + for (j = 0; j < row.length; j++) { + cell = this.inline.output(row[j]); + body += this.token.align[j] + ? '' + cell + '\n' + : '' + cell + '\n'; + } + body += '\n'; + } + body += '\n'; + + return '\n' + + body + + '
\n'; + } + + case 'blockquote_start': { + var body = ''; + + while (this.next().type !== 'blockquote_end') { + body += '| ' + this.tok(); + } + + return body; + } + + case 'list_start': { + var ordered = this.token.ordered; + var order = 1; + var body = ''; + + while (this.next().type !== 'list_end') { + body += (ordered ? util.format('%d. ', order++) : '* ') + this.tok(); + } + + return body + '\n'; + } + + case 'list_item_start': { + var body = ''; + + while (this.next().type !== 'list_item_end') { + body += this.token.type === 'text' + ? this.parseText() + : this.tok(); + } + + return body + '\n'; + } + + case 'loose_item_start': { + var body = ''; + + while (this.next().type !== 'list_item_end') { + body += this.tok(); + } + + return body + '\n'; + } + + case 'html': { + return !this.token.pre && !this.options.pedantic + ? this.inline.output(this.token.text) + : this.token.text; + } + + case 'paragraph': { + return this.inline.output(this.token.text) + '\n\n'; + } + + case 'text': { + return this.parseText() + '\n'; + } } - - return body; }; -Consoul.prototype.tok = function() { - var body = ''; - - switch (this.token.type) { - case 'space': - return ''; - - case 'hr': - return new Array(80).join('-') + '\n\n'; - - case 'heading': - body = this.token.text; - - switch (this.token.depth) { - case 1: - body = body.toUpperCase(); - default: - body = body.underline; - } - return body + '\n\n'; - - case 'code': - return this.token.text + '\n'; - - case 'table': - var heading, i, row, cell, j; - - // header - body += '\n\n'; - for (i = 0; i < this.token.header.length; i++) { - heading = this.inline.output(this.token.header[i]); - body += this.token.align[i] - ? '' + heading + '\n' - : '' + heading + '\n'; - } - body += '\n\n'; - - // body - body += '\n'; - for (i = 0; i < this.token.cells.length; i++) { - row = this.token.cells[i]; - body += '\n'; - for (j = 0; j < row.length; j++) { - cell = this.inline.output(row[j]); - body += this.token.align[j] - ? '' + cell + '\n' - : '' + cell + '\n'; - } - body += '\n'; - } - body += '\n'; - - return '\n' + body + '
\n'; - - case 'blockquote_start': - while (this.next().type !== 'blockquote_end') { - body += '| ' + this.tok(); - } - - return body + '\n'; - - case 'list_start': - var ordered = this.token.ordered; - var order = 1; - var prefix = ''; - - while (this.next().type !== 'list_end') { - prefix = ordered ? fmt('%d. ', order++) : '* '; - body += prefix + this.tok(); - } - - return body + '\n'; - - case 'list_item_start': - while (this.next().type !== 'list_item_end') { - body += this.token.type === 'text' - ? this.parseText() - : this.tok(); - } - - return body + '\n'; - - case 'loose_item_start': - while (this.next().type !== 'list_item_end') { - body += this.tok(); - } - - return body + '\n'; - - case 'html': - return !this.token.pre && !this.options.pedantic - ? this.inline.output(this.token.text) - : this.token.text; - - case 'paragraph': - return this.token.text + '\n\n'; - - case 'text': - return this.parseText() + '\n'; - - } -}; - -var consoul = new Consoul(); +var parser = new Parser(); fs.readFile('./README.md', function (err, buffer) { var src = marked.lexer(buffer.toString()); + var output = parser.parse(src); + + console.log('>>>>>>>>>>>>>>>>> SOURCE >>>>>>>>>>>>>>>>\n'); console.log(src); + console.log('>>>>>>>>>>>>>>>>> MARKDOWN >>>>>>>>>>>>>>>>\n'); console.log(buffer.toString()); - var output = consoul.parse(src); + console.log('>>>>>>>>>>>>>>>>> OUTPUT >>>>>>>>>>>>>>>>\n'); process.stdout.write(output); }); diff --git a/package.json b/package.json index af1c877..6a9dcac 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ "license": "MIT", "dependencies": { "marked": "~0.2.9", - "colors": "~0.6.1" + "colors": "~0.6.1", + "highlight.js": "~7.3.0" } }