@leichtgewicht/msee
Advanced tools
Comparing version 0.2.5 to 0.3.0
@@ -22,4 +22,11 @@ msee | ||
// ...with options | ||
// @see https://github.com/firede/msee/blob/master/lib/msee.js#L9 | ||
msee.parse(str, { | ||
collapseNewlines: false, | ||
width: 120 | ||
}); | ||
// parse markdown file | ||
msee.parseFile('~/doc/readme.md'); | ||
``` |
@@ -0,2 +1,7 @@ | ||
0.2.0 / 2015-11-03 | ||
================== | ||
* Added table support #13 | ||
* Added *Example.md* | ||
0.1.2 / 2015-10-14 | ||
@@ -3,0 +8,0 @@ ================== |
@@ -7,3 +7,3 @@ var chalk = require('chalk'); | ||
"hr": chalk.grey, | ||
"code": chalk.yellow, | ||
"code": chalk.grey, | ||
"blockquote": chalk.blue, | ||
@@ -10,0 +10,0 @@ "bold": chalk.bold, |
211
lib/msee.js
@@ -8,3 +8,4 @@ var fs = require('fs'); | ||
var table = require('text-table'); | ||
var vw = require('@leichtgewicht/visualwidth'); | ||
var chalk = require('chalk'); | ||
var wcstring = require('wcstring'); | ||
@@ -20,5 +21,9 @@ var defaultOptions = { | ||
headingIndentChar: '#', | ||
headingIndent: function (token) { | ||
return Array(token.depth + 1).join(this.headingIndentChar) | ||
}, | ||
codeStart: '\n', | ||
codeEnd: '\n\n', | ||
codePad: ' ', | ||
codeTheme: require('./syntaxColor'), | ||
blockquoteStart: '\n', | ||
@@ -37,7 +42,5 @@ blockquoteEnd: '\n\n', | ||
paragraphStart: '', | ||
paragraphEnd: '', | ||
paragraphColor: 'paragraph', | ||
paragraphPad: '', | ||
paragraphPadColor: 'paragraph', | ||
width: process.stdout.columns ? process.stdout.columns - 2 : 84, | ||
paragraphEnd: '\n\n', | ||
paragraphPad: ' ', | ||
width: process.stdout.columns || 80, | ||
tableStart: '\n', | ||
@@ -160,2 +163,7 @@ tableSeparator: ' ', | ||
function getOption(key) { | ||
var value = options[key]; | ||
return typeof value === 'function' ? value(token) : value; | ||
} | ||
switch (type) { | ||
@@ -170,10 +178,10 @@ case 'space': { | ||
case 'heading': { | ||
var syntaxFlag = color( | ||
Array(token.depth + 1).join(options.headingIndentChar), | ||
'syntax' | ||
); | ||
text = processInline(text); | ||
content = color(text, type); | ||
text = blockFormat(processInline(text), { | ||
block_color: type, | ||
pad_str: options.headingIndent(token) + ' ', | ||
pad_color: 'syntax', | ||
width: options.width | ||
}) | ||
return options.headingStart + syntaxFlag + ' ' + content + options.headingEnd; | ||
return options.headingStart + text + options.headingEnd; | ||
} | ||
@@ -183,4 +191,12 @@ case 'code': { | ||
if (token.lang === 'raw') { | ||
return text + '\n'; | ||
} | ||
try { | ||
content = cardinal.highlight(text); | ||
content = cardinal.highlight(text, { | ||
theme: chalk.supportsColor | ||
? options.codeTheme | ||
: require('cardinal/themes/empty') | ||
}); | ||
} | ||
@@ -191,6 +207,3 @@ catch (e) { | ||
content = blockFormat(content, { | ||
pad_str: options.codePad.lang ? (typeof options.codePad.lang[token.lang] === 'string' ? options.codePad.lang[token.lang] : options.defaultCodePad) : options.codePad, | ||
width: options.width | ||
}); | ||
content = content.replace(/^/gm, getOption('codePad')); | ||
@@ -243,4 +256,3 @@ return options.codeStart + content + options.codeEnd; | ||
block_color: options.listItemColor, | ||
first_pad_str: options.listItemPad, | ||
pad_str: new Array(options.listItemPad.length + 1).join(' '), | ||
pad_str: options.listItemPad, | ||
pad_color: options.listItemPadColor, | ||
@@ -254,11 +266,14 @@ width: options.width | ||
if (blockDepth > 0) { | ||
return text.split('\n').join(' '); | ||
return text; | ||
} | ||
content = blockFormat(processInline(text), { | ||
block_color: options.paragraphColor, | ||
pad_str: options.paragraphPad, | ||
pad_color: options.paragraphPadColor, | ||
width: options.width | ||
}); | ||
return options.paragraphStart + content + options.paragraphEnd; | ||
text = blockFormat( | ||
processInline(text), | ||
{ | ||
block_color: type, | ||
pad_str: options.paragraphPad, | ||
pad_color: options.paragraphPadColor, | ||
width: options.width | ||
} | ||
); | ||
return options.paragraphStart + text + options.paragraphEnd; | ||
} | ||
@@ -278,54 +293,41 @@ default: { | ||
function blockFormat(src, opts) { | ||
var lines = src.split('\n'); | ||
opts = opts || {}; | ||
var padStr = opts.pad_str || ''; | ||
var firstPadStr = opts.first_pad_str || padStr; | ||
var padColor = opts.pad_color || opts.block_color; | ||
var retLines = []; | ||
var secondWidth = opts.width - vw.width(padStr, true); | ||
var firstWidth = opts.width - vw.width(firstPadStr, true); | ||
var first = true; | ||
var width = opts.width; | ||
width -= padStr.length; | ||
if (padColor) { | ||
padStr = color(padStr, padColor); | ||
firstPadStr = color(firstPadStr, padColor); | ||
} | ||
lines.forEach(function(rawLine) { | ||
var rawWidth = vw.width(rawLine, true); | ||
for (var i = 0; i < rawWidth;) { | ||
var width = first ? firstWidth : secondWidth; | ||
var end = i + width; | ||
var line; | ||
if (end > rawWidth) { | ||
line = vw.substr(rawLine, i, null, true); | ||
i = end; | ||
} else { | ||
var lastSpace = vw.lastIndexOf(rawLine, ' ', end, true); | ||
if (lastSpace === -1 || lastSpace < i) { | ||
line = vw.substring(rawLine, i, end, true); | ||
i = end; | ||
} else { | ||
line = vw.substring(rawLine, i, lastSpace, true); | ||
i = lastSpace+1; | ||
} | ||
} | ||
if (opts.block_color) { | ||
line = color(line, opts.block_color); | ||
} | ||
retLines.push((first ? firstPadStr : padStr) + line); | ||
first = false; | ||
} | ||
src = wcstring(src).wrap(width); | ||
return src.replace(/^(.*)$/mg, function (found) { | ||
return padStr + color(found, opts.block_color) | ||
}); | ||
return retLines.join('\n'); | ||
} | ||
function tableFormat (token, options) { | ||
var aligns = token.align.map(function(a){ return (a===null)? 'l' : a[0] }); | ||
var rows = token.cells.map(function(row){ return row.map(processInline) }); | ||
rows.unshift(token.header.map(function(){ return '--' })); | ||
rows.unshift(token.header.map(function(s){ return processInline('**'+s+'**') })); | ||
var aligns = token.align.map(function (a) { | ||
return (a===null) ? 'l' : a[0]; | ||
}); | ||
var rows = token.cells.map(function (row) { | ||
return row.map(processInline); | ||
}); | ||
rows.unshift( | ||
token.header.map(function () { | ||
return '--'; | ||
}) | ||
); | ||
rows.unshift( | ||
token.header.map(function (s) { | ||
return processInline('**'+s+'**'); | ||
}) | ||
); | ||
return table(rows, { | ||
align: aligns, | ||
stringLength: ansiLength, | ||
stringLength: getStringWidth, | ||
hsep: options.tableSeparator | ||
@@ -336,12 +338,73 @@ }); | ||
/** | ||
* Trim formatting from string | ||
* @see https://github.com/medikoo/cli-color/blob/master/trim.js | ||
* Returns the number of columns required to display the given string. | ||
* @see https://github.com/nodejs/node/blob/cff7300a578be1b10001f2d967aaedc88aee6402/lib/readline.js#L1345 | ||
*/ | ||
function getStringWidth(str) { | ||
// Trim formatting from string | ||
var rAnsi = require('ansi-regex')(); | ||
str = str.replace(rAnsi, ''); | ||
function ansiLength (str) { | ||
var r = new RegExp('\x1b(?:\\[(?:\\d+[ABCDEFGJKSTm]|\\d+;\\d+[Hfm]|' + | ||
'\\d+;\\d+;\\d+m|6n|s|u|\\?25[lh])|\\w)', 'g'); | ||
return str.replace(r, '').length; | ||
var width = 0; | ||
for (var i = 0, len = str.length; i < len; i++) { | ||
var code = str.codePointAt(i); | ||
if (code >= 0x10000) { // surrogates | ||
i++; | ||
} | ||
if (isFullWidthCodePoint(code)) { | ||
width += 2; | ||
} | ||
else { | ||
width++; | ||
} | ||
} | ||
return width; | ||
} | ||
function isFullWidthCodePoint(code) { | ||
if (isNaN(code)) { | ||
return false; | ||
} | ||
// Code points are derived from: | ||
// http://www.unicode.org/Public/UNIDATA/EastAsianWidth.txt | ||
if (code >= 0x1100 | ||
&& ( | ||
// Hangul Jamo | ||
code <= 0x115f | ||
// LEFT-POINTING ANGLE BRACKET | ||
|| 0x2329 === code | ||
// RIGHT-POINTING ANGLE BRACKET | ||
|| 0x232a === code | ||
// CJK Radicals Supplement .. Enclosed CJK Letters and Months | ||
|| (0x2e80 <= code && code <= 0x3247 && code !== 0x303f) | ||
// Enclosed CJK Letters and Months .. CJK Unified Ideographs Extension A | ||
|| 0x3250 <= code && code <= 0x4dbf | ||
// CJK Unified Ideographs .. Yi Radicals | ||
|| 0x4e00 <= code && code <= 0xa4c6 | ||
// Hangul Jamo Extended-A | ||
|| 0xa960 <= code && code <= 0xa97c | ||
// Hangul Syllables | ||
|| 0xac00 <= code && code <= 0xd7a3 | ||
// CJK Compatibility Ideographs | ||
|| 0xf900 <= code && code <= 0xfaff | ||
// Vertical Forms | ||
|| 0xfe10 <= code && code <= 0xfe19 | ||
// CJK Compatibility Forms .. Small Form Variants | ||
|| 0xfe30 <= code && code <= 0xfe6b | ||
// Halfwidth and Fullwidth Forms | ||
|| 0xff01 <= code && code <= 0xff60 | ||
|| 0xffe0 <= code && code <= 0xffe6 | ||
// Kana Supplement | ||
|| 0x1b000 <= code && code <= 0x1b001 | ||
// Enclosed Ideographic Supplement | ||
|| 0x1f200 <= code && code <= 0x1f251 | ||
// CJK Unified Ideographs Extension B .. Tertiary Ideographic Plane | ||
|| 0x20000 <= code && code <= 0x3fffd | ||
) | ||
) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
exports.parse = function(text, options) { | ||
@@ -359,6 +422,4 @@ tokens = marked.lexer(text); | ||
output = outputArr.join('\n') | ||
if (options.collapseNewlines) { | ||
output = output.replace(/\n\n/g, '\n'); | ||
output = outputArr.join('').replace(/\n\n\n/g, '\n\n'); | ||
} | ||
@@ -365,0 +426,0 @@ |
{ | ||
"name": "@leichtgewicht/msee", | ||
"version": "0.2.5", | ||
"version": "0.3.0", | ||
"description": "[Fork] Msee is a command-line tool to read Markdown file in your terminal, and it's a library help your command-line software to output readable markdown content.", | ||
@@ -12,9 +12,10 @@ "main": "./lib/msee.js", | ||
"dependencies": { | ||
"ansi-regex": "^2.0.0", | ||
"cardinal": "^0.5.0", | ||
"chalk": "~0.4.0", | ||
"marked": "~0.3.0", | ||
"nopt": "~2.1.1", | ||
"@leichtgewicht/visualwidth": "^0.1.0", | ||
"chalk": "^1.1.1", | ||
"marked": "^0.3.5", | ||
"nopt": "^3.0.4", | ||
"text-table": "^0.2.0", | ||
"xtend": "~2.1.1" | ||
"wcstring": "^1.0.1", | ||
"xtend": "^4.0.0" | ||
}, | ||
@@ -21,0 +22,0 @@ "keywords": [ |
msee | ||
=== | ||
[](https://david-dm.org/firede/msee) | ||
[](https://npmjs.org/package/msee) | ||
[](https://david-dm.org/firede/msee) | ||
[](https://npmjs.org/package/msee) | ||
[](https://npmjs.org/package/msee) | ||
@@ -12,3 +15,3 @@ *msee* is a command-line tool to read markdown file. | ||
 | ||
<img width="752" alt="msee" src="https://cloud.githubusercontent.com/assets/157338/10902801/531ba216-823d-11e5-87ac-986b8d5ea4cc.png"> | ||
@@ -45,4 +48,4 @@ ## Installation | ||
=== | ||
--- | ||
[](https://bitdeli.com/free "Bitdeli Badge") |
21074
11
589
50
8
+ Addedansi-regex@^2.0.0
+ Addedwcstring@^1.0.1
+ Addedansi-regex@2.1.1(transitive)
+ Addedansi-styles@2.2.1(transitive)
+ Addedchalk@1.1.3(transitive)
+ Addedescape-string-regexp@1.0.5(transitive)
+ Addedhas-ansi@2.0.0(transitive)
+ Addednopt@3.0.6(transitive)
+ Addedstrip-ansi@3.0.1(transitive)
+ Addedsupports-color@2.0.0(transitive)
+ Addedvarsize-string@1.0.1(transitive)
+ Addedwcsize@1.0.0(transitive)
+ Addedwcstring@1.0.1(transitive)
+ Addedxtend@4.0.2(transitive)
- Removed@leichtgewicht/visualwidth@^0.1.0
- Removed@leichtgewicht/visualwidth@0.1.0(transitive)
- Removedansi-styles@1.0.0(transitive)
- Removedchalk@0.4.0(transitive)
- Removedhas-color@0.1.7(transitive)
- Removednopt@2.1.2(transitive)
- Removedobject-keys@0.4.0(transitive)
- Removedstrip-ansi@0.1.1(transitive)
- Removedxtend@2.1.2(transitive)
Updatedchalk@^1.1.1
Updatedmarked@^0.3.5
Updatednopt@^3.0.4
Updatedxtend@^4.0.0