-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Very rudamentary syntax highlighting.
- Loading branch information
1 parent
5adb296
commit 32ae409
Showing
3 changed files
with
176 additions
and
146 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 '<pre><code' | ||
// + (this.token.lang | ||
// ? ' class="' | ||
// + this.options.langPrefix | ||
// + this.token.lang | ||
// + '"' | ||
// : '') | ||
// + '>' | ||
// + this.token.text | ||
// + '</code></pre>\n'; | ||
} | ||
|
||
case 'table': { | ||
var body = '' | ||
, heading | ||
, i | ||
, row | ||
, cell | ||
, j; | ||
|
||
// header | ||
body += '<thead>\n<tr>\n'; | ||
for (i = 0; i < this.token.header.length; i++) { | ||
heading = this.inline.output(this.token.header[i]); | ||
body += this.token.align[i] | ||
? '<th align="' + this.token.align[i] + '">' + heading + '</th>\n' | ||
: '<th>' + heading + '</th>\n'; | ||
} | ||
body += '</tr>\n</thead>\n'; | ||
|
||
// body | ||
body += '<tbody>\n' | ||
for (i = 0; i < this.token.cells.length; i++) { | ||
row = this.token.cells[i]; | ||
body += '<tr>\n'; | ||
for (j = 0; j < row.length; j++) { | ||
cell = this.inline.output(row[j]); | ||
body += this.token.align[j] | ||
? '<td align="' + this.token.align[j] + '">' + cell + '</td>\n' | ||
: '<td>' + cell + '</td>\n'; | ||
} | ||
body += '</tr>\n'; | ||
} | ||
body += '</tbody>\n'; | ||
|
||
return '<table>\n' | ||
+ body | ||
+ '</table>\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 += '<thead>\n<tr>\n'; | ||
for (i = 0; i < this.token.header.length; i++) { | ||
heading = this.inline.output(this.token.header[i]); | ||
body += this.token.align[i] | ||
? '<th align="' + this.token.align[i] + '">' + heading + '</th>\n' | ||
: '<th>' + heading + '</th>\n'; | ||
} | ||
body += '</tr>\n</thead>\n'; | ||
|
||
// body | ||
body += '<tbody>\n'; | ||
for (i = 0; i < this.token.cells.length; i++) { | ||
row = this.token.cells[i]; | ||
body += '<tr>\n'; | ||
for (j = 0; j < row.length; j++) { | ||
cell = this.inline.output(row[j]); | ||
body += this.token.align[j] | ||
? '<td align="' + this.token.align[j] + '">' + cell + '</td>\n' | ||
: '<td>' + cell + '</td>\n'; | ||
} | ||
body += '</tr>\n'; | ||
} | ||
body += '</tbody>\n'; | ||
|
||
return '<table>\n' + body + '</table>\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); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters